Skip to content

Commit 0aab9e5

Browse files
authored
fix: Retarget multiple branches pointing at the same commit (#30)
1 parent 6510a13 commit 0aab9e5

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
* Retarget multiple branches pointing at the same commit
4+
35
# Version 0.2.3
46

57
* Allow setting the diff theme

src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,14 @@ fn do_rebase_inner(
176176
Ok(())
177177
}
178178

179-
struct RepoBranches<'a>(HashMap<Oid, Branch<'a>>);
179+
struct RepoBranches<'a>(HashMap<Oid, Vec<Branch<'a>>>);
180180

181181
impl<'a> RepoBranches<'a> {
182182
fn for_repo(repo: &'a Repository) -> Result<RepoBranches<'a>, anyhow::Error> {
183-
let mut branches: HashMap<Oid, Branch> = HashMap::new();
183+
let mut branches: HashMap<Oid, Vec<Branch>> = HashMap::new();
184184
for (branch, _type) in repo.branches(Some(git2::BranchType::Local))?.flatten() {
185185
let oid = branch.get().peel_to_commit()?.id();
186-
// TODO: handle multiple branches pointing to the same commit
187-
branches.insert(oid, branch);
186+
branches.entry(oid).or_default().push(branch);
188187
}
189188
Ok(RepoBranches(branches))
190189
}
@@ -196,13 +195,14 @@ impl<'a> RepoBranches<'a> {
196195
target_commit: Oid,
197196
rebase: &mut Rebase<'_>,
198197
) -> Result<(), anyhow::Error> {
199-
if let Some(branch) = self.0.get_mut(&original_commit) {
198+
if let Some(branches) = self.0.get_mut(&original_commit) {
200199
// Don't retarget the last branch, rebase.finish does that for us
201-
// TODO: handle multiple branches
202200
if rebase.operation_current() != Some(rebase.len() - 1) {
203-
branch
204-
.get_mut()
205-
.set_target(target_commit, "git-instafix retarget historical branch")?;
201+
for branch in branches.iter_mut() {
202+
branch
203+
.get_mut()
204+
.set_target(target_commit, "git-instafix retarget historical branch")?;
205+
}
206206
}
207207
}
208208
Ok(())

tests/basic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,15 @@ fn retarget_branches_in_range() {
269269
git_commits(&["a", "b"], &td);
270270
git(&["checkout", "-b", "intermediate"], &td);
271271
git_commits(&["target", "c", "d"], &td);
272+
git(&["checkout", "-b", "points-at-intermediate"], &td);
272273

273274
git(&["checkout", "-b", "changes"], &td);
274275
git_commits(&["e", "f"], &td);
275276

276277
let expected = "\
277278
* f HEAD -> changes
278279
* e
279-
* d intermediate
280+
* d points-at-intermediate, intermediate
280281
* c
281282
* target
282283
* b main

0 commit comments

Comments
 (0)