From d045a283e324d50dec41d70703aa33c3884a8891 Mon Sep 17 00:00:00 2001 From: Maksim Denisov Date: Wed, 4 Dec 2024 13:18:39 +0100 Subject: GitCAS: retain git_repository alive. (cherry-picked from 7b50ad08180edb160d023ed61518cd9256f65f70) --- src/buildtool/file_system/git_cas.cpp | 23 ++++++++++++++++------- src/buildtool/file_system/git_cas.hpp | 8 ++++++++ src/buildtool/file_system/git_utils.cpp | 6 ++++++ src/buildtool/file_system/git_utils.hpp | 3 +++ 4 files changed, 33 insertions(+), 7 deletions(-) (limited to 'src') 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( - 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(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 { + 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 odb_{nullptr, odb_closer}; + std::unique_ptr 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 odb) { #endif } +void repository_closer(gsl::owner repository) { +#ifndef BOOTSTRAP_BUILD_TOOL + git_repository_free(repository); +#endif +} + void tree_closer(gsl::owner 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 odb); +void repository_closer(gsl::owner repository); + void tree_closer(gsl::owner tree); void signature_closer(gsl::owner signature); -- cgit v1.2.3