1#include <GitBase.h>
2#include <GitQlientSettings.h>
3#include <GitSubtree.h>
4
5#include <QLogger.h>
6
7using namespace QLogger;
8
9GitSubtree::GitSubtree(const QSharedPointer<GitBase> &gitBase)
10 : mGitBase(gitBase)
11{
12}
13
14GitExecResult GitSubtree::add(const QString &url, const QString &ref, const QString &name, bool squash)
15{
16 QLog_Debug("UI", "Adding a subtree");
17
18 GitQlientSettings settings(mGitBase->getGitDir());
19
20 for (auto i = 0;; ++i)
21 {
22 const auto repo = settings.localValue(QString("Subtrees/%1.prefix").arg(i)).toString();
23
24 if (repo == name)
25 {
26 settings.setLocalValue(QString("Subtrees/%1.url").arg(i), url);
27 settings.setLocalValue(QString("Subtrees/%1.ref").arg(i), ref);
28
29 auto cmd = QString("git subtree add --prefix=%1 %2 %3").arg(name, url, ref);
30
31 QLog_Trace("Git", QString("Adding a subtree: {%1}").arg(cmd));
32
33 if (squash)
34 cmd.append(" --squash");
35
36 auto ret = mGitBase->run(cmd);
37
38 if (ret.output.contains("Cannot"))
39 ret.success = false;
40
41 return ret;
42 }
43 else if (repo.isEmpty())
44 {
45 settings.setLocalValue(QString("Subtrees/%1.prefix").arg(i), name);
46 settings.setLocalValue(QString("Subtrees/%1.url").arg(i), url);
47 settings.setLocalValue(QString("Subtrees/%1.ref").arg(i), ref);
48
49 QLog_Trace("Git", QString("Updating subtree info: {%1}").arg(name));
50
51 return { true, "" };
52 }
53 }
54
55 return { false, "" };
56}
57
58GitExecResult GitSubtree::pull(const QString &url, const QString &ref, const QString &prefix) const
59{
60 QLog_Debug("UI", "Pulling a subtree");
61
62 const auto cmd = QString("git subtree pull --prefix=%1 %2 %3").arg(prefix, url, ref);
63
64 QLog_Trace("Git", QString("Pulling a subtree: {%1}").arg(cmd));
65
66 auto ret = mGitBase->run(cmd);
67
68 if (ret.output.contains("Cannot"))
69 ret.success = false;
70
71 return ret;
72}
73
74GitExecResult GitSubtree::push(const QString &url, const QString &ref, const QString &prefix) const
75{
76 QLog_Debug("UI", "Pushing changes to a subtree");
77
78 const auto cmd = QString("git subtree push --prefix=%1 %2 %3").arg(prefix, url, ref);
79
80 QLog_Trace("Git", QString("Pushing changes to a subtree: {%1}").arg(cmd));
81
82 auto ret = mGitBase->run(cmd);
83
84 if (ret.output.contains("Cannot"))
85 ret.success = false;
86
87 return ret;
88}
89
90GitExecResult GitSubtree::merge(const QString &sha) const
91{
92 QLog_Debug("UI", "Merging changes from the remote of a subtree");
93
94 const auto cmd = QString("git subtree merge %1").arg(sha);
95
96 QLog_Trace("Git", QString("Merging changes from the remote of a subtree: {%1}").arg(cmd));
97
98 auto ret = mGitBase->run(cmd);
99
100 if (ret.output.contains("Cannot"))
101 ret.success = false;
102
103 return ret;
104}
105
106GitExecResult GitSubtree::list() const
107{
108 QLog_Debug("UI", "Listing all subtrees");
109
110 const auto cmd = QString("git log --pretty=format:%b --grep=git-subtree-dir");
111
112 QLog_Trace("Git", QString("Listing all subtrees: {%1}").arg(cmd));
113
114 return mGitBase->run(cmd);
115}
116