1#include "GitConfig.h"
2
3#include <GitBase.h>
4#include <GitCloneProcess.h>
5
6#include <QLogger.h>
7
8using namespace QLogger;
9
10bool GitUserInfo::isValid() const
11{
12 return !mUserEmail.isNull() && !mUserEmail.isEmpty() && !mUserName.isNull() && !mUserName.isEmpty();
13}
14
15GitConfig::GitConfig(QSharedPointer<GitBase> gitBase, QObject *parent)
16 : QObject(parent)
17 , mGitBase(gitBase)
18{
19}
20
21GitUserInfo GitConfig::getGlobalUserInfo() const
22{
23 GitUserInfo userInfo;
24
25 QLog_Debug("Git", QString("Getting global user info"));
26
27 const auto nameRequest = mGitBase->run("git config --get --global user.name");
28
29 if (nameRequest.success)
30 userInfo.mUserName = nameRequest.output.trimmed();
31
32 const auto emailRequest = mGitBase->run("git config --get --global user.email");
33
34 if (emailRequest.success)
35 userInfo.mUserEmail = emailRequest.output.trimmed();
36
37 return userInfo;
38}
39
40void GitConfig::setGlobalUserInfo(const GitUserInfo &info)
41{
42 QLog_Debug("Git", QString("Setting global user info"));
43
44 mGitBase->run(QString("git config --global user.name \"%1\"").arg(info.mUserName));
45 mGitBase->run(QString("git config --global user.email %1").arg(info.mUserEmail));
46}
47
48GitExecResult GitConfig::setGlobalData(const QString &key, const QString &value)
49{
50 QLog_Debug("Git", QString("Configuring global key {%1} with value {%2}").arg(key, value));
51
52 const auto ret = mGitBase->run(QString("git config --global %1 \"%2\"").arg(key, value));
53
54 return ret;
55}
56
57GitUserInfo GitConfig::getLocalUserInfo() const
58{
59 QLog_Debug("Git", QString("Getting local user info"));
60
61 GitUserInfo userInfo;
62
63 const auto nameRequest = mGitBase->run("git config --get --local user.name");
64
65 if (nameRequest.success)
66 userInfo.mUserName = nameRequest.output.trimmed();
67
68 const auto emailRequest = mGitBase->run("git config --get --local user.email");
69
70 if (emailRequest.success)
71 userInfo.mUserEmail = emailRequest.output.trimmed();
72
73 return userInfo;
74}
75
76void GitConfig::setLocalUserInfo(const GitUserInfo &info)
77{
78 QLog_Debug("Git", QString("Setting local user info"));
79
80 mGitBase->run(QString("git config --local user.name \"%1\"").arg(info.mUserName));
81 mGitBase->run(QString("git config --local user.email %1").arg(info.mUserEmail));
82}
83
84GitExecResult GitConfig::setLocalData(const QString &key, const QString &value)
85{
86 QLog_Debug("Git", QString("Configuring local key {%1} with value {%2}").arg(key, value));
87
88 const auto ret = mGitBase->run(QString("git config --local %1 \"%2\"").arg(key, value));
89
90 return ret;
91}
92
93GitExecResult GitConfig::clone(const QString &url, const QString &fullPath)
94{
95 QLog_Debug("Git", QString("Starting the clone process for repo {%1} at {%2}.").arg(url, fullPath));
96
97 const auto asyncRun = new GitCloneProcess(mGitBase->getWorkingDir());
98 connect(asyncRun, &GitCloneProcess::signalProgress, this, &GitConfig::signalCloningProgress, Qt::DirectConnection);
99 connect(asyncRun, &GitCloneProcess::signalCloningFailure, this, &GitConfig::signalCloningFailure,
100 Qt::DirectConnection);
101
102 mGitBase->setWorkingDir(fullPath);
103
104 return asyncRun->run(QString("git clone --progress %1 %2").arg(url, fullPath));
105}
106
107GitExecResult GitConfig::initRepo(const QString &fullPath)
108{
109 QLog_Debug("Git", QString("Initializing a new repository at {%1}").arg(fullPath));
110
111 const auto ret = mGitBase->run(QString("git init %1").arg(fullPath));
112
113 if (ret.success)
114 mGitBase->setWorkingDir(fullPath);
115
116 return ret;
117}
118
119GitExecResult GitConfig::getLocalConfig() const
120{
121 QLog_Debug("Git", QString("Getting local config"));
122
123 const auto ret = mGitBase->run("git config --local --list");
124
125 return ret;
126}
127
128GitExecResult GitConfig::getGlobalConfig() const
129{
130 QLog_Debug("Git", QString("Getting global config"));
131
132 const auto ret = mGitBase->run("git config --global --list");
133
134 return ret;
135}
136
137GitExecResult GitConfig::getRemoteForBranch(const QString &branch)
138{
139 QLog_Debug("Git", QString("Getting remote for branch {%1}.").arg(branch));
140
141 const auto config = getLocalConfig();
142
143 if (config.success)
144 {
145#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
146 const auto values = config.output.split('\n', Qt::SkipEmptyParts);
147#else
148 const auto values = config.output.split('\n', QString::SkipEmptyParts);
149#endif
150 const auto configKey = QString("branch.%1.remote=").arg(branch);
151 QString configValue;
152
153 for (const auto &value : values)
154 {
155 if (value.startsWith(configKey))
156 {
157 configValue = value.split("=").last();
158 break;
159 }
160 }
161
162 if (!configValue.isEmpty())
163 return { true, configValue };
164 }
165
166 return GitExecResult();
167}
168
169GitExecResult GitConfig::getGitValue(const QString &key) const
170{
171 QLog_Debug("Git", QString("Getting value for config key {%1}").arg(key));
172
173 const auto ret = mGitBase->run(QString("git config --get %1").arg(key));
174
175 return ret;
176}
177
178QString GitConfig::getServerUrl() const
179{
180 auto serverUrl = getGitValue("remote.origin.url").output.trimmed();
181
182 if (serverUrl.startsWith("git@"))
183 {
184 serverUrl.remove("git@");
185 serverUrl.replace(":", "/");
186 }
187
188 serverUrl = serverUrl.mid(0, serverUrl.lastIndexOf("."));
189
190 return serverUrl;
191}
192
193QString GitConfig::getServerHost() const
194{
195 auto serverUrl = getGitValue("remote.origin.url").output.trimmed();
196
197 if (serverUrl.startsWith("git@"))
198 {
199 serverUrl.remove("git@");
200 serverUrl.replace(":", "/");
201 }
202 else if (serverUrl.startsWith("https://"))
203 {
204 serverUrl.remove("https://");
205 }
206
207 serverUrl = serverUrl.mid(0, serverUrl.indexOf("/"));
208
209 return serverUrl;
210}
211
212QPair<QString, QString> GitConfig::getCurrentRepoAndOwner() const
213{
214 auto serverUrl = getGitValue("remote.origin.url").output.trimmed();
215 QString repo;
216
217 if (serverUrl.startsWith("git@"))
218 {
219 serverUrl.remove("git@");
220 repo = serverUrl.mid(serverUrl.lastIndexOf(":") + 1);
221 serverUrl.replace(":", "/");
222 }
223 else if (serverUrl.startsWith("https://"))
224 {
225 serverUrl.remove("https://");
226 repo = serverUrl.mid(serverUrl.indexOf("/") + 1);
227 }
228
229 serverUrl = serverUrl.mid(0, serverUrl.indexOf("/"));
230 repo.remove(".git");
231
232 const auto parts = repo.split("/");
233
234 return qMakePair(parts.constFirst(), parts.constLast());
235}
236