0db8250c5817a822021af4ceda1780ebddeaa553
   1#!/bin/sh
   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# - 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.
  22
  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"
  39echo 'rebase -i script before editing:'
  40cat "$1".tmp
  41action=pick
  42for 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
  54done
  55echo 'rebase -i script after editing:'
  56cat "$1"
  57EOF
  58
  59        test_set_editor "$(pwd)/fake-editor.sh"
  60        chmod a+x fake-editor.sh
  61}