OSDN Git Service

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