Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions projects/compass.cli/include/compass.cli/ops/blame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ struct blamed_lines {
[[nodiscard]] std::optional<std::uint64_t> 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<std::string, std::string, std::less<>>
parse_added_commits(std::span<const std::string> 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
Expand Down
34 changes: 31 additions & 3 deletions projects/compass.cli/src/app/blame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ struct tracer {
std::map<std::string, types::culprit> read;
std::map<std::pair<std::string, std::string>, std::set<finding_key>>
probed;
std::map<std::string, std::string, std::less<>> added;
std::size_t probes = 0;

std::vector<std::string> git(std::vector<std::string> args) const {
Expand Down Expand Up @@ -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<std::string>& paths) {
std::set<std::string, std::less<>> asked;
for (const auto& path : paths)
if (!path.empty())
asked.insert(path);
if (asked.empty())
return;
std::vector<std::string> 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;
Expand All @@ -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;
Expand Down Expand Up @@ -549,6 +570,13 @@ types::blame_report blame(const types::options& o,
.method = blame_method_of(o, f.rule),
.subject = {}, .culprits = {}, .note = {}});

std::vector<std::string> 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<std::string, std::vector<std::size_t>> by_path;
for (std::size_t i = 0; i < r.findings.size(); ++i) {
auto& b = r.findings[i];
Expand Down
15 changes: 15 additions & 0 deletions projects/compass.cli/src/ops/blame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ std::optional<std::uint64_t> pull_request_in(std::string_view subject) {
subject.size() - open - 3));
}

std::map<std::string, std::string, std::less<>> parse_added_commits(
std::span<const std::string> lines) {
std::map<std::string, std::string, std::less<>> 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<std::uint32_t, std::uint32_t> differing_lines(
std::string_view file, std::string_view generated) {
const auto a = lines_of(file);
Expand Down
13 changes: 13 additions & 0 deletions projects/compass.cli/tests/ops_blame_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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") ==
Expand Down
Loading