cvsutils.cpp 8.87 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
30 31 32

#include "cvsutils.h"

33 34 35
#include <QDebug>
#include <QRegExp>
#include <QStringList>
36

hjk's avatar
hjk committed
37
namespace Cvs {
38 39
namespace Internal {

hjk's avatar
hjk committed
40
CvsRevision::CvsRevision(const QString &rev) :
41 42 43 44
    revision(rev)
{
}

hjk's avatar
hjk committed
45
CvsLogEntry::CvsLogEntry(const QString &f) :
46 47 48 49
    file(f)
{
}

hjk's avatar
hjk committed
50
QDebug operator<<(QDebug d, const CvsLogEntry &e)
51 52 53
{
    QDebug nospace = d.nospace();
    nospace << "File: " << e.file << e.revisions.size() << '\n';
54
    foreach (const CvsRevision &r, e.revisions)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        nospace << "  " << r.revision << ' ' << r.date << ' ' << r.commitId << '\n';
    return d;
}

/* Parse:
\code
RCS file: /repo/foo.h
Working file: foo.h
head: 1.2
...
----------------------------
revision 1.2
date: 2009-07-14 13:30:25 +0200;  author: <author>;  state: dead;  lines: +0 -0;  commitid: <id>;
<message>
----------------------------
revision 1.1
...
=============================================================================
\endcode */

hjk's avatar
hjk committed
75 76
QList<CvsLogEntry> parseLogEntries(const QString &o,
                                   const QString &directory,
77
                                   const QString &filterCommitId)
78 79 80
{
    enum ParseState { FileState, RevisionState, StatusLineState };

hjk's avatar
hjk committed
81
    QList<CvsLogEntry> rc;
82 83 84 85
    const QStringList lines = o.split(QString(QLatin1Char('\n')), QString::SkipEmptyParts);
    ParseState state = FileState;

    const QString workingFilePrefix = QLatin1String("Working file: ");
86 87
    QRegExp statusPattern = QRegExp(QLatin1String("^date: ([\\d\\-]+) .*commitid: ([^;]+);$"));
    QRegExp revisionPattern = QRegExp(QLatin1String("^revision ([\\d\\.]+)$"));
88 89 90 91 92 93
    const QChar slash = QLatin1Char('/');
    Q_ASSERT(statusPattern.isValid() && revisionPattern.isValid());
    const QString fileSeparator = QLatin1String("=============================================================================");

    // Parse using a state enumeration and regular expressions as not to fall for weird
    // commit messages in state 'RevisionState'
94
    foreach (const QString &line, lines) {
95 96 97 98 99 100 101
        switch (state) {
            case FileState:
            if (line.startsWith(workingFilePrefix)) {
                QString file = directory;
                if (!file.isEmpty())
                    file += slash;
                file += line.mid(workingFilePrefix.size()).trimmed();
hjk's avatar
hjk committed
102
                rc.push_back(CvsLogEntry(file));
103 104 105 106 107
                state = RevisionState;
            }
            break;
        case RevisionState:
            if (revisionPattern.exactMatch(line)) {
hjk's avatar
hjk committed
108
                rc.back().revisions.push_back(CvsRevision(revisionPattern.cap(1)));
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
                state = StatusLineState;
            } else {
                if (line == fileSeparator)
                    state = FileState;
            }
            break;
        case StatusLineState:
            if (statusPattern.exactMatch(line)) {
                const QString commitId = statusPattern.cap(2);
                if (filterCommitId.isEmpty() || filterCommitId == commitId) {
                    rc.back().revisions.back().date = statusPattern.cap(1);
                    rc.back().revisions.back().commitId = commitId;
                } else {
                    rc.back().revisions.pop_back();
                }
                state = RevisionState;
            }
        }
    }
    // Purge out files with no matching commits
    if (!filterCommitId.isEmpty()) {
hjk's avatar
hjk committed
130
        for (QList<CvsLogEntry>::iterator it = rc.begin(); it != rc.end(); ) {
131
            if (it->revisions.empty())
132
                it = rc.erase(it);
133
            else
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
                ++it;
        }
    }
    return rc;
}

QString fixDiffOutput(QString d)
{
    if (d.isEmpty())
        return d;
    // Kill all lines starting with '?'
    const QChar questionMark = QLatin1Char('?');
    const QChar newLine = QLatin1Char('\n');
    for (int pos = 0; pos < d.size(); ) {
        const int endOfLinePos = d.indexOf(newLine, pos);
        if (endOfLinePos == -1)
            break;
        const int nextLinePos = endOfLinePos + 1;
152
        if (d.at(pos) == questionMark)
153
            d.remove(pos, nextLinePos - pos);
154
        else
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
            pos = nextLinePos;
    }
    return d;
}

// Parse "cvs status" output for added/modified/deleted files
// "File: <foo> Status: Up-to-date"
// "File:  <foo> Status: Locally Modified"
// "File: no file <foo> Status: Locally Removed"
// "File: hup Status: Locally Added"
// Not handled for commit purposes: "Needs Patch/Needs Merge"
// In between, we might encounter "cvs status: Examining subdir"...
// As we run the status command from the repository directory,
// we need to add the full path, again.
// stdout/stderr need to be merged to catch directories.

// Parse out status keywords, return state enum or -1
hjk's avatar
hjk committed
172
static int stateFromKeyword(const QString &s)
173 174 175 176
{
    if (s == QLatin1String("Up-to-date"))
        return -1;
    if (s == QLatin1String("Locally Modified"))
hjk's avatar
hjk committed
177
        return CvsSubmitEditor::LocallyModified;
178
    if (s == QLatin1String("Locally Added"))
hjk's avatar
hjk committed
179
        return CvsSubmitEditor::LocallyAdded;
180
    if (s == QLatin1String("Locally Removed"))
hjk's avatar
hjk committed
181
        return CvsSubmitEditor::LocallyRemoved;
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    return -1;
}

StateList parseStatusOutput(const QString &directory, const QString &output)
{
    StateList changeSet;
    const QString fileKeyword = QLatin1String("File: ");
    const QString statusKeyword = QLatin1String("Status: ");
    const QString noFileKeyword = QLatin1String("no file ");
    const QString directoryKeyword = QLatin1String("cvs status: Examining ");
    const QString dotDir = QString(QLatin1Char('.'));
    const QChar slash = QLatin1Char('/');

    const QStringList list = output.split(QLatin1Char('\n'), QString::SkipEmptyParts);

    QString path = directory;
    if (!path.isEmpty())
        path += slash;
    foreach (const QString &l, list) {
        // Status line containing file
        if (l.startsWith(fileKeyword)) {
            // Parse state
            const int statusPos = l.indexOf(statusKeyword);
            if (statusPos == -1)
                continue;
            const int state = stateFromKeyword(l.mid(statusPos + statusKeyword.size()).trimmed());
            if (state == -1)
                continue;
            // Concatenate file name, Correct "no file <foo>"
            QString fileName = l.mid(fileKeyword.size(), statusPos - fileKeyword.size()).trimmed();
hjk's avatar
hjk committed
212
            if (state == CvsSubmitEditor::LocallyRemoved && fileName.startsWith(noFileKeyword))
213
                fileName.remove(0, noFileKeyword.size());
hjk's avatar
hjk committed
214
            changeSet.push_back(CvsSubmitEditor::StateFilePair(static_cast<CvsSubmitEditor::State>(state), path + fileName));
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
            continue;
        }
        // Examining a new subdirectory
        if (l.startsWith(directoryKeyword)) {
            path = directory;
            if (!path.isEmpty())
                path += slash;
            const QString newSubDir = l.mid(directoryKeyword.size()).trimmed();
            if (newSubDir != dotDir) { // Skip Examining '.'
                path += newSubDir;
                path += slash;
            }
            continue;
        }
    }
    return changeSet;
}

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
// Decrement version number "1.2" -> "1.1"
QString previousRevision(const QString &rev)
{
    const int dotPos = rev.lastIndexOf(QLatin1Char('.'));
    if (dotPos == -1)
        return rev;
    const int minor = rev.mid(dotPos + 1).toInt();
    return rev.left(dotPos + 1) + QString::number(minor - 1);
}

// Is "[1.2...].1"?
bool isFirstRevision(const QString &r)
{
    return r.endsWith(QLatin1String(".1"));
}

249
} // namespace Internal
hjk's avatar
hjk committed
250
} // namespace Cvs