From 6e9151cfbdd816ce59f6340a0ca5800efabb894f Mon Sep 17 00:00:00 2001 From: Paul Cristian Sarbu Date: Thu, 19 Jan 2023 10:23:18 +0100 Subject: GitRepo: Change FetchFromRemote to fetch based on branch name This also removes the need to call the GET_BRANCH_REFNAME critical operation. --- src/buildtool/file_system/git_repo.cpp | 35 ++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) (limited to 'src/buildtool/file_system/git_repo.cpp') diff --git a/src/buildtool/file_system/git_repo.cpp b/src/buildtool/file_system/git_repo.cpp index 7a4915a3..a3d7e388 100644 --- a/src/buildtool/file_system/git_repo.cpp +++ b/src/buildtool/file_system/git_repo.cpp @@ -844,7 +844,7 @@ auto GitRepo::GetCommitFromRemote(std::string const& repo_url, } auto GitRepo::FetchFromRemote(std::string const& repo_url, - std::string const& refspec, + std::optional const& branch, anon_logger_ptr const& logger) noexcept -> bool { #ifdef BOOTSTRAP_BUILD_TOOL return false; @@ -870,29 +870,36 @@ auto GitRepo::FetchFromRemote(std::string const& repo_url, git_remote_free(remote_ptr); return false; } + // wrap remote object auto remote = std::unique_ptr( remote_ptr, remote_closer); + // define default fetch options + git_fetch_options fetch_opts{}; + git_fetch_options_init(&fetch_opts, GIT_FETCH_OPTIONS_VERSION); + + // disable update of the FETCH_HEAD pointer + fetch_opts.update_fetchhead = 0; + // setup fetch refspecs array git_strarray refspecs_array_obj{}; - if (not refspec.empty()) { - PopulateStrarray(&refspecs_array_obj, {refspec}); + if (branch) { + // make sure we check for tags as well + std::string tag = fmt::format("+refs/tags/{}", *branch); + std::string head = fmt::format("+refs/heads/{}", *branch); + PopulateStrarray(&refspecs_array_obj, {tag, head}); } auto refspecs_array = std::unique_ptr( &refspecs_array_obj, strarray_deleter); - // do the fetch - git_fetch_options fetch_opts{}; - git_fetch_init_options(&fetch_opts, GIT_FETCH_OPTIONS_VERSION); - if (git_remote_fetch(remote.get(), - refspec.empty() ? nullptr : refspecs_array.get(), - &fetch_opts, - nullptr) != 0) { + if (git_remote_fetch( + remote.get(), refspecs_array.get(), &fetch_opts, nullptr) != + 0) { (*logger)( - fmt::format("fetch of refspec {} in git repository {} failed " + fmt::format("fetching{} in git repository {} failed " "with:\n{}", - refspec, + branch ? fmt::format(" branch {}", *branch) : "", GetGitCAS()->git_path_.string(), GitLastError()), true /*fatal*/); @@ -1201,7 +1208,7 @@ auto GitRepo::UpdateCommitViaTmpRepo(std::filesystem::path const& tmp_repo_path, auto GitRepo::FetchViaTmpRepo(std::filesystem::path const& tmp_repo_path, std::string const& repo_url, - std::string const& refspec, + std::optional const& branch, anon_logger_ptr const& logger) noexcept -> bool { #ifdef BOOTSTRAP_BUILD_TOOL return false; @@ -1234,7 +1241,7 @@ auto GitRepo::FetchViaTmpRepo(std::filesystem::path const& tmp_repo_path, "While doing branch fetch via tmp repo:\n{}", msg), fatal); }); - return tmp_repo->FetchFromRemote(repo_url, refspec, wrapped_logger); + return tmp_repo->FetchFromRemote(repo_url, branch, wrapped_logger); } return false; } catch (std::exception const& ex) { -- cgit v1.2.3