Skip to content

Commit fad38b7

Browse files
committed
adding some functions to repository
1 parent 0dd4d54 commit fad38b7

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

include/cppgit/repository.hpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,21 @@
22
#define CPPGIT_HPP
33

44
#include "cppgit/git2api.hpp"
5-
#include <tuple>
5+
#include <git2/types.h>
6+
#include <memory>
7+
#include <string>
8+
#include <optional>
9+
610

711
namespace cppgit {
12+
13+
enum class branch_type
14+
{
15+
LOCAL,
16+
REMOTE,
17+
ALL
18+
};
19+
820
// =====================================================================================
921
// Class: Repository
1022
// Description:
@@ -13,8 +25,12 @@ namespace cppgit {
1325
class Repository: private Git2API {
1426
public:
1527
// ==================== LIFECYCLE =======================================
16-
Repository();
28+
Repository(std::string_view path, bool is_bare);
1729
// ==================== ACCESSORS =======================================
30+
[[nodiscard]] bool is_bare() const;
31+
[[nodiscard]] const char* path() const;
32+
[[nodiscard]] const char* workdir() const;
33+
[[nodiscard]] static std::optional<std::string> discover_path(std::string_view start_path, bool across_fs = false, std::string_view ceiling_dirs = "");
1834
// ==================== MUTATORS =======================================
1935

2036
// ==================== OPERATORS =======================================
@@ -28,7 +44,8 @@ class Repository: private Git2API {
2844
// ==================== METHODS =======================================
2945

3046
// ==================== DATA MEMBERS =======================================
31-
47+
struct Destroy { void operator() (git_repository *) const; };
48+
std::unique_ptr<git_repository, Destroy> m_repo;
3249
}; // ----- end of class Repository -----
3350
} // namespace cppgit
3451

src/repository.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
11
#include "cppgit/repository.hpp"
2+
#include "cppgit/exception.hpp"
23
#include <git2.h>
34

45
namespace cppgit {
5-
Repository::Repository(){
6-
7-
}
6+
Repository::Repository(std::string_view path, bool is_bare){
7+
git_repository* repo = nullptr;
8+
if (git_repository_init(&repo, path.data(), static_cast<unsigned int>(is_bare)) != 0) {
9+
throw Exception();
10+
}
11+
m_repo.reset(repo);
12+
}
13+
14+
void Repository::Destroy::operator() (git_repository* repo) const
15+
{
16+
git_repository_free(repo);
17+
}
18+
19+
bool Repository::is_bare() const {
20+
return git_repository_is_bare(m_repo.get()) != 0;
21+
}
22+
23+
const char* Repository::path() const
24+
{
25+
return git_repository_path(m_repo.get());
26+
}
27+
const char* Repository::workdir() const
28+
{
29+
return git_repository_workdir(m_repo.get());
30+
}
31+
32+
std::optional<std::string> Repository::discover_path(std::string_view start_path, bool across_fs, std::string_view ceiling_dirs) {
33+
git_buf buffer = {.ptr = nullptr, .size = 0};
34+
if (git_repository_discover(&buffer, start_path.data(), static_cast<int>(across_fs), ceiling_dirs.data()) != 0) {
35+
return std::nullopt;
36+
}
37+
return std::string(buffer.ptr, buffer.size);
38+
}
839
}

0 commit comments

Comments
 (0)