1 | #include "GitSubmodules.h" |
2 | |
3 | #include <GitBase.h> |
4 | #include <QLogger.h> |
5 | |
6 | #include <QFile> |
7 | #include <QTemporaryFile> |
8 | #include <QTextStream> |
9 | #include <QVector> |
10 | |
11 | using namespace QLogger; |
12 | |
13 | GitSubmodules::GitSubmodules(const QSharedPointer<GitBase> &gitBase) |
14 | : mGitBase(gitBase) |
15 | { |
16 | } |
17 | |
18 | QVector<QString> GitSubmodules::getSubmodules() |
19 | { |
20 | QLog_Debug("Git" , QString("Getting submodules" )); |
21 | |
22 | const auto cmd = QString("git config --file .gitmodules --name-only --get-regexp path" ); |
23 | |
24 | QLog_Trace("Git" , QString("Getting submodules: {%1}" ).arg(cmd)); |
25 | |
26 | QVector<QString> submodulesList; |
27 | |
28 | if (const auto ret = mGitBase->run(cmd); ret.success) |
29 | { |
30 | const auto submodules = ret.output.split('\n'); |
31 | |
32 | for (const auto &submodule : submodules) |
33 | { |
34 | if (!submodule.isEmpty() && submodule != "\n" ) |
35 | submodulesList.append(submodule.split('.').at(1)); |
36 | } |
37 | } |
38 | |
39 | return submodulesList; |
40 | } |
41 | |
42 | bool GitSubmodules::submoduleAdd(const QString &url, const QString &name) |
43 | { |
44 | QLog_Debug("Git" , QString("Adding a submodule: {%1} {%2}" ).arg(url, name)); |
45 | |
46 | const auto cmd = QString("git submodule add %1 %2" ).arg(url, name); |
47 | |
48 | QLog_Trace("Git" , QString("Adding a submodule: {%1}" ).arg(cmd)); |
49 | |
50 | const auto ret = mGitBase->run(cmd).success; |
51 | |
52 | return ret; |
53 | } |
54 | |
55 | bool GitSubmodules::submoduleUpdate(const QString &submodule) |
56 | { |
57 | if (submodule.isEmpty()) |
58 | QLog_Debug("Git" , QString("Updating all submodules" )); |
59 | else |
60 | QLog_Debug("Git" , QString("Updating submodule: {%1}" ).arg(submodule)); |
61 | |
62 | auto cmd = QString("git submodule update --init --recursive" ); |
63 | |
64 | if (!submodule.isEmpty()) |
65 | cmd.append(QString(" %1" ).arg(submodule)); |
66 | |
67 | QLog_Trace("Git" , QString("Updating submodules: {%1}" ).arg(cmd)); |
68 | |
69 | const auto ret = mGitBase->run(cmd).success; |
70 | |
71 | return ret; |
72 | } |
73 | |
74 | bool GitSubmodules::submoduleRemove(const QString &submodule) |
75 | { |
76 | QLog_Debug("Git" , QString("Removing a submodule: {%1}" ).arg(submodule)); |
77 | |
78 | auto cmd = QString("git submodule deinit -f %1" ).arg(submodule); |
79 | |
80 | QLog_Trace("Git" , QString("Deinitializing the submodule: {%1}" ).arg(cmd)); |
81 | |
82 | auto ret = mGitBase->run(cmd); |
83 | |
84 | cmd = QString("git rm -f --cached %1" ).arg(submodule); |
85 | |
86 | QLog_Trace("Git" , QString("Removing cache: {%1}" ).arg(cmd)); |
87 | |
88 | ret = mGitBase->run(cmd); |
89 | |
90 | cmd = QString("rm -rf %1/.git/modules/%2" ).arg(mGitBase->getGitDir(), submodule); |
91 | |
92 | QLog_Trace("Git" , QString("Removing the submodule: {%1}" ).arg(cmd)); |
93 | |
94 | ret = mGitBase->run(cmd); |
95 | |
96 | QFile gitmodules(QString("%1/.gitmodules" ).arg(mGitBase->getWorkingDir())); |
97 | QTemporaryFile gitTmp; |
98 | |
99 | if (gitmodules.open(QIODevice::ReadOnly) && gitTmp.open()) |
100 | { |
101 | QTextStream out(&gitmodules); |
102 | QTextStream in(&gitTmp); |
103 | auto removed = false; |
104 | |
105 | while (!out.atEnd()) |
106 | { |
107 | auto line = out.readLine(); |
108 | |
109 | if (line.contains(QString("[submodule \"%1\"]" ).arg(submodule))) |
110 | { |
111 | out.readLine(); |
112 | out.readLine(); |
113 | removed = true; |
114 | } |
115 | else |
116 | in << line.append('\n'); |
117 | } |
118 | gitmodules.close(); |
119 | gitTmp.close(); |
120 | |
121 | if (removed) |
122 | { |
123 | gitmodules.remove(); |
124 | gitTmp.copy(QString("%1/.gitmodules" ).arg(mGitBase->getWorkingDir())); |
125 | } |
126 | } |
127 | |
128 | return ret.success; |
129 | } |
130 | |