git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Lars Schneider <larsxschneider@gmail.com>
To: Eric Sunshine <sunshine@sunshineco.com>
Cc: git <git@vger.kernel.org>,
	matthew.k.gumbel@intel.com, Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH 2/2] worktree: add: change to new worktree directory before running hook
Date: Mon, 12 Feb 2018 21:01:01 +0100	[thread overview]
Message-ID: <C2FFE6FB-4B3C-4246-9BCA-272EC874FA8B@gmail.com> (raw)
In-Reply-To: <20180212031526.40039-3-sunshine@sunshineco.com>


> On 12 Feb 2018, at 04:15, Eric Sunshine <sunshine@sunshineco.com> wrote:
> 
> Although "git worktree add" learned to run the 'post-checkout' hook in
> ade546be47 (worktree: invoke post-checkout hook, 2017-12-07), it
> neglects to change to the directory of the newly-created worktree
> before running the hook. Instead, the hook is run within the directory
> from which the "git worktree add" command itself was invoked, which
> effectively neuters the hook since it knows nothing about the new
> worktree directory.
> 
> Fix this by changing to the new worktree's directory before running
> the hook, and adjust the tests to verify that the hook is indeed run
> within the correct directory.
> 
> While at it, also add a test to verify that the hook is run within the
> correct directory even when the new worktree is created from a sibling
> worktree (as opposed to the main worktree).
> 
> Reported-by: Lars Schneider <larsxschneider@gmail.com>
> Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
> ---
> builtin/worktree.c      | 11 ++++++++---
> t/t2025-worktree-add.sh | 25 ++++++++++++++++++++++---
> 2 files changed, 30 insertions(+), 6 deletions(-)
> 
> diff --git a/builtin/worktree.c b/builtin/worktree.c
> index 7cef5b120b..b55c55a26c 100644
> --- a/builtin/worktree.c
> +++ b/builtin/worktree.c
> @@ -345,9 +345,14 @@ static int add_worktree(const char *path, const char *refname,
> 	 * Hook failure does not warrant worktree deletion, so run hook after
> 	 * is_junk is cleared, but do return appropriate code when hook fails.
> 	 */
> -	if (!ret && opts->checkout)
> -		ret = run_hook_le(NULL, "post-checkout", oid_to_hex(&null_oid),
> -				  oid_to_hex(&commit->object.oid), "1", NULL);
> +	if (!ret && opts->checkout) {
> +		char *p = absolute_pathdup(path);
> +		ret = run_hook_cd_le(p, NULL, "post-checkout",
> +				     oid_to_hex(&null_oid),
> +				     oid_to_hex(&commit->object.oid),
> +				     "1", NULL);
> +		free(p);
> +	}
> 
> 	argv_array_clear(&child_env);
> 	strbuf_release(&sb);
> diff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh
> index 2b95944973..cf0aaeaf88 100755
> --- a/t/t2025-worktree-add.sh
> +++ b/t/t2025-worktree-add.sh
> @@ -454,20 +454,29 @@ post_checkout_hook () {
> 	test_when_finished "rm -f .git/hooks/post-checkout" &&
> 	mkdir -p .git/hooks &&
> 	write_script .git/hooks/post-checkout <<-\EOF
> -	echo $* >hook.actual
> +	{
> +		echo $*
> +		git rev-parse --show-toplevel
> +	} >../hook.actual
> 	EOF
> }
> 
> test_expect_success '"add" invokes post-checkout hook (branch)' '
> 	post_checkout_hook &&
> -	printf "%s %s 1\n" $_z40 $(git rev-parse HEAD) >hook.expect &&
> +	{
> +		echo $_z40 $(git rev-parse HEAD) 1 &&
> +		echo $(pwd)/gumby
> +	} >hook.expect &&
> 	git worktree add gumby &&
> 	test_cmp hook.expect hook.actual
> '
> 
> test_expect_success '"add" invokes post-checkout hook (detached)' '
> 	post_checkout_hook &&
> -	printf "%s %s 1\n" $_z40 $(git rev-parse HEAD) >hook.expect &&
> +	{
> +		echo $_z40 $(git rev-parse HEAD) 1 &&
> +		echo $(pwd)/grumpy
> +	} >hook.expect &&
> 	git worktree add --detach grumpy &&
> 	test_cmp hook.expect hook.actual
> '
> @@ -479,4 +488,14 @@ test_expect_success '"add --no-checkout" suppresses post-checkout hook' '
> 	test_path_is_missing hook.actual
> '
> 
> +test_expect_success '"add" within worktree invokes post-checkout hook' '
> +	post_checkout_hook &&
> +	{
> +		echo $_z40 $(git rev-parse HEAD) 1 &&
> +		echo $(pwd)/guppy
> +	} >hook.expect &&
> +	git -C gloopy worktree add --detach ../guppy &&
> +	test_cmp hook.expect hook.actual
> +'
> +
> test_done
> -- 
> 2.16.1.291.g4437f3f132
> 

Looks good but I think we are not quite there yet. It does not work
for bare repos. You can test this if you apply the following patch on
top of your changes. Or get the patch from here:
https://github.com/larsxschneider/git/commit/253130e65b37a2ef250e9c6369d292ec725e62e7.patch

Please note that also '"add" within worktree invokes post-checkout hook'
seems to fail with my extended test case.

Thanks,
Lars


diff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh
index cf0aaeaf88..0580b12d50 100755
--- a/t/t2025-worktree-add.sh
+++ b/t/t2025-worktree-add.sh
@@ -451,20 +451,41 @@ test_expect_success 'git worktree --no-guess-remote option overrides config' '
 '
 
 post_checkout_hook () {
-	test_when_finished "rm -f .git/hooks/post-checkout" &&
-	mkdir -p .git/hooks &&
-	write_script .git/hooks/post-checkout <<-\EOF
+	test_when_finished "rm -f $1/hooks/post-checkout" &&
+	mkdir -p $1/hooks &&
+	write_script $1/hooks/post-checkout <<-\EOF
 	{
 		echo $*
-		git rev-parse --show-toplevel
+		git rev-parse --git-dir --show-toplevel
+		pwd
 	} >../hook.actual
 	EOF
 }
 
+test_expect_success '"add" invokes post-checkout hook (branch, bare)' '
+	rm -rf bare &&
+	git clone --bare . bare &&
+	{
+		echo $_z40 $(git --git-dir=bare rev-parse HEAD) 1 &&
+		echo $(pwd)/bare/worktrees/wt-bare
+		echo $(pwd)/wt-bare
+		echo $(pwd)/wt-bare
+	} >hook.expect &&
+	post_checkout_hook bare &&
+	(
+		cd bare &&
+		git worktree add -b branch ../wt-bare master
+	) &&
+	test_cmp hook.expect hook.actual
+'
+
+
 test_expect_success '"add" invokes post-checkout hook (branch)' '
-	post_checkout_hook &&
+	post_checkout_hook .git &&
 	{
 		echo $_z40 $(git rev-parse HEAD) 1 &&
+		echo $(pwd)/.git/worktrees/gumby
+		echo $(pwd)/gumby
 		echo $(pwd)/gumby
 	} >hook.expect &&
 	git worktree add gumby &&
@@ -472,9 +493,11 @@ test_expect_success '"add" invokes post-checkout hook (branch)' '
 '
 
 test_expect_success '"add" invokes post-checkout hook (detached)' '
-	post_checkout_hook &&
+	post_checkout_hook .git &&
 	{
 		echo $_z40 $(git rev-parse HEAD) 1 &&
+		echo $(pwd)/.git/worktrees/grumpy
+		echo $(pwd)/grumpy
 		echo $(pwd)/grumpy
 	} >hook.expect &&
 	git worktree add --detach grumpy &&
@@ -482,16 +505,18 @@ test_expect_success '"add" invokes post-checkout hook (detached)' '
 '
 
 test_expect_success '"add --no-checkout" suppresses post-checkout hook' '
-	post_checkout_hook &&
+	post_checkout_hook .git &&
 	rm -f hook.actual &&
 	git worktree add --no-checkout gloopy &&
 	test_path_is_missing hook.actual
 '
 
 test_expect_success '"add" within worktree invokes post-checkout hook' '
-	post_checkout_hook &&
+	post_checkout_hook .git &&
 	{
 		echo $_z40 $(git rev-parse HEAD) 1 &&
+		echo $(pwd)/.git/worktrees/guppy
+		echo $(pwd)/guppy
 		echo $(pwd)/guppy
 	} >hook.expect &&
 	git -C gloopy worktree add --detach ../guppy &&



  parent reply	other threads:[~2018-02-12 20:01 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-10  1:01 [PATCH v1] worktree: set worktree environment in post-checkout hook lars.schneider
2018-02-10  1:28 ` Lars Schneider
2018-02-12  3:15 ` [PATCH 0/2] worktree: change to new worktree dir before running hook(s) Eric Sunshine
2018-02-12  3:15   ` [PATCH 1/2] run-command: teach 'run_hook' about alternate worktrees Eric Sunshine
2018-02-12 20:58     ` Lars Schneider
2018-02-12 21:49       ` Eric Sunshine
2018-02-12  3:15   ` [PATCH 2/2] worktree: add: change to new worktree directory before running hook Eric Sunshine
2018-02-12 19:37     ` Junio C Hamano
2018-02-12 20:31       ` Eric Sunshine
2018-02-12 20:01     ` Lars Schneider [this message]
2018-02-13  4:42       ` Eric Sunshine
2018-02-13  4:48         ` Eric Sunshine
2018-02-13  7:27     ` Johannes Sixt
2018-02-13  7:34       ` Eric Sunshine
2018-02-15 19:18   ` [PATCH v2] worktree: add: fix 'post-checkout' not knowing new worktree location Eric Sunshine
2018-02-15 20:52     ` Junio C Hamano
2018-02-15 21:27       ` Eric Sunshine
2018-02-15 21:36         ` Junio C Hamano
2018-02-15 23:09         ` Eric Sunshine
2018-02-15 23:09     ` [PATCH v3] " Eric Sunshine
2018-02-16 16:55       ` Lars Schneider
2018-02-16 18:27         ` Eric Sunshine
2018-02-16 18:05       ` Junio C Hamano
2018-02-12  3:27 ` [PATCH v1] worktree: set worktree environment in post-checkout hook Eric Sunshine

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=C2FFE6FB-4B3C-4246-9BCA-272EC874FA8B@gmail.com \
    --to=larsxschneider@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=matthew.k.gumbel@intel.com \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).