summaryrefslogtreecommitdiff
path: root/src/other_tools/root_maps/commit_git_map.cpp
diff options
context:
space:
mode:
authorPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2023-01-19 10:23:18 +0100
committerPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2023-01-24 15:47:31 +0100
commit6e9151cfbdd816ce59f6340a0ca5800efabb894f (patch)
treebb4352e6a3c52b55b7194bbd87c39a5ef15ee618 /src/other_tools/root_maps/commit_git_map.cpp
parent24d1fae0733b6f89c55797d3a93aba220de2ae44 (diff)
downloadjustbuild-6e9151cfbdd816ce59f6340a0ca5800efabb894f.tar.gz
GitRepo: Change FetchFromRemote to fetch based on branch name
This also removes the need to call the GET_BRANCH_REFNAME critical operation.
Diffstat (limited to 'src/other_tools/root_maps/commit_git_map.cpp')
-rw-r--r--src/other_tools/root_maps/commit_git_map.cpp169
1 files changed, 60 insertions, 109 deletions
diff --git a/src/other_tools/root_maps/commit_git_map.cpp b/src/other_tools/root_maps/commit_git_map.cpp
index 8e34be91..86ab3447 100644
--- a/src/other_tools/root_maps/commit_git_map.cpp
+++ b/src/other_tools/root_maps/commit_git_map.cpp
@@ -131,27 +131,63 @@ void EnsureCommit(GitRepoInfo const& repo_info,
}
if (not is_commit_present.value()) {
// if commit not there, fetch it
- // get refspec for branch
+ auto tmp_dir = JustMR::Utils::CreateTypedTmpDir("fetch");
+ if (not tmp_dir) {
+ (*logger)("Failed to create fetch tmp directory!",
+ /*fatal=*/true);
+ return;
+ }
+ // setup wrapped logger
+ auto wrapped_logger = std::make_shared<AsyncMapConsumerLogger>(
+ [logger](auto const& msg, bool fatal) {
+ (*logger)(fmt::format("While fetching via tmp repo:\n{}", msg),
+ fatal);
+ });
+ if (not git_repo->FetchViaTmpRepo(tmp_dir->GetPath(),
+ repo_info.repo_url,
+ repo_info.branch,
+ wrapped_logger)) {
+ return;
+ }
+ // setup wrapped logger
+ wrapped_logger = std::make_shared<AsyncMapConsumerLogger>(
+ [logger](auto const& msg, bool fatal) {
+ (*logger)(fmt::format("While checking commit exists:\n{}", msg),
+ fatal);
+ });
+ // check if commit exists now, after fetch
+ auto is_commit_present =
+ git_repo->CheckCommitExists(repo_info.hash, wrapped_logger);
+ if (not is_commit_present) {
+ return;
+ }
+ if (not *is_commit_present) {
+ // commit could not be fetched, so fail
+ (*logger)(fmt::format("Could not fetch commit {} from branch "
+ "{} for remote {}",
+ repo_info.hash,
+ repo_info.branch,
+ repo_info.repo_url),
+ /*fatal=*/true);
+ return;
+ }
+ // keep tag
GitOpKey op_key = {{
- repo_root, // target_path
- "", // git_hash
- repo_info.branch, // branch
+ repo_root, // target_path
+ repo_info.hash, // git_hash
+ "", // branch
+ "Keep referenced tree alive" // message
},
- GitOpType::GET_BRANCH_REFNAME};
+ GitOpType::KEEP_TAG};
critical_git_op_map->ConsumeAfterKeysReady(
ts,
{std::move(op_key)},
- [critical_git_op_map,
- git_cas,
- repo_info,
- repo_root,
- ts,
- ws_setter,
- logger](auto const& values) {
+ [git_cas, repo_info, repo_root, ws_setter, logger](
+ auto const& values) {
GitOpValue op_result = *values[0];
// check flag
if (not op_result.result) {
- (*logger)("Get branch refname failed",
+ (*logger)("Keep tag failed",
/*fatal=*/true);
return;
}
@@ -164,112 +200,27 @@ void EnsureCommit(GitRepoInfo const& repo_info,
/*fatal=*/true);
return;
}
- // do fetch
- auto tmp_dir = JustMR::Utils::CreateTypedTmpDir("fetch");
- if (not tmp_dir) {
- (*logger)("Failed to create fetch tmp directory!",
- /*fatal=*/true);
- return;
- }
// setup wrapped logger
auto wrapped_logger = std::make_shared<AsyncMapConsumerLogger>(
[logger](auto const& msg, bool fatal) {
- (*logger)(fmt::format(
- "While fetching via tmp repo:\n{}", msg),
- fatal);
- });
- if (not git_repo->FetchViaTmpRepo(tmp_dir->GetPath(),
- repo_info.repo_url,
- *op_result.result,
- wrapped_logger)) {
- return;
- }
- // setup wrapped logger
- wrapped_logger = std::make_shared<AsyncMapConsumerLogger>(
- [logger](auto const& msg, bool fatal) {
- (*logger)(fmt::format(
- "While checking commit exists:\n{}", msg),
+ (*logger)(fmt::format("While getting subtree "
+ "from commit:\n{}",
+ msg),
fatal);
});
- // check if commit exists now, after fetch
- auto is_commit_present =
- git_repo->CheckCommitExists(repo_info.hash, wrapped_logger);
- if (not is_commit_present) {
- return;
- }
- if (not *is_commit_present) {
- // commit could not be fetched, so fail
- (*logger)(
- fmt::format("Could not fetch commit {} from branch "
- "{} for remote {}",
- repo_info.hash,
- repo_info.branch,
- repo_info.repo_url),
- /*fatal=*/true);
+ // get tree id and return workspace root
+ auto subtree = git_repo->GetSubtreeFromCommit(
+ repo_info.hash, repo_info.subdir, wrapped_logger);
+ if (not subtree) {
return;
}
- // keep tag
- GitOpKey op_key = {{
- repo_root, // target_path
- repo_info.hash, // git_hash
- "", // branch
- "Keep referenced tree alive" // message
- },
- GitOpType::KEEP_TAG};
- critical_git_op_map->ConsumeAfterKeysReady(
- ts,
- {std::move(op_key)},
- [git_cas, repo_info, repo_root, ws_setter, logger](
- auto const& values) {
- GitOpValue op_result = *values[0];
- // check flag
- if (not op_result.result) {
- (*logger)("Keep tag failed",
- /*fatal=*/true);
- return;
- }
- // ensure commit exists, and fetch if needed
- auto git_repo =
- GitRepo::Open(git_cas); // link fake repo to odb
- if (not git_repo) {
- (*logger)(
- fmt::format("Could not open repository {}",
- repo_root.string()),
- /*fatal=*/true);
- return;
- }
- // setup wrapped logger
- auto wrapped_logger =
- std::make_shared<AsyncMapConsumerLogger>(
- [logger](auto const& msg, bool fatal) {
- (*logger)(
- fmt::format("While getting subtree "
- "from commit:\n{}",
- msg),
- fatal);
- });
- // get tree id and return workspace root
- auto subtree = git_repo->GetSubtreeFromCommit(
- repo_info.hash, repo_info.subdir, wrapped_logger);
- if (not subtree) {
- return;
- }
- // set the workspace root
- (*ws_setter)(nlohmann::json::array(
- {"git tree", *subtree, repo_root}));
- },
- [logger, target_path = repo_root](auto const& msg,
- bool fatal) {
- (*logger)(fmt::format("While running critical Git op "
- "KEEP_TAG for target {}:\n{}",
- target_path.string(),
- msg),
- fatal);
- });
+ // set the workspace root
+ (*ws_setter)(
+ nlohmann::json::array({"git tree", *subtree, repo_root}));
},
[logger, target_path = repo_root](auto const& msg, bool fatal) {
(*logger)(fmt::format("While running critical Git op "
- "GET_BRANCH_REFNAME for target {}:\n{}",
+ "KEEP_TAG for target {}:\n{}",
target_path.string(),
msg),
fatal);