File: bazaarutils.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (248 lines) | stat: -rw-r--r-- 11,859 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/***************************************************************************
 *   Copyright 2013-2014 Maciej Poleski                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as        *
 *   published by the Free Software Foundation; either version 2 of        *
 *   the License or (at your option) version 3 or any later version        *
 *   accepted by the membership of KDE e.V. (or its successor approved     *
 *   by the membership of KDE e.V.), which shall act as a proxy            *
 *   defined in Section 14 of version 3 of the license.                    *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#include "bazaarutils.h"

#include <QDateTime>
#include <QDebug>

#include <vcs/vcsrevision.h>
#include <vcs/vcsstatusinfo.h>
#include <vcs/vcsevent.h>

QDir BazaarUtils::toQDir(const QUrl& url)
{
    return QDir(url.toLocalFile());
}

QDir BazaarUtils::workingCopy(const QUrl& path)
{
    QDir dir = BazaarUtils::toQDir(path);
    while (!dir.exists(QStringLiteral(".bzr")) && dir.cdUp());

    return dir;
}

QString BazaarUtils::getRevisionSpec(const KDevelop::VcsRevision& revision)
{
    if (revision.revisionType() == KDevelop::VcsRevision::Special) {
        if (revision.specialType() == KDevelop::VcsRevision::Head)
            return QStringLiteral("-rlast:1");
        else if (revision.specialType() == KDevelop::VcsRevision::Base)
            return QString();  // Workaround strange KDevelop behaviour
        else if (revision.specialType() == KDevelop::VcsRevision::Working)
            return QString();
        else if (revision.specialType() == KDevelop::VcsRevision::Start)
            return QStringLiteral("-r1");
        else
            return QString(); // Don't know how to handle this situation
    } else if (revision.revisionType() == KDevelop::VcsRevision::GlobalNumber)
        return QLatin1String("-r") + QString::number(revision.revisionValue().toLongLong());
    else
        return QString(); // Don't know how to handle this situation
}

QString BazaarUtils::getRevisionSpecRange(const KDevelop::VcsRevision& end)
{
    if (end.revisionType() == KDevelop::VcsRevision::Special) {
        if (end.specialType() == KDevelop::VcsRevision::Head) {
            return QStringLiteral("-r..last:1");
        } else if (end.specialType() == KDevelop::VcsRevision::Base) {
            return QStringLiteral("-r..last:1"); // Workaround strange KDevelop behaviour
        } else if (end.specialType() == KDevelop::VcsRevision::Working) {
            return QString();
        } else if (end.specialType() == KDevelop::VcsRevision::Start) {
            return QStringLiteral("-..r1");
        } else {
            return QString(); // Don't know how to handle this situation
        }
    } else if (end.revisionType() == KDevelop::VcsRevision::GlobalNumber) {
        return QStringLiteral("-r") + QString::number(end.revisionValue().toLongLong());
    }

    return QString();    // Don't know how to handle this situation
}

QString BazaarUtils::getRevisionSpecRange(const KDevelop::VcsRevision& begin,
                             const KDevelop::VcsRevision& end)
{
    if (begin.revisionType() == KDevelop::VcsRevision::Special) {
        if (begin.specialType() == KDevelop::VcsRevision::Previous) {
            if (end.revisionType() == KDevelop::VcsRevision::Special) {
                if (end.specialType() == KDevelop::VcsRevision::Base ||
                        end.specialType() == KDevelop::VcsRevision::Head)
                    return QStringLiteral("-rlast:2..last:1");
                else if (end.specialType() == KDevelop::VcsRevision::Working)
                    return QString();
                else if (end.specialType() == KDevelop::VcsRevision::Start)
                    return QStringLiteral("-r0..1");        // That's wrong revision range
            } else if (end.revisionType() == KDevelop::VcsRevision::GlobalNumber)
                return QStringLiteral("-r") +
                       QString::number(end.revisionValue().toLongLong() - 1)
                       + QLatin1String("..") + QString::number(end.revisionValue().toLongLong());
            else
                return QString(); // Don't know how to handle this situation
        } else if (begin.specialType() == KDevelop::VcsRevision::Base ||
                   begin.specialType() == KDevelop::VcsRevision::Head) {
            // Only one possibility: comparing working copy to last commit
            return QString();
        }
    } else if (begin.revisionType() == KDevelop::VcsRevision::GlobalNumber) {
        if (end.revisionType() == KDevelop::VcsRevision::Special) {
            // Assuming working copy
            return QLatin1String("-r") + QString::number(begin.revisionValue().toLongLong());
        } else {
            return QLatin1String("-r") + QString::number(begin.revisionValue().toLongLong())
                   + QLatin1String("..") + QString::number(end.revisionValue().toLongLong());
        }
    }
    return QString(); // Don't know how to handle this situation
}

bool BazaarUtils::isValidDirectory(const QUrl& dirPath)
{
    QDir dir = BazaarUtils::workingCopy(dirPath);

    return dir.cd(QStringLiteral(".bzr")) && dir.exists(QStringLiteral("branch"));
}

KDevelop::VcsStatusInfo BazaarUtils::parseVcsStatusInfoLine(const QString& line)
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
    const QStringList tokens = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
#else
    QStringList tokens = line.split(QLatin1Char(' '), QString::SkipEmptyParts);
#endif
    KDevelop::VcsStatusInfo result;
    if (tokens.size() < 2) // Don't know how to handle this situation (it is an error)
        return result;
    result.setUrl(QUrl::fromLocalFile(tokens.back()));
    if (tokens[0] == QLatin1String("M")) {
        result.setState(KDevelop::VcsStatusInfo::ItemModified);
    } else if (tokens[0] == QLatin1String("C")) {
        result.setState(KDevelop::VcsStatusInfo::ItemHasConflicts);
    } else if (tokens[0] == QLatin1String("+N")) {
        result.setState(KDevelop::VcsStatusInfo::ItemAdded);
    } else if (tokens[0] == QLatin1String("?")) {
        result.setState(KDevelop::VcsStatusInfo::ItemUnknown);
    } else if (tokens[0] == QLatin1String("D")) {
        result.setState(KDevelop::VcsStatusInfo::ItemDeleted);
    } else {
        result.setState(KDevelop::VcsStatusInfo::ItemUserState);
        qWarning() << "Unsupported status: " << tokens[0];
    }
    return result;
}

QString BazaarUtils::concatenatePath(const QDir& workingCopy, const QUrl& pathInWorkingCopy)
{
    return QFileInfo(workingCopy.absolutePath() + QDir::separator()
                     + pathInWorkingCopy.toLocalFile()).absoluteFilePath();
}

KDevelop::VcsEvent BazaarUtils::parseBzrLogPart(const QString& output)
{
    const QStringList outputLines = output.split(QLatin1Char('\n'));
    KDevelop::VcsEvent commitInfo;
    bool atMessage = false;
    QString message;
    bool afterMessage = false;
    QHash<QString, KDevelop::VcsItemEvent::Actions> fileToActionsMapping;
    KDevelop::VcsItemEvent::Action currentAction;
    for (const QString &line : outputLines) {
        if (!atMessage) {
            if (line.startsWith(QLatin1String("revno"))) {
                QString revno = line.mid(QStringLiteral("revno: ").length());
                revno = revno.left(revno.indexOf(QLatin1Char(' ')));
                KDevelop::VcsRevision revision;
                revision.setRevisionValue(revno.toLongLong(), KDevelop::VcsRevision::GlobalNumber);
                commitInfo.setRevision(revision);
            } else if (line.startsWith(QLatin1String("committer: "))) {
                QString commiter = line.mid(QStringLiteral("committer: ").length());
                commitInfo.setAuthor(commiter);     // Author goes after committer, but only if is different
            } else if (line.startsWith(QLatin1String("author"))) {
                QString author = line.mid(QStringLiteral("author: ").length());
                commitInfo.setAuthor(author);       // It may override committer (In fact committer is not supported by VcsEvent)
            } else if (line.startsWith(QLatin1String("timestamp"))) {
                const QString formatString = QStringLiteral("yyyy-MM-dd hh:mm:ss");
                QString timestamp = line.mid(QStringLiteral("timestamp: ddd ").length(), formatString.length());
                commitInfo.setDate(QDateTime::fromString(timestamp, formatString));
            } else if (line.startsWith(QLatin1String("message"))) {
                atMessage = true;
            }
        } else if (atMessage && !afterMessage) {
            if (!line.isEmpty() && line[0].isSpace()) {
                message += line.trimmed() + QLatin1Char('\n');
            } else if (!line.isEmpty()) {
                afterMessage = true;
                // leave atMessage = true
                currentAction = BazaarUtils::parseActionDescription(line);
            } // if line is empty - ignore and get next
        } else if (afterMessage) {
            if (!line.isEmpty() && !line[0].isSpace()) {
                currentAction = BazaarUtils::parseActionDescription(line);
            } else if (!line.isEmpty()) {
                fileToActionsMapping[line.trimmed()] |= currentAction;
            } // if line is empty - ignore and get next
        }
    }
    if (atMessage)
        commitInfo.setMessage(message.trimmed());
    for (auto i = fileToActionsMapping.begin(); i != fileToActionsMapping.end(); ++i) {
        KDevelop::VcsItemEvent itemEvent;
        itemEvent.setRepositoryLocation(i.key());
        itemEvent.setActions(i.value());
        commitInfo.addItem(itemEvent);
    }
    return commitInfo;
}

KDevelop::VcsItemEvent::Action BazaarUtils::parseActionDescription(const QString& action)
{
    if (action == QLatin1String("added:")) {
        return KDevelop::VcsItemEvent::Added;
    } else if (action == QLatin1String("modified:")) {
        return KDevelop::VcsItemEvent::Modified;
    } else if (action == QLatin1String("removed:")) {
        return KDevelop::VcsItemEvent::Deleted;
    } else if (action == QLatin1String("kind changed:")) {
        return KDevelop::VcsItemEvent::Replaced; // Best approximation
    } else if (action.startsWith(QLatin1String("renamed"))) {
        return KDevelop::VcsItemEvent::Modified; // Best approximation
    } else {
        qCritical("Unsupported action: %s", action.toLocal8Bit().constData());
        return KDevelop::VcsItemEvent::Action();
    }
}

QList<QUrl> BazaarUtils::handleRecursion(const QList<QUrl>& listOfUrls, KDevelop::IBasicVersionControl::RecursionMode recursion)
{
    if (recursion == KDevelop::IBasicVersionControl::Recursive) {
        return listOfUrls;      // Nothing to do
    } else {
        QList<QUrl> result;
        for (const auto& url : listOfUrls) {
            if (url.isLocalFile() && QFileInfo(url.toLocalFile()).isFile()) {
                result.push_back(url);
            }
        }
        return result;
    }
}