summaryrefslogtreecommitdiff
path: root/src/git.h
blob: 1dce92ee01e78b1d26f6a93c8bf1c4c832856ef1 (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
#ifndef GITFS_GIT_H
#define GITFS_GIT_H

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

namespace GitFS::Git {
	template<typename E> [[noreturn]] void throwError(int err);

	template<typename E, typename... P, typename... A>
	void
	gitSafe(int (*func)(P...), A... p)
	{
		if (int _giterror = func(p...)) {
			throwError<E>(_giterror);
		}
	}

	template<typename T> using TPtr = std::shared_ptr<T>;

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

	git_oid OidParse(const std::string_view & str);

	using RepositoryPtr = TPtr<git_repository>;
	RepositoryPtr RepositoryOpenBare(const std::string & path);

	using BlobPtr = TPtr<const git_blob>;
	BlobPtr BlobLookup(const RepositoryPtr & repo, const git_oid & blob);

	using CommitPtr = TPtr<const git_commit>;
	CommitPtr CommitLookup(const RepositoryPtr & repo, const git_oid & commitId);

	using TreePtr = TPtr<const git_tree>;
	TreePtr TreeLookup(const RepositoryPtr & repo, const git_oid & treeId);

	using TreeEntryPtr = TPtr<const git_tree_entry>;
	TreeEntryPtr TreeEntryByPath(const TreePtr & tree, const std::string & path);

	using RefPtr = TPtr<const git_reference>;
	RefPtr Commitish(const RepositoryPtr & repo, const std::string & name);
	RefPtr Resolve(const RefPtr &);
}

namespace NetFS {
	struct Attr;
	Attr & operator<<(Attr &, const git_tree_entry &);
	Attr & operator<<(Attr &, const git_commit &);
	Attr & operator<<(Attr &, const git_blob &);
}

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

#endif