File: bzrannotatejob.cpp

package info (click to toggle)
kdevelop 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 71,888 kB
  • sloc: cpp: 290,869; python: 3,626; javascript: 3,518; sh: 1,316; ansic: 703; xml: 401; php: 95; lisp: 66; makefile: 31; sed: 12
file content (185 lines) | stat: -rw-r--r-- 6,995 bytes parent folder | download | duplicates (2)
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
/*
    SPDX-FileCopyrightText: 2013-2014 Maciej Poleski

    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/

#include "bzrannotatejob.h"

#include <functional>

#include <QTimer>
#include <QDateTime>
#include <QDir>

#include <interfaces/iplugin.h>
#include <util/stringviewhelpers.h>
#include <vcs/dvcs/dvcsjob.h>
#include <vcs/vcsannotation.h>
#include <vcs/vcsrevision.h>

using namespace KDevelop;

BzrAnnotateJob::BzrAnnotateJob(const QDir& workingDir, const QString& revisionSpec, const QUrl& localLocation, KDevelop::IPlugin* parent, KDevelop::OutputJob::OutputJobVerbosity verbosity)
    : VcsJob(parent, verbosity), m_workingDir(workingDir), m_revisionSpec(revisionSpec), m_localLocation(localLocation), m_vcsPlugin(parent), m_status(KDevelop::VcsJob::JobNotStarted)
{
    setType(JobType::Annotate);
    setCapabilities(Killable);
}

bool BzrAnnotateJob::doKill()
{
    m_status = KDevelop::VcsJob::JobCanceled;
    if (m_job)
        return m_job->kill(KJob::Quietly);
    else
        return true;
}

void BzrAnnotateJob::start()
{
    if (m_status != KDevelop::VcsJob::JobNotStarted)
        return;
    auto* job = new KDevelop::DVcsJob(m_workingDir, m_vcsPlugin, KDevelop::OutputJob::Silent);
    *job << "bzr" << "annotate" << "--all" << m_revisionSpec << m_localLocation;
    connect(job, &DVcsJob::readyForParsing, this, &BzrAnnotateJob::parseBzrAnnotateOutput);
    m_status = VcsJob::JobRunning;
    m_job = job;
    job->start();
}

void BzrAnnotateJob::parseBzrAnnotateOutput(KDevelop::DVcsJob* job)
{
    m_outputLines = job->output().split(QLatin1Char('\n'));
    m_currentLine = 0;
    if (m_status == KDevelop::VcsJob::JobRunning)
        QTimer::singleShot(0, this, &BzrAnnotateJob::parseNextLine);
}

void BzrAnnotateJob::parseNextLine()
{
    for(;;)
    {
        Q_ASSERT(m_currentLine<=m_outputLines.size());
        if (m_currentLine == m_outputLines.size()) {
            m_status = KDevelop::VcsJob::JobSucceeded;
            emitResult();
            emit resultsReady(this);
            break;
        }
        const QStringView currentLine = m_outputLines.at(m_currentLine);
        if (currentLine.isEmpty()) {
            ++m_currentLine;
            continue;
        }
        bool revOk;
        const auto revision = leftOfNeedleOrEntireView(currentLine, QLatin1Char{' '}).toULong(&revOk);
        if (!revOk) {
            // Future compatibility - not a revision yet
            ++m_currentLine;
            continue;
        }
        auto i = m_commits.find(revision);
        if (i != m_commits.end()) {
            KDevelop::VcsAnnotationLine line;
            line.setAuthor(i.value().author());
            line.setCommitMessage(i.value().message());
            line.setDate(i.value().date());
            line.setLineNumber(m_currentLine);
            line.setRevision(i.value().revision());
            m_results.append(QVariant::fromValue(line));
            ++m_currentLine;
            continue;
        } else {
            prepareCommitInfo(revision);
            break;  //Will reenter this function when commit info will be ready
        }
    }
}

void BzrAnnotateJob::prepareCommitInfo(std::size_t revision)
{
    if (m_status != KDevelop::VcsJob::JobRunning)
        return;
    auto* job = new KDevelop::DVcsJob(m_workingDir, m_vcsPlugin, KDevelop::OutputJob::Silent);
    job->setType(KDevelop::VcsJob::Log);
    *job << "bzr" << "log" << "--long" << "-r" << QString::number(revision);
    connect(job, &DVcsJob::readyForParsing, this, &BzrAnnotateJob::parseBzrLog);
    m_job = job;
    job->start();
}

/*
 * This is slightly different from BazaarUtils::parseBzrLogPart(...).
 * This function parses only commit general info. It does not parse signle
 * actions. In fact output parsed by this function is slightly different
 * from output parsed by BazaarUtils. As a result parsing this output using
 * BazaarUtils would yield different results.
 * NOTE: This is all about parsing 'message'.
 */
void BzrAnnotateJob::parseBzrLog(KDevelop::DVcsJob* job)
{
    const QStringList outputLines = job->output().split(QLatin1Char('\n'));
    KDevelop::VcsEvent commitInfo;
    int revision=-1;
    bool atMessage = false;
    QString message;
    for (const QString& line : outputLines) {
        if (!atMessage) {
            if (line.startsWith(QLatin1String("revno"))) {
                QString revno = line.mid(QStringLiteral("revno: ").length());
                // In future there is possibility that "revno: " will change to
                // "revno??". If that's all, then we recover matching only
                // "revno" prefix and assuming placeholder of length 2 (": " or
                // "??").
                // The same below with exception of "committer" which possibly
                // can have also some suffix which changes meaning like
                // "committer-some_property: "...
                revno = revno.left(revno.indexOf(QLatin1Char(' ')));
                revision = revno.toInt();
                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 {
            message += line.trimmed() + QLatin1Char('\n');
        }
    }
    if (atMessage)
        commitInfo.setMessage(message.trimmed());
    Q_ASSERT(revision!=-1);
    m_commits[revision] = commitInfo;
    // Invoke from event loop to protect against stack overflow (it could happen
    // on very big files with very big history of changes if tail-recursion
    // optimization had failed here).
    QTimer::singleShot(0, this, &BzrAnnotateJob::parseNextLine);
}

QVariant BzrAnnotateJob::fetchResults()
{
    return m_results;
}

KDevelop::VcsJob::JobStatus BzrAnnotateJob::status() const
{
    return m_status;
}

KDevelop::IPlugin* BzrAnnotateJob::vcsPlugin() const
{
    return m_vcsPlugin;
}

#include "moc_bzrannotatejob.cpp"