OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / cvs / cvsutils.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "cvsutils.h"
34
35 #include <QtCore/QDebug>
36 #include <QtCore/QRegExp>
37 #include <QtCore/QStringList>
38
39 namespace CVS {
40 namespace Internal {
41
42 CVS_Revision::CVS_Revision(const QString &rev) :
43     revision(rev)
44 {
45 }
46
47 CVS_LogEntry::CVS_LogEntry(const QString &f) :
48     file(f)
49 {
50 }
51
52 QDebug operator<<(QDebug d, const CVS_LogEntry &e)
53 {
54     QDebug nospace = d.nospace();
55     nospace << "File: " << e.file << e.revisions.size() << '\n';
56     foreach(const CVS_Revision &r, e.revisions)
57         nospace << "  " << r.revision << ' ' << r.date << ' ' << r.commitId << '\n';
58     return d;
59 }
60
61 /* Parse:
62 \code
63 RCS file: /repo/foo.h
64 Working file: foo.h
65 head: 1.2
66 ...
67 ----------------------------
68 revision 1.2
69 date: 2009-07-14 13:30:25 +0200;  author: <author>;  state: dead;  lines: +0 -0;  commitid: <id>;
70 <message>
71 ----------------------------
72 revision 1.1
73 ...
74 =============================================================================
75 \endcode */
76
77 QList<CVS_LogEntry> parseLogEntries(const QString &o,
78                                     const QString &directory,
79                                     const QString filterCommitId)
80 {
81     enum ParseState { FileState, RevisionState, StatusLineState };
82
83     QList<CVS_LogEntry> rc;
84     const QStringList lines = o.split(QString(QLatin1Char('\n')), QString::SkipEmptyParts);
85     ParseState state = FileState;
86
87     const QString workingFilePrefix = QLatin1String("Working file: ");
88     const QString revisionPrefix = QLatin1String("revision ");
89     const QString statusPrefix = QLatin1String("date: ");
90     const QString commitId = QLatin1String("commitid: ");
91     const QRegExp statusPattern = QRegExp(QLatin1String("^date: ([\\d\\-]+) .*commitid: ([^;]+);$"));
92     const QRegExp revisionPattern = QRegExp(QLatin1String("^revision ([\\d\\.]+)$"));
93     const QChar slash = QLatin1Char('/');
94     Q_ASSERT(statusPattern.isValid() && revisionPattern.isValid());
95     const QString fileSeparator = QLatin1String("=============================================================================");
96
97     // Parse using a state enumeration and regular expressions as not to fall for weird
98     // commit messages in state 'RevisionState'
99     foreach(const QString &line, lines) {
100         switch (state) {
101             case FileState:
102             if (line.startsWith(workingFilePrefix)) {
103                 QString file = directory;
104                 if (!file.isEmpty())
105                     file += slash;
106                 file += line.mid(workingFilePrefix.size()).trimmed();
107                 rc.push_back(CVS_LogEntry(file));
108                 state = RevisionState;
109             }
110             break;
111         case RevisionState:
112             if (revisionPattern.exactMatch(line)) {
113                 rc.back().revisions.push_back(CVS_Revision(revisionPattern.cap(1)));
114                 state = StatusLineState;
115             } else {
116                 if (line == fileSeparator)
117                     state = FileState;
118             }
119             break;
120         case StatusLineState:
121             if (statusPattern.exactMatch(line)) {
122                 const QString commitId = statusPattern.cap(2);
123                 if (filterCommitId.isEmpty() || filterCommitId == commitId) {
124                     rc.back().revisions.back().date = statusPattern.cap(1);
125                     rc.back().revisions.back().commitId = commitId;
126                 } else {
127                     rc.back().revisions.pop_back();
128                 }
129                 state = RevisionState;
130             }
131         }
132     }
133     // Purge out files with no matching commits
134     if (!filterCommitId.isEmpty()) {
135         for (QList<CVS_LogEntry>::iterator it = rc.begin(); it != rc.end(); ) {
136             if (it->revisions.empty()) {
137                 it = rc.erase(it);
138             } else {
139                 ++it;
140             }
141         }
142     }
143     return rc;
144 }
145
146 QString fixDiffOutput(QString d)
147 {
148     if (d.isEmpty())
149         return d;
150     // Kill all lines starting with '?'
151     const QChar questionMark = QLatin1Char('?');
152     const QChar newLine = QLatin1Char('\n');
153     for (int pos = 0; pos < d.size(); ) {
154         const int endOfLinePos = d.indexOf(newLine, pos);
155         if (endOfLinePos == -1)
156             break;
157         const int nextLinePos = endOfLinePos + 1;
158         if (d.at(pos) == questionMark) {
159             d.remove(pos, nextLinePos - pos);
160         } else {
161             pos = nextLinePos;
162         }
163     }
164     return d;
165 }
166
167 // Parse "cvs status" output for added/modified/deleted files
168 // "File: <foo> Status: Up-to-date"
169 // "File:  <foo> Status: Locally Modified"
170 // "File: no file <foo> Status: Locally Removed"
171 // "File: hup Status: Locally Added"
172 // Not handled for commit purposes: "Needs Patch/Needs Merge"
173 // In between, we might encounter "cvs status: Examining subdir"...
174 // As we run the status command from the repository directory,
175 // we need to add the full path, again.
176 // stdout/stderr need to be merged to catch directories.
177
178 // Parse out status keywords, return state enum or -1
179 inline int stateFromKeyword(const QString &s)
180 {
181     if (s == QLatin1String("Up-to-date"))
182         return -1;
183     if (s == QLatin1String("Locally Modified"))
184         return CVSSubmitEditor::LocallyModified;
185     if (s == QLatin1String("Locally Added"))
186         return CVSSubmitEditor::LocallyAdded;
187     if (s == QLatin1String("Locally Removed"))
188         return CVSSubmitEditor::LocallyRemoved;
189     return -1;
190 }
191
192 StateList parseStatusOutput(const QString &directory, const QString &output)
193 {
194     StateList changeSet;
195     const QString fileKeyword = QLatin1String("File: ");
196     const QString statusKeyword = QLatin1String("Status: ");
197     const QString noFileKeyword = QLatin1String("no file ");
198     const QString directoryKeyword = QLatin1String("cvs status: Examining ");
199     const QString dotDir = QString(QLatin1Char('.'));
200     const QChar slash = QLatin1Char('/');
201
202     const QStringList list = output.split(QLatin1Char('\n'), QString::SkipEmptyParts);
203
204     QString path = directory;
205     if (!path.isEmpty())
206         path += slash;
207     foreach (const QString &l, list) {
208         // Status line containing file
209         if (l.startsWith(fileKeyword)) {
210             // Parse state
211             const int statusPos = l.indexOf(statusKeyword);
212             if (statusPos == -1)
213                 continue;
214             const int state = stateFromKeyword(l.mid(statusPos + statusKeyword.size()).trimmed());
215             if (state == -1)
216                 continue;
217             // Concatenate file name, Correct "no file <foo>"
218             QString fileName = l.mid(fileKeyword.size(), statusPos - fileKeyword.size()).trimmed();
219             if (state == CVSSubmitEditor::LocallyRemoved && fileName.startsWith(noFileKeyword))
220                 fileName.remove(0, noFileKeyword.size());
221             changeSet.push_back(CVSSubmitEditor::StateFilePair(static_cast<CVSSubmitEditor::State>(state), path + fileName));
222             continue;
223         }
224         // Examining a new subdirectory
225         if (l.startsWith(directoryKeyword)) {
226             path = directory;
227             if (!path.isEmpty())
228                 path += slash;
229             const QString newSubDir = l.mid(directoryKeyword.size()).trimmed();
230             if (newSubDir != dotDir) { // Skip Examining '.'
231                 path += newSubDir;
232                 path += slash;
233             }
234             continue;
235         }
236     }
237     return changeSet;
238 }
239
240 // Decrement version number "1.2" -> "1.1"
241 QString previousRevision(const QString &rev)
242 {
243     const int dotPos = rev.lastIndexOf(QLatin1Char('.'));
244     if (dotPos == -1)
245         return rev;
246     const int minor = rev.mid(dotPos + 1).toInt();
247     return rev.left(dotPos + 1) + QString::number(minor - 1);
248 }
249
250 // Is "[1.2...].1"?
251 bool isFirstRevision(const QString &r)
252 {
253     return r.endsWith(QLatin1String(".1"));
254 }
255
256 } // namespace Internal
257 } // namespace CVS