t/t3437: fixup here-docs in the 'setup' test
[git] / t / lib-rebase.sh
1 # Helper functions used by interactive rebase tests.
2
3 # After setting the fake editor with this function, you can
4 #
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:
13 #
14 #   "<lineno>" -- add a "pick" line with the SHA1 taken from the
15 #       specified line.
16 #
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.
20 #
21 #   "_" -- add a space, like "fixup_-C" implies "fixup -C" and
22 #       "exec_cmd_with_args" add an "exec cmd with args" line.
23 #
24 #   "#" -- Add a comment line.
25 #
26 #   ">" -- Add a blank line.
27
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                         test "# # GETTEXT POISON #" = "$(sed -n '1p' < "$1")" ||
35                         exit
36                 test -z "$FAKE_COMMIT_MESSAGE" || echo "$FAKE_COMMIT_MESSAGE" > "$1"
37                 test -z "$FAKE_COMMIT_AMEND" || echo "$FAKE_COMMIT_AMEND" >> "$1"
38                 test -z "$FAKE_MESSAGE_COPY" || cat "$1" >"$FAKE_MESSAGE_COPY"
39                 exit
40                 ;;
41         esac
42         test -z "$EXPECT_COUNT" ||
43                 test "$EXPECT_COUNT" = $(sed -e '/^#/d' -e '/^$/d' < "$1" | wc -l) ||
44                 exit
45         test -z "$FAKE_LINES" && exit
46         grep -v '^#' < "$1" > "$1".tmp
47         rm -f "$1"
48         echo 'rebase -i script before editing:'
49         cat "$1".tmp
50         action=\&
51         for line in $FAKE_LINES; do
52                 case $line in
53                 pick|p|squash|s|fixup|f|edit|e|reword|r|drop|d|label|l|reset|r|merge|m)
54                         action="$line";;
55                 exec_*|x_*|break|b)
56                         echo "$line" | sed 's/_/ /g' >> "$1";;
57                 merge_*|fixup_*)
58                         action=$(echo "$line" | sed 's/_/ /g');;
59                 "#")
60                         echo '# comment' >> "$1";;
61                 ">")
62                         echo >> "$1";;
63                 bad)
64                         action="badcmd";;
65                 fakesha)
66                         test \& != "$action" || action=pick
67                         echo "$action XXXXXXX False commit" >> "$1"
68                         action=pick;;
69                 *)
70                         sed -n "${line}s/^[a-z][a-z]*/$action/p" < "$1".tmp >> "$1"
71                         action=\&;;
72                 esac
73         done
74         echo 'rebase -i script after editing:'
75         cat "$1"
76         EOF
77
78         test_set_editor "$(pwd)/fake-editor.sh"
79 }
80
81 # After set_cat_todo_editor, rebase -i will write the todo list (ignoring
82 # blank lines and comments) to stdout, and exit failure (so you should run
83 # it with test_must_fail).  This can be used to verify the expected user
84 # experience, for todo list changes that do not affect the outcome of
85 # rebase; or as an extra check in addition to checking the outcome.
86
87 set_cat_todo_editor () {
88         write_script fake-editor.sh <<-\EOF
89         grep "^[^#]" "$1"
90         exit 1
91         EOF
92         test_set_editor "$(pwd)/fake-editor.sh"
93 }
94
95 # checks that the revisions in "$2" represent a linear range with the
96 # subjects in "$1"
97 test_linear_range () {
98         revlist_merges=$(git rev-list --merges "$2") &&
99         test -z "$revlist_merges" &&
100         expected=$1
101         set -- $(git log --reverse --format=%s "$2")
102         test "$expected" = "$*"
103 }
104
105 reset_rebase () {
106         test_might_fail git rebase --abort &&
107         git reset --hard &&
108         git clean -f
109 }
110
111 cherry_pick () {
112         git cherry-pick -n "$2" &&
113         git commit -m "$1" &&
114         git tag "$1"
115 }
116
117 revert () {
118         git revert -n "$2" &&
119         git commit -m "$1" &&
120         git tag "$1"
121 }
122
123 make_empty () {
124         git commit --allow-empty -m "$1" &&
125         git tag "$1"
126 }
127
128 # Call this (inside test_expect_success) at the end of a test file to
129 # check that no tests have changed editor related environment
130 # variables or config settings
131 test_editor_unchanged () {
132         # We're only interested in exported variables hence 'sh -c'
133         sh -c 'cat >actual <<-EOF
134         EDITOR=$EDITOR
135         FAKE_COMMIT_AMEND=$FAKE_COMMIT_AMEND
136         FAKE_COMMIT_MESSAGE=$FAKE_COMMIT_MESSAGE
137         FAKE_LINES=$FAKE_LINES
138         GIT_EDITOR=$GIT_EDITOR
139         GIT_SEQUENCE_EDITOR=$GIT_SEQUENCE_EDITOR
140         core.editor=$(git config core.editor)
141         sequence.editor=$(git config sequence.editor)
142         EOF'
143         cat >expect <<-\EOF
144         EDITOR=:
145         FAKE_COMMIT_AMEND=
146         FAKE_COMMIT_MESSAGE=
147         FAKE_LINES=
148         GIT_EDITOR=
149         GIT_SEQUENCE_EDITOR=
150         core.editor=
151         sequence.editor=
152         EOF
153         test_cmp expect actual
154 }