summaryrefslogtreecommitdiff
path: root/src/git.cpp
blob: f3f437e4f6f7bc51f51e497044e6aa01dd0951f7 (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
109
110
111
112
113
114
115
#include "git.h"
#include <execinfo.h>
#include <exceptions.h>
#include <types.h>
#include <sys/stat.h>

namespace GitFS::Git {
	template<>
	[[noreturn]]
	void
	throwError<NetFS::SystemError>(int err)
	{
		if (err == GIT_ENOTFOUND) {
			throw NetFS::SystemError(ENOENT);
		}
		throw NetFS::SystemError(EIO);
	}

	template<>
	[[noreturn]]
	void
	throwError<NetFS::ConfigError>(int)
	{
		throw NetFS::ConfigError();
	}

	git_oid OidParse(const std::string_view & str)
	{
		git_oid oid;
		gitSafe<NetFS::ConfigError>(git_oid_fromstrn, &oid, str.data(), str.length());
		return oid;
	}

	RepositoryPtr RepositoryOpenBare(const std::string & path)
	{
		return gitSafeGet<NetFS::ConfigError>(
				git_repository_open_bare, git_repository_free, path.c_str());
	}

	BlobPtr BlobLookup(const RepositoryPtr & repo, const git_oid & blob)
	{
		return gitSafeGet<NetFS::SystemError>(
				git_blob_lookup, git_blob_free, repo.get(), &blob);
	}

	CommitPtr CommitLookup(const RepositoryPtr & repo, const git_oid & commitId)
	{
		return gitSafeGet<NetFS::ConfigError>(
				git_commit_lookup, git_commit_free, repo.get(), &commitId);
	}

	TreePtr TreeLookup(const RepositoryPtr & repo, const git_oid & treeId)
	{
		return gitSafeGet<NetFS::SystemError>(
				git_tree_lookup, git_tree_free, repo.get(), &treeId);
	}

	TreeEntryPtr TreeEntryByPath(const TreePtr & tree, const std::string & path)
	{
		return gitSafeGet<NetFS::SystemError>(
				git_tree_entry_bypath, git_tree_entry_free, tree.get(), path.c_str() + 1);
	}
	RefPtr Commitish(const RepositoryPtr & repo, const std::string & name)
	{
		return gitSafeGet<NetFS::ConfigError>(
				git_reference_dwim, git_reference_free, repo.get(), name.c_str());
	}
	RefPtr Resolve(const RefPtr & ref)
	{
		return gitSafeGet<NetFS::ConfigError>(
				git_reference_resolve, git_reference_free, ref.get());
	}
}

namespace NetFS {
	Attr & operator<<(Attr & a, const git_tree_entry & e)
	{
		a.mode = git_tree_entry_filemode(&e);
		if (S_ISDIR(a.mode)) {
			a.mode |= S_IXUSR | S_IXGRP | S_IXOTH | S_IRUSR | S_IRGRP | S_IROTH;
		}
		else if (S_ISLNK(a.mode)) {
			a.mode |= S_IRUSR | S_IRGRP | S_IROTH;
		}
		else {
			a.mode ^= S_IWUSR;
		}
		return a;
	}

	Attr & operator<<(Attr & a, const git_commit & c)
	{
		a.ctime = a.atime = a.mtime = git_commit_time(&c);
		return a;
	}

	Attr & operator<<(Attr & a, const git_blob & b)
	{
		a.blockSize = 1;
		a.blocks = a.size = git_blob_rawsize(&b);
		return a;
	}
}

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;
	}
}