1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
36 #include <QtCore/QDebug>
37 #include <QtCore/QRegExp>
38 #include <QtCore/QStringList>
43 CVS_Revision::CVS_Revision(const QString &rev) :
48 CVS_LogEntry::CVS_LogEntry(const QString &f) :
53 QDebug operator<<(QDebug d, const CVS_LogEntry &e)
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';
68 ----------------------------
70 date: 2009-07-14 13:30:25 +0200; author: <author>; state: dead; lines: +0 -0; commitid: <id>;
72 ----------------------------
75 =============================================================================
78 QList<CVS_LogEntry> parseLogEntries(const QString &o,
79 const QString &directory,
80 const QString filterCommitId)
82 enum ParseState { FileState, RevisionState, StatusLineState };
84 QList<CVS_LogEntry> rc;
85 const QStringList lines = o.split(QString(QLatin1Char('\n')), QString::SkipEmptyParts);
86 ParseState state = FileState;
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("=============================================================================");
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) {
103 if (line.startsWith(workingFilePrefix)) {
104 QString file = directory;
107 file += line.mid(workingFilePrefix.size()).trimmed();
108 rc.push_back(CVS_LogEntry(file));
109 state = RevisionState;
113 if (revisionPattern.exactMatch(line)) {
114 rc.back().revisions.push_back(CVS_Revision(revisionPattern.cap(1)));
115 state = StatusLineState;
117 if (line == fileSeparator)
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;
128 rc.back().revisions.pop_back();
130 state = RevisionState;
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()) {
147 QString fixDiffOutput(QString 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)
158 const int nextLinePos = endOfLinePos + 1;
159 if (d.at(pos) == questionMark) {
160 d.remove(pos, nextLinePos - pos);
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.
179 // Parse out status keywords, return state enum or -1
180 inline int stateFromKeyword(const QString &s)
182 if (s == QLatin1String("Up-to-date"))
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;
193 StateList parseStatusOutput(const QString &directory, const QString &output)
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('/');
203 const QStringList list = output.split(QLatin1Char('\n'), QString::SkipEmptyParts);
205 QString path = directory;
208 foreach (const QString &l, list) {
209 // Status line containing file
210 if (l.startsWith(fileKeyword)) {
212 const int statusPos = l.indexOf(statusKeyword);
215 const int state = stateFromKeyword(l.mid(statusPos + statusKeyword.size()).trimmed());
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));
225 // Examining a new subdirectory
226 if (l.startsWith(directoryKeyword)) {
230 const QString newSubDir = l.mid(directoryKeyword.size()).trimmed();
231 if (newSubDir != dotDir) { // Skip Examining '.'
241 // Decrement version number "1.2" -> "1.1"
242 QString previousRevision(const QString &rev)
244 const int dotPos = rev.lastIndexOf(QLatin1Char('.'));
247 const int minor = rev.mid(dotPos + 1).toInt();
248 return rev.left(dotPos + 1) + QString::number(minor - 1);
252 bool isFirstRevision(const QString &r)
254 return r.endsWith(QLatin1String(".1"));
257 } // namespace Internal