Repository navigation
Expand file tree
/
Copy pathgitinterface.cpp
More file actions
215 lines (175 loc) · 7.14 KB
/
Copy pathgitinterface.cpp
File metadata and controls
215 lines (175 loc) · 7.14 KB
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
/* This file is part of Kit.
Copyright (C) 2017 Alexander Bikadorov
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 3 of the License, or
(at your option) any later version.
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 "gitinterface.h"
#include <QDebug>
#include <QDir>
GitInterface::GitInterface(QObject *parent) : QObject(parent), m_root(), m_watcher(this) {
connect(&m_watcher, &QFileSystemWatcher::fileChanged, this, &GitInterface::slotFileChanged);
}
void GitInterface::startUpdate(const QUrl &url) {
const QUrl root = gitRoot(url);
if (root == m_root) {
// nothing to do
return;
}
m_root = root;
updateBranches(url);
updateStatus(url);
updateStashes(url);
const QStringList watchingFiles = m_watcher.files();
if (!watchingFiles.isEmpty()) {
m_watcher.removePaths(watchingFiles);
}
const QDir gitDir = QDir(QDir(m_root.path()).filePath(".git"));
m_watcher.addPath(gitDir.filePath("index")); // changes: status
m_watcher.addPath(gitDir.filePath("HEAD")); // changes: branches
}
void GitInterface::checkout(const QString &branchName) {
gitOutput(argStart(m_root)
// TODO does not work
//<< "read-tree" << "-um" << "HEAD" << branchname
<< "checkout" << branchName);
// TODO show error output
emit repoChanged();
startUpdate(m_root);
}
void GitInterface::updateBranches(const QUrl &url) {
QList<BranchEntry> branchList;
if (url.isEmpty()) {
emit updatedBranches(branchList);
return;
}
const QString output =
gitOutput(argStart(url) << "for-each-ref"
<< "--shell"
<< "--format=%(HEAD) %(refname:short) %(authordate:iso8601)"
<< "refs/heads/");
const QStringList branches = output.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
// parse output; example line: "'*' 'master' '2016-02-11 16:09:38 +0100'"
for (const QString line : branches) {
const QStringList values = line.split("'", QString::SkipEmptyParts);
const bool isHead = values[0] == "*";
const QString name = values[2];
const QDateTime date = QDateTime::fromString(values[4], Qt::ISODate);
branchList.append(BranchEntry(name, date, isHead));
}
emit updatedBranches(branchList);
}
void GitInterface::updateStatus(const QUrl &url) {
QList<StatusEntry> statusList;
if (url.isEmpty()) {
emit updatedStatus(statusList);
return;
}
// NOTE: "porcelain" but stable output
const QString output = gitOutput(argStart(url) << "status" << "-z");
const QStringList lines = output.split(QChar::Tabulation, QString::SkipEmptyParts);
for (int i=0; i < lines.length(); i++) {
const QString line = lines[i];
const FileStatus statusIndex = static_cast<FileStatus>(line[0].toLatin1());
const FileStatus statusTree = static_cast<FileStatus>(line[1].toLatin1());
const QString file = line.mid(3);
statusList.append(StatusEntry(file, statusIndex, statusTree,
// if renamed next line is the file renamed file
statusIndex == STATUS_RENAMED ? lines[++i] : QString()));
}
emit updatedStatus(statusList);
}
void GitInterface::updateStashes(const QUrl &url)
{
// NOTE: 'git-stash' is only a shell script.
// See https://github.com/git/git/blob/master/git-stash.sh
QList<StashEntry> stashList;
if (url.isEmpty()) {
emit updatedStashes(stashList);
return;
}
// do we have at least one stash?
bool noStash = gitExitCode(argStart(url) << "rev-parse" << "--verify" << "refs/stash");
if (noStash) {
emit updatedStashes(stashList);
return;
}
// TODO porcelain
const QString output = gitOutput(argStart(url) << "log"
<< "--format='%gd' '%gs'"
<< "-g" << "--first-parent" <<"-m" << "refs/stash" << "--");
const QStringList branches = output.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
// parse output; example line: 'stash@{0}' 'WIP on master: 99db58d Separated header'"
for (const QString line : branches) {
const QStringList values = line.split("'", QString::SkipEmptyParts);
stashList.append(StashEntry(values[0], values[2]));
}
emit updatedStashes(stashList);
}
QUrl GitInterface::gitRoot(const QUrl &url) {
const QString output = gitOutput(argStart(url) << "rev-parse"
<< "--show-toplevel");
return QUrl::fromLocalFile(output.trimmed());
}
QString GitInterface::gitOutput(const QStringList &arguments) {
QProcess *gitProcess = gitRun(arguments);
if (gitProcess->exitCode()) {
qDebug() << "process failed; prog=" << gitProcess->program()
<< " args=" << gitProcess->arguments() << " "
<< " exitCode=" << gitProcess->exitCode()
<< " errOutput: " << gitProcess->readAllStandardError();
}
QByteArray rawOutput = gitProcess->readAllStandardOutput();
// 'git status -z' divides by \0 character
return QString::fromUtf8(rawOutput.replace(QChar::Null, QChar::Tabulation));
}
int GitInterface::gitExitCode(const QStringList &arguments) {
QProcess *gitProcess = gitRun(arguments);
return gitProcess->exitCode();
}
QProcess *GitInterface::gitRun(const QStringList &arguments) {
QProcess *gitProcess = new QProcess(this);
gitProcess->deleteLater();
// TODO absolute path?
gitProcess->start("/usr/bin/git", arguments, QIODevice::ReadOnly);
// TODO blocking
bool success = gitProcess->waitForFinished(3000);
if (!success) {
qDebug() << "process error; prog=" << gitProcess->program()
<< " args=" << gitProcess->arguments() << " "
<< " error=" << gitProcess->error();
}
return gitProcess;
}
QStringList GitInterface::argStart(const QUrl &url) {
QStringList arguments;
arguments << "-C" << url.toLocalFile();
return arguments;
}
FileStatus GitInterface::letterToStatus(const QChar &c) {
switch (c.toLatin1()) {
case ' ': return STATUS_UNCHANGED;
case 'A': return STATUS_ADDED;
}
return STATUS_IGNORED;
if (c == 'A') return STATUS_ADDED;
if (c == 'M') return STATUS_MODIFIED;
}
void GitInterface::slotFileChanged(const QString &path) {
qDebug() << "file changed: " << path;
const QString name = QFileInfo(path).fileName();
if (name == "index") {
updateStatus(m_root);
} else if (name == "HEAD") {
updateBranches(m_root);
}
// bug in QFileSystemWatcher, file not watched anymore
m_watcher.addPath(path);
}