1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
35 #include <QtCore/QDebug>
36 #include <QtCore/QRegExp>
37 #include <QtCore/QStringList>
42 CVS_Revision::CVS_Revision(const QString &rev) :
47 CVS_LogEntry::CVS_LogEntry(const QString &f) :
52 QDebug operator<<(QDebug d, const CVS_LogEntry &e)
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';
67 ----------------------------
69 date: 2009-07-14 13:30:25 +0200; author: <author>; state: dead; lines: +0 -0; commitid: <id>;
71 ----------------------------
74 =============================================================================
77 QList<CVS_LogEntry> parseLogEntries(const QString &o,
78 const QString &directory,
79 const QString filterCommitId)
81 enum ParseState { FileState, RevisionState, StatusLineState };
83 QList<CVS_LogEntry> rc;
84 const QStringList lines = o.split(QString(QLatin1Char('\n')), QString::SkipEmptyParts);
85 ParseState state = FileState;
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("=============================================================================");
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) {
102 if (line.startsWith(workingFilePrefix)) {
103 QString file = directory;
106 file += line.mid(workingFilePrefix.size()).trimmed();
107 rc.push_back(CVS_LogEntry(file));
108 state = RevisionState;
112 if (revisionPattern.exactMatch(line)) {
113 rc.back().revisions.push_back(CVS_Revision(revisionPattern.cap(1)));
114 state = StatusLineState;
116 if (line == fileSeparator)
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;
127 rc.back().revisions.pop_back();
129 state = RevisionState;
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()) {
146 QString fixDiffOutput(QString 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)
157 const int nextLinePos = endOfLinePos + 1;
158 if (d.at(pos) == questionMark) {
159 d.remove(pos, nextLinePos - pos);
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.
178 // Parse out status keywords, return state enum or -1
179 inline int stateFromKeyword(const QString &s)
181 if (s == QLatin1String("Up-to-date"))
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;
192 StateList parseStatusOutput(const QString &directory, const QString &output)
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('/');
202 const QStringList list = output.split(QLatin1Char('\n'), QString::SkipEmptyParts);
204 QString path = directory;
207 foreach (const QString &l, list) {
208 // Status line containing file
209 if (l.startsWith(fileKeyword)) {
211 const int statusPos = l.indexOf(statusKeyword);
214 const int state = stateFromKeyword(l.mid(statusPos + statusKeyword.size()).trimmed());
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));
224 // Examining a new subdirectory
225 if (l.startsWith(directoryKeyword)) {
229 const QString newSubDir = l.mid(directoryKeyword.size()).trimmed();
230 if (newSubDir != dotDir) { // Skip Examining '.'
240 // Decrement version number "1.2" -> "1.1"
241 QString previousRevision(const QString &rev)
243 const int dotPos = rev.lastIndexOf(QLatin1Char('.'));
246 const int minor = rev.mid(dotPos + 1).toInt();
247 return rev.left(dotPos + 1) + QString::number(minor - 1);
251 bool isFirstRevision(const QString &r)
253 return r.endsWith(QLatin1String(".1"));
256 } // namespace Internal