]> git.ipfire.org Git - thirdparty/git.git/blame - t/lib-rebase.sh
lib-rebase: Allow comments and blank lines to be added to the rebase script
[thirdparty/git.git] / t / lib-rebase.sh
CommitLineData
29a03348
JS
1#!/bin/sh
2
03af0870
JS
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# - check that non-commit messages have a certain line count with $EXPECT_COUNT
05c95dbe
MH
8# - rewrite a rebase -i script as directed by $FAKE_LINES.
9# $FAKE_LINES consists of a sequence of words separated by spaces.
10# The following word combinations are possible:
03af0870 11#
05c95dbe
MH
12# "<lineno>" -- add a "pick" line with the SHA1 taken from the
13# specified line.
03af0870 14#
05c95dbe
MH
15# "<cmd> <lineno>" -- add a line with the specified command
16# ("squash", "fixup", "edit", or "reword") and the SHA1 taken
17# from the specified line.
18#
19# "#" -- Add a comment line.
20#
21# ">" -- Add a blank line.
03af0870 22
29a03348
JS
23set_fake_editor () {
24 echo "#!$SHELL_PATH" >fake-editor.sh
25 cat >> fake-editor.sh <<\EOF
26case "$1" in
27*/COMMIT_EDITMSG)
28 test -z "$FAKE_COMMIT_MESSAGE" || echo "$FAKE_COMMIT_MESSAGE" > "$1"
29 test -z "$FAKE_COMMIT_AMEND" || echo "$FAKE_COMMIT_AMEND" >> "$1"
30 exit
31 ;;
32esac
33test -z "$EXPECT_COUNT" ||
34 test "$EXPECT_COUNT" = $(sed -e '/^#/d' -e '/^$/d' < "$1" | wc -l) ||
35 exit
36test -z "$FAKE_LINES" && exit
37grep -v '^#' < "$1" > "$1".tmp
38rm -f "$1"
f64b4856 39echo 'rebase -i script before editing:'
29a03348
JS
40cat "$1".tmp
41action=pick
42for line in $FAKE_LINES; do
43 case $line in
0205e72f 44 squash|fixup|edit|reword)
29a03348 45 action="$line";;
05c95dbe
MH
46 "#")
47 echo '# comment' >> "$1";;
48 ">")
49 echo >> "$1";;
29a03348 50 *)
29a03348
JS
51 sed -n "${line}s/^pick/$action/p" < "$1".tmp >> "$1"
52 action=pick;;
53 esac
54done
f64b4856
MH
55echo 'rebase -i script after editing:'
56cat "$1"
29a03348
JS
57EOF
58
59 test_set_editor "$(pwd)/fake-editor.sh"
60 chmod a+x fake-editor.sh
61}