Merge branch 'os/rebase-runs-post-checkout-hook'

"git rebase" internally runs "checkout" to switch between branches,
and the command used to call the post-checkout hook, but the
reimplementation stopped doing so, which is getting fixed.

* os/rebase-runs-post-checkout-hook:
  rebase: run post-checkout hook on checkout
  t5403: simplify by using a single repository
This commit is contained in:
Junio C Hamano 2019-01-29 12:47:55 -08:00
commit d94ade7f1f
2 changed files with 52 additions and 56 deletions

View File

@ -533,6 +533,7 @@ finished_rebase:
#define RESET_HEAD_DETACH (1<<0) #define RESET_HEAD_DETACH (1<<0)
#define RESET_HEAD_HARD (1<<1) #define RESET_HEAD_HARD (1<<1)
#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)
static int reset_head(struct object_id *oid, const char *action, static int reset_head(struct object_id *oid, const char *action,
const char *switch_to_branch, unsigned flags, const char *switch_to_branch, unsigned flags,
@ -540,6 +541,7 @@ static int reset_head(struct object_id *oid, const char *action,
{ {
unsigned detach_head = flags & RESET_HEAD_DETACH; unsigned detach_head = flags & RESET_HEAD_DETACH;
unsigned reset_hard = flags & RESET_HEAD_HARD; unsigned reset_hard = flags & RESET_HEAD_HARD;
unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
struct object_id head_oid; struct object_id head_oid;
struct tree_desc desc[2] = { { NULL }, { NULL } }; struct tree_desc desc[2] = { { NULL }, { NULL } };
struct lock_file lock = LOCK_INIT; struct lock_file lock = LOCK_INIT;
@ -639,6 +641,10 @@ static int reset_head(struct object_id *oid, const char *action,
ret = update_ref(reflog_head, "HEAD", oid, NULL, 0, ret = update_ref(reflog_head, "HEAD", oid, NULL, 0,
UPDATE_REFS_MSG_ON_ERR); UPDATE_REFS_MSG_ON_ERR);
} }
if (run_hook)
run_hook_le(NULL, "post-checkout",
oid_to_hex(orig ? orig : &null_oid),
oid_to_hex(oid), "1", NULL);
leave_reset_head: leave_reset_head:
strbuf_release(&msg); strbuf_release(&msg);
@ -1506,7 +1512,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
getenv(GIT_REFLOG_ACTION_ENVIRONMENT), getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
options.switch_to); options.switch_to);
if (reset_head(&oid, "checkout", if (reset_head(&oid, "checkout",
options.head_name, 0, options.head_name,
RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
NULL, buf.buf) < 0) { NULL, buf.buf) < 0) {
ret = !!error(_("could not switch to " ret = !!error(_("could not switch to "
"%s"), "%s"),
@ -1580,7 +1587,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
strbuf_addf(&msg, "%s: checkout %s", strbuf_addf(&msg, "%s: checkout %s",
getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name); getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
if (reset_head(&options.onto->object.oid, "checkout", NULL, if (reset_head(&options.onto->object.oid, "checkout", NULL,
RESET_HEAD_DETACH, NULL, msg.buf)) RESET_HEAD_DETACH | RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
NULL, msg.buf))
die(_("Could not detach HEAD")); die(_("Could not detach HEAD"));
strbuf_release(&msg); strbuf_release(&msg);

View File

@ -7,82 +7,70 @@ test_description='Test the post-checkout hook.'
. ./test-lib.sh . ./test-lib.sh
test_expect_success setup ' test_expect_success setup '
echo Data for commit0. >a && mkdir -p .git/hooks &&
echo Data for commit0. >b && write_script .git/hooks/post-checkout <<-\EOF &&
git update-index --add a && echo "$@" >.git/post-checkout.args
git update-index --add b && EOF
tree0=$(git write-tree) && test_commit one &&
commit0=$(echo setup | git commit-tree $tree0) && test_commit two &&
git update-ref refs/heads/master $commit0 && test_commit rebase-on-me &&
git clone ./. clone1 && git reset --hard HEAD^ &&
git clone ./. clone2 && test_commit three
GIT_DIR=clone2/.git git branch new2 &&
echo Data for commit1. >clone2/b &&
GIT_DIR=clone2/.git git add clone2/b &&
GIT_DIR=clone2/.git git commit -m new2
'
for clone in 1 2; do
cat >clone${clone}/.git/hooks/post-checkout <<'EOF'
#!/bin/sh
echo $@ > $GIT_DIR/post-checkout.args
EOF
chmod u+x clone${clone}/.git/hooks/post-checkout
done
test_expect_success 'post-checkout runs as expected ' '
GIT_DIR=clone1/.git git checkout master &&
test -e clone1/.git/post-checkout.args
' '
test_expect_success 'post-checkout receives the right arguments with HEAD unchanged ' ' test_expect_success 'post-checkout receives the right arguments with HEAD unchanged ' '
old=$(awk "{print \$1}" clone1/.git/post-checkout.args) && test_when_finished "rm -f .git/post-checkout.args" &&
new=$(awk "{print \$2}" clone1/.git/post-checkout.args) && git checkout master &&
flag=$(awk "{print \$3}" clone1/.git/post-checkout.args) && read old new flag <.git/post-checkout.args &&
test $old = $new && test $flag = 1 test $old = $new && test $flag = 1
' '
test_expect_success 'post-checkout runs as expected ' '
GIT_DIR=clone1/.git git checkout master &&
test -e clone1/.git/post-checkout.args
'
test_expect_success 'post-checkout args are correct with git checkout -b ' ' test_expect_success 'post-checkout args are correct with git checkout -b ' '
GIT_DIR=clone1/.git git checkout -b new1 && test_when_finished "rm -f .git/post-checkout.args" &&
old=$(awk "{print \$1}" clone1/.git/post-checkout.args) && git checkout -b new1 &&
new=$(awk "{print \$2}" clone1/.git/post-checkout.args) && read old new flag <.git/post-checkout.args &&
flag=$(awk "{print \$3}" clone1/.git/post-checkout.args) &&
test $old = $new && test $flag = 1 test $old = $new && test $flag = 1
' '
test_expect_success 'post-checkout receives the right args with HEAD changed ' ' test_expect_success 'post-checkout receives the right args with HEAD changed ' '
GIT_DIR=clone2/.git git checkout new2 && test_when_finished "rm -f .git/post-checkout.args" &&
old=$(awk "{print \$1}" clone2/.git/post-checkout.args) && git checkout two &&
new=$(awk "{print \$2}" clone2/.git/post-checkout.args) && read old new flag <.git/post-checkout.args &&
flag=$(awk "{print \$3}" clone2/.git/post-checkout.args) &&
test $old != $new && test $flag = 1 test $old != $new && test $flag = 1
' '
test_expect_success 'post-checkout receives the right args when not switching branches ' ' test_expect_success 'post-checkout receives the right args when not switching branches ' '
GIT_DIR=clone2/.git git checkout master b && test_when_finished "rm -f .git/post-checkout.args" &&
old=$(awk "{print \$1}" clone2/.git/post-checkout.args) && git checkout master -- three.t &&
new=$(awk "{print \$2}" clone2/.git/post-checkout.args) && read old new flag <.git/post-checkout.args &&
flag=$(awk "{print \$3}" clone2/.git/post-checkout.args) &&
test $old = $new && test $flag = 0 test $old = $new && test $flag = 0
' '
if test "$(git config --bool core.filemode)" = true; then test_expect_success 'post-checkout is triggered on rebase' '
mkdir -p templates/hooks test_when_finished "rm -f .git/post-checkout.args" &&
cat >templates/hooks/post-checkout <<'EOF' git checkout -b rebase-test master &&
#!/bin/sh rm -f .git/post-checkout.args &&
echo $@ > $GIT_DIR/post-checkout.args git rebase rebase-on-me &&
EOF read old new flag <.git/post-checkout.args &&
chmod +x templates/hooks/post-checkout test $old != $new && test $flag = 1
'
test_expect_success 'post-checkout is triggered on rebase with fast-forward' '
test_when_finished "rm -f .git/post-checkout.args" &&
git checkout -b ff-rebase-test rebase-on-me^ &&
rm -f .git/post-checkout.args &&
git rebase rebase-on-me &&
read old new flag <.git/post-checkout.args &&
test $old != $new && test $flag = 1
'
test_expect_success 'post-checkout hook is triggered by clone' ' test_expect_success 'post-checkout hook is triggered by clone' '
mkdir -p templates/hooks &&
write_script templates/hooks/post-checkout <<-\EOF &&
echo "$@" >$GIT_DIR/post-checkout.args
EOF
git clone --template=templates . clone3 && git clone --template=templates . clone3 &&
test -f clone3/.git/post-checkout.args test -f clone3/.git/post-checkout.args
' '
fi
test_done test_done