1 | #include "GitPatches.h" |
2 | |
3 | #include <GitBase.h> |
4 | #include <QLogger.h> |
5 | |
6 | using namespace QLogger; |
7 | |
8 | #include <QFile> |
9 | |
10 | GitPatches::GitPatches(const QSharedPointer<GitBase> &gitBase) |
11 | : mGitBase(gitBase) |
12 | { |
13 | } |
14 | |
15 | GitExecResult GitPatches::exportPatch(const QStringList &shaList) |
16 | { |
17 | QLog_Debug("Git" , QString("Executing exportPatch: {%1}" ).arg(shaList.join("," ))); |
18 | |
19 | auto val = 1; |
20 | QStringList files; |
21 | |
22 | for (const auto &sha : shaList) |
23 | { |
24 | const auto cmd = QString("git format-patch -1 %1" ).arg(sha); |
25 | |
26 | QLog_Trace("Git" , QString("Exporting patch: {%1}" ).arg(cmd)); |
27 | |
28 | const auto ret = mGitBase->run(cmd); |
29 | |
30 | if (!ret.success) |
31 | break; |
32 | else |
33 | { |
34 | auto filename = ret.output; |
35 | filename = filename.remove("\n" ); |
36 | const auto text = filename.mid(filename.indexOf("-" ) + 1); |
37 | const auto number = QString("%1" ).arg(val, 4, 10, QChar('0')); |
38 | const auto newFileName = QString("%1-%2" ).arg(number, text); |
39 | files.append(newFileName); |
40 | |
41 | QFile::rename(QString("%1/%2" ).arg(mGitBase->getWorkingDir(), filename), |
42 | QString("%1/%2" ).arg(mGitBase->getWorkingDir(), newFileName)); |
43 | ++val; |
44 | } |
45 | } |
46 | |
47 | if (val != shaList.count()) |
48 | QLog_Error("Git" , QString("Problem generating patches. Stop after {%1} iterations" ).arg(val)); |
49 | |
50 | return qMakePair(true, files.join("<br>" )); |
51 | } |
52 | |
53 | bool GitPatches::applyPatch(const QString &fileName, bool asCommit) |
54 | { |
55 | QLog_Debug("Git" , QString("Applying patch: {%1} %2" ).arg(fileName, asCommit ? QString("as commit." ) : QString())); |
56 | |
57 | auto cmd = asCommit ? QString("git am --signof " ) : QString("git apply " ); |
58 | |
59 | cmd.append(fileName); |
60 | |
61 | QLog_Trace("Git" , QString("Applaying patch: {%1}" ).arg(cmd)); |
62 | |
63 | const auto ret = mGitBase->run(cmd); |
64 | |
65 | return ret.success; |
66 | } |
67 | |
68 | GitExecResult GitPatches::stagePatch(const QString &fileName) const |
69 | { |
70 | QLog_Debug("Git" , QString("Staging patch: {%1}" ).arg(fileName)); |
71 | |
72 | const auto cmd = QString("git apply --cached %1" ).arg(fileName); |
73 | |
74 | QLog_Trace("Git" , QString("Staging patch: {%1}" ).arg(cmd)); |
75 | |
76 | return mGitBase->run(cmd); |
77 | } |
78 | |