ArKanjo 0.5.1
A tool for find code duplicated functions in codebases
repository.cpp
Go to the documentation of this file.
2
3GitRepository::GitRepository(const fs::path& path) {
4 git_libgit2_init();
5
6 git_repository* repo = nullptr;
7
8 int error = git_repository_open_ext(
9 &repo,
10 path.string().c_str(),
11 0,
12 nullptr
13 );
14
15 if (error != 0)
16 return;
17
18 repository = repo;
19}
20
22 if (repository)
23 git_repository_free(repository);
24
25 git_libgit2_shutdown();
26}
27
29 return repository != nullptr;
30}
31
32std::filesystem::path GitRepository::root() const {
33 const char* path = git_repository_workdir(repository);
34
35 if (!path)
36 return {};
37
38 return path;
39}
40
41std::string GitRepository::head_commit() const {
42 git_oid oid;
43
44 git_reference* head = nullptr;
45
46 if (git_repository_head(&head, repository) != 0)
47 return {};
48
49 const git_oid* target = git_reference_target(head);
50
51 std::string result = git_oid_tostr_s(target);
52
53 git_reference_free(head);
54
55 return result;
56}
bool valid() const
Definition: repository.cpp:28
GitRepository(const fs::path &path)
Definition: repository.cpp:3
std::string head_commit() const
Definition: repository.cpp:41
fs::path root() const
Definition: repository.cpp:32