summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2023-01-19 11:13:33 +0100
committerPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2023-01-24 15:47:31 +0100
commit2b316cf9bccc3608b439ddc69039377686350784 (patch)
tree2e00926ca30ef55e3ae66c381ea5a0e650173de7 /src
parent51130a4ef9bfb4139ffb748d1e0e4f491a442bfd (diff)
downloadjustbuild-2b316cf9bccc3608b439ddc69039377686350784.tar.gz
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.
Diffstat (limited to 'src')
-rw-r--r--src/buildtool/file_system/git_repo.cpp24
-rw-r--r--src/buildtool/file_system/git_repo.hpp8
-rw-r--r--src/other_tools/ops_maps/git_update_map.cpp4
3 files changed, 17 insertions, 19 deletions
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<std::string> {
#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<std::string> {
#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{}",
diff --git a/src/buildtool/file_system/git_repo.hpp b/src/buildtool/file_system/git_repo.hpp
index 3fda0516..3e3bf178 100644
--- a/src/buildtool/file_system/git_repo.hpp
+++ b/src/buildtool/file_system/git_repo.hpp
@@ -146,13 +146,13 @@ class GitRepo {
std::string const& branch,
anon_logger_ptr const& logger) noexcept -> std::optional<std::string>;
- /// \brief Retrieve commit hash from remote branch given its refname.
+ /// \brief Retrieve commit hash from remote branch given its name.
/// Only possible with real repository and thus non-thread-safe.
/// Returns the retrieved commit hash, or nullopt if failure.
/// It guarantees the logger is called exactly once with fatal if failure.
[[nodiscard]] auto GetCommitFromRemote(
std::string const& repo_url,
- std::string const& branch_refname_local,
+ std::string const& branch,
anon_logger_ptr const& logger) noexcept -> std::optional<std::string>;
/// \brief Fetch from given remote. It can either fetch a given named
@@ -206,7 +206,7 @@ class GitRepo {
/// \brief Get commit from remote via a temporary repository.
/// Calling it from a fake repository allows thread-safe use.
/// Creates a temporary real repository at the given location and uses it to
- /// retrieve from the remote the commit of a branch given its refname.
+ /// retrieve from the remote the commit of a branch given its name.
/// Caller needs to make sure the temporary directory exists and that the
/// given path is thread- and process-safe!
/// Returns the commit hash, as a string, or nullopt if failure.
@@ -214,7 +214,7 @@ class GitRepo {
[[nodiscard]] auto 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<std::string>;
diff --git a/src/other_tools/ops_maps/git_update_map.cpp b/src/other_tools/ops_maps/git_update_map.cpp
index 73eccfe0..f2f6c3e5 100644
--- a/src/other_tools/ops_maps/git_update_map.cpp
+++ b/src/other_tools/ops_maps/git_update_map.cpp
@@ -51,10 +51,8 @@ auto CreateGitUpdateMap(GitCASPtr const& git_cas, std::size_t jobs)
fatal);
});
// update commit
- auto refname = std::string("refs/heads/") +
- key.second; // assume branch ref format
auto new_commit = git_repo->UpdateCommitViaTmpRepo(
- tmp_dir->GetPath(), key.first, refname, wrapped_logger);
+ tmp_dir->GetPath(), key.first, key.second, wrapped_logger);
if (not new_commit) {
return;
}