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
|
#include "git.h"
#include <array>
#include <cassert>
#include <compileTimeFormatter.h>
#include <cstdlib>
#include <execinfo.h>
#include <logWriter.h>
#include <logger.h>
#include <maintenance.h>
namespace Gentoo::Utils::Git {
void
throwError(void * const func, int err)
{
const git_error * e = git_error_last();
std::unique_ptr<char *, decltype(&free)> fn {backtrace_symbols(&func, 1), free};
assert(fn);
assert(*fn);
throw ::Gentoo::GitError(*fn, 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, static_cast<unsigned int>(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);
std::array<char *, 1> s {refspec.data()};
git_strarray refs = {s.data(), 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 %? -> %?");
AdHocFormatter(CheckOut, "Checking out %?");
git_oid
gitFastForward(git_repository * repo, const git_annotated_commit * fetch_head)
{
auto log = LOGMANAGER()->getLogger(__FUNCTION__);
// Test fast-forward is possible
std::array<const git_annotated_commit *, 1> 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.data(), 1LU);
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);
log->messagectf<FastForward>(
IceTray::Logging::LogLevel::INFO, *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
log->messagectf<CheckOut>(IceTray::Logging::LogLevel::INFO, 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 %?/%?");
AdHocFormatter(UpdateComplete, "Update complete to %?");
void
updateRepository(const std::string & path, const std::string & upstream, const std::string & branch)
{
auto log = LOGMANAGER()->getLogger(__FUNCTION__);
log->messagectf<Updating>(IceTray::Logging::LogLevel::INFO, 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());
log->messagectf<UpdateComplete>(IceTray::Logging::LogLevel::INFO, oid);
}
}
namespace std {
std::ostream &
operator<<(std::ostream & s, const git_oid & oid)
{
std::array<char, GIT_OID_HEXSZ + 1> str {};
git_oid_tostr(str.data(), str.size(), &oid);
s.write(str.data(), GIT_OID_HEXSZ);
return s;
}
}
|