-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathgit.cpp
133 lines (125 loc) · 3.55 KB
/
git.cpp
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
#include "git.h"
#include "smallUsefulFunctions.h"
#include "buildmanager.h"
GIT::GIT(QObject *parent) : QObject(parent)
{
}
/*!
* Return a filename suitable to pass to an GIT command line call. This does:
* 1. enquote the filename in double qoutes.
* 2. add '@' at the end if the filename contains an @. This prevents the interpretation
* of the contained @ as revision marker. See https://stackoverflow.com/a/757510/2726279
*/
QString GIT::quote(QString filename) {
if (filename.contains('@')) {
filename += '@';
}
return quotePath(filename);
}
/*!
* \brief generate string which contains command "GIT action args"
* \param action
* \param args
* \return
*/
QString GIT::makeCmd(QString action, QString args)
{
return BuildManager::CMD_GIT + " " + action + " " + args;
}
/*!
* \brief GIT commit filename
* \param filename
* \param message commit message
*/
void GIT::commit(QString filename, QString message)
{
runGit("add", quote(filename));
runGit("commit", "-m " + enquoteStr(message) + " " + quote(filename));
}
/*!
* \brief GIT push
* \param filename
*/
void GIT::push(QString filename)
{
runGit("push", quote(filename),"");
}
/*!
* \brief get GIT status filename
* \param filename
* \return
*/
GIT::Status GIT::status(QString filename)
{
QString output = runGit("status -s", quote(filename));
output=output.trimmed(); // git delivers a leading space sometimes (?)
if (output.isEmpty()) return GIT::CheckedIn;
if (output.startsWith("?")) return GIT::Unmanaged;
if (output.startsWith("fatal")) return GIT::NoRepository;
if (output.startsWith("M")) return GIT::Modified;
if (output.startsWith("C")) return GIT::InConflict;
if (output.startsWith("L")) return GIT::Locked;
return GIT::Unknown;
}
/*!
* \brief get GIT log
* \param filename
* \return
*/
QStringList GIT::log(QString filename)
{
const QString path = QFileInfo(filename).absolutePath();
QString fn = QFileInfo(filename).fileName();
QString output = runGit("log --pretty='%h %s@@@'", quote(path),quote(fn));
#if (QT_VERSION>=QT_VERSION_CHECK(5,14,0))
QStringList revisions = output.split("@@@", Qt::SkipEmptyParts);
#else
QStringList revisions = output.split("@@@", QString::SkipEmptyParts);
#endif
// circumvent strange behaviour of git adding \n now and then ...
for(QString& elem:revisions){
elem=elem.simplified();
}
if(!revisions.isEmpty() && revisions.last().isEmpty()){
revisions.takeLast();
}
return revisions;
}
/*!
* \brief create a GIT repository
* \param filename
*/
void GIT::createRepository(QString filename)
{
const QString path = QFileInfo(filename).absolutePath();
//setStatusMessageProcess(QString(" GIT create repo "));
runGit("init", quote(path));
}
/*!
* \brief run GIT command
* \param action
* \param args
* \return
*/
QString GIT::runGit(QString action, QString args)
{
QString output;
emit statusMessage(QString(" GIT %1 ").arg(action));
emit runCommand(makeCmd(action, args), &output);
return output;
}
/*!
* \brief run GIT command
* \param action
* \param path
* \param args
* \return
* This variant is specifically useful for git log as it can't handle absolute file paths
*/
QString GIT::runGit(QString action,QString path, QString args)
{
QString output;
emit statusMessage(QString(" GIT %1 ").arg(action));
emit runCommand(makeCmd("-C "+path+" "+action, args), &output);
return output;
}