From 6fe7adec9cb63b368084fc593dfc96ccf016d695 Mon Sep 17 00:00:00 2001 From: Marco Craveiro Date: Sat, 5 Dec 2026 11:53:50 +0000 Subject: [PATCH] [cli] Read the added-file history once for every finding blame asked git for the commit that added each file with a --follow walk of the whole history, once per finding. On ORE Studio that is 8-15 s a call, and 277 inherited findings ran past the gate's 900 s timeout. Walk the history once with the findings' paths as pathspecs and answer each from the map. The pass on ores_dev_bright_faraday drops from more than 900 s (and still running after 300 s) to 111.6 s, the same as the ratchet without blame. Co-Authored-By: Claude Code --- .../include/compass.cli/ops/blame.hpp | 8 +++++ projects/compass.cli/src/app/blame.cpp | 34 +++++++++++++++++-- projects/compass.cli/src/ops/blame.cpp | 15 ++++++++ .../compass.cli/tests/ops_blame_tests.cpp | 13 +++++++ 4 files changed, 67 insertions(+), 3 deletions(-) diff --git a/projects/compass.cli/include/compass.cli/ops/blame.hpp b/projects/compass.cli/include/compass.cli/ops/blame.hpp index 42c903e4..d5557efb 100644 --- a/projects/compass.cli/include/compass.cli/ops/blame.hpp +++ b/projects/compass.cli/include/compass.cli/ops/blame.hpp @@ -79,6 +79,14 @@ struct blamed_lines { [[nodiscard]] std::optional pull_request_in( std::string_view subject); +/** + * @brief The commit that added each path, read from git log + * --diff-filter=A --name-only --format=%H: the first (newest) commit + * that lists a path, which is the one that last added it. + */ +[[nodiscard]] std::map> +parse_added_commits(std::span lines); + /** * @brief The 1-based lines of @p file, first and last, that differ from * @p generated: those between the lines both start with and the lines diff --git a/projects/compass.cli/src/app/blame.cpp b/projects/compass.cli/src/app/blame.cpp index 429b4a04..5f7f51fb 100644 --- a/projects/compass.cli/src/app/blame.cpp +++ b/projects/compass.cli/src/app/blame.cpp @@ -118,6 +118,7 @@ struct tracer { std::map read; std::map, std::set> probed; + std::map> added; std::size_t probes = 0; std::vector git(std::vector args) const { @@ -227,6 +228,24 @@ struct tracer { } } + /** + * @brief Reads the commit that added each of @p paths in one walk of + * history, which the added findings answer from; a --follow walk per + * path would repeat the whole history for every finding. + */ + void load_added(const std::vector& paths) { + std::set> asked; + for (const auto& path : paths) + if (!path.empty()) + asked.insert(path); + if (asked.empty()) + return; + std::vector args{"-c", "core.quotePath=false", "log", + "--diff-filter=A", "--name-only", "--format=%H", "HEAD", "--"}; + args.insert(args.end(), asked.begin(), asked.end()); + added = ops::parse_added_commits(git(std::move(args))); + } + void blame_added(types::blamed_finding& b) { const auto& f = b.found; b.subject = f.subject.empty() ? f.path : f.subject; @@ -237,9 +256,11 @@ struct tracer { if (found.empty() && !f.subject_text.empty()) b.note = "no commit ever changed '" + f.subject_text + "' in " + b.subject + ", so the requirement may be newer than the file"; - if (found.empty()) - found = git({"log", "-1", "--follow", "--diff-filter=A", - "--format=%H", "HEAD", "--", b.subject}); + if (found.empty()) { + const auto it = added.find(b.subject); + if (it != added.end()) + found.push_back(it->second); + } if (found.empty()) { b.note = "no commit added " + b.subject; return; @@ -549,6 +570,13 @@ types::blame_report blame(const types::options& o, .method = blame_method_of(o, f.rule), .subject = {}, .culprits = {}, .note = {}}); + std::vector added_paths; + for (const auto& b : r.findings) + if (b.method == blame_method::added) + added_paths.push_back(b.found.subject.empty() ? b.found.path + : b.found.subject); + t.load_added(added_paths); + std::map> by_path; for (std::size_t i = 0; i < r.findings.size(); ++i) { auto& b = r.findings[i]; diff --git a/projects/compass.cli/src/ops/blame.cpp b/projects/compass.cli/src/ops/blame.cpp index 2a6f93ba..e1a63b5a 100644 --- a/projects/compass.cli/src/ops/blame.cpp +++ b/projects/compass.cli/src/ops/blame.cpp @@ -131,6 +131,21 @@ std::optional pull_request_in(std::string_view subject) { subject.size() - open - 3)); } +std::map> parse_added_commits( + std::span lines) { + std::map> out; + std::string commit; + for (const std::string_view line : lines) { + if (is_commit_id(line)) { + commit = std::string(line); + continue; + } + if (!line.empty() && !commit.empty()) + out.emplace(std::string(line), commit); + } + return out; +} + std::pair differing_lines( std::string_view file, std::string_view generated) { const auto a = lines_of(file); diff --git a/projects/compass.cli/tests/ops_blame_tests.cpp b/projects/compass.cli/tests/ops_blame_tests.cpp index 25cad78b..7cbd8a69 100644 --- a/projects/compass.cli/tests/ops_blame_tests.cpp +++ b/projects/compass.cli/tests/ops_blame_tests.cpp @@ -70,6 +70,19 @@ TEST_CASE("blame reads the commit of each line and each commit's time from " CHECK(r.times.at(b) == 200); } +TEST_CASE("blame reads the commit that added each path from one history " + "walk", "[ops][blame]") { + const std::string a(40, 'a'); + const std::string b(40, 'b'); + const std::vector lines{a, "", "x.sql", "y.sql", b, "", + "x.sql", "z.sql"}; + const auto added = ops::parse_added_commits(lines); + REQUIRE(added.size() == 3); + CHECK(added.at("x.sql") == a); + CHECK(added.at("y.sql") == a); + CHECK(added.at("z.sql") == b); +} + TEST_CASE("blame finds the pull request a merge or a squash subject names", "[ops][blame]") { CHECK(ops::pull_request_in("Merge pull request #4966 from o/feature/x") ==