]> git.ipfire.org Git - thirdparty/git.git/blame - t/lib-rebase.sh
Merge branch 'cm/rebase-i'
[thirdparty/git.git] / t / lib-rebase.sh
CommitLineData
c74c7203 1# Helper functions used by interactive rebase tests.
29a03348 2
03af0870
JS
3# After setting the fake editor with this function, you can
4#
959c0d06 5# - override the commit message with $FAKE_COMMIT_MESSAGE
03af0870 6# - amend the commit message with $FAKE_COMMIT_AMEND
1d410cd8 7# - copy the original commit message to a file with $FAKE_MESSAGE_COPY
03af0870 8# - check that non-commit messages have a certain line count with $EXPECT_COUNT
959c0d06 9# - check the commit count in the commit message header with $EXPECT_HEADER_COUNT
05c95dbe
MH
10# - rewrite a rebase -i script as directed by $FAKE_LINES.
11# $FAKE_LINES consists of a sequence of words separated by spaces.
12# The following word combinations are possible:
03af0870 13#
05c95dbe
MH
14# "<lineno>" -- add a "pick" line with the SHA1 taken from the
15# specified line.
03af0870 16#
05c95dbe 17# "<cmd> <lineno>" -- add a line with the specified command
7afb0d67
SG
18# ("pick", "squash", "fixup", "edit", "reword" or "drop") and the
19# SHA1 taken from the specified line.
05c95dbe 20#
296fa993
AP
21# "exec_cmd_with_args" -- add an "exec cmd with args" line.
22#
05c95dbe
MH
23# "#" -- Add a comment line.
24#
25# ">" -- Add a blank line.
03af0870 26
29a03348 27set_fake_editor () {
a4952815
AP
28 write_script fake-editor.sh <<-\EOF
29 case "$1" in
30 */COMMIT_EDITMSG)
31 test -z "$EXPECT_HEADER_COUNT" ||
32 test "$EXPECT_HEADER_COUNT" = "$(sed -n '1s/^# This is a combination of \(.*\) commits\./\1/p' < "$1")" ||
33 exit
34 test -z "$FAKE_COMMIT_MESSAGE" || echo "$FAKE_COMMIT_MESSAGE" > "$1"
35 test -z "$FAKE_COMMIT_AMEND" || echo "$FAKE_COMMIT_AMEND" >> "$1"
1d410cd8 36 test -z "$FAKE_MESSAGE_COPY" || cat "$1" >"$FAKE_MESSAGE_COPY"
959c0d06 37 exit
a4952815 38 ;;
29a03348 39 esac
a4952815
AP
40 test -z "$EXPECT_COUNT" ||
41 test "$EXPECT_COUNT" = $(sed -e '/^#/d' -e '/^$/d' < "$1" | wc -l) ||
42 exit
43 test -z "$FAKE_LINES" && exit
44 grep -v '^#' < "$1" > "$1".tmp
45 rm -f "$1"
46 echo 'rebase -i script before editing:'
47 cat "$1".tmp
5dcdd740 48 action=\&
a4952815
AP
49 for line in $FAKE_LINES; do
50 case $line in
5dcdd740 51 pick|p|squash|s|fixup|f|edit|e|reword|r|drop|d|label|l|reset|r|merge|m)
a4952815 52 action="$line";;
b3635c15 53 exec_*|x_*|break|b)
a4952815 54 echo "$line" | sed 's/_/ /g' >> "$1";;
1d410cd8
CM
55 merge_*|fixup_*)
56 action=$(echo "$line" | sed 's/_/ /g');;
a4952815
AP
57 "#")
58 echo '# comment' >> "$1";;
59 ">")
60 echo >> "$1";;
804098bb
GR
61 bad)
62 action="badcmd";;
63 fakesha)
5dcdd740 64 test \& != "$action" || action=pick
804098bb
GR
65 echo "$action XXXXXXX False commit" >> "$1"
66 action=pick;;
a4952815 67 *)
5dcdd740
JS
68 sed -n "${line}s/^[a-z][a-z]*/$action/p" < "$1".tmp >> "$1"
69 action=\&;;
a4952815
AP
70 esac
71 done
72 echo 'rebase -i script after editing:'
73 cat "$1"
74 EOF
29a03348
JS
75
76 test_set_editor "$(pwd)/fake-editor.sh"
29a03348 77}
2aad7cac 78
22c5b136
AP
79# After set_cat_todo_editor, rebase -i will write the todo list (ignoring
80# blank lines and comments) to stdout, and exit failure (so you should run
81# it with test_must_fail). This can be used to verify the expected user
82# experience, for todo list changes that do not affect the outcome of
83# rebase; or as an extra check in addition to checking the outcome.
84
85set_cat_todo_editor () {
86 write_script fake-editor.sh <<-\EOF
87 grep "^[^#]" "$1"
88 exit 1
89 EOF
90 test_set_editor "$(pwd)/fake-editor.sh"
91}
92
2aad7cac
MZ
93# checks that the revisions in "$2" represent a linear range with the
94# subjects in "$1"
95test_linear_range () {
96 revlist_merges=$(git rev-list --merges "$2") &&
97 test -z "$revlist_merges" &&
98 expected=$1
99 set -- $(git log --reverse --format=%s "$2")
100 test "$expected" = "$*"
101}
102
103reset_rebase () {
104 test_might_fail git rebase --abort &&
105 git reset --hard &&
106 git clean -f
107}
5b5e1c7c
MZ
108
109cherry_pick () {
110 git cherry-pick -n "$2" &&
111 git commit -m "$1" &&
112 git tag "$1"
113}
114
115revert () {
116 git revert -n "$2" &&
117 git commit -m "$1" &&
118 git tag "$1"
119}
120
121make_empty () {
122 git commit --allow-empty -m "$1" &&
123 git tag "$1"
124}
6a619ca0
PW
125
126# Call this (inside test_expect_success) at the end of a test file to
127# check that no tests have changed editor related environment
128# variables or config settings
129test_editor_unchanged () {
130 # We're only interested in exported variables hence 'sh -c'
131 sh -c 'cat >actual <<-EOF
132 EDITOR=$EDITOR
133 FAKE_COMMIT_AMEND=$FAKE_COMMIT_AMEND
134 FAKE_COMMIT_MESSAGE=$FAKE_COMMIT_MESSAGE
135 FAKE_LINES=$FAKE_LINES
136 GIT_EDITOR=$GIT_EDITOR
137 GIT_SEQUENCE_EDITOR=$GIT_SEQUENCE_EDITOR
138 core.editor=$(git config core.editor)
139 sequence.editor=$(git config sequence.editor)
140 EOF'
141 cat >expect <<-\EOF
142 EDITOR=:
143 FAKE_COMMIT_AMEND=
144 FAKE_COMMIT_MESSAGE=
145 FAKE_LINES=
146 GIT_EDITOR=
147 GIT_SEQUENCE_EDITOR=
148 core.editor=
149 sequence.editor=
150 EOF
151 test_cmp expect actual
152}