Skip to content

Commit f4d5564

Browse files
runningcodeclaude
andcommitted
refactor: Use iterator chain for branch searching instead of explicit loop
Replace the explicit for loop with a more idiomatic iterator chain using find_map(). This addresses the PR review comment about simplifying the branching code with method chaining. The new implementation: - Uses find_map() to search and transform in one step - Eliminates the explicit for loop and early returns - Maintains the same functionality with cleaner, more idiomatic Rust 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 27c41db commit f4d5564

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

src/utils/vcs.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -293,20 +293,21 @@ fn find_merge_base_ref(
293293
let merge_base_oid = repo.merge_base(head_commit.id(), remote_commit.id())?;
294294

295295
// Try to find a branch name that points to this commit
296-
let branches = repo.branches(Some(git2::BranchType::Local))?;
297-
for (branch, _) in branches.flatten() {
298-
if let Ok(branch_commit) = branch.get().peel_to_commit() {
296+
let branch_name = repo
297+
.branches(Some(git2::BranchType::Local))?
298+
.flatten()
299+
.find_map(|(branch, _)| {
300+
let branch_commit = branch.get().peel_to_commit().ok()?;
299301
if branch_commit.id() == merge_base_oid {
300-
if let Some(branch_name) = branch.name()? {
301-
debug!("Found base branch reference: {}", branch_name);
302-
return Ok(Some(branch_name.to_owned()));
303-
}
302+
let branch_name = branch.name().ok()??;
303+
debug!("Found base branch reference: {}", branch_name);
304+
Some(branch_name.to_owned())
305+
} else {
306+
None
304307
}
305-
}
306-
}
308+
});
307309

308-
// If no branch name found, return None (only return branch names)
309-
Ok(None)
310+
Ok(branch_name)
310311
}
311312

312313
fn find_reference_url(repo: &str, repos: &[Repo]) -> Result<Option<String>> {

0 commit comments

Comments
 (0)