summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/buildtool/file_system/git_cas.cpp23
-rw-r--r--src/buildtool/file_system/git_cas.hpp8
-rw-r--r--src/buildtool/file_system/git_utils.cpp6
-rw-r--r--src/buildtool/file_system/git_utils.hpp3
4 files changed, 33 insertions, 7 deletions
diff --git a/src/buildtool/file_system/git_cas.cpp b/src/buildtool/file_system/git_cas.cpp
index f40c83cf..3d0b7063 100644
--- a/src/buildtool/file_system/git_cas.cpp
+++ b/src/buildtool/file_system/git_cas.cpp
@@ -77,12 +77,11 @@ auto GitCAS::Open(std::filesystem::path const& repo_path) noexcept
GitLastError());
return nullptr;
}
- auto const repo =
- std::unique_ptr<git_repository, decltype(&git_repository_free)>(
- repo_ptr, git_repository_free);
+ result->repo_.reset(repo_ptr);
git_odb* odb_ptr = nullptr;
- if (git_repository_odb(&odb_ptr, repo.get()) != 0 or odb_ptr == nullptr) {
+ if (git_repository_odb(&odb_ptr, result->repo_.get()) != 0 or
+ odb_ptr == nullptr) {
Logger::Log(LogLevel::Error,
"Obtaining git object database {} failed with:\n{}",
repo_path.string(),
@@ -92,9 +91,9 @@ auto GitCAS::Open(std::filesystem::path const& repo_path) noexcept
result->odb_.reset(odb_ptr);
auto const git_path =
- git_repository_is_bare(repo.get()) != 0
- ? ToNormalPath(git_repository_path(repo.get()))
- : ToNormalPath(git_repository_workdir(repo.get()));
+ git_repository_is_bare(result->repo_.get()) != 0
+ ? ToNormalPath(git_repository_path(result->repo_.get()))
+ : ToNormalPath(git_repository_workdir(result->repo_.get()));
try {
result->git_path_ = std::filesystem::absolute(git_path);
@@ -123,6 +122,16 @@ auto GitCAS::CreateEmpty() noexcept -> GitCASPtr {
return nullptr;
}
result->odb_.reset(odb_ptr); // retain odb pointer
+
+ git_repository* repo_ptr = nullptr;
+ if (git_repository_wrap_odb(&repo_ptr, result->odb_.get()) != 0 or
+ repo_ptr == nullptr) {
+ Logger::Log(LogLevel::Error,
+ "creating an empty repository failed with:\n{}",
+ GitLastError());
+ return nullptr;
+ }
+ result->repo_.reset(repo_ptr); // retain repo pointer
return std::const_pointer_cast<GitCAS const>(result);
#endif
}
diff --git a/src/buildtool/file_system/git_cas.hpp b/src/buildtool/file_system/git_cas.hpp
index f358e65b..78d03c99 100644
--- a/src/buildtool/file_system/git_cas.hpp
+++ b/src/buildtool/file_system/git_cas.hpp
@@ -52,6 +52,11 @@ class GitCAS {
return odb_.get();
}
+ [[nodiscard]] auto GetRepository() const noexcept
+ -> gsl::not_null<git_repository*> {
+ return repo_.get();
+ }
+
/// \brief Read object from CAS.
/// \param id The object id.
/// \param is_hex_id Specify whether `id` is hex string or raw.
@@ -70,6 +75,9 @@ class GitCAS {
private:
std::unique_ptr<git_odb, decltype(&odb_closer)> odb_{nullptr, odb_closer};
+ std::unique_ptr<git_repository, decltype(&repository_closer)> repo_{
+ nullptr,
+ repository_closer};
// git folder path of repo
std::filesystem::path git_path_;
diff --git a/src/buildtool/file_system/git_utils.cpp b/src/buildtool/file_system/git_utils.cpp
index 8612d039..61ba6533 100644
--- a/src/buildtool/file_system/git_utils.cpp
+++ b/src/buildtool/file_system/git_utils.cpp
@@ -76,6 +76,12 @@ void odb_closer(gsl::owner<git_odb*> odb) {
#endif
}
+void repository_closer(gsl::owner<git_repository*> repository) {
+#ifndef BOOTSTRAP_BUILD_TOOL
+ git_repository_free(repository);
+#endif
+}
+
void tree_closer(gsl::owner<git_tree*> tree) {
#ifndef BOOTSTRAP_BUILD_TOOL
git_tree_free(tree);
diff --git a/src/buildtool/file_system/git_utils.hpp b/src/buildtool/file_system/git_utils.hpp
index 916e9fbe..d6efb37d 100644
--- a/src/buildtool/file_system/git_utils.hpp
+++ b/src/buildtool/file_system/git_utils.hpp
@@ -24,6 +24,7 @@
extern "C" {
struct git_oid;
struct git_odb;
+struct git_repository;
struct git_tree;
struct git_signature;
struct git_object;
@@ -47,6 +48,8 @@ constexpr std::size_t kGitLockNumTries{10};
void odb_closer(gsl::owner<git_odb*> odb);
+void repository_closer(gsl::owner<git_repository*> repository);
+
void tree_closer(gsl::owner<git_tree*> tree);
void signature_closer(gsl::owner<git_signature*> signature);