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
|
/***************************************************************************
* 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 "bzrannotatejob.h"
#include <functional>
#include <QTimer>
#include <QDateTime>
#include <QDir>
#include <vcs/dvcs/dvcsjob.h>
#include <vcs/vcsannotation.h>
#include <vcs/vcsrevision.h>
#include <interfaces/iplugin.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;
}
QString currentLine = m_outputLines[m_currentLine];
if (currentLine.isEmpty()) {
++m_currentLine;
continue;
}
bool revOk;
auto revision = currentLine.leftRef(currentLine.indexOf(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;
}
|