Merge branch 'ds/mailmap'
[git/debian.git] / t / lib-rebase.sh
blobec6b9b107da4eaab70e221b07c25c9f949f09b14
1 # Helper functions used by interactive rebase tests.
3 # After setting the fake editor with this function, you can
5 # - override the commit message with $FAKE_COMMIT_MESSAGE
6 # - amend the commit message with $FAKE_COMMIT_AMEND
7 # - copy the original commit message to a file with $FAKE_MESSAGE_COPY
8 # - check that non-commit messages have a certain line count with $EXPECT_COUNT
9 # - check the commit count in the commit message header with $EXPECT_HEADER_COUNT
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:
14 # "<lineno>" -- add a "pick" line with the SHA1 taken from the
15 # specified line.
17 # "<cmd> <lineno>" -- add a line with the specified command
18 # ("pick", "squash", "fixup"|"fixup_-C"|"fixup_-c", "edit", "reword" or "drop")
19 # and the SHA1 taken from the specified line.
21 # "_" -- add a space, like "fixup_-C" implies "fixup -C" and
22 # "exec_cmd_with_args" add an "exec cmd with args" line.
24 # "#" -- Add a comment line.
26 # ">" -- Add a blank line.
28 set_fake_editor () {
29 write_script fake-editor.sh <<-\EOF
30 case "$1" in
31 */COMMIT_EDITMSG)
32 test -z "$EXPECT_HEADER_COUNT" ||
33 test "$EXPECT_HEADER_COUNT" = "$(sed -n '1s/^# This is a combination of \(.*\) commits\./\1/p' < "$1")" ||
34 exit
35 test -z "$FAKE_COMMIT_MESSAGE" || echo "$FAKE_COMMIT_MESSAGE" > "$1"
36 test -z "$FAKE_COMMIT_AMEND" || echo "$FAKE_COMMIT_AMEND" >> "$1"
37 test -z "$FAKE_MESSAGE_COPY" || cat "$1" >"$FAKE_MESSAGE_COPY"
38 exit
40 esac
41 test -z "$EXPECT_COUNT" ||
42 test "$EXPECT_COUNT" = $(sed -e '/^#/d' -e '/^$/d' < "$1" | wc -l) ||
43 exit
44 test -z "$FAKE_LINES" && exit
45 grep -v '^#' < "$1" > "$1".tmp
46 rm -f "$1"
47 echo 'rebase -i script before editing:'
48 cat "$1".tmp
49 action=\&
50 for line in $FAKE_LINES; do
51 case $line in
52 pick|p|squash|s|fixup|f|edit|e|reword|r|drop|d|label|l|reset|r|merge|m)
53 action="$line";;
54 exec_*|x_*|break|b)
55 echo "$line" | sed 's/_/ /g' >> "$1";;
56 merge_*|fixup_*)
57 action=$(echo "$line" | sed 's/_/ /g');;
58 "#")
59 echo '# comment' >> "$1";;
60 ">")
61 echo >> "$1";;
62 bad)
63 action="badcmd";;
64 fakesha)
65 test \& != "$action" || action=pick
66 echo "$action XXXXXXX False commit" >> "$1"
67 action=pick;;
69 sed -n "${line}s/^[a-z][a-z]*/$action/p" < "$1".tmp >> "$1"
70 action=\&;;
71 esac
72 done
73 echo 'rebase -i script after editing:'
74 cat "$1"
75 EOF
77 test_set_editor "$(pwd)/fake-editor.sh"
80 # After set_cat_todo_editor, rebase -i will write the todo list (ignoring
81 # blank lines and comments) to stdout, and exit failure (so you should run
82 # it with test_must_fail). This can be used to verify the expected user
83 # experience, for todo list changes that do not affect the outcome of
84 # rebase; or as an extra check in addition to checking the outcome.
86 set_cat_todo_editor () {
87 write_script fake-editor.sh <<-\EOF
88 grep "^[^#]" "$1"
89 exit 1
90 EOF
91 test_set_editor "$(pwd)/fake-editor.sh"
94 # checks that the revisions in "$2" represent a linear range with the
95 # subjects in "$1"
96 test_linear_range () {
97 revlist_merges=$(git rev-list --merges "$2") &&
98 test -z "$revlist_merges" &&
99 expected=$1
100 set -- $(git log --reverse --format=%s "$2")
101 test "$expected" = "$*"
104 reset_rebase () {
105 test_might_fail git rebase --abort &&
106 git reset --hard &&
107 git clean -f
110 cherry_pick () {
111 git cherry-pick -n "$2" &&
112 git commit -m "$1" &&
113 git tag "$1"
116 revert () {
117 git revert -n "$2" &&
118 git commit -m "$1" &&
119 git tag "$1"
122 make_empty () {
123 git commit --allow-empty -m "$1" &&
124 git tag "$1"
127 # Call this (inside test_expect_success) at the end of a test file to
128 # check that no tests have changed editor related environment
129 # variables or config settings
130 test_editor_unchanged () {
131 # We're only interested in exported variables hence 'sh -c'
132 sh -c 'cat >actual <<-EOF
133 EDITOR=$EDITOR
134 FAKE_COMMIT_AMEND=$FAKE_COMMIT_AMEND
135 FAKE_COMMIT_MESSAGE=$FAKE_COMMIT_MESSAGE
136 FAKE_LINES=$FAKE_LINES
137 GIT_EDITOR=$GIT_EDITOR
138 GIT_SEQUENCE_EDITOR=$GIT_SEQUENCE_EDITOR
139 core.editor=$(git config core.editor)
140 sequence.editor=$(git config sequence.editor)
141 EOF'
142 cat >expect <<-\EOF
143 EDITOR=:
144 FAKE_COMMIT_AMEND=
145 FAKE_COMMIT_MESSAGE=
146 FAKE_LINES=
147 GIT_EDITOR=
148 GIT_SEQUENCE_EDITOR=
149 core.editor=
150 sequence.editor=
152 test_cmp expect actual
155 # Set up an editor for testing reword commands
156 # Checks that there are no uncommitted changes when rewording and that the
157 # todo-list is reread after each
158 set_reword_editor () {
159 >reword-actual &&
160 >reword-oid &&
162 # Check rewording keeps the original authorship
163 GIT_AUTHOR_NAME="Reword Author"
164 GIT_AUTHOR_EMAIL="reword.author@example.com"
165 GIT_AUTHOR_DATE=@123456
167 write_script reword-sequence-editor.sh <<-\EOF &&
168 todo="$(cat "$1")" &&
169 echo "exec git log -1 --pretty=format:'%an <%ae> %at%n%B%n' \
170 >>reword-actual" >"$1" &&
171 printf "%s\n" "$todo" >>"$1"
174 write_script reword-editor.sh <<-EOF &&
175 # Save the oid of the first reworded commit so we can check rebase
176 # fast-forwards to it. Also check that we do not write .git/MERGE_MSG
177 # when fast-forwarding
178 if ! test -s reword-oid
179 then
180 git rev-parse HEAD >reword-oid &&
181 if test -f .git/MERGE_MSG
182 then
183 echo 1>&2 "error: .git/MERGE_MSG exists"
184 exit 1
186 fi &&
187 # There should be no uncommited changes
188 git diff --exit-code HEAD &&
189 # The todo-list should be re-read after a reword
190 GIT_SEQUENCE_EDITOR="\"$PWD/reword-sequence-editor.sh\"" \
191 git rebase --edit-todo &&
192 echo edited >>"\$1"
195 test_set_editor "$PWD/reword-editor.sh"
198 # Check the results of a rebase after calling set_reword_editor
199 # Pass the commits that were reworded in the order that they were picked
200 # Expects the first pick to be a fast-forward
201 check_reworded_commits () {
202 test_cmp_rev "$(cat reword-oid)" "$1^{commit}" &&
203 git log --format="%an <%ae> %at%n%B%nedited%n" --no-walk=unsorted "$@" \
204 >reword-expected &&
205 test_cmp reword-expected reword-actual &&
206 git log --format="%an <%ae> %at%n%B" -n $# --first-parent --reverse \
207 >reword-log &&
208 test_cmp reword-expected reword-log