summaryrefslogtreecommitdiff
path: root/gentoobrowse-api/service/utils/git.cpp
blob: 9a1cae242dfa5cb7dc1ff0b73131912c6399d119 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#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)
			{
#if LIBGIT2_VER_MINOR >= 28
				const git_error * e = git_error_last();
#else
				const git_error * e = giterr_last();
#endif
				char ** fn = backtrace_symbols(&func, 1);
				assert(fn);
				assert(*fn);
				std::string funcName(*fn);
				free(fn);
				throw ::Gentoo::GitError(funcName, err, e->klass, e->message);
			}

			std::string
			operator*(const git_oid & oid)
			{
				std::string str(GIT_OID_HEXSZ, ' ');
				git_oid_tostr(&str.front(), GIT_OID_HEXSZ + 1, &oid);
				return str;
			}

			AdHocFormatter(RefSpec, "refs/heads/%?:refs/remotes/%?/%?");
			GitAnnotatedCommitPtr
			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");
			}

			AdHocFormatter(FastForward, "Performing fast-forward %? -> %?\n");
			AdHocFormatter(CheckOut, "Checking out %?\n");
			git_oid
			gitFastForward(git_repository * repo, const 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);
				FastForward::write(std::cerr, *git_reference_target(head.get()), fetch_head_id);
				gitSafeGet(git_reference_set_target, git_reference_free, head.get(), &fetch_head_id, "fast-forward");

				// Checkout new head
				CheckOut::write(std::cerr, fetch_head_id);
				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;
			}

			AdHocFormatter(Updating, "Updating repository in %? from %?/%?\n");
			AdHocFormatter(UpdateComplete, "Update complete to %?\n");
			void
			updateRepository(const std::string & path, const std::string & upstream, const std::string & branch)
			{
				Updating::write(std::cerr, path, upstream, 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());
				auto oid = gitFastForward(repo.get(), fetchHead.get());
				UpdateComplete::write(std::cerr, oid);
			}
		}
	}
}

namespace std {
	std::ostream &
	operator<<(std::ostream & s, const git_oid & oid)
	{
		char str[GIT_OID_HEXSZ + 1];
		git_oid_tostr(str, sizeof(str), &oid);
		s.write(str, GIT_OID_HEXSZ);
		return s;
	}
}