Skip to content

Commit 0006876

Browse files
committed
fix(amend): Don't drop commits after target
When amending a non-HEAD commit, we dropped all of the commits after it as we accidentally moved the branch to point to the fixup target.
1 parent 85b17e5 commit 0006876

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/bin/git-stack/amend.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ impl AmendArgs {
144144
)
145145
.with_code(proc_exit::Code::FAILURE)?;
146146
if let Some(fixup_id) = fixup_id {
147-
graph.insert(git_stack::graph::Node::new(fixup_id), head_id);
147+
if let Some(parent_id) = repo.parent_ids(fixup_id).expect("commit exists").first() {
148+
graph.insert(git_stack::graph::Node::new(fixup_id), *parent_id);
149+
}
148150
graph.commit_set(fixup_id, git_stack::graph::Fixup);
149151
}
150152
graph

tests/testsuite/amend.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ fn reword_rebases() {
237237
snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("git-stack"))
238238
.arg("amend")
239239
.arg("--message=new B")
240+
.arg("target")
240241
.current_dir(root_path)
241242
.assert()
242243
.success()
@@ -247,7 +248,7 @@ fn reword_rebases() {
247248
.stderr_matches(
248249
"\
249250
Saved working directory and index state WIP on local (amend): [..]
250-
Amended to [..]: C
251+
Amended to [..]: B
251252
Dropped refs/stash [..]
252253
note: to undo, run `git branch-stash pop git-stack`
253254
",
@@ -256,6 +257,10 @@ note: to undo, run `git branch-stash pop git-stack`
256257
let new_head_id = repo.head_commit().id;
257258
assert_ne!(old_head_id, new_head_id);
258259

260+
let local_branch = repo.find_local_branch("local").unwrap();
261+
let local_commit = repo.find_commit(local_branch.id).unwrap();
262+
snapbox::assert_eq(local_commit.summary.to_str_lossy().into_owned(), "C");
263+
259264
snapbox::assert_eq(std::fs::read(root_path.join("a")).unwrap(), "unstaged a");
260265

261266
root.close().unwrap();
@@ -479,6 +484,10 @@ note: to undo, run `git branch-stash pop git-stack`
479484
let new_head_id = repo.head_commit().id;
480485
assert_ne!(old_head_id, new_head_id);
481486

487+
let local_branch = repo.find_local_branch("local").unwrap();
488+
let local_commit = repo.find_commit(local_branch.id).unwrap();
489+
snapbox::assert_eq(local_commit.summary.to_str_lossy().into_owned(), "C");
490+
482491
snapbox::assert_eq(std::fs::read(root_path.join("a")).unwrap(), "unstaged a");
483492

484493
root.close().unwrap();
@@ -636,6 +645,10 @@ note: to undo, run `git branch-stash pop git-stack`
636645
let new_head_id = repo.head_commit().id;
637646
assert_ne!(old_head_id, new_head_id);
638647

648+
let local_branch = repo.find_local_branch("local").unwrap();
649+
let local_commit = repo.find_commit(local_branch.id).unwrap();
650+
snapbox::assert_eq(local_commit.summary.to_str_lossy().into_owned(), "C");
651+
639652
snapbox::assert_eq(std::fs::read(root_path.join("a")).unwrap(), "unstaged a");
640653

641654
root.close().unwrap();
@@ -717,9 +730,13 @@ note: to undo, run `git branch-stash pop git-stack`
717730
let new_head_id = repo.head_commit().id;
718731
assert_ne!(old_head_id, new_head_id);
719732

720-
snapbox::assert_eq(std::fs::read(root_path.join("a")).unwrap(), "unstaged a");
721-
722733
snapbox::assert_eq(repo.head_commit().summary.to_str().unwrap(), "fixup! B");
723734

735+
let local_branch = repo.find_local_branch("local").unwrap();
736+
let local_commit = repo.find_commit(local_branch.id).unwrap();
737+
snapbox::assert_eq(local_commit.summary.to_str_lossy().into_owned(), "fixup! B");
738+
739+
snapbox::assert_eq(std::fs::read(root_path.join("a")).unwrap(), "unstaged a");
740+
724741
root.close().unwrap();
725742
}

tests/testsuite/reword.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ note: to undo, run `git branch-stash pop git-stack`
258258
let commit = repo.find_commit(branch.id).unwrap();
259259
snapbox::assert_eq(commit.summary.to_str().unwrap(), "new B");
260260

261+
let local_branch = repo.find_local_branch("local").unwrap();
262+
let local_commit = repo.find_commit(local_branch.id).unwrap();
263+
snapbox::assert_eq(local_commit.summary.to_str_lossy().into_owned(), "C");
264+
261265
let new_head_id = repo.head_commit().id;
262266
assert_ne!(old_head_id, new_head_id);
263267

0 commit comments

Comments
 (0)