summaryrefslogtreecommitdiff
path: root/gentoobrowse-api/service/utils/git.cpp
blob: ce1556aaadb2e468368847caf92f6646d4b0f2fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "git.h"
#include <maintenance.h>
#include <execinfo.h>
#include <compileTimeFormatter.h>

namespace Gentoo {
	namespace Utils {
		namespace Git {
			void
			throwError(void * const func, int err)
			{
				const git_error * e = giterr_last();
				char ** fn = backtrace_symbols(&func, 1);
				assert(fn);
				assert(*fn);
				throw ::Gentoo::GitError(*fn, err, e->klass, e->message);
			}

			AdHocFormatter(RefSpec, "refs/heads/%?:refs/remotes/%?/%?");
			std::unique_ptr<git_annotated_commit, void (*)(git_annotated_commit*)>
			gitFetch(git_repository * repo, git_remote * remote, const char * remoteBranchName)
			{
				auto opts = gitSafeGet(git_fetch_init_options, GIT_FETCH_OPTIONS_VERSION);
				opts.prune = GIT_FETCH_PRUNE;
				opts.update_fetchhead = 1;
				auto localBranch = gitSafeGet(git_repository_head, git_reference_free, repo);
				auto localBranchName = gitSafeGet(git_branch_name, localBranch.get());
				auto refspec = RefSpec::get(localBranchName, git_remote_name(remote), remoteBranchName);
				char * s[] = { &refspec.front() };
				git_strarray refs = { s, 1 };
				gitSafe(git_remote_fetch, remote, &refs, &opts, nullptr);
				return gitSafeGet(git_annotated_commit_from_revspec, git_annotated_commit_free, repo, "FETCH_HEAD");
			}

			git_oid
			gitFastForward(git_repository * repo, git_annotated_commit * fetch_head)
			{
				// Test fast-forward is possible
				const git_annotated_commit * heads[] = { fetch_head };
				git_merge_analysis_t analysis = GIT_MERGE_ANALYSIS_NONE;
				git_merge_preference_t preference = GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY;
				gitSafe(git_merge_analysis, &analysis, &preference, repo, heads, 1);

				auto head = gitSafeGet(git_repository_head, git_reference_free, repo);
				if (analysis == GIT_MERGE_ANALYSIS_UP_TO_DATE) {
					return *git_reference_target(head.get());
				}
				if (!(analysis & (GIT_MERGE_ANALYSIS_NORMAL | GIT_MERGE_ANALYSIS_FASTFORWARD))) {
					throw GitError("Merge analysis", 0, 0, "Could not fast-forward branch");
				}

				// Perform fast-forward
				auto fetch_head_id = *git_annotated_commit_id(fetch_head);
				auto fetch_head_object = gitSafeGet(git_object_lookup, git_object_free, repo, &fetch_head_id, GIT_OBJ_ANY);
				gitSafeGet(git_reference_set_target, git_reference_free, head.get(), &fetch_head_id, "fast-forward");

				// Checkout new head
				auto checkout_options = gitSafeGet(git_checkout_init_options, 0u + GIT_CHECKOUT_OPTIONS_VERSION);
				checkout_options.checkout_strategy = GIT_CHECKOUT_FORCE;
				gitSafe(git_checkout_head, repo, &checkout_options);
				return fetch_head_id;
			}

			void
			updateRepository(const std::string & path, const std::string & upstream, const std::string & branch)
			{
				auto repo = gitSafeGet(git_repository_open, git_repository_free, path.c_str());
				auto origin = gitSafeGet(git_remote_lookup, git_remote_free, repo.get(), upstream.c_str());
				auto fetchHead = gitFetch(repo.get(), origin.get(), branch.c_str());
				gitFastForward(repo.get(), fetchHead.get());
			}

		}
	}
}