summaryrefslogtreecommitdiff
path: root/src/buildtool/file_system/git_cas.cpp
diff options
context:
space:
mode:
authorPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2022-08-24 16:49:21 +0200
committerPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2022-12-20 16:02:13 +0100
commit6d938afb02d276afeb0e56689a9c3e8cb756ec37 (patch)
tree6ee6b62130610ecc9f0e71d64b93d51a484468e0 /src/buildtool/file_system/git_cas.cpp
parentb8e5ec6a4a28163e8f9ff5b23c3ebf1df3d73720 (diff)
downloadjustbuild-6d938afb02d276afeb0e56689a9c3e8cb756ec37.tar.gz
Git CAS: Add a Git context class to maintain the libgit2 state
Diffstat (limited to 'src/buildtool/file_system/git_cas.cpp')
-rw-r--r--src/buildtool/file_system/git_cas.cpp46
1 files changed, 17 insertions, 29 deletions
diff --git a/src/buildtool/file_system/git_cas.cpp b/src/buildtool/file_system/git_cas.cpp
index 799068f2..d1ea0870 100644
--- a/src/buildtool/file_system/git_cas.cpp
+++ b/src/buildtool/file_system/git_cas.cpp
@@ -317,22 +317,12 @@ auto GitCAS::Open(std::filesystem::path const& repo_path) noexcept
return nullptr;
}
-GitCAS::GitCAS() noexcept {
-#ifndef BOOTSTRAP_BUILD_TOOL
- if (not(initialized_ = (git_libgit2_init() >= 0))) {
- Logger::Log(LogLevel::Error, "initializing libgit2 failed");
- }
-#endif
-}
GitCAS::~GitCAS() noexcept {
#ifndef BOOTSTRAP_BUILD_TOOL
if (odb_ != nullptr) {
git_odb_free(odb_);
odb_ = nullptr;
}
- if (initialized_) {
- git_libgit2_shutdown();
- }
#endif
}
@@ -341,7 +331,7 @@ auto GitCAS::ReadObject(std::string const& id, bool is_hex_id) const noexcept
#ifdef BOOTSTRAP_BUILD_TOOL
return std::nullopt;
#else
- if (not initialized_) {
+ if (odb_ == nullptr) {
return std::nullopt;
}
@@ -419,7 +409,7 @@ auto GitCAS::ReadTree(std::string const& id, bool is_hex_id) const noexcept
auto GitCAS::ReadHeader(std::string const& id, bool is_hex_id) const noexcept
-> std::optional<std::pair<std::size_t, ObjectType>> {
#ifndef BOOTSTRAP_BUILD_TOOL
- if (not initialized_) {
+ if (odb_ == nullptr) {
return std::nullopt;
}
@@ -507,29 +497,27 @@ auto GitCAS::OpenODB(std::filesystem::path const& repo_path) noexcept -> bool {
#ifdef BOOTSTRAP_BUILD_TOOL
return false;
#else
- if (initialized_) {
- { // lock as git_repository API has no thread-safety guarantees
- std::unique_lock lock{repo_mutex};
- git_repository* repo = nullptr;
- if (git_repository_open(&repo, repo_path.c_str()) != 0) {
- Logger::Log(LogLevel::Error,
- "opening git repository {} failed with:\n{}",
- repo_path.string(),
- GitLastError());
- return false;
- }
- git_repository_odb(&odb_, repo);
- git_repository_free(repo);
- }
- if (odb_ == nullptr) {
+ { // lock as git_repository API has no thread-safety guarantees
+ std::unique_lock lock{repo_mutex};
+ git_repository* repo = nullptr;
+ if (git_repository_open(&repo, repo_path.c_str()) != 0) {
Logger::Log(LogLevel::Error,
- "obtaining git object database {} failed with:\n{}",
+ "opening git repository {} failed with:\n{}",
repo_path.string(),
GitLastError());
return false;
}
+ git_repository_odb(&odb_, repo);
+ git_repository_free(repo);
+ }
+ if (odb_ == nullptr) {
+ Logger::Log(LogLevel::Error,
+ "obtaining git object database {} failed with:\n{}",
+ repo_path.string(),
+ GitLastError());
+ return false;
}
- return initialized_;
+ return true;
#endif
}