From 17f88a4752243f09ede9571014b8d723aca6ca44 Mon Sep 17 00:00:00 2001 From: Paul Cristian Sarbu Date: Tue, 31 Oct 2023 15:50:59 +0100 Subject: just-mr: Add 'mirrors' field to 'git' repositories --- src/other_tools/root_maps/commit_git_map.cpp | 42 +++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) (limited to 'src/other_tools/root_maps/commit_git_map.cpp') diff --git a/src/other_tools/root_maps/commit_git_map.cpp b/src/other_tools/root_maps/commit_git_map.cpp index 40399811..6959dfe6 100644 --- a/src/other_tools/root_maps/commit_git_map.cpp +++ b/src/other_tools/root_maps/commit_git_map.cpp @@ -355,18 +355,52 @@ void EnsureCommit(GitRepoInfo const& repo_info, /*fatal=*/true); return; } - // setup wrapped logger + // store failed attempts for subsequent logging + std::string err_messages{}; auto wrapped_logger = std::make_shared( - [logger](auto const& msg, bool fatal) { - (*logger)(fmt::format("While fetching via tmp repo:\n{}", msg), - fatal); + [fetch_repo, &err_messages](auto const& msg, bool /*fatal*/) { + err_messages += + fmt::format("\nWhile attempting fetch from remote {}:\n{}", + fetch_repo, + msg); }); + // try the main fetch URL + bool fetched{false}; if (not git_repo->FetchViaTmpRepo(tmp_dir->GetPath(), fetch_repo, repo_info.branch, git_bin, launcher, wrapped_logger)) { + // try to fetch from mirrors, in order, if given + for (auto mirror : repo_info.mirrors) { + if (GitURLIsPath(mirror)) { + mirror = std::filesystem::absolute(mirror).string(); + } + wrapped_logger = std::make_shared( + [mirror, &err_messages](auto const& msg, bool /*fatal*/) { + err_messages += fmt::format( + "\nWhile attempting fetch from mirror {}:\n{}", + mirror, + msg); + }); + if (git_repo->FetchViaTmpRepo(tmp_dir->GetPath(), + mirror, + repo_info.branch, + git_bin, + launcher, + wrapped_logger)) { + fetched = true; + break; + } + } + } + if (not fetched) { + // log fetch failure details separately to reduce verbosity + (*logger)( + fmt::format("While fetching via tmp repo:{}", err_messages), + /*fatal=*/false); + (*logger)("Failed to fetch from provided remotes", /*fatal=*/true); return; } // setup wrapped logger -- cgit v1.2.3