Skip to content

Fix: Improve merge commit information retrieval #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dashed
Copy link
Owner

@dashed dashed commented May 21, 2025

The get_merge_commit_info function was previously only checking the latest commit on a branch for merge information. This could lead to incomplete or misleading reports if earlier merge commits existed on the branch.

This commit modifies get_merge_commit_info to iterate through all commits on the specified branch. It now correctly identifies all merge commits that merged from the given parent branch and extracts their respective commit messages and statistics.

Additionally, a new test case (test_get_merge_commit_info_multiple_merges) has been added to tests/merge.rs. This test verifies that the updated function correctly retrieves information for multiple merge commits on a single branch, ensuring more robust and accurate reporting of merge activities.

The `get_merge_commit_info` function was previously only checking the
latest commit on a branch for merge information. This could lead to
incomplete or misleading reports if earlier merge commits existed on the
branch.

This commit modifies `get_merge_commit_info` to iterate through all
commits on the specified branch. It now correctly identifies all merge
commits that merged from the given parent branch and extracts their
respective commit messages and statistics.

Additionally, a new test case (`test_get_merge_commit_info_multiple_merges`)
has been added to `tests/merge.rs`. This test verifies that the updated
function correctly retrieves information for multiple merge commits on a
single branch, ensuring more robust and accurate reporting of merge
activities.
@dashed dashed requested a review from Copilot May 21, 2025 01:06
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances get_merge_commit_info to scan all commits on a branch for merge commits from a given parent branch and adds a test to verify multiple merges are detected.

  • Iterate through all commit hashes instead of only the latest
  • Refine parsing of merge messages and stats, collecting multiple MergeCommitInfo
  • Add test_get_merge_commit_info_multiple_merges to cover multiple merge scenarios

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
tests/merge.rs Added a new test to validate multiple merge commit retrieval
src/main.rs Overhauled get_merge_commit_info to loop commits and aggregate merge info

Comment on lines +1783 to +1786
// A merge commit must have a line starting with "Merge: "
let is_actual_merge_commit = commit_lines.iter().any(|line| line.starts_with("Merge: "));

if let Some(files_idx) = line.find("file changed") {
if let Some(files_num) = line[..files_idx].split_whitespace().last() {
files_changed = files_num.parse().unwrap_or(0);
}
} else if let Some(files_idx) = line.find("files changed") {
if let Some(files_num) = line[..files_idx].split_whitespace().last() {
files_changed = files_num.parse().unwrap_or(0);
}
}
if is_actual_merge_commit && (is_merge_from_parent || (parent_branch.is_empty() && is_generic_merge)) {
Copy link
Preview

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Detection currently only matches messages starting with "Merge branch...". Custom merge messages (e.g., from -m) won’t be recognized. Consider checking the commit’s parent count or using git log --merges to reliably identify merge commits.

Suggested change
// A merge commit must have a line starting with "Merge: "
let is_actual_merge_commit = commit_lines.iter().any(|line| line.starts_with("Merge: "));
if let Some(files_idx) = line.find("file changed") {
if let Some(files_num) = line[..files_idx].split_whitespace().last() {
files_changed = files_num.parse().unwrap_or(0);
}
} else if let Some(files_idx) = line.find("files changed") {
if let Some(files_num) = line[..files_idx].split_whitespace().last() {
files_changed = files_num.parse().unwrap_or(0);
}
}
if is_actual_merge_commit && (is_merge_from_parent || (parent_branch.is_empty() && is_generic_merge)) {
// A merge commit is identified by having more than one parent.
let is_actual_merge_commit = commit.parent_count() > 1;
if is_actual_merge_commit {

Copilot uses AI. Check for mistakes.

let mut command = Command::new("git");
command.args(["log", "--oneline", "-1", branch_name]);
command.args(["log", "--format=%H", branch_name]); // Get only commit hashes
Copy link
Preview

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running git show on every commit can be slow. You could use git log --merges --format=%H <branch> to directly list merge commits and avoid iterating non-merge commits.

Suggested change
command.args(["log", "--format=%H", branch_name]); // Get only commit hashes
command.args(["log", "--merges", "--format=%H", branch_name]); // Get only merge commit hashes

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant