Skip to content

Commit 52a6e8c

Browse files
authored
Fix a bug in 'abandon' when the current branch is 'main'. (#68)
Before this change we incorrectly tried to rebase the bottom commit which resulted in malformed state of the result.
1 parent b49df6a commit 52a6e8c

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/stack_pr/cli.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,14 +1110,18 @@ def command_land(args: CommonArgs):
11101110
# ===----------------------------------------------------------------------=== #
11111111
# ABANDON
11121112
# ===----------------------------------------------------------------------=== #
1113-
def strip_metadata(e: StackEntry, verbose: bool) -> str:
1113+
def strip_metadata(e: StackEntry, needs_rebase: bool, verbose: bool) -> str:
11141114
m = e.commit.commit_msg()
11151115

11161116
m = RE_STACK_INFO_LINE.sub("", m)
1117-
run_shell_command(
1118-
["git", "rebase", e.base, e.head, "--committer-date-is-author-date"],
1119-
quiet=not verbose,
1120-
)
1117+
if needs_rebase:
1118+
run_shell_command(
1119+
["git", "rebase", e.base, e.head, "--committer-date-is-author-date"],
1120+
quiet=not verbose,
1121+
)
1122+
else:
1123+
run_shell_command(["git", "checkout", e.head], quiet=not verbose)
1124+
11211125
run_shell_command(
11221126
["git", "commit", "--amend", "-F", "-"],
11231127
input=m.encode(),
@@ -1146,8 +1150,13 @@ def command_abandon(args: CommonArgs):
11461150
log(h("Stripping stack metadata from commit messages"), level=1)
11471151

11481152
last_hash = ""
1153+
# The first commit doesn't need to be rebased since its will not change.
1154+
# The rest of the commits need to be rebased since their base will be
1155+
# changed as we strip the metadata from the commit messages.
1156+
need_rebase = False
11491157
for e in st:
1150-
last_hash = strip_metadata(e, args.verbose)
1158+
last_hash = strip_metadata(e, need_rebase, args.verbose)
1159+
need_rebase = True
11511160

11521161
log(h("Rebasing the current branch on top of updated top branch"), level=1)
11531162
run_shell_command(

0 commit comments

Comments
 (0)