git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: lars.schneider@autodesk.com
To: git@vger.kernel.org
Cc: sunshine@sunshineco.com, matthew.k.gumbel@intel.com,
	Lars Schneider <larsxschneider@gmail.com>
Subject: [PATCH v1] worktree: set worktree environment in post-checkout hook
Date: Sat, 10 Feb 2018 02:01:32 +0100	[thread overview]
Message-ID: <20180210010132.33629-1-lars.schneider@autodesk.com> (raw)

From: Lars Schneider <larsxschneider@gmail.com>

In ade546be47 (worktree: invoke post-checkout hook (unless
--no-checkout), 2017-12-07) we taught Git to run the post-checkout hook
in worktrees. Unfortunately, the environment of the hook was not made
aware of the worktree. Consequently, a 'git rev-parse --show-toplevel'
call in the post-checkout hook would return a wrong result.

Fix this by setting the 'GIT_WORK_TREE' environment variable to make
Git calls within the post-checkout hook aware of the worktree.

Signed-off-by: Lars Schneider <larsxschneider@gmail.com>
---

Hi,

I think this is a bug in Git 2.16. We noticed it because it caused a
problem in Git LFS [1]. The modified test case fails with Git 2.16 and
succeeds with this patch.

Cheers,
Lars


[1] https://github.com/git-lfs/git-lfs/issues/2848


Notes:
    Base Ref: v2.16.1
    Web-Diff: https://github.com/larsxschneider/git/commit/214e9342e7
    Checkout: git fetch https://github.com/larsxschneider/git fix-worktree-add-v1 && git checkout 214e9342e7

 builtin/worktree.c      |  7 +++++--
 t/t2025-worktree-add.sh | 11 +++++++++--
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/builtin/worktree.c b/builtin/worktree.c
index 7cef5b120b..032f9b86bf 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -345,9 +345,12 @@ 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),
+	if (!ret && opts->checkout) {
+		struct argv_array env = ARGV_ARRAY_INIT;
+		argv_array_pushf(&env, "GIT_WORK_TREE=%s", absolute_path(path));
+		ret = run_hook_le(env.argv, "post-checkout", oid_to_hex(&null_oid),
 				  oid_to_hex(&commit->object.oid), "1", NULL);
+	}

 	argv_array_clear(&child_env);
 	strbuf_release(&sb);
diff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh
index 2b95944973..d022ac0c26 100755
--- a/t/t2025-worktree-add.sh
+++ b/t/t2025-worktree-add.sh
@@ -455,19 +455,26 @@ post_checkout_hook () {
 	mkdir -p .git/hooks &&
 	write_script .git/hooks/post-checkout <<-\EOF
 	echo $* >hook.actual
+	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 &&
+	cat >hook.expect <<-EOF &&
+		$_z40 $(git rev-parse HEAD) 1
+		$(pwd)/gumby
+	EOF
 	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 &&
+	cat >hook.expect <<-EOF &&
+		$_z40 $(git rev-parse HEAD) 1
+		$(pwd)/grumpy
+	EOF
 	git worktree add --detach grumpy &&
 	test_cmp hook.expect hook.actual
 '

base-commit: 8279ed033f703d4115bee620dccd32a9ec94d9aa
--
2.16.1


             reply	other threads:[~2018-02-10  1:02 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-10  1:01 lars.schneider [this message]
2018-02-10  1:28 ` [PATCH v1] worktree: set worktree environment in post-checkout hook 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
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=20180210010132.33629-1-lars.schneider@autodesk.com \
    --to=lars.schneider@autodesk.com \
    --cc=git@vger.kernel.org \
    --cc=larsxschneider@gmail.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).