Josh England | 1abbe47 | 2007-09-26 15:31:01 -0600 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2006 Josh England |
| 4 | # |
| 5 | |
| 6 | test_description='Test the post-checkout hook.' |
Johannes Schindelin | 966b4be | 2020-11-18 23:44:29 +0000 | [diff] [blame] | 7 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
Johannes Schindelin | 334afbc | 2020-11-18 23:44:19 +0000 | [diff] [blame] | 8 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 9 | |
Josh England | 1abbe47 | 2007-09-26 15:31:01 -0600 | [diff] [blame] | 10 | . ./test-lib.sh |
| 11 | |
| 12 | test_expect_success setup ' |
Orgad Shaneh | 10499a9 | 2018-12-29 23:37:58 +0200 | [diff] [blame] | 13 | mkdir -p .git/hooks && |
| 14 | write_script .git/hooks/post-checkout <<-\EOF && |
| 15 | echo "$@" >.git/post-checkout.args |
| 16 | EOF |
| 17 | test_commit one && |
| 18 | test_commit two && |
Orgad Shaneh | 8581df6 | 2018-12-29 23:37:59 +0200 | [diff] [blame] | 19 | test_commit rebase-on-me && |
| 20 | git reset --hard HEAD^ && |
Orgad Shaneh | 10499a9 | 2018-12-29 23:37:58 +0200 | [diff] [blame] | 21 | test_commit three |
Josh England | 1abbe47 | 2007-09-26 15:31:01 -0600 | [diff] [blame] | 22 | ' |
| 23 | |
| 24 | test_expect_success 'post-checkout receives the right arguments with HEAD unchanged ' ' |
Orgad Shaneh | 10499a9 | 2018-12-29 23:37:58 +0200 | [diff] [blame] | 25 | test_when_finished "rm -f .git/post-checkout.args" && |
Johannes Schindelin | 966b4be | 2020-11-18 23:44:29 +0000 | [diff] [blame] | 26 | git checkout main && |
Orgad Shaneh | 10499a9 | 2018-12-29 23:37:58 +0200 | [diff] [blame] | 27 | read old new flag <.git/post-checkout.args && |
Elia Pinto | 7281f36 | 2014-06-06 07:55:59 -0700 | [diff] [blame] | 28 | test $old = $new && test $flag = 1 |
Josh England | 1abbe47 | 2007-09-26 15:31:01 -0600 | [diff] [blame] | 29 | ' |
| 30 | |
Josh England | 1abbe47 | 2007-09-26 15:31:01 -0600 | [diff] [blame] | 31 | test_expect_success 'post-checkout args are correct with git checkout -b ' ' |
Orgad Shaneh | 10499a9 | 2018-12-29 23:37:58 +0200 | [diff] [blame] | 32 | test_when_finished "rm -f .git/post-checkout.args" && |
| 33 | git checkout -b new1 && |
| 34 | read old new flag <.git/post-checkout.args && |
Elia Pinto | 7281f36 | 2014-06-06 07:55:59 -0700 | [diff] [blame] | 35 | test $old = $new && test $flag = 1 |
Josh England | 1abbe47 | 2007-09-26 15:31:01 -0600 | [diff] [blame] | 36 | ' |
| 37 | |
| 38 | test_expect_success 'post-checkout receives the right args with HEAD changed ' ' |
Orgad Shaneh | 10499a9 | 2018-12-29 23:37:58 +0200 | [diff] [blame] | 39 | test_when_finished "rm -f .git/post-checkout.args" && |
| 40 | git checkout two && |
| 41 | read old new flag <.git/post-checkout.args && |
Elia Pinto | 7281f36 | 2014-06-06 07:55:59 -0700 | [diff] [blame] | 42 | test $old != $new && test $flag = 1 |
Josh England | 1abbe47 | 2007-09-26 15:31:01 -0600 | [diff] [blame] | 43 | ' |
| 44 | |
| 45 | test_expect_success 'post-checkout receives the right args when not switching branches ' ' |
Orgad Shaneh | 10499a9 | 2018-12-29 23:37:58 +0200 | [diff] [blame] | 46 | test_when_finished "rm -f .git/post-checkout.args" && |
Johannes Schindelin | 966b4be | 2020-11-18 23:44:29 +0000 | [diff] [blame] | 47 | git checkout main -- three.t && |
Orgad Shaneh | 10499a9 | 2018-12-29 23:37:58 +0200 | [diff] [blame] | 48 | read old new flag <.git/post-checkout.args && |
Elia Pinto | 7281f36 | 2014-06-06 07:55:59 -0700 | [diff] [blame] | 49 | test $old = $new && test $flag = 0 |
Josh England | 1abbe47 | 2007-09-26 15:31:01 -0600 | [diff] [blame] | 50 | ' |
| 51 | |
Phillip Wood | bd55eee | 2022-01-26 13:05:37 +0000 | [diff] [blame] | 52 | test_rebase () { |
| 53 | args="$*" && |
| 54 | test_expect_success "post-checkout is triggered on rebase $args" ' |
| 55 | test_when_finished "rm -f .git/post-checkout.args" && |
| 56 | git checkout -B rebase-test main && |
| 57 | rm -f .git/post-checkout.args && |
| 58 | git rebase $args rebase-on-me && |
| 59 | read old new flag <.git/post-checkout.args && |
| 60 | test_cmp_rev main $old && |
| 61 | test_cmp_rev rebase-on-me $new && |
| 62 | test $flag = 1 |
| 63 | ' |
Orgad Shaneh | 8581df6 | 2018-12-29 23:37:59 +0200 | [diff] [blame] | 64 | |
Phillip Wood | bd55eee | 2022-01-26 13:05:37 +0000 | [diff] [blame] | 65 | test_expect_success "post-checkout is triggered on rebase $args with fast-forward" ' |
| 66 | test_when_finished "rm -f .git/post-checkout.args" && |
| 67 | git checkout -B ff-rebase-test rebase-on-me^ && |
| 68 | rm -f .git/post-checkout.args && |
| 69 | git rebase $args rebase-on-me && |
| 70 | read old new flag <.git/post-checkout.args && |
| 71 | test_cmp_rev rebase-on-me^ $old && |
| 72 | test_cmp_rev rebase-on-me $new && |
| 73 | test $flag = 1 |
| 74 | ' |
Phillip Wood | 69f4c23 | 2022-01-26 13:05:38 +0000 | [diff] [blame] | 75 | |
| 76 | test_expect_success "rebase $args fast-forward branch checkout runs post-checkout hook" ' |
| 77 | test_when_finished "test_might_fail git rebase --abort" && |
| 78 | test_when_finished "rm -f .git/post-checkout.args" && |
| 79 | git update-ref refs/heads/rebase-fast-forward three && |
| 80 | git checkout two && |
| 81 | rm -f .git/post-checkout.args && |
| 82 | git rebase $args HEAD rebase-fast-forward && |
| 83 | read old new flag <.git/post-checkout.args && |
| 84 | test_cmp_rev two $old && |
| 85 | test_cmp_rev three $new && |
| 86 | test $flag = 1 |
| 87 | ' |
Phillip Wood | ab2fba0 | 2022-01-26 13:05:39 +0000 | [diff] [blame] | 88 | |
| 89 | test_expect_success "rebase $args checkout does not remove untracked files" ' |
| 90 | test_when_finished "test_might_fail git rebase --abort" && |
Phillip Wood | 4840002 | 2022-01-26 13:05:40 +0000 | [diff] [blame] | 91 | test_when_finished "rm -f .git/post-checkout.args" && |
Phillip Wood | ab2fba0 | 2022-01-26 13:05:39 +0000 | [diff] [blame] | 92 | git update-ref refs/heads/rebase-fast-forward three && |
| 93 | git checkout two && |
Phillip Wood | 4840002 | 2022-01-26 13:05:40 +0000 | [diff] [blame] | 94 | rm -f .git/post-checkout.args && |
Phillip Wood | ab2fba0 | 2022-01-26 13:05:39 +0000 | [diff] [blame] | 95 | echo untracked >three.t && |
| 96 | test_when_finished "rm three.t" && |
| 97 | test_must_fail git rebase $args HEAD rebase-fast-forward 2>err && |
Phillip Wood | 4840002 | 2022-01-26 13:05:40 +0000 | [diff] [blame] | 98 | grep "untracked working tree files would be overwritten by checkout" err && |
| 99 | test_path_is_missing .git/post-checkout.args |
| 100 | |
Phillip Wood | ab2fba0 | 2022-01-26 13:05:39 +0000 | [diff] [blame] | 101 | ' |
Phillip Wood | bd55eee | 2022-01-26 13:05:37 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | test_rebase --apply && |
| 105 | test_rebase --merge |
Orgad Shaneh | 8581df6 | 2018-12-29 23:37:59 +0200 | [diff] [blame] | 106 | |
Jeff King | dfa7a6c | 2009-03-03 00:37:51 -0500 | [diff] [blame] | 107 | test_expect_success 'post-checkout hook is triggered by clone' ' |
Orgad Shaneh | 10499a9 | 2018-12-29 23:37:58 +0200 | [diff] [blame] | 108 | mkdir -p templates/hooks && |
| 109 | write_script templates/hooks/post-checkout <<-\EOF && |
Randall S. Becker | a250d41 | 2019-02-08 06:32:33 -0500 | [diff] [blame] | 110 | echo "$@" >"$GIT_DIR/post-checkout.args" |
Orgad Shaneh | 10499a9 | 2018-12-29 23:37:58 +0200 | [diff] [blame] | 111 | EOF |
Jeff King | dfa7a6c | 2009-03-03 00:37:51 -0500 | [diff] [blame] | 112 | git clone --template=templates . clone3 && |
| 113 | test -f clone3/.git/post-checkout.args |
| 114 | ' |
| 115 | |
Josh England | 1abbe47 | 2007-09-26 15:31:01 -0600 | [diff] [blame] | 116 | test_done |