Skip to content

Commit 760e71b

Browse files
committed
Merge pull request #1992 from orgads/rebase-post-checkout-hook
Rebase: Run post-checkout hook on checkout
2 parents 4894819 + 9a093f4 commit 760e71b

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
@@ -530,13 +530,15 @@ static int run_specific_rebase(struct rebase_options *opts)
530530

531531
#define RESET_HEAD_DETACH (1<<0)
532532
#define RESET_HEAD_HARD (1<<1)
533+
#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)
533534

534535
static int reset_head(struct object_id *oid, const char *action,
535536
const char *switch_to_branch, unsigned flags,
536537
const char *reflog_orig_head, const char *reflog_head)
537538
{
538539
unsigned detach_head = flags & RESET_HEAD_DETACH;
539540
unsigned reset_hard = flags & RESET_HEAD_HARD;
541+
unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
540542
struct object_id head_oid;
541543
struct tree_desc desc[2] = { { NULL }, { NULL } };
542544
struct lock_file lock = LOCK_INIT;
@@ -636,6 +638,10 @@ static int reset_head(struct object_id *oid, const char *action,
636638
ret = update_ref(reflog_head, "HEAD", oid, NULL, 0,
637639
UPDATE_REFS_MSG_ON_ERR);
638640
}
641+
if (run_hook)
642+
run_hook_le(NULL, "post-checkout",
643+
oid_to_hex(orig ? orig : &null_oid),
644+
oid_to_hex(oid), "1", NULL);
639645

640646
leave_reset_head:
641647
strbuf_release(&msg);
@@ -1465,7 +1471,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
14651471
getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
14661472
options.switch_to);
14671473
if (reset_head(&oid, "checkout",
1468-
options.head_name, 0,
1474+
options.head_name,
1475+
RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
14691476
NULL, buf.buf) < 0) {
14701477
ret = !!error(_("could not switch to "
14711478
"%s"),
@@ -1539,7 +1546,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
15391546
strbuf_addf(&msg, "%s: checkout %s",
15401547
getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
15411548
if (reset_head(&options.onto->object.oid, "checkout", NULL,
1542-
RESET_HEAD_DETACH, NULL, msg.buf))
1549+
RESET_HEAD_DETACH | RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
1550+
NULL, msg.buf))
15431551
die(_("Could not detach HEAD"));
15441552
strbuf_release(&msg);
15451553

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)