blob: 6d87961e419e10257b3f619d42004bae0a35d7cf [file] [log] [blame]
Jonathan Niederc74c7202013-11-25 13:03:06 -08001# Helper functions used by interactive rebase tests.
Johannes Schindelin29a03342009-01-27 23:34:29 +01002
Johannes Schindelin03af0872009-01-27 23:34:35 +01003# After setting the fake editor with this function, you can
4#
Michael Haggerty959c0d02010-01-14 06:54:48 +01005# - override the commit message with $FAKE_COMMIT_MESSAGE
Johannes Schindelin03af0872009-01-27 23:34:35 +01006# - amend the commit message with $FAKE_COMMIT_AMEND
7# - check that non-commit messages have a certain line count with $EXPECT_COUNT
Michael Haggerty959c0d02010-01-14 06:54:48 +01008# - check the commit count in the commit message header with $EXPECT_HEADER_COUNT
Michael Haggerty05c95db2010-01-12 16:38:35 +01009# - rewrite a rebase -i script as directed by $FAKE_LINES.
10# $FAKE_LINES consists of a sequence of words separated by spaces.
11# The following word combinations are possible:
Johannes Schindelin03af0872009-01-27 23:34:35 +010012#
Michael Haggerty05c95db2010-01-12 16:38:35 +010013# "<lineno>" -- add a "pick" line with the SHA1 taken from the
14# specified line.
Johannes Schindelin03af0872009-01-27 23:34:35 +010015#
Michael Haggerty05c95db2010-01-12 16:38:35 +010016# "<cmd> <lineno>" -- add a line with the specified command
SZEDER Gábor7afb0d62018-08-23 12:09:15 +020017# ("pick", "squash", "fixup", "edit", "reword" or "drop") and the
18# SHA1 taken from the specified line.
Michael Haggerty05c95db2010-01-12 16:38:35 +010019#
Andrew Pimlott296fa992013-07-01 09:20:35 -070020# "exec_cmd_with_args" -- add an "exec cmd with args" line.
21#
Michael Haggerty05c95db2010-01-12 16:38:35 +010022# "#" -- Add a comment line.
23#
24# ">" -- Add a blank line.
Johannes Schindelin03af0872009-01-27 23:34:35 +010025
Johannes Schindelin29a03342009-01-27 23:34:29 +010026set_fake_editor () {
Andrew Pimlotta4952812013-07-01 09:23:38 -070027 write_script fake-editor.sh <<-\EOF
28 case "$1" in
29 */COMMIT_EDITMSG)
30 test -z "$EXPECT_HEADER_COUNT" ||
31 test "$EXPECT_HEADER_COUNT" = "$(sed -n '1s/^# This is a combination of \(.*\) commits\./\1/p' < "$1")" ||
Vasco Almeidaf2d17062016-06-17 20:21:05 +000032 test "# # GETTEXT POISON #" = "$(sed -n '1p' < "$1")" ||
Andrew Pimlotta4952812013-07-01 09:23:38 -070033 exit
34 test -z "$FAKE_COMMIT_MESSAGE" || echo "$FAKE_COMMIT_MESSAGE" > "$1"
35 test -z "$FAKE_COMMIT_AMEND" || echo "$FAKE_COMMIT_AMEND" >> "$1"
Michael Haggerty959c0d02010-01-14 06:54:48 +010036 exit
Andrew Pimlotta4952812013-07-01 09:23:38 -070037 ;;
Johannes Schindelin29a03342009-01-27 23:34:29 +010038 esac
Andrew Pimlotta4952812013-07-01 09:23:38 -070039 test -z "$EXPECT_COUNT" ||
40 test "$EXPECT_COUNT" = $(sed -e '/^#/d' -e '/^$/d' < "$1" | wc -l) ||
41 exit
42 test -z "$FAKE_LINES" && exit
43 grep -v '^#' < "$1" > "$1".tmp
44 rm -f "$1"
45 echo 'rebase -i script before editing:'
46 cat "$1".tmp
Johannes Schindelin5dcdd742019-07-31 08:18:48 -070047 action=\&
Andrew Pimlotta4952812013-07-01 09:23:38 -070048 for line in $FAKE_LINES; do
49 case $line in
Johannes Schindelin5dcdd742019-07-31 08:18:48 -070050 pick|p|squash|s|fixup|f|edit|e|reword|r|drop|d|label|l|reset|r|merge|m)
Andrew Pimlotta4952812013-07-01 09:23:38 -070051 action="$line";;
Junio C Hamanob3635c12018-11-06 15:50:22 +090052 exec_*|x_*|break|b)
Andrew Pimlotta4952812013-07-01 09:23:38 -070053 echo "$line" | sed 's/_/ /g' >> "$1";;
54 "#")
55 echo '# comment' >> "$1";;
56 ">")
57 echo >> "$1";;
Galan Rémi804098b2015-06-29 22:20:32 +020058 bad)
59 action="badcmd";;
60 fakesha)
Johannes Schindelin5dcdd742019-07-31 08:18:48 -070061 test \& != "$action" || action=pick
Galan Rémi804098b2015-06-29 22:20:32 +020062 echo "$action XXXXXXX False commit" >> "$1"
63 action=pick;;
Andrew Pimlotta4952812013-07-01 09:23:38 -070064 *)
Johannes Schindelin5dcdd742019-07-31 08:18:48 -070065 sed -n "${line}s/^[a-z][a-z]*/$action/p" < "$1".tmp >> "$1"
66 action=\&;;
Andrew Pimlotta4952812013-07-01 09:23:38 -070067 esac
68 done
69 echo 'rebase -i script after editing:'
70 cat "$1"
71 EOF
Johannes Schindelin29a03342009-01-27 23:34:29 +010072
73 test_set_editor "$(pwd)/fake-editor.sh"
Johannes Schindelin29a03342009-01-27 23:34:29 +010074}
Martin von Zweigbergk2aad7ca2013-06-06 23:11:37 -070075
Andrew Pimlott22c5b132013-06-27 12:26:31 -070076# After set_cat_todo_editor, rebase -i will write the todo list (ignoring
77# blank lines and comments) to stdout, and exit failure (so you should run
78# it with test_must_fail). This can be used to verify the expected user
79# experience, for todo list changes that do not affect the outcome of
80# rebase; or as an extra check in addition to checking the outcome.
81
82set_cat_todo_editor () {
83 write_script fake-editor.sh <<-\EOF
84 grep "^[^#]" "$1"
85 exit 1
86 EOF
87 test_set_editor "$(pwd)/fake-editor.sh"
88}
89
Martin von Zweigbergk2aad7ca2013-06-06 23:11:37 -070090# checks that the revisions in "$2" represent a linear range with the
91# subjects in "$1"
92test_linear_range () {
93 revlist_merges=$(git rev-list --merges "$2") &&
94 test -z "$revlist_merges" &&
95 expected=$1
96 set -- $(git log --reverse --format=%s "$2")
97 test "$expected" = "$*"
98}
99
100reset_rebase () {
101 test_might_fail git rebase --abort &&
102 git reset --hard &&
103 git clean -f
104}
Martin von Zweigbergk5b5e1c72013-06-06 23:11:38 -0700105
106cherry_pick () {
107 git cherry-pick -n "$2" &&
108 git commit -m "$1" &&
109 git tag "$1"
110}
111
112revert () {
113 git revert -n "$2" &&
114 git commit -m "$1" &&
115 git tag "$1"
116}
117
118make_empty () {
119 git commit --allow-empty -m "$1" &&
120 git tag "$1"
121}