summaryrefslogtreecommitdiff
path: root/gentoobrowse-api/service/utils/git.h
blob: d240b51b0366227d05d9c35a0660024098ff06fa (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
#pragma once

#include <git2.h>
#include <memory>
#include <ostream>

namespace Gentoo::Utils::Git {
	void throwError(void * const func, int err);

	template<typename... P, typename... A>
	void
	gitSafe(int (*func)(P...), A... p)
	{
		if (int giterror_ = func(p...) < 0) {
			throwError(reinterpret_cast<void * const>(func), giterror_);
		}
	}

	template<typename T> using GitTPtr = std::unique_ptr<T, void (*)(T *)>;

	template<typename R, typename... P, typename... A>
	GitTPtr<R>
	gitSafeGet(int (*get)(R **, P...), void (*release)(R *), A... p)
	{
		R * r = nullptr;
		gitSafe(get, &r, p...);
		return GitTPtr<R>(r, release);
	}

	template<typename R, typename... P, typename... A>
	R
	gitSafeGet(int (*get)(R *, P...), A... p)
	{
		R r {};
		gitSafe(get, &r, p...);
		return r;
	}

	std::string operator*(const git_oid &);

	using GitAnnotatedCommitPtr = GitTPtr<git_annotated_commit>;
	GitAnnotatedCommitPtr gitFetch(git_repository * repo, git_remote * remote, const char * branch);

	git_oid gitFastForward(git_repository * repo, const git_annotated_commit * fetch_head);

	void updateRepository(const std::string & path, const std::string & upstream, const std::string & branch);
}

namespace std {
	std::ostream & operator<<(std::ostream &, const git_oid &);
}