-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGit.cpp
More file actions
197 lines (166 loc) · 5.1 KB
/
Git.cpp
File metadata and controls
197 lines (166 loc) · 5.1 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
#include "Git.h"
#include "Settings.h"
#include "Exceptions.h"
#include <QFileInfo>
QString Git::git_exe_override = "";
bool Git::isRepo(QString dir)
{
return QFile::exists(dir + "/.git");
}
QHash<QString, GitStatus> Git::status(QString dir)
{
//execute
QProcess process;
process.setProcessChannelMode(QProcess::MergedChannels);
process.start(gitExe(), QStringList() << "-C" << dir << "status" << "--porcelain");
if (!process.waitForFinished(-1)) throwException(process);
QStringList text = QString(process.readAll()).split("\n");
//convert git status lines to hash
QHash<QString, GitStatus> output;
foreach(QString line, text)
{
line = line.trimmed();
if (line.isEmpty()) continue;
line = line.replace(" ", " ").replace("\"", ""); //handle escaping of files with spaces
int pos = line.indexOf(' ');
if (pos==-1) continue;
//convert status to enum
QString status = line.left(pos);
GitStatus status_enum = GitStatus::NOT_VERSIONED;
if (status=="M")
{
status_enum = GitStatus::MODIFIED;
}
else if (status=="D")
{
status_enum = GitStatus::DELETED;
}
else if (status=="A" || status=="AM")
{
status_enum = GitStatus::ADDED;
}
else if (status=="??")
{
status_enum = GitStatus::NOT_VERSIONED;
}
else
{
THROW(FileParseException, "Git status '" + status + "' not known!");
}
QString filename = dir + "/" + line.mid(pos+1);
filename = filename.replace("//", "/"); //remove duplicated slashes
if (status_enum!=GitStatus::DELETED) filename = QFileInfo(filename).canonicalFilePath();
output[filename] = status_enum;
}
return output;
}
bool Git::pullAvailable(QString dir)
{
QString git_exe = gitExe();
//update remote info
QProcess process;
process.setProcessChannelMode(QProcess::MergedChannels);
process.start(git_exe, QStringList() << "-C" << dir << "remote" << "update");
if (!process.waitForFinished(-1)) throwException(process);
QByteArrayList output = process.readAll().split('\n');
//get status
process.start(git_exe, QStringList() << "-C" << dir << "status" << "-u" << "no");
if (!process.waitForFinished(-1)) throwException(process);
output = process.readAll().split('\n');
//check status
foreach(const QByteArray& line, output)
{
if (line.contains("Your branch is behind") && line.contains("can be fast-forwarded")) return true;
}
return false;
}
bool Git::pushAvailable(QString dir)
{
//get status
QProcess process;
process.start(gitExe(), QStringList() << "-C" << dir << "status" << "-u" << "no");
if (!process.waitForFinished(-1)) throwException(process);
QByteArrayList output = process.readAll().split('\n');
//check status
foreach(const QByteArray& line, output)
{
if (line.contains("Your branch is ahead of")) return true;
}
return false;
}
QByteArray Git::pull(QString dir)
{
QString git_exe = gitExe();
QProcess process;
process.setProcessChannelMode(QProcess::MergedChannels);
process.setWorkingDirectory(dir);
process.start(git_exe, QStringList() << "pull" << "--recurse-submodules");
if (!process.waitForFinished(-1)) throwException(process);
return process.readAll();
}
QByteArray Git::push(QString dir)
{
QString git_exe = gitExe();
QProcess process;
process.setProcessChannelMode(QProcess::MergedChannels);
process.setWorkingDirectory(dir);
process.start(git_exe, QStringList() << "push");
if (!process.waitForFinished(-1)) throwException(process);
return process.readAll();
}
QByteArray Git::stash(QString dir)
{
QString git_exe = gitExe();
QProcess process;
process.setProcessChannelMode(QProcess::MergedChannels);
process.setWorkingDirectory(dir);
process.start(git_exe, QStringList() << "stash");
if (!process.waitForFinished(-1)) throwException(process);
return process.readAll();
}
QByteArray Git::stashPop(QString dir)
{
QString git_exe = gitExe();
QProcess process;
process.setProcessChannelMode(QProcess::MergedChannels);
process.setWorkingDirectory(dir);
process.start(git_exe, QStringList() << "stash" << "pop");
if (!process.waitForFinished(-1)) throwException(process);
return process.readAll();
}
QByteArray Git::branch(QString dir)
{
QString git_exe = gitExe();
QProcess process;
process.setProcessChannelMode(QProcess::MergedChannels);
process.setWorkingDirectory(dir);
process.start(git_exe, QStringList() << "branch");
if (!process.waitForFinished(-1)) throwException(process);
QByteArrayList lines = process.readAll().split('\n');
foreach(QByteArray line, lines)
{
line = line.trimmed();
if (line.startsWith("* "))
{
QByteArray branch = line.mid(2);
if (branch=="master" || branch=="main") continue;
return branch;
}
}
return "";
}
void Git::setGitOverride(QString exe)
{
git_exe_override = exe;
}
QString Git::gitExe()
{
QString exe = git_exe_override;
if (exe=="") exe = Settings::string("git_exe", true);
if (exe=="") THROW(InformationMissingException, "Git executable not found in settings!");
return exe;
}
void Git::throwException(QProcess& process)
{
THROW(Exception, "Could not execute '"+process.program() + " " + process.arguments().join(" ")+"'\n\nERROR:\n" + process.errorString() + "\nOutput:\n"+process.readAll());
}