From 2b316cf9bccc3608b439ddc69039377686350784 Mon Sep 17 00:00:00 2001 From: Paul Cristian Sarbu Date: Thu, 19 Jan 2023 11:13:33 +0100 Subject: GitRepo: Remove refspec argument in retrieving commit from remote... ...and use instead the branch name. A valid direct refspec (as those retrieved by a remote_ls call) will always end in the branch name, so checking the last path component ('/'-delimited substring) of a retrieved refspec is enough. --- src/buildtool/file_system/git_repo.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 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 3ad5ba0c..f05be1c2 100644 --- a/src/buildtool/file_system/git_repo.cpp +++ b/src/buildtool/file_system/git_repo.cpp @@ -758,7 +758,7 @@ auto GitRepo::GetBranchLocalRefname(std::string const& branch, } auto GitRepo::GetCommitFromRemote(std::string const& repo_url, - std::string const& branch_refname_local, + std::string const& branch, anon_logger_ptr const& logger) noexcept -> std::optional { #ifdef BOOTSTRAP_BUILD_TOOL @@ -825,21 +825,22 @@ auto GitRepo::GetCommitFromRemote(std::string const& repo_url, } // figure out what remote branch the local one is tracking for (size_t i = 0; i < refs_len; ++i) { + // by treating each read reference string as a path we can easily + // check for the branch name // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - std::string ref_name{refs[i]->name}; - if (ref_name == branch_refname_local) { + std::filesystem::path ref_name_as_path{refs[i]->name}; + if (ref_name_as_path.filename() == branch) { // branch found! // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) std::string new_commit_hash{git_oid_tostr_s(&refs[i]->oid)}; return new_commit_hash; } } - (*logger)( - fmt::format("could not find branch with refname {} for remote {}", - branch_refname_local, - repo_url, - GitLastError()), - true /*fatal*/); + (*logger)(fmt::format("could not find branch {} for remote {}", + branch, + repo_url, + GitLastError()), + true /*fatal*/); return std::nullopt; } catch (std::exception const& ex) { Logger::Log(LogLevel::Error, @@ -1180,7 +1181,7 @@ auto GitRepo::CheckCommitExists(std::string const& commit, auto GitRepo::UpdateCommitViaTmpRepo(std::filesystem::path const& tmp_repo_path, std::string const& repo_url, - std::string const& branch_refname, + std::string const& branch, anon_logger_ptr const& logger) const noexcept -> std::optional { #ifdef BOOTSTRAP_BUILD_TOOL @@ -1205,8 +1206,7 @@ auto GitRepo::UpdateCommitViaTmpRepo(std::filesystem::path const& tmp_repo_path, msg), fatal); }); - return tmp_repo->GetCommitFromRemote( - repo_url, branch_refname, wrapped_logger); + return tmp_repo->GetCommitFromRemote(repo_url, branch, wrapped_logger); } catch (std::exception const& ex) { Logger::Log(LogLevel::Error, "update commit via tmp repo failed with:\n{}", -- cgit v1.2.3