| 1 | #include "GitRemote.h" |
| 2 | |
| 3 | #include <GitBase.h> |
| 4 | #include <GitConfig.h> |
| 5 | #include <GitQlientSettings.h> |
| 6 | #include <GitSubmodules.h> |
| 7 | |
| 8 | #include <QLogger.h> |
| 9 | |
| 10 | using namespace QLogger; |
| 11 | |
| 12 | GitRemote::GitRemote(const QSharedPointer<GitBase> &gitBase) |
| 13 | : mGitBase(gitBase) |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | GitExecResult GitRemote::pushBranch(const QString &branchName, bool force) |
| 18 | { |
| 19 | QLog_Debug("Git" , QString("Executing push" )); |
| 20 | |
| 21 | QScopedPointer<GitConfig> gitConfig(new GitConfig(mGitBase)); |
| 22 | auto ret = gitConfig->getRemoteForBranch(branchName); |
| 23 | |
| 24 | if (ret.success) |
| 25 | { |
| 26 | const auto remote = ret.output.isEmpty() ? QString("origin" ) : ret.output; |
| 27 | ret = mGitBase->run(QString("git push %1 %2 %3" ).arg(remote, branchName, force ? QString("--force" ) : QString())); |
| 28 | } |
| 29 | |
| 30 | return ret; |
| 31 | } |
| 32 | |
| 33 | GitExecResult GitRemote::push(bool force) |
| 34 | { |
| 35 | QLog_Debug("Git" , QString("Executing push" )); |
| 36 | |
| 37 | const auto ret = mGitBase->run(QString("git push " ).append(force ? QString("--force" ) : QString())); |
| 38 | |
| 39 | return ret; |
| 40 | } |
| 41 | |
| 42 | GitExecResult GitRemote::pushCommit(const QString &sha, const QString &remoteBranch) |
| 43 | { |
| 44 | QLog_Debug("Git" , QString("Executing pushCommit" )); |
| 45 | |
| 46 | QScopedPointer<GitConfig> gitConfig(new GitConfig(mGitBase)); |
| 47 | const auto remote = gitConfig->getRemoteForBranch(remoteBranch); |
| 48 | |
| 49 | return mGitBase->run(QString("git push %1 %2:refs/heads/%3" ) |
| 50 | .arg(remote.success ? remote.output : QString("origin" ), sha, remoteBranch)); |
| 51 | } |
| 52 | |
| 53 | GitExecResult GitRemote::pull() |
| 54 | { |
| 55 | QLog_Debug("Git" , QString("Executing pull" )); |
| 56 | |
| 57 | auto ret = mGitBase->run("git pull --ff-only" ); |
| 58 | |
| 59 | GitQlientSettings settings(mGitBase->getGitDir()); |
| 60 | const auto updateOnPull = settings.localValue("UpdateOnPull" , true).toBool(); |
| 61 | |
| 62 | if (ret.success && updateOnPull) |
| 63 | { |
| 64 | QScopedPointer<GitSubmodules> git(new GitSubmodules(mGitBase)); |
| 65 | const auto updateRet = git->submoduleUpdate(QString()); |
| 66 | |
| 67 | if (!updateRet) |
| 68 | { |
| 69 | return { updateRet, |
| 70 | "There was a problem updating the submodules after pull. Please review that you don't have any local " |
| 71 | "modifications in the submodules" }; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | bool GitRemote::fetch() |
| 79 | { |
| 80 | QLog_Debug("Git" , QString("Executing fetch with prune" )); |
| 81 | |
| 82 | GitQlientSettings settings(mGitBase->getGitDir()); |
| 83 | const auto pruneOnFetch = settings.localValue("PruneOnFetch" , true).toBool(); |
| 84 | |
| 85 | const auto cmd |
| 86 | = QString("git fetch --all --tags --force %1" ).arg(pruneOnFetch ? QString("--prune --prune-tags" ) : QString()); |
| 87 | const auto ret = mGitBase->run(cmd).success; |
| 88 | |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | GitExecResult GitRemote::prune() |
| 93 | { |
| 94 | QLog_Debug("Git" , QString("Executing prune" )); |
| 95 | |
| 96 | const auto ret = mGitBase->run("git remote prune origin" ); |
| 97 | |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | GitExecResult GitRemote::addRemote(const QString &remoteRepo, const QString &remoteName) |
| 102 | { |
| 103 | QLog_Debug("Git" , QString("Adding a remote repository" )); |
| 104 | |
| 105 | const auto ret = mGitBase->run(QString("git remote add %1 %2" ).arg(remoteName, remoteRepo)); |
| 106 | |
| 107 | if (ret.success) |
| 108 | { |
| 109 | const auto ret2 = mGitBase->run(QString("git fetch %1" ).arg(remoteName)); |
| 110 | } |
| 111 | |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | GitExecResult GitRemote::removeRemote(const QString &remoteName) |
| 116 | { |
| 117 | QLog_Debug("Git" , QString("Removing a remote repository" )); |
| 118 | |
| 119 | return mGitBase->run(QString("git remote rm %1" ).arg(remoteName)); |
| 120 | } |
| 121 | |