-
Notifications
You must be signed in to change notification settings - Fork 7
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
dashed
wants to merge
1
commit into
master
Choose a base branch
from
fix-merge-commit-info
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1725,9 +1725,9 @@ impl GitChain { | |||||||||||||||||||||||||||||||||||
| parent_branch: &str, | ||||||||||||||||||||||||||||||||||||
| branch_name: &str, | ||||||||||||||||||||||||||||||||||||
| ) -> Result<Vec<MergeCommitInfo>, Error> { | ||||||||||||||||||||||||||||||||||||
| // Get the latest commit on the branch | ||||||||||||||||||||||||||||||||||||
| // 1. Fetch all commit hashes for the given `branch_name`. | ||||||||||||||||||||||||||||||||||||
| let mut command = Command::new("git"); | ||||||||||||||||||||||||||||||||||||
| command.args(["log", "--oneline", "-1", branch_name]); | ||||||||||||||||||||||||||||||||||||
| command.args(["log", "--format=%H", branch_name]); // Get only commit hashes | ||||||||||||||||||||||||||||||||||||
| let output = match command.output() { | ||||||||||||||||||||||||||||||||||||
| Ok(output) => output, | ||||||||||||||||||||||||||||||||||||
| Err(_) => return Ok(vec![]), // Return empty vec on error | ||||||||||||||||||||||||||||||||||||
|
|
@@ -1737,92 +1737,101 @@ impl GitChain { | |||||||||||||||||||||||||||||||||||
| return Ok(vec![]); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| let latest_commit = String::from_utf8_lossy(&output.stdout).trim().to_string(); | ||||||||||||||||||||||||||||||||||||
| if latest_commit.is_empty() { | ||||||||||||||||||||||||||||||||||||
| return Ok(vec![]); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| let commit_hashes_str = String::from_utf8_lossy(&output.stdout); | ||||||||||||||||||||||||||||||||||||
| let commit_hashes: Vec<&str> = commit_hashes_str.trim().lines().collect(); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Check if it's a merge commit by looking for parent commits | ||||||||||||||||||||||||||||||||||||
| let commit_hash = latest_commit.split_whitespace().next().unwrap_or(""); | ||||||||||||||||||||||||||||||||||||
| if commit_hash.is_empty() { | ||||||||||||||||||||||||||||||||||||
| if commit_hashes.is_empty() { | ||||||||||||||||||||||||||||||||||||
| return Ok(vec![]); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Get commit information | ||||||||||||||||||||||||||||||||||||
| let mut command = Command::new("git"); | ||||||||||||||||||||||||||||||||||||
| command.args(["show", "--stat", commit_hash]); | ||||||||||||||||||||||||||||||||||||
| let output = match command.output() { | ||||||||||||||||||||||||||||||||||||
| Ok(output) => output, | ||||||||||||||||||||||||||||||||||||
| Err(_) => return Ok(vec![]), | ||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||
| let mut merge_commits_info = Vec::new(); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if !output.status.success() { | ||||||||||||||||||||||||||||||||||||
| return Ok(vec![]); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| // 2. Iterate through each commit hash. | ||||||||||||||||||||||||||||||||||||
| for commit_hash in commit_hashes { | ||||||||||||||||||||||||||||||||||||
| if commit_hash.is_empty() { | ||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // 3. For each commit, use `git show --stat <commit_hash>` to get its information. | ||||||||||||||||||||||||||||||||||||
| let mut show_command = Command::new("git"); | ||||||||||||||||||||||||||||||||||||
| show_command.args(["show", "--stat", commit_hash]); | ||||||||||||||||||||||||||||||||||||
| let show_output = match show_command.output() { | ||||||||||||||||||||||||||||||||||||
| Ok(output) => output, | ||||||||||||||||||||||||||||||||||||
| Err(_) => continue, // Skip this commit on error | ||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if !show_output.status.success() { | ||||||||||||||||||||||||||||||||||||
| continue; // Skip this commit on error | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| let commit_info = String::from_utf8_lossy(&output.stdout).to_string(); | ||||||||||||||||||||||||||||||||||||
| let commit_info_str = String::from_utf8_lossy(&show_output.stdout).to_string(); | ||||||||||||||||||||||||||||||||||||
| let commit_lines: Vec<&str> = commit_info_str.lines().collect(); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Check if it's a merge commit, which typically contains "Merge" in the commit message | ||||||||||||||||||||||||||||||||||||
| if commit_info.contains(&format!("Merge branch '{}'", parent_branch)) | ||||||||||||||||||||||||||||||||||||
| || commit_info.contains("Merge branch") | ||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||
| // Extract commit message (first line after commit hash) | ||||||||||||||||||||||||||||||||||||
| let commit_lines: Vec<&str> = commit_info.lines().collect(); | ||||||||||||||||||||||||||||||||||||
| let message = commit_lines | ||||||||||||||||||||||||||||||||||||
| // 4. If the commit is a merge commit (contains "Merge branch" in its message AND refers to `parent_branch`), | ||||||||||||||||||||||||||||||||||||
| // extract its message and stats. | ||||||||||||||||||||||||||||||||||||
| let merge_line_prefix = format!("Merge branch '{}'", parent_branch); | ||||||||||||||||||||||||||||||||||||
| let is_merge_from_parent = commit_lines | ||||||||||||||||||||||||||||||||||||
| .iter() | ||||||||||||||||||||||||||||||||||||
| .position(|line| line.trim().starts_with("Merge branch")) | ||||||||||||||||||||||||||||||||||||
| .map(|idx| commit_lines[idx].trim().to_string()); | ||||||||||||||||||||||||||||||||||||
| .any(|line| line.trim().starts_with(&merge_line_prefix)); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Extract stats | ||||||||||||||||||||||||||||||||||||
| let stats_line = commit_lines | ||||||||||||||||||||||||||||||||||||
| // Also check for generic "Merge branch" if parent_branch is empty (e.g. for initial merge to main) | ||||||||||||||||||||||||||||||||||||
| // or if the specific format isn't exact. | ||||||||||||||||||||||||||||||||||||
| let is_generic_merge = commit_lines | ||||||||||||||||||||||||||||||||||||
| .iter() | ||||||||||||||||||||||||||||||||||||
| .find(|line| line.contains("files changed") || line.contains("file changed")); | ||||||||||||||||||||||||||||||||||||
| .any(|line| line.trim().starts_with("Merge branch")); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| let stats = stats_line.map(|line| { | ||||||||||||||||||||||||||||||||||||
| let mut files_changed = 0; | ||||||||||||||||||||||||||||||||||||
| let mut insertions = 0; | ||||||||||||||||||||||||||||||||||||
| let mut deletions = 0; | ||||||||||||||||||||||||||||||||||||
| // 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)) { | ||||||||||||||||||||||||||||||||||||
|
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)) { | |
| // 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running
git showon every commit can be slow. You could usegit log --merges --format=%H <branch>to directly list merge commits and avoid iterating non-merge commits.