fixup.cc5711424b7ae36276a40c06ede5d95f87ca20f0
[git/dscho.git] / t / lib-rebase.sh
blob459204212fd8a57f746b17161ed2c115f5c54bc3
1 #!/bin/sh
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 # - check that non-commit messages have a certain line count with $EXPECT_COUNT
8 # - check the commit count in the commit message header with $EXPECT_HEADER_COUNT
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:
13 # "<lineno>" -- add a "pick" line with the SHA1 taken from the
14 # specified line.
16 # "<cmd> <lineno>" -- add a line with the specified command
17 # ("squash", "fixup", "edit", or "reword") and the SHA1 taken
18 # from the specified line.
20 # "#" -- Add a comment line.
22 # ">" -- Add a blank line.
24 set_fake_editor () {
25 echo "#!$SHELL_PATH" >fake-editor.sh
26 cat >> fake-editor.sh <<\EOF
27 case "$1" in
28 */COMMIT_EDITMSG)
29 test -z "$EXPECT_HEADER_COUNT" ||
30 test "$EXPECT_HEADER_COUNT" = "$(sed -n '1s/^# This is a combination of \(.*\) commits\./\1/p' < "$1")" ||
31 exit
32 test -z "$FAKE_COMMIT_MESSAGE" || echo "$FAKE_COMMIT_MESSAGE" > "$1"
33 test -z "$FAKE_COMMIT_AMEND" || echo "$FAKE_COMMIT_AMEND" >> "$1"
34 exit
36 */git-rebase-todo)
37 test -z "$REBASE_SCRIPT" || {
38 mv "$REBASE_SCRIPT" "$1" && exit
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 squash|fixup|edit|reword)
54 action="$line";;
55 "#")
56 echo '# comment' >> "$1";;
57 ">")
58 echo >> "$1";;
59 [0-9]*)
60 sed -n "${line}s/^[^ ]*/$action/p" < "$1".tmp >> "$1"
61 action=\&
64 case "$action" in
65 \&) action="$line";;
66 *) echo "$action $line" >> "$1"; action=\&;;
67 esac
69 esac
70 done
71 echo 'rebase -i script after editing:'
72 cat "$1"
73 EOF
75 test_set_editor "$(pwd)/fake-editor.sh"
76 chmod a+x fake-editor.sh