t / lib-rebase.shon commit Merge branch 'ar/wildmatch-foldcase' into maint (4b0d73f)
   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# - 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:
  12#
  13#   "<lineno>" -- add a "pick" line with the SHA1 taken from the
  14#       specified line.
  15#
  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.
  19#
  20#   "exec_cmd_with_args" -- add an "exec cmd with args" line.
  21#
  22#   "#" -- Add a comment line.
  23#
  24#   ">" -- Add a blank line.
  25
  26set_fake_editor () {
  27        echo "#!$SHELL_PATH" >fake-editor.sh
  28        cat >> fake-editor.sh <<\EOF
  29case "$1" in
  30*/COMMIT_EDITMSG)
  31        test -z "$EXPECT_HEADER_COUNT" ||
  32                test "$EXPECT_HEADER_COUNT" = "$(sed -n '1s/^# This is a combination of \(.*\) commits\./\1/p' < "$1")" ||
  33                exit
  34        test -z "$FAKE_COMMIT_MESSAGE" || echo "$FAKE_COMMIT_MESSAGE" > "$1"
  35        test -z "$FAKE_COMMIT_AMEND" || echo "$FAKE_COMMIT_AMEND" >> "$1"
  36        exit
  37        ;;
  38esac
  39test -z "$EXPECT_COUNT" ||
  40        test "$EXPECT_COUNT" = $(sed -e '/^#/d' -e '/^$/d' < "$1" | wc -l) ||
  41        exit
  42test -z "$FAKE_LINES" && exit
  43grep -v '^#' < "$1" > "$1".tmp
  44rm -f "$1"
  45echo 'rebase -i script before editing:'
  46cat "$1".tmp
  47action=pick
  48for line in $FAKE_LINES; do
  49        case $line in
  50        squash|fixup|edit|reword)
  51                action="$line";;
  52        exec*)
  53                echo "$line" | sed 's/_/ /g' >> "$1";;
  54        "#")
  55                echo '# comment' >> "$1";;
  56        ">")
  57                echo >> "$1";;
  58        *)
  59                sed -n "${line}s/^pick/$action/p" < "$1".tmp >> "$1"
  60                action=pick;;
  61        esac
  62done
  63echo 'rebase -i script after editing:'
  64cat "$1"
  65EOF
  66
  67        test_set_editor "$(pwd)/fake-editor.sh"
  68        chmod a+x fake-editor.sh
  69}