rebase: run post-checkout hook on checkout
authorOrgad Shaneh <orgads@gmail.com>
Sat, 29 Dec 2018 21:37:59 +0000 (23:37 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 2 Jan 2019 21:18:33 +0000 (13:18 -0800)
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 <orgads@gmail.com>
Acked-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rebase.c
t/t5403-post-checkout-hook.sh
index b5c99ec10c200a5fccb293c0acadae80642f2808..8402765a797ba3bbfb3ab8ca57fb3d281acf4852 100644 (file)
@@ -530,6 +530,7 @@ static int run_specific_rebase(struct rebase_options *opts)
 
 #define RESET_HEAD_DETACH (1<<0)
 #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,
                      const char *switch_to_branch, unsigned flags,
@@ -537,6 +538,7 @@ static int reset_head(struct object_id *oid, const char *action,
 {
        unsigned detach_head = flags & RESET_HEAD_DETACH;
        unsigned reset_hard = flags & RESET_HEAD_HARD;
+       unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
        struct object_id head_oid;
        struct tree_desc desc[2] = { { NULL }, { NULL } };
        struct lock_file lock = LOCK_INIT;
@@ -636,6 +638,10 @@ static int reset_head(struct object_id *oid, const char *action,
                        ret = update_ref(reflog_head, "HEAD", oid, NULL, 0,
                                         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:
        strbuf_release(&msg);
@@ -1465,7 +1471,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
                                            getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
                                            options.switch_to);
                                if (reset_head(&oid, "checkout",
-                                              options.head_name, 0,
+                                              options.head_name,
+                                              RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
                                               NULL, buf.buf) < 0) {
                                        ret = !!error(_("could not switch to "
                                                        "%s"),
@@ -1539,7 +1546,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
        strbuf_addf(&msg, "%s: checkout %s",
                    getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
        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"));
        strbuf_release(&msg);
 
index 1d15a1031f9ee2290217a1c42db02ac4e16a9f1c..a539ffc0803a746c70d458a95f27144c9039afff 100755 (executable)
@@ -13,6 +13,8 @@ test_expect_success setup '
        EOF
        test_commit one &&
        test_commit two &&
+       test_commit rebase-on-me &&
+       git reset --hard HEAD^ &&
        test_commit three
 '
 
@@ -44,6 +46,24 @@ test_expect_success 'post-checkout receives the right args when not switching br
        test $old = $new && test $flag = 0
 '
 
+test_expect_success 'post-checkout is triggered on rebase' '
+       test_when_finished "rm -f .git/post-checkout.args" &&
+       git checkout -b rebase-test master &&
+       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 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' '
        mkdir -p templates/hooks &&
        write_script templates/hooks/post-checkout <<-\EOF &&