Skip to content

Commit

Permalink
ui: Show messages for ref updates (#31)
Browse files Browse the repository at this point in the history
Co-authored-by: Brandon W Maister <[email protected]>
  • Loading branch information
quodlibetor and Brandon W Maister authored Jun 22, 2024
1 parent f2652ef commit 3373eee
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

# Version 0.2.4

* Retarget multiple branches pointing at the same commit
* Retarget multiple branches pointing at the same commit, eg:
> updated branch my-cool-branch: deadbeef -> c0ffee
# Version 0.2.3

Expand Down
40 changes: 36 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ fn apply_diff_in_rebase(
let rewrit_id = target_commit.amend(None, None, None, None, None, Some(&tree))?;
let rewrit_object = repo.find_object(rewrit_id, None)?;
let rewrit_commit_id = repo.find_commit(rewrit_object.id())?.id();
branches.retarget_branches(target_commit.id(), rewrit_commit_id, rebase)?;
let retargeted =
branches.retarget_branches(target_commit.id(), rewrit_commit_id, rebase)?;
for b in retargeted {
println!("{}", b);
}

repo.reset(&rewrit_object, git2::ResetType::Soft, None)?;
}
Expand Down Expand Up @@ -162,7 +166,10 @@ fn do_rebase_inner(
let message = commit.message();
if message.is_some() && message != fixup_message {
let new_id = rebase.commit(None, &sig, None)?;
branches.retarget_branches(commit.id(), new_id, rebase)?;
let retargeted = branches.retarget_branches(commit.id(), new_id, rebase)?;
for b in retargeted {
println!("{}", b);
}
}
}
Some(Fixup) | Some(Squash) | Some(Exec) | Some(Edit) | Some(Reword) => {
Expand All @@ -178,6 +185,21 @@ fn do_rebase_inner(

struct RepoBranches<'a>(HashMap<Oid, Vec<Branch<'a>>>);

struct RetargetedBranch {
name: String,
from: Oid,
to: Oid,
}

impl std::fmt::Display for RetargetedBranch {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let from = &self.from.to_string()[..15];
let to = &self.to.to_string()[..15];
let name = &self.name;
f.write_fmt(format_args!("updated branch {name}: {from} -> {to}"))
}
}

impl<'a> RepoBranches<'a> {
fn for_repo(repo: &'a Repository) -> Result<RepoBranches<'a>, anyhow::Error> {
let mut branches: HashMap<Oid, Vec<Branch>> = HashMap::new();
Expand All @@ -194,18 +216,28 @@ impl<'a> RepoBranches<'a> {
original_commit: Oid,
target_commit: Oid,
rebase: &mut Rebase<'_>,
) -> Result<(), anyhow::Error> {
) -> Result<Vec<RetargetedBranch>, anyhow::Error> {
let mut retargeted = vec![];
if let Some(branches) = self.0.get_mut(&original_commit) {
// Don't retarget the last branch, rebase.finish does that for us
if rebase.operation_current() != Some(rebase.len() - 1) {
for branch in branches.iter_mut() {
retargeted.push(RetargetedBranch {
name: branch
.name()
.context("getting a branch name")?
.ok_or(anyhow!("branch should have a name"))?
.to_owned(),
from: original_commit,
to: target_commit,
});
branch
.get_mut()
.set_target(target_commit, "git-instafix retarget historical branch")?;
}
}
}
Ok(())
Ok(retargeted)
}
}

Expand Down

0 comments on commit 3373eee

Please sign in to comment.