t / lib-rebase.shon commit Merge branch 'ap/rebase-multiple-fixups' (eb3a4fc)
   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#   "#" -- Add a comment line.
  21#
  22#   ">" -- Add a blank line.
  23
  24set_fake_editor () {
  25        write_script fake-editor.sh <<-\EOF
  26        case "$1" in
  27        */COMMIT_EDITMSG)
  28                test -z "$EXPECT_HEADER_COUNT" ||
  29                        test "$EXPECT_HEADER_COUNT" = "$(sed -n '1s/^# This is a combination of \(.*\) commits\./\1/p' < "$1")" ||
  30                        exit
  31                test -z "$FAKE_COMMIT_MESSAGE" || echo "$FAKE_COMMIT_MESSAGE" > "$1"
  32                test -z "$FAKE_COMMIT_AMEND" || echo "$FAKE_COMMIT_AMEND" >> "$1"
  33                exit
  34                ;;
  35        esac
  36        test -z "$EXPECT_COUNT" ||
  37                test "$EXPECT_COUNT" = $(sed -e '/^#/d' -e '/^$/d' < "$1" | wc -l) ||
  38                exit
  39        test -z "$FAKE_LINES" && exit
  40        grep -v '^#' < "$1" > "$1".tmp
  41        rm -f "$1"
  42        echo 'rebase -i script before editing:'
  43        cat "$1".tmp
  44        action=pick
  45        for line in $FAKE_LINES; do
  46                case $line in
  47                squash|fixup|edit|reword)
  48                        action="$line";;
  49                exec*)
  50                        echo "$line" | sed 's/_/ /g' >> "$1";;
  51                "#")
  52                        echo '# comment' >> "$1";;
  53                ">")
  54                        echo >> "$1";;
  55                *)
  56                        sed -n "${line}s/^pick/$action/p" < "$1".tmp >> "$1"
  57                        action=pick;;
  58                esac
  59        done
  60        echo 'rebase -i script after editing:'
  61        cat "$1"
  62        EOF
  63
  64        test_set_editor "$(pwd)/fake-editor.sh"
  65}
  66
  67# After set_cat_todo_editor, rebase -i will write the todo list (ignoring
  68# blank lines and comments) to stdout, and exit failure (so you should run
  69# it with test_must_fail).  This can be used to verify the expected user
  70# experience, for todo list changes that do not affect the outcome of
  71# rebase; or as an extra check in addition to checking the outcome.
  72
  73set_cat_todo_editor () {
  74        write_script fake-editor.sh <<-\EOF
  75        grep "^[^#]" "$1"
  76        exit 1
  77        EOF
  78        test_set_editor "$(pwd)/fake-editor.sh"
  79}
  80
  81# checks that the revisions in "$2" represent a linear range with the
  82# subjects in "$1"
  83test_linear_range () {
  84        revlist_merges=$(git rev-list --merges "$2") &&
  85        test -z "$revlist_merges" &&
  86        expected=$1
  87        set -- $(git log --reverse --format=%s "$2")
  88        test "$expected" = "$*"
  89}
  90
  91reset_rebase () {
  92        test_might_fail git rebase --abort &&
  93        git reset --hard &&
  94        git clean -f
  95}
  96
  97cherry_pick () {
  98        git cherry-pick -n "$2" &&
  99        git commit -m "$1" &&
 100        git tag "$1"
 101}
 102
 103revert () {
 104        git revert -n "$2" &&
 105        git commit -m "$1" &&
 106        git tag "$1"
 107}
 108
 109make_empty () {
 110        git commit --allow-empty -m "$1" &&
 111        git tag "$1"
 112}