Skip to content

Commit 7b95654

Browse files
authored
Merge pull request #1992 from orgads/rebase-post-checkout-hook
Rebase: Run post-checkout hook on checkout
2 parents 7c9fbc0 + 93f5484 commit 7b95654

File tree

2 files changed

+43
-38
lines changed

2 files changed

+43
-38
lines changed

builtin/rebase.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ static void add_var(struct strbuf *buf, const char *name, const char *value)
369369
#define RESET_HEAD_DETACH (1<<0)
370370
#define RESET_HEAD_HARD (1<<1)
371371
#define RESET_HEAD_REFS_ONLY (1<<2)
372+
#define RESET_HEAD_RUN_HOOK (1<<3)
372373

373374
static int reset_head(struct object_id *oid, const char *action,
374375
const char *switch_to_branch, unsigned flags,
@@ -377,6 +378,7 @@ static int reset_head(struct object_id *oid, const char *action,
377378
unsigned detach_head = flags & RESET_HEAD_DETACH;
378379
unsigned reset_hard = flags & RESET_HEAD_HARD;
379380
unsigned refs_only = flags & RESET_HEAD_REFS_ONLY;
381+
unsigned run_hook = flags & RESET_HEAD_RUN_HOOK;
380382
struct object_id head_oid;
381383
struct tree_desc desc[2] = { { NULL }, { NULL } };
382384
struct lock_file lock = LOCK_INIT;
@@ -481,6 +483,10 @@ static int reset_head(struct object_id *oid, const char *action,
481483
ret = create_symref("HEAD", switch_to_branch,
482484
reflog_head);
483485
}
486+
if (run_hook)
487+
run_hook_le(NULL, "post-checkout",
488+
oid_to_hex(orig ? orig : &null_oid),
489+
oid_to_hex(oid), "1", NULL);
484490

485491
leave_reset_head:
486492
strbuf_release(&msg);
@@ -1729,7 +1735,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
17291735
strbuf_addf(&msg, "%s: checkout %s",
17301736
getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
17311737
if (reset_head(&options.onto->object.oid, "checkout", NULL,
1732-
RESET_HEAD_DETACH, NULL, msg.buf))
1738+
RESET_HEAD_DETACH | RESET_HEAD_RUN_HOOK, NULL, msg.buf))
17331739
die(_("Could not detach HEAD"));
17341740
strbuf_release(&msg);
17351741

t/t5403-post-checkout-hook.sh

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,70 +7,69 @@ 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
10+
test_commit one &&
11+
test_commit two &&
12+
test_commit rebase-on-me &&
13+
git reset --hard HEAD^ &&
14+
test_commit three three &&
15+
mv .git/hooks-disabled .git/hooks
2316
'
2417

25-
for clone in 1 2; do
26-
cat >clone${clone}/.git/hooks/post-checkout <<'EOF'
18+
cat >.git/hooks/post-checkout <<'EOF'
2719
#!/bin/sh
28-
echo $@ > $GIT_DIR/post-checkout.args
20+
echo $@ > .git/post-checkout.args
2921
EOF
30-
chmod u+x clone${clone}/.git/hooks/post-checkout
31-
done
22+
chmod u+x .git/hooks/post-checkout
3223

3324
test_expect_success 'post-checkout runs as expected ' '
34-
GIT_DIR=clone1/.git git checkout master &&
35-
test -e clone1/.git/post-checkout.args
25+
git checkout master &&
26+
test -e .git/post-checkout.args
3627
'
3728

3829
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) &&
30+
read old new flag < .git/post-checkout.args &&
4231
test $old = $new && test $flag = 1
4332
'
4433

4534
test_expect_success 'post-checkout runs as expected ' '
46-
GIT_DIR=clone1/.git git checkout master &&
47-
test -e clone1/.git/post-checkout.args
35+
git checkout master &&
36+
test -e .git/post-checkout.args
4837
'
4938

5039
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) &&
40+
git checkout -b new1 &&
41+
read old new flag < .git/post-checkout.args &&
5542
test $old = $new && test $flag = 1
5643
'
5744

5845
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) &&
46+
git checkout two &&
47+
read old new flag < .git/post-checkout.args &&
6348
test $old != $new && test $flag = 1
6449
'
6550

6651
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) &&
52+
git checkout master -- three &&
53+
read old new flag < .git/post-checkout.args &&
7154
test $old = $new && test $flag = 0
7255
'
7356

57+
test_expect_success 'post-checkout is triggered on rebase' '
58+
git checkout -b rebase-test master &&
59+
rm -f .git/post-checkout.args &&
60+
git rebase rebase-on-me &&
61+
read old new flag < .git/post-checkout.args &&
62+
test $old != $new && test $flag = 1
63+
'
64+
65+
test_expect_success 'post-checkout is triggered on rebase with fast-forward' '
66+
git checkout -b ff-rebase-test rebase-on-me^ &&
67+
rm -f .git/post-checkout.args &&
68+
git rebase rebase-on-me &&
69+
read old new flag < .git/post-checkout.args &&
70+
test $old != $new && test $flag = 1
71+
'
72+
7473
if test "$(git config --bool core.filemode)" = true; then
7574
mkdir -p templates/hooks
7675
cat >templates/hooks/post-checkout <<'EOF'

0 commit comments

Comments
 (0)