Jonathan Nieder | c74c720 | 2013-11-25 13:03:06 -0800 | [diff] [blame] | 1 | # Helper functions used by interactive rebase tests. |
Johannes Schindelin | 29a0334 | 2009-01-27 23:34:29 +0100 | [diff] [blame] | 2 | |
Johannes Schindelin | 03af087 | 2009-01-27 23:34:35 +0100 | [diff] [blame] | 3 | # After setting the fake editor with this function, you can |
| 4 | # |
Michael Haggerty | 959c0d0 | 2010-01-14 06:54:48 +0100 | [diff] [blame] | 5 | # - override the commit message with $FAKE_COMMIT_MESSAGE |
Johannes Schindelin | 03af087 | 2009-01-27 23:34:35 +0100 | [diff] [blame] | 6 | # - amend the commit message with $FAKE_COMMIT_AMEND |
| 7 | # - check that non-commit messages have a certain line count with $EXPECT_COUNT |
Michael Haggerty | 959c0d0 | 2010-01-14 06:54:48 +0100 | [diff] [blame] | 8 | # - check the commit count in the commit message header with $EXPECT_HEADER_COUNT |
Michael Haggerty | 05c95db | 2010-01-12 16:38:35 +0100 | [diff] [blame] | 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: |
Johannes Schindelin | 03af087 | 2009-01-27 23:34:35 +0100 | [diff] [blame] | 12 | # |
Michael Haggerty | 05c95db | 2010-01-12 16:38:35 +0100 | [diff] [blame] | 13 | # "<lineno>" -- add a "pick" line with the SHA1 taken from the |
| 14 | # specified line. |
Johannes Schindelin | 03af087 | 2009-01-27 23:34:35 +0100 | [diff] [blame] | 15 | # |
Michael Haggerty | 05c95db | 2010-01-12 16:38:35 +0100 | [diff] [blame] | 16 | # "<cmd> <lineno>" -- add a line with the specified command |
SZEDER Gábor | 7afb0d6 | 2018-08-23 12:09:15 +0200 | [diff] [blame] | 17 | # ("pick", "squash", "fixup", "edit", "reword" or "drop") and the |
| 18 | # SHA1 taken from the specified line. |
Michael Haggerty | 05c95db | 2010-01-12 16:38:35 +0100 | [diff] [blame] | 19 | # |
Andrew Pimlott | 296fa99 | 2013-07-01 09:20:35 -0700 | [diff] [blame] | 20 | # "exec_cmd_with_args" -- add an "exec cmd with args" line. |
| 21 | # |
Michael Haggerty | 05c95db | 2010-01-12 16:38:35 +0100 | [diff] [blame] | 22 | # "#" -- Add a comment line. |
| 23 | # |
| 24 | # ">" -- Add a blank line. |
Johannes Schindelin | 03af087 | 2009-01-27 23:34:35 +0100 | [diff] [blame] | 25 | |
Johannes Schindelin | 29a0334 | 2009-01-27 23:34:29 +0100 | [diff] [blame] | 26 | set_fake_editor () { |
Andrew Pimlott | a495281 | 2013-07-01 09:23:38 -0700 | [diff] [blame] | 27 | write_script fake-editor.sh <<-\EOF |
| 28 | case "$1" in |
| 29 | */COMMIT_EDITMSG) |
| 30 | test -z "$EXPECT_HEADER_COUNT" || |
| 31 | test "$EXPECT_HEADER_COUNT" = "$(sed -n '1s/^# This is a combination of \(.*\) commits\./\1/p' < "$1")" || |
Vasco Almeida | f2d1706 | 2016-06-17 20:21:05 +0000 | [diff] [blame] | 32 | test "# # GETTEXT POISON #" = "$(sed -n '1p' < "$1")" || |
Andrew Pimlott | a495281 | 2013-07-01 09:23:38 -0700 | [diff] [blame] | 33 | exit |
| 34 | test -z "$FAKE_COMMIT_MESSAGE" || echo "$FAKE_COMMIT_MESSAGE" > "$1" |
| 35 | test -z "$FAKE_COMMIT_AMEND" || echo "$FAKE_COMMIT_AMEND" >> "$1" |
Michael Haggerty | 959c0d0 | 2010-01-14 06:54:48 +0100 | [diff] [blame] | 36 | exit |
Andrew Pimlott | a495281 | 2013-07-01 09:23:38 -0700 | [diff] [blame] | 37 | ;; |
Johannes Schindelin | 29a0334 | 2009-01-27 23:34:29 +0100 | [diff] [blame] | 38 | esac |
Andrew Pimlott | a495281 | 2013-07-01 09:23:38 -0700 | [diff] [blame] | 39 | test -z "$EXPECT_COUNT" || |
| 40 | test "$EXPECT_COUNT" = $(sed -e '/^#/d' -e '/^$/d' < "$1" | wc -l) || |
| 41 | exit |
| 42 | test -z "$FAKE_LINES" && exit |
| 43 | grep -v '^#' < "$1" > "$1".tmp |
| 44 | rm -f "$1" |
| 45 | echo 'rebase -i script before editing:' |
| 46 | cat "$1".tmp |
Johannes Schindelin | 5dcdd74 | 2019-07-31 08:18:48 -0700 | [diff] [blame] | 47 | action=\& |
Andrew Pimlott | a495281 | 2013-07-01 09:23:38 -0700 | [diff] [blame] | 48 | for line in $FAKE_LINES; do |
| 49 | case $line in |
Johannes Schindelin | 5dcdd74 | 2019-07-31 08:18:48 -0700 | [diff] [blame] | 50 | pick|p|squash|s|fixup|f|edit|e|reword|r|drop|d|label|l|reset|r|merge|m) |
Andrew Pimlott | a495281 | 2013-07-01 09:23:38 -0700 | [diff] [blame] | 51 | action="$line";; |
Junio C Hamano | b3635c1 | 2018-11-06 15:50:22 +0900 | [diff] [blame] | 52 | exec_*|x_*|break|b) |
Andrew Pimlott | a495281 | 2013-07-01 09:23:38 -0700 | [diff] [blame] | 53 | echo "$line" | sed 's/_/ /g' >> "$1";; |
| 54 | "#") |
| 55 | echo '# comment' >> "$1";; |
| 56 | ">") |
| 57 | echo >> "$1";; |
Galan Rémi | 804098b | 2015-06-29 22:20:32 +0200 | [diff] [blame] | 58 | bad) |
| 59 | action="badcmd";; |
| 60 | fakesha) |
Johannes Schindelin | 5dcdd74 | 2019-07-31 08:18:48 -0700 | [diff] [blame] | 61 | test \& != "$action" || action=pick |
Galan Rémi | 804098b | 2015-06-29 22:20:32 +0200 | [diff] [blame] | 62 | echo "$action XXXXXXX False commit" >> "$1" |
| 63 | action=pick;; |
Andrew Pimlott | a495281 | 2013-07-01 09:23:38 -0700 | [diff] [blame] | 64 | *) |
Johannes Schindelin | 5dcdd74 | 2019-07-31 08:18:48 -0700 | [diff] [blame] | 65 | sed -n "${line}s/^[a-z][a-z]*/$action/p" < "$1".tmp >> "$1" |
| 66 | action=\&;; |
Andrew Pimlott | a495281 | 2013-07-01 09:23:38 -0700 | [diff] [blame] | 67 | esac |
| 68 | done |
| 69 | echo 'rebase -i script after editing:' |
| 70 | cat "$1" |
| 71 | EOF |
Johannes Schindelin | 29a0334 | 2009-01-27 23:34:29 +0100 | [diff] [blame] | 72 | |
| 73 | test_set_editor "$(pwd)/fake-editor.sh" |
Johannes Schindelin | 29a0334 | 2009-01-27 23:34:29 +0100 | [diff] [blame] | 74 | } |
Martin von Zweigbergk | 2aad7ca | 2013-06-06 23:11:37 -0700 | [diff] [blame] | 75 | |
Andrew Pimlott | 22c5b13 | 2013-06-27 12:26:31 -0700 | [diff] [blame] | 76 | # After set_cat_todo_editor, rebase -i will write the todo list (ignoring |
| 77 | # blank lines and comments) to stdout, and exit failure (so you should run |
| 78 | # it with test_must_fail). This can be used to verify the expected user |
| 79 | # experience, for todo list changes that do not affect the outcome of |
| 80 | # rebase; or as an extra check in addition to checking the outcome. |
| 81 | |
| 82 | set_cat_todo_editor () { |
| 83 | write_script fake-editor.sh <<-\EOF |
| 84 | grep "^[^#]" "$1" |
| 85 | exit 1 |
| 86 | EOF |
| 87 | test_set_editor "$(pwd)/fake-editor.sh" |
| 88 | } |
| 89 | |
Martin von Zweigbergk | 2aad7ca | 2013-06-06 23:11:37 -0700 | [diff] [blame] | 90 | # checks that the revisions in "$2" represent a linear range with the |
| 91 | # subjects in "$1" |
| 92 | test_linear_range () { |
| 93 | revlist_merges=$(git rev-list --merges "$2") && |
| 94 | test -z "$revlist_merges" && |
| 95 | expected=$1 |
| 96 | set -- $(git log --reverse --format=%s "$2") |
| 97 | test "$expected" = "$*" |
| 98 | } |
| 99 | |
| 100 | reset_rebase () { |
| 101 | test_might_fail git rebase --abort && |
| 102 | git reset --hard && |
| 103 | git clean -f |
| 104 | } |
Martin von Zweigbergk | 5b5e1c7 | 2013-06-06 23:11:38 -0700 | [diff] [blame] | 105 | |
| 106 | cherry_pick () { |
| 107 | git cherry-pick -n "$2" && |
| 108 | git commit -m "$1" && |
| 109 | git tag "$1" |
| 110 | } |
| 111 | |
| 112 | revert () { |
| 113 | git revert -n "$2" && |
| 114 | git commit -m "$1" && |
| 115 | git tag "$1" |
| 116 | } |
| 117 | |
| 118 | make_empty () { |
| 119 | git commit --allow-empty -m "$1" && |
| 120 | git tag "$1" |
| 121 | } |