| 1 | #include "GitMerge.h" |
| 2 | |
| 3 | #include <GitBase.h> |
| 4 | #include <GitRepoLoader.h> |
| 5 | #include <GitWip.h> |
| 6 | #include <QLogger.h> |
| 7 | |
| 8 | #include <QFile> |
| 9 | |
| 10 | using namespace QLogger; |
| 11 | |
| 12 | GitMerge::GitMerge(const QSharedPointer<GitBase> &gitBase, QSharedPointer<GitCache> cache) |
| 13 | : mGitBase(gitBase) |
| 14 | , mCache(cache) |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | bool GitMerge::isInMerge() const |
| 19 | { |
| 20 | QFile mergeHead(QString("%1/MERGE_HEAD" ).arg(mGitBase->getGitDir())); |
| 21 | |
| 22 | return mergeHead.exists(); |
| 23 | } |
| 24 | |
| 25 | GitExecResult GitMerge::merge(const QString &into, QStringList sources) |
| 26 | { |
| 27 | QLog_Debug("Git" , QString("Executing merge: {%1} into {%2}" ).arg(sources.join("," ), into)); |
| 28 | |
| 29 | { |
| 30 | const auto cmd = QString("git checkout -q %1" ).arg(into); |
| 31 | |
| 32 | QLog_Trace("Git" , QString("Checking out the current branch: {%1}" ).arg(cmd)); |
| 33 | |
| 34 | const auto retCheckout = mGitBase->run(cmd); |
| 35 | |
| 36 | if (!retCheckout.success) |
| 37 | return retCheckout; |
| 38 | } |
| 39 | |
| 40 | const auto cmd2 = QString("git merge -Xignore-all-space " ).append(sources.join(" " )); |
| 41 | |
| 42 | QLog_Trace("Git" , QString("Merging ignoring spaces: {%1}" ).arg(cmd2)); |
| 43 | |
| 44 | const auto retMerge = mGitBase->run(cmd2); |
| 45 | |
| 46 | if (retMerge.success) |
| 47 | { |
| 48 | QScopedPointer<GitWip> git(new GitWip(mGitBase, mCache)); |
| 49 | git->updateWip(); |
| 50 | } |
| 51 | |
| 52 | return retMerge; |
| 53 | } |
| 54 | |
| 55 | GitExecResult GitMerge::abortMerge() const |
| 56 | { |
| 57 | QLog_Debug("Git" , QString("Aborting merge" )); |
| 58 | |
| 59 | const auto cmd = QString("git merge --abort" ); |
| 60 | |
| 61 | QLog_Trace("Git" , QString("Aborting merge: {%1}" ).arg(cmd)); |
| 62 | |
| 63 | const auto ret = mGitBase->run(cmd); |
| 64 | |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | GitExecResult GitMerge::applyMerge() const |
| 69 | { |
| 70 | QLog_Debug("Git" , QString("Commiting merge" )); |
| 71 | |
| 72 | const auto cmd = QString("git commit --no-edit" ); |
| 73 | |
| 74 | QLog_Trace("Git" , QString("Commiting merge: {%1}" ).arg(cmd)); |
| 75 | |
| 76 | const auto ret = mGitBase->run(cmd); |
| 77 | |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | GitExecResult GitMerge::squashMerge(const QString &into, QStringList sources, const QString &msg) const |
| 82 | { |
| 83 | QLog_Debug("Git" , QString("Executing squash merge: {%1} into {%2}" ).arg(sources.join("," ), into)); |
| 84 | |
| 85 | { |
| 86 | const auto cmd = QString("git checkout -q %1" ).arg(into); |
| 87 | |
| 88 | QLog_Trace("Git" , QString("Checking out the current branch: {%1}" ).arg(cmd)); |
| 89 | |
| 90 | const auto retCheckout = mGitBase->run(cmd); |
| 91 | |
| 92 | if (!retCheckout.success) |
| 93 | return retCheckout; |
| 94 | } |
| 95 | |
| 96 | const auto cmd2 = QString("git merge -Xignore-all-space --squash " ).append(sources.join(" " )); |
| 97 | |
| 98 | const auto retMerge = mGitBase->run(cmd2); |
| 99 | |
| 100 | if (retMerge.success) |
| 101 | { |
| 102 | if (msg.isEmpty()) |
| 103 | { |
| 104 | const auto commitCmd = QString("git commit --no-edit" ); |
| 105 | mGitBase->run(commitCmd); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | const auto cmd = QString("git commit -m \"%1\"" ).arg(msg); |
| 110 | mGitBase->run(cmd); |
| 111 | } |
| 112 | |
| 113 | QScopedPointer<GitWip> git(new GitWip(mGitBase, mCache)); |
| 114 | git->updateWip(); |
| 115 | } |
| 116 | |
| 117 | return retMerge; |
| 118 | } |
| 119 | |