diff mbox series

[2/2] Rebase: Run post-checkout hook on checkout

Message ID 20181224212425.16596-3-orgads@gmail.com (mailing list archive)
State New, archived
Headers show
Series Rebase: Run post-checkout hook on checkout | expand

Commit Message

Orgad Shaneh Dec. 24, 2018, 9:24 p.m. UTC
From: Orgad Shaneh <orgads@gmail.com>

Signed-off-by: Orgad Shaneh <orgads@gmail.com>
---
 builtin/rebase.c              | 11 +++++++++--
 t/t5403-post-checkout-hook.sh | 20 ++++++++++++++++++++
 2 files changed, 29 insertions(+), 2 deletions(-)

Comments

Junio C Hamano Dec. 28, 2018, 10:53 p.m. UTC | #1
orgads@gmail.com writes:

> From: Orgad Shaneh <orgads@gmail.com>

> Re: [PATCH 2/2] Rebase: Run post-checkout hook on checkout

There is no explanation here?  

Is this a regression fix (i.e. scripted version of "rebase" used to
run the hook)?  Or a new feature (i.e. no earlier version of
"rebase" run the hook but you think it ought to run it)?

> Signed-off-by: Orgad Shaneh <orgads@gmail.com>
> ---
>  builtin/rebase.c              | 11 +++++++++--
>  t/t5403-post-checkout-hook.sh | 20 ++++++++++++++++++++
>  2 files changed, 29 insertions(+), 2 deletions(-)
>
> diff --git a/builtin/rebase.c b/builtin/rebase.c
> index b5c99ec10c..7f7a2c738e 100644
> --- a/builtin/rebase.c
> +++ b/builtin/rebase.c
> @@ -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_HOOK (1<<2)

Would it be plausible that the only possible hook that can be run by
reset_head() function will always be post-checkout and nothing else,
I wonder?  Shouldn't this bit be called *_RUN_POST_CHECKOUT to make
sure it is specific enough?

> @@ -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_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);
>  

This function is never about checking out paths from tree-ish/index
and is always about checking out a branch, so hardcoded "1" is
justified here.  Good.

> @@ -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_HOOK,
>  					       NULL, buf.buf) < 0) {
>  					ret = !!error(_("could not switch to "
>  							"%s"),
> @@ -1539,7 +1546,7 @@ 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_HOOK, NULL, msg.buf))
>  		die(_("Could not detach HEAD"));
>  	strbuf_release(&msg);

So... among 6 callers of reset_head(), these two whose *action is
"checkout" gets the RUN_HOOK bit.  Makes sense.

> diff --git a/t/t5403-post-checkout-hook.sh b/t/t5403-post-checkout-hook.sh
> index 9f9a5163c5..5b4e582caa 100755
> --- a/t/t5403-post-checkout-hook.sh
> +++ b/t/t5403-post-checkout-hook.sh
> @@ -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 three
>  '
>  
> @@ -51,6 +53,24 @@ test_expect_success 'post-checkout receives the right args when not switching br
>  	rm -f .git/post-checkout.args
>  '
>  
> +test_expect_success 'post-checkout is triggered on rebase' '
> +	git checkout -b rebase-test master &&
> +	rm -f .git/post-checkout.args &&

Read the title of this whole test script file; it should verify what
is in the file before removing it.

> +	git rebase rebase-on-me &&
> +	read old new flag < .git/post-checkout.args &&

No SP between "<" and ".git/post-checkout.args".

> +	test $old != $new && test $flag = 1 &&
> +	rm -f .git/post-checkout.args
> +'

Regarding the clean-up of this file, see my review on the previous
one.


> +test_expect_success 'post-checkout is triggered on rebase with fast-forward' '

The same comment as above applies to this.


Thanks.
Orgad Shaneh Dec. 29, 2018, 9:31 p.m. UTC | #2
On Sat, Dec 29, 2018 at 12:53 AM Junio C Hamano <gitster@pobox.com> wrote:
>
> orgads@gmail.com writes:
>
> > From: Orgad Shaneh <orgads@gmail.com>
>
> > Re: [PATCH 2/2] Rebase: Run post-checkout hook on checkout
>
> There is no explanation here?
>
> Is this a regression fix (i.e. scripted version of "rebase" used to
> run the hook)?  Or a new feature (i.e. no earlier version of
> "rebase" run the hook but you think it ought to run it)?

Added an explanation.

[snip]
>
> > +#define RESET_HEAD_RUN_HOOK (1<<2)
>
> Would it be plausible that the only possible hook that can be run by
> reset_head() function will always be post-checkout and nothing else,
> I wonder?  Shouldn't this bit be called *_RUN_POST_CHECKOUT to make
> sure it is specific enough?

Done

[snip]
>
> > +test_expect_success 'post-checkout is triggered on rebase' '
> > +     git checkout -b rebase-test master &&
> > +     rm -f .git/post-checkout.args &&
>
> Read the title of this whole test script file; it should verify what
> is in the file before removing it.

Why? Other tests already do it. This test is meant for rebase, not
plain checkout.

>
> > +     git rebase rebase-on-me &&
> > +     read old new flag < .git/post-checkout.args &&
>
> No SP between "<" and ".git/post-checkout.args".

Done

> > +     test $old != $new && test $flag = 1 &&
> > +     rm -f .git/post-checkout.args
> > +'
>
> Regarding the clean-up of this file, see my review on the previous
> one.

Done

> > +test_expect_success 'post-checkout is triggered on rebase with fast-forward' '
>
> The same comment as above applies to this.

Done

Thanks,
- Orgad
diff mbox series

Patch

diff --git a/builtin/rebase.c b/builtin/rebase.c
index b5c99ec10c..7f7a2c738e 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -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_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_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_HOOK,
 					       NULL, buf.buf) < 0) {
 					ret = !!error(_("could not switch to "
 							"%s"),
@@ -1539,7 +1546,7 @@  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_HOOK, NULL, msg.buf))
 		die(_("Could not detach HEAD"));
 	strbuf_release(&msg);
 
diff --git a/t/t5403-post-checkout-hook.sh b/t/t5403-post-checkout-hook.sh
index 9f9a5163c5..5b4e582caa 100755
--- a/t/t5403-post-checkout-hook.sh
+++ b/t/t5403-post-checkout-hook.sh
@@ -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 three
 '
 
@@ -51,6 +53,24 @@  test_expect_success 'post-checkout receives the right args when not switching br
 	rm -f .git/post-checkout.args
 '
 
+test_expect_success 'post-checkout is triggered on rebase' '
+	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 &&
+	rm -f .git/post-checkout.args
+'
+
+test_expect_success 'post-checkout is triggered on rebase with fast-forward' '
+	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 &&
+	rm -f .git/post-checkout.args
+'
+
 if test "$(git config --bool core.filemode)" = true; then
 mkdir -p templates/hooks
 write_script templates/hooks/post-checkout <<-\EOF