Skip to content

Commit 8581df6

Browse files
orgadsgitster
authored andcommitted
rebase: run post-checkout hook on checkout
The scripted version of rebase used to run this hook on the initial checkout. The transition to built-in introduced a regression. Signed-off-by: Orgad Shaneh <[email protected]> Acked-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 10499a9 commit 8581df6

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ test_expect_success setup '
1313
EOF
1414
test_commit one &&
1515
test_commit two &&
16+
test_commit rebase-on-me &&
17+
git reset --hard HEAD^ &&
1618
test_commit three
1719
'
1820

@@ -44,6 +46,24 @@ test_expect_success 'post-checkout receives the right args when not switching br
4446
test $old = $new && test $flag = 0
4547
'
4648

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+
'
66+
4767
test_expect_success 'post-checkout hook is triggered by clone' '
4868
mkdir -p templates/hooks &&
4969
write_script templates/hooks/post-checkout <<-\EOF &&

0 commit comments

Comments
 (0)