summaryrefslogtreecommitdiff
path: root/src/git.cpp
blob: 850b8b36ab35d22f9a4bbdd72ea93171b925dc7c (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
#include "git.h"
#include <execinfo.h>
#include <exceptions.h>
#include <types.h>
#include <sys/stat.h>

namespace GitFS {
	namespace Git {
		void
		throwError(int err)
		{
#if LIBGIT2_VER_MINOR >= 28
			const git_error * e = git_error_last();
#else
			const git_error * e = giterr_last();
#endif
			throw Error { err, e->klass, e->message };
		}

		[[noreturn]] void ErrorToSystemError(const Error & e)
		{
			if (e.err == GIT_ENOTFOUND) {
				throw NetFS::SystemError(ENOENT);
			}
			throw NetFS::SystemError(EIO);
		}

	}
}

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)
	{
		char str[GIT_OID_HEXSZ + 1];
		git_oid_tostr(str, sizeof(str), &oid);
		s.write(str, GIT_OID_HEXSZ);
		return s;
	}
}