Code

lib-rebase: Allow comments and blank lines to be added to the rebase script
[git.git] / t / lib-rebase.sh
1 #!/bin/sh
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
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:
11 #
12 #   "<lineno>" -- add a "pick" line with the SHA1 taken from the
13 #       specified line.
14 #
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.
23 set_fake_editor () {
24         echo "#!$SHELL_PATH" >fake-editor.sh
25         cat >> fake-editor.sh <<\EOF
26 case "$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         ;;
32 esac
33 test -z "$EXPECT_COUNT" ||
34         test "$EXPECT_COUNT" = $(sed -e '/^#/d' -e '/^$/d' < "$1" | wc -l) ||
35         exit
36 test -z "$FAKE_LINES" && exit
37 grep -v '^#' < "$1" > "$1".tmp
38 rm -f "$1"
39 echo 'rebase -i script before editing:'
40 cat "$1".tmp
41 action=pick
42 for line in $FAKE_LINES; do
43         case $line in
44         squash|fixup|edit|reword)
45                 action="$line";;
46         "#")
47                 echo '# comment' >> "$1";;
48         ">")
49                 echo >> "$1";;
50         *)
51                 sed -n "${line}s/^pick/$action/p" < "$1".tmp >> "$1"
52                 action=pick;;
53         esac
54 done
55 echo 'rebase -i script after editing:'
56 cat "$1"
57 EOF
59         test_set_editor "$(pwd)/fake-editor.sh"
60         chmod a+x fake-editor.sh
61 }