Skip to content

Commit d94ade7

Browse files
committed
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
2 parents 33e4ae9 + 8581df6 commit d94ade7

File tree

2 files changed

+52
-56
lines changed

2 files changed

+52
-56
lines changed

builtin/rebase.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,13 +533,15 @@ static int run_specific_rebase(struct rebase_options *opts)
533533

534534
#define RESET_HEAD_DETACH (1<<0)
535535
#define RESET_HEAD_HARD (1<<1)
536+
#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)
536537

537538
static int reset_head(struct object_id *oid, const char *action,
538539
const char *switch_to_branch, unsigned flags,
539540
const char *reflog_orig_head, const char *reflog_head)
540541
{
541542
unsigned detach_head = flags & RESET_HEAD_DETACH;
542543
unsigned reset_hard = flags & RESET_HEAD_HARD;
544+
unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
543545
struct object_id head_oid;
544546
struct tree_desc desc[2] = { { NULL }, { NULL } };
545547
struct lock_file lock = LOCK_INIT;
@@ -639,6 +641,10 @@ static int reset_head(struct object_id *oid, const char *action,
639641
ret = update_ref(reflog_head, "HEAD", oid, NULL, 0,
640642
UPDATE_REFS_MSG_ON_ERR);
641643
}
644+
if (run_hook)
645+
run_hook_le(NULL, "post-checkout",
646+
oid_to_hex(orig ? orig : &null_oid),
647+
oid_to_hex(oid), "1", NULL);
642648

643649
leave_reset_head:
644650
strbuf_release(&msg);
@@ -1506,7 +1512,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
15061512
getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
15071513
options.switch_to);
15081514
if (reset_head(&oid, "checkout",
1509-
options.head_name, 0,
1515+
options.head_name,
1516+
RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
15101517
NULL, buf.buf) < 0) {
15111518
ret = !!error(_("could not switch to "
15121519
"%s"),
@@ -1580,7 +1587,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
15801587
strbuf_addf(&msg, "%s: checkout %s",
15811588
getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
15821589
if (reset_head(&options.onto->object.oid, "checkout", NULL,
1583-
RESET_HEAD_DETACH, NULL, msg.buf))
1590+
RESET_HEAD_DETACH | RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
1591+
NULL, msg.buf))
15841592
die(_("Could not detach HEAD"));
15851593
strbuf_release(&msg);
15861594

t/t5403-post-checkout-hook.sh

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,82 +7,70 @@ test_description='Test the post-checkout hook.'
77
. ./test-lib.sh
88

99
test_expect_success setup '
10-
echo Data for commit0. >a &&
11-
echo Data for commit0. >b &&
12-
git update-index --add a &&
13-
git update-index --add b &&
14-
tree0=$(git write-tree) &&
15-
commit0=$(echo setup | git commit-tree $tree0) &&
16-
git update-ref refs/heads/master $commit0 &&
17-
git clone ./. clone1 &&
18-
git clone ./. clone2 &&
19-
GIT_DIR=clone2/.git git branch new2 &&
20-
echo Data for commit1. >clone2/b &&
21-
GIT_DIR=clone2/.git git add clone2/b &&
22-
GIT_DIR=clone2/.git git commit -m new2
23-
'
24-
25-
for clone in 1 2; do
26-
cat >clone${clone}/.git/hooks/post-checkout <<'EOF'
27-
#!/bin/sh
28-
echo $@ > $GIT_DIR/post-checkout.args
29-
EOF
30-
chmod u+x clone${clone}/.git/hooks/post-checkout
31-
done
32-
33-
test_expect_success 'post-checkout runs as expected ' '
34-
GIT_DIR=clone1/.git git checkout master &&
35-
test -e clone1/.git/post-checkout.args
10+
mkdir -p .git/hooks &&
11+
write_script .git/hooks/post-checkout <<-\EOF &&
12+
echo "$@" >.git/post-checkout.args
13+
EOF
14+
test_commit one &&
15+
test_commit two &&
16+
test_commit rebase-on-me &&
17+
git reset --hard HEAD^ &&
18+
test_commit three
3619
'
3720

3821
test_expect_success 'post-checkout receives the right arguments with HEAD unchanged ' '
39-
old=$(awk "{print \$1}" clone1/.git/post-checkout.args) &&
40-
new=$(awk "{print \$2}" clone1/.git/post-checkout.args) &&
41-
flag=$(awk "{print \$3}" clone1/.git/post-checkout.args) &&
22+
test_when_finished "rm -f .git/post-checkout.args" &&
23+
git checkout master &&
24+
read old new flag <.git/post-checkout.args &&
4225
test $old = $new && test $flag = 1
4326
'
4427

45-
test_expect_success 'post-checkout runs as expected ' '
46-
GIT_DIR=clone1/.git git checkout master &&
47-
test -e clone1/.git/post-checkout.args
48-
'
49-
5028
test_expect_success 'post-checkout args are correct with git checkout -b ' '
51-
GIT_DIR=clone1/.git git checkout -b new1 &&
52-
old=$(awk "{print \$1}" clone1/.git/post-checkout.args) &&
53-
new=$(awk "{print \$2}" clone1/.git/post-checkout.args) &&
54-
flag=$(awk "{print \$3}" clone1/.git/post-checkout.args) &&
29+
test_when_finished "rm -f .git/post-checkout.args" &&
30+
git checkout -b new1 &&
31+
read old new flag <.git/post-checkout.args &&
5532
test $old = $new && test $flag = 1
5633
'
5734

5835
test_expect_success 'post-checkout receives the right args with HEAD changed ' '
59-
GIT_DIR=clone2/.git git checkout new2 &&
60-
old=$(awk "{print \$1}" clone2/.git/post-checkout.args) &&
61-
new=$(awk "{print \$2}" clone2/.git/post-checkout.args) &&
62-
flag=$(awk "{print \$3}" clone2/.git/post-checkout.args) &&
36+
test_when_finished "rm -f .git/post-checkout.args" &&
37+
git checkout two &&
38+
read old new flag <.git/post-checkout.args &&
6339
test $old != $new && test $flag = 1
6440
'
6541

6642
test_expect_success 'post-checkout receives the right args when not switching branches ' '
67-
GIT_DIR=clone2/.git git checkout master b &&
68-
old=$(awk "{print \$1}" clone2/.git/post-checkout.args) &&
69-
new=$(awk "{print \$2}" clone2/.git/post-checkout.args) &&
70-
flag=$(awk "{print \$3}" clone2/.git/post-checkout.args) &&
43+
test_when_finished "rm -f .git/post-checkout.args" &&
44+
git checkout master -- three.t &&
45+
read old new flag <.git/post-checkout.args &&
7146
test $old = $new && test $flag = 0
7247
'
7348

74-
if test "$(git config --bool core.filemode)" = true; then
75-
mkdir -p templates/hooks
76-
cat >templates/hooks/post-checkout <<'EOF'
77-
#!/bin/sh
78-
echo $@ > $GIT_DIR/post-checkout.args
79-
EOF
80-
chmod +x templates/hooks/post-checkout
49+
test_expect_success 'post-checkout is triggered on rebase' '
50+
test_when_finished "rm -f .git/post-checkout.args" &&
51+
git checkout -b rebase-test master &&
52+
rm -f .git/post-checkout.args &&
53+
git rebase rebase-on-me &&
54+
read old new flag <.git/post-checkout.args &&
55+
test $old != $new && test $flag = 1
56+
'
57+
58+
test_expect_success 'post-checkout is triggered on rebase with fast-forward' '
59+
test_when_finished "rm -f .git/post-checkout.args" &&
60+
git checkout -b ff-rebase-test rebase-on-me^ &&
61+
rm -f .git/post-checkout.args &&
62+
git rebase rebase-on-me &&
63+
read old new flag <.git/post-checkout.args &&
64+
test $old != $new && test $flag = 1
65+
'
8166

8267
test_expect_success 'post-checkout hook is triggered by clone' '
68+
mkdir -p templates/hooks &&
69+
write_script templates/hooks/post-checkout <<-\EOF &&
70+
echo "$@" >$GIT_DIR/post-checkout.args
71+
EOF
8372
git clone --template=templates . clone3 &&
8473
test -f clone3/.git/post-checkout.args
8574
'
86-
fi
8775

8876
test_done

0 commit comments

Comments
 (0)