]> git.ipfire.org Git - thirdparty/git.git/blame - t/lib-rebase.sh
Merge branch 'js/update-index-ignore-removal-for-skip-worktree'
[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
JS
6# - amend the commit message with $FAKE_COMMIT_AMEND
7# - check that non-commit messages have a certain line count with $EXPECT_COUNT
959c0d06 8# - check the commit count in the commit message header with $EXPECT_HEADER_COUNT
05c95dbe
MH
9# - 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:
03af0870 12#
05c95dbe
MH
13# "<lineno>" -- add a "pick" line with the SHA1 taken from the
14# specified line.
03af0870 15#
05c95dbe 16# "<cmd> <lineno>" -- add a line with the specified command
7afb0d67
SG
17# ("pick", "squash", "fixup", "edit", "reword" or "drop") and the
18# SHA1 taken from the specified line.
05c95dbe 19#
296fa993
AP
20# "exec_cmd_with_args" -- add an "exec cmd with args" line.
21#
05c95dbe
MH
22# "#" -- Add a comment line.
23#
24# ">" -- Add a blank line.
03af0870 25
29a03348 26set_fake_editor () {
a4952815
AP
27 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")" ||
f2d17068 32 test "# # GETTEXT POISON #" = "$(sed -n '1p' < "$1")" ||
a4952815
AP
33 exit
34 test -z "$FAKE_COMMIT_MESSAGE" || echo "$FAKE_COMMIT_MESSAGE" > "$1"
35 test -z "$FAKE_COMMIT_AMEND" || echo "$FAKE_COMMIT_AMEND" >> "$1"
959c0d06 36 exit
a4952815 37 ;;
29a03348 38 esac
a4952815
AP
39 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
5dcdd740 47 action=\&
a4952815
AP
48 for line in $FAKE_LINES; do
49 case $line in
5dcdd740 50 pick|p|squash|s|fixup|f|edit|e|reword|r|drop|d|label|l|reset|r|merge|m)
a4952815 51 action="$line";;
b3635c15 52 exec_*|x_*|break|b)
a4952815
AP
53 echo "$line" | sed 's/_/ /g' >> "$1";;
54 "#")
55 echo '# comment' >> "$1";;
56 ">")
57 echo >> "$1";;
804098bb
GR
58 bad)
59 action="badcmd";;
60 fakesha)
5dcdd740 61 test \& != "$action" || action=pick
804098bb
GR
62 echo "$action XXXXXXX False commit" >> "$1"
63 action=pick;;
a4952815 64 *)
5dcdd740
JS
65 sed -n "${line}s/^[a-z][a-z]*/$action/p" < "$1".tmp >> "$1"
66 action=\&;;
a4952815
AP
67 esac
68 done
69 echo 'rebase -i script after editing:'
70 cat "$1"
71 EOF
29a03348
JS
72
73 test_set_editor "$(pwd)/fake-editor.sh"
29a03348 74}
2aad7cac 75
22c5b136
AP
76# 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
2aad7cac
MZ
90# 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}
5b5e1c7c
MZ
105
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}
6a619ca0
PW
122
123# Call this (inside test_expect_success) at the end of a test file to
124# check that no tests have changed editor related environment
125# variables or config settings
126test_editor_unchanged () {
127 # We're only interested in exported variables hence 'sh -c'
128 sh -c 'cat >actual <<-EOF
129 EDITOR=$EDITOR
130 FAKE_COMMIT_AMEND=$FAKE_COMMIT_AMEND
131 FAKE_COMMIT_MESSAGE=$FAKE_COMMIT_MESSAGE
132 FAKE_LINES=$FAKE_LINES
133 GIT_EDITOR=$GIT_EDITOR
134 GIT_SEQUENCE_EDITOR=$GIT_SEQUENCE_EDITOR
135 core.editor=$(git config core.editor)
136 sequence.editor=$(git config sequence.editor)
137 EOF'
138 cat >expect <<-\EOF
139 EDITOR=:
140 FAKE_COMMIT_AMEND=
141 FAKE_COMMIT_MESSAGE=
142 FAKE_LINES=
143 GIT_EDITOR=
144 GIT_SEQUENCE_EDITOR=
145 core.editor=
146 sequence.editor=
147 EOF
148 test_cmp expect actual
149}