fix 'last commit cannot be folded' check with reordered commits
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
diff --git a/got/got.c b/got/got.c
index 7de4b71..6885219 100644
--- a/got/got.c
+++ b/got/got.c
@@ -5009,7 +5009,8 @@ histedit_check_script(struct got_histedit_list *histedit_cmds,
}
}
- if (hle->cmd->code == GOT_HISTEDIT_FOLD)
+ hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
+ if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
return got_error_msg(GOT_ERR_HISTEDIT_CMD,
"last commit in histedit script cannot be folded");
diff --git a/regress/cmdline/histedit.sh b/regress/cmdline/histedit.sh
index 5aa888a..30628b7 100755
--- a/regress/cmdline/histedit.sh
+++ b/regress/cmdline/histedit.sh
@@ -1118,6 +1118,71 @@ function test_histedit_outside_refs_heads {
test_done "$testroot" "$ret"
}
+function test_histedit_fold_last_commit_swap {
+ local testroot=`test_init histedit_fold_last_commit_swap`
+
+ local orig_commit=`git_show_head $testroot/repo`
+
+ echo "modified alpha on master" > $testroot/repo/alpha
+ (cd $testroot/repo && git rm -q beta)
+ echo "new file on master" > $testroot/repo/epsilon/new
+ (cd $testroot/repo && git add epsilon/new)
+ git_commit $testroot/repo -m "committing changes"
+ local old_commit1=`git_show_head $testroot/repo`
+
+ echo "modified zeta on master" > $testroot/repo/epsilon/zeta
+ git_commit $testroot/repo -m "committing to zeta on master"
+ local old_commit2=`git_show_head $testroot/repo`
+
+ got checkout -c $orig_commit $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ # fold commit2 into commit1 (requires swapping commits)
+ echo "fold $old_commit2" > $testroot/histedit-script
+ echo "pick $old_commit1" >> $testroot/histedit-script
+ echo "mesg committing folded changes" >> $testroot/histedit-script
+
+ (cd $testroot/wt && got histedit -F $testroot/histedit-script \
+ > $testroot/stdout 2> $testroot/stderr)
+
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "histedit failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ local new_commit=`git_show_head $testroot/repo`
+
+ local short_old_commit1=`trim_obj_id 28 $old_commit1`
+ local short_old_commit2=`trim_obj_id 28 $old_commit2`
+ local short_new_commit=`trim_obj_id 28 $new_commit`
+
+ echo "G epsilon/zeta" >> $testroot/stdout.expected
+ echo -n "$short_old_commit2 -> fold commit: committing to zeta " \
+ >> $testroot/stdout.expected
+ echo "on master" >> $testroot/stdout.expected
+ echo "G alpha" >> $testroot/stdout.expected
+ echo "D beta" >> $testroot/stdout.expected
+ echo "A epsilon/new" >> $testroot/stdout.expected
+ echo -n "$short_old_commit1 -> $short_new_commit: " \
+ >> $testroot/stdout.expected
+ echo "committing folded changes" >> $testroot/stdout.expected
+ echo "Switching work tree to refs/heads/master" \
+ >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
run_test test_histedit_no_op
run_test test_histedit_swap
run_test test_histedit_drop
@@ -1129,3 +1194,4 @@ run_test test_histedit_abort
run_test test_histedit_path_prefix_drop
run_test test_histedit_path_prefix_edit
run_test test_histedit_outside_refs_heads
+run_test test_histedit_fold_last_commit_swap