# [PATCH v2 1/3] setup: Provide GIT_PREFIX to built-ins

8 messages from 2011-05-26 to 2011-05-26. Participants: David Aguilar, Junio C Hamano.
Thread: https://gitlist.dev/t/27461

## David Aguilar, 2011-05-26 03:37

Subject: [PATCH v2 1/3] setup: Provide GIT_PREFIX to built-ins
Message-ID: <1306381034-44190-1-git-send-email-davvid@gmail.com>
URL: https://gitlist.dev/e/1306381034-44190-1-git-send-email-davvid%40gmail.com

```
GIT_PREFIX was added in 7cf16a14f5c070f7b14cf28023769450133172ae so that
aliases can know the directory from which a !alias was called.

Knowing the prefix relative to the root is helpful in other programs
so export it to built-ins as well.

Signed-off-by: David Aguilar <davvid@gmail.com>
---

The original version of this patch did not set GIT_PREFIX
to an empty string when prefix returns NULL.
Now we do.

 setup.c                 |    5 +++++
 t/t1020-subdirectory.sh |   16 ++++++++++++++++
 2 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/setup.c b/setup.c
index 013ad11..4569974 100644
--- a/setup.c
+++ b/setup.c
@@ -710,6 +710,11 @@ const char *setup_git_directory_gently(int *nongit_ok)
 	const char *prefix;
 
 	prefix = setup_git_directory_gently_1(nongit_ok);
+	if (prefix)
+		setenv("GIT_PREFIX", prefix, 1);
+	else
+		setenv("GIT_PREFIX", "", 1);
+
 	if (startup_info) {
 		startup_info->have_repository = !nongit_ok || !*nongit_ok;
 		startup_info->prefix = prefix;
diff --git a/t/t1020-subdirectory.sh b/t/t1020-subdirectory.sh
index ddc3921..3c74480 100755
--- a/t/t1020-subdirectory.sh
+++ b/t/t1020-subdirectory.sh
@@ -139,6 +139,22 @@ test_expect_success 'GIT_PREFIX for !alias' '
 	test_cmp expect actual
 '
 
+test_expect_success 'GIT_PREFIX for built-ins' '
+	# Use GIT_EXTERNAL_DIFF to test that the "diff" built-in
+	# receives the GIT_PREFIX variable.
+	printf "dir/" >expect &&
+	printf "#!/bin/sh\n" >diff &&
+	printf "printf \"\$GIT_PREFIX\"" >>diff &&
+	chmod +x diff &&
+	(
+		cd dir &&
+		printf "change" >two &&
+		env GIT_EXTERNAL_DIFF=./diff git diff >../actual
+		git checkout -- two
+	) &&
+	test_cmp expect actual
+'
+
 test_expect_success 'no file/rev ambiguity check inside .git' '
 	git commit -a -m 1 &&
 	(
-- 
1.7.5.2.660.g9f46

```

## David Aguilar, 2011-05-26 03:37

Subject: [PATCH v2 2/3] git: Remove handling for GIT_PREFIX
Message-ID: <1306381034-44190-2-git-send-email-davvid@gmail.com>
URL: https://gitlist.dev/e/1306381034-44190-2-git-send-email-davvid%40gmail.com
In-Reply-To: <1306381034-44190-1-git-send-email-davvid@gmail.com>

```
handle_alias() no longer needs to set GIT_PREFIX since it is defined
in setup_git_directory_gently().  Remove the duplicated effort and use
run_command_v_opt() since there is no need to setup the environment.

Signed-off-by: David Aguilar <davvid@gmail.com>
---

This patch is unchanged.

 git.c |   10 +---------
 1 files changed, 1 insertions(+), 9 deletions(-)

diff --git a/git.c b/git.c
index 89721d4..8828c18 100644
--- a/git.c
+++ b/git.c
@@ -183,8 +183,6 @@ static int handle_alias(int *argcp, const char ***argv)
 		if (alias_string[0] == '!') {
 			const char **alias_argv;
 			int argc = *argcp, i;
-			struct strbuf sb = STRBUF_INIT;
-			const char *env[2];
 
 			commit_pager_choice();
 
@@ -195,13 +193,7 @@ static int handle_alias(int *argcp, const char ***argv)
 				alias_argv[i] = (*argv)[i];
 			alias_argv[argc] = NULL;
 
-			strbuf_addstr(&sb, "GIT_PREFIX=");
-			if (subdir)
-				strbuf_addstr(&sb, subdir);
-			env[0] = sb.buf;
-			env[1] = NULL;
-			ret = run_command_v_opt_cd_env(alias_argv, RUN_USING_SHELL, NULL, env);
-			strbuf_release(&sb);
+			ret = run_command_v_opt(alias_argv, RUN_USING_SHELL);
 			if (ret >= 0)   /* normal exit */
 				exit(ret);
 
-- 
1.7.5.2.660.g9f46

```

## David Aguilar, 2011-05-26 03:37

Subject: [PATCH v2 3/3] git-mergetool--lib: Make vimdiff retain the current directory
Message-ID: <1306381034-44190-3-git-send-email-davvid@gmail.com>
URL: https://gitlist.dev/e/1306381034-44190-3-git-send-email-davvid%40gmail.com
In-Reply-To: <1306381034-44190-1-git-send-email-davvid@gmail.com>

```
When using difftool with vimdiff it can be unexpected that
the current directory changes to the root of the project.
Tell vim to chdir to the value of $GIT_PREFIX to fix this.

Care is taken to quote the variable so that vim expands it.
This avoids problems when directory names contain spaces.

Signed-off-by: David Aguilar <davvid@gmail.com>
Reported-by: Frédéric Heitzmann <frederic.heitzmann@gmail.com>
---

This version of the patch resolves GIT_PREFIX unconditionally.
We did not do so before.

 git-mergetool--lib.sh |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)

diff --git a/git-mergetool--lib.sh b/git-mergetool--lib.sh
index 4db9212..2cef8a2 100644
--- a/git-mergetool--lib.sh
+++ b/git-mergetool--lib.sh
@@ -89,6 +89,7 @@ run_merge_tool () {
 	merge_tool_path="$(get_merge_tool_path "$1")" || exit
 	base_present="$2"
 	status=0
+	resolve_git_prefix
 
 	case "$1" in
 	araxis)
@@ -188,6 +189,7 @@ run_merge_tool () {
 			check_unchanged
 		else
 			"$merge_tool_path" -R -f -d -c "wincmd l" \
+				-c 'cd $GIT_PREFIX' \
 				"$LOCAL" "$REMOTE"
 		fi
 		;;
@@ -199,6 +201,7 @@ run_merge_tool () {
 			check_unchanged
 		else
 			"$merge_tool_path" -R -f -d -c "wincmd l" \
+				-c 'cd $GIT_PREFIX' \
 				"$LOCAL" "$REMOTE"
 		fi
 		;;
@@ -437,3 +440,12 @@ get_merge_tool () {
 	fi
 	echo "$merge_tool"
 }
+
+resolve_git_prefix() {
+	# If GIT_PREFIX is empty then we cannot use it in tools
+	# that expect to be able to chdir() to its value.
+	if test -z "$GIT_PREFIX"; then
+		GIT_PREFIX=.
+		export GIT_PREFIX
+	fi
+}
-- 
1.7.5.2.660.g9f46

```

## David Aguilar, 2011-05-26 05:05

Subject: Re: [PATCH v2 3/3] git-mergetool--lib: Make vimdiff retain the current directory
Message-ID: <BANLkTinWUK8NDDd3i7fRRTEXjSmTtMwL_Q@mail.gmail.com>
URL: https://gitlist.dev/e/BANLkTinWUK8NDDd3i7fRRTEXjSmTtMwL_Q%40mail.gmail.com
In-Reply-To: <1306381034-44190-3-git-send-email-davvid@gmail.com>

```
On Wed, May 25, 2011 at 8:37 PM, David Aguilar <davvid@gmail.com> wrote:
> +
> +resolve_git_prefix() {
> +       # If GIT_PREFIX is empty then we cannot use it in tools
> +       # that expect to be able to chdir() to its value.
> +       if test -z "$GIT_PREFIX"; then
> +               GIT_PREFIX=.
> +               export GIT_PREFIX
> +       fi
> +}

Oops, I forgot to replace the function call with : GIT_PREFIX=${GIT_PREFIX:-.}
-- 
    David

```

## David Aguilar, 2011-05-26 05:29

Subject: [PATCH] git-mergetool--lib: Make vimdiff retain the current directory
Message-ID: <1306387776-14593-1-git-send-email-davvid@gmail.com>
URL: https://gitlist.dev/e/1306387776-14593-1-git-send-email-davvid%40gmail.com
In-Reply-To: <1306381034-44190-3-git-send-email-davvid@gmail.com>

```
When using difftool with vimdiff it can be unexpected that
the current directory changes to the root of the project.
Tell vim to chdir to the value of $GIT_PREFIX to fix this.

Care is taken to quote the variable so that vim expands it.
This avoids problems when directory names contain spaces.

Signed-off-by: David Aguilar <davvid@gmail.com>
Reported-by: Frédéric Heitzmann <frederic.heitzmann@gmail.com>
---
Replacement for the last patch: uses ${GIT_PREFIX:-.}

 git-mergetool--lib.sh |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/git-mergetool--lib.sh b/git-mergetool--lib.sh
index 4db9212..43a9a28 100644
--- a/git-mergetool--lib.sh
+++ b/git-mergetool--lib.sh
@@ -86,6 +86,10 @@ get_merge_tool_cmd () {
 }
 
 run_merge_tool () {
+	# If GIT_PREFIX is empty then we cannot use it in tools
+	# that expect to be able to chdir() to its value.
+	: GIT_PREFIX=${GIT_PREFIX:-.}
+
 	merge_tool_path="$(get_merge_tool_path "$1")" || exit
 	base_present="$2"
 	status=0
@@ -188,6 +192,7 @@ run_merge_tool () {
 			check_unchanged
 		else
 			"$merge_tool_path" -R -f -d -c "wincmd l" \
+				-c 'cd $GIT_PREFIX' \
 				"$LOCAL" "$REMOTE"
 		fi
 		;;
@@ -199,6 +204,7 @@ run_merge_tool () {
 			check_unchanged
 		else
 			"$merge_tool_path" -R -f -d -c "wincmd l" \
+				-c 'cd $GIT_PREFIX' \
 				"$LOCAL" "$REMOTE"
 		fi
 		;;
-- 
1.7.5.2.660.g9f46c

```

## David Aguilar, 2011-05-26 06:21

Subject: [PATCH v4 3/3] git-mergetool--lib: Make vimdiff retain the current directory
Message-ID: <1306390861-57054-1-git-send-email-davvid@gmail.com>
URL: https://gitlist.dev/e/1306390861-57054-1-git-send-email-davvid%40gmail.com
In-Reply-To: <1306387776-14593-1-git-send-email-davvid@gmail.com>

```
When using difftool with vimdiff it can be unexpected that
the current directory changes to the root of the project.
Tell vim to chdir to the value of $GIT_PREFIX to fix this.

Care is taken to quote the variable so that vim expands it.
This avoids problems when directory names contain spaces.

Signed-off-by: David Aguilar <davvid@gmail.com>
Reported-by: Frédéric Heitzmann <frederic.heitzmann@gmail.com>
---
How embarassing.  I'm going to bed now.  This one works.

 git-mergetool--lib.sh |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)

diff --git a/git-mergetool--lib.sh b/git-mergetool--lib.sh
index 4db9212..91f90ac 100644
--- a/git-mergetool--lib.sh
+++ b/git-mergetool--lib.sh
@@ -86,6 +86,11 @@ get_merge_tool_cmd () {
 }
 
 run_merge_tool () {
+	# If GIT_PREFIX is empty then we cannot use it in tools
+	# that expect to be able to chdir() to its value.
+	GIT_PREFIX=${GIT_PREFIX:-.}
+	export GIT_PREFIX
+
 	merge_tool_path="$(get_merge_tool_path "$1")" || exit
 	base_present="$2"
 	status=0
@@ -188,6 +193,7 @@ run_merge_tool () {
 			check_unchanged
 		else
 			"$merge_tool_path" -R -f -d -c "wincmd l" \
+				-c 'cd $GIT_PREFIX' \
 				"$LOCAL" "$REMOTE"
 		fi
 		;;
@@ -199,6 +205,7 @@ run_merge_tool () {
 			check_unchanged
 		else
 			"$merge_tool_path" -R -f -d -c "wincmd l" \
+				-c 'cd $GIT_PREFIX' \
 				"$LOCAL" "$REMOTE"
 		fi
 		;;
-- 
1.7.5.2.663.gfcb11.dirty

```

## Junio C Hamano, 2011-05-26 17:45

Subject: Re: [PATCH v2 1/3] setup: Provide GIT_PREFIX to built-ins
Message-ID: <7vfwo1xsn4.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vfwo1xsn4.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1306381034-44190-1-git-send-email-davvid@gmail.com>

```
David Aguilar <davvid@gmail.com> writes:

> GIT_PREFIX was added in 7cf16a14f5c070f7b14cf28023769450133172ae so that
> aliases can know the directory from which a !alias was called.
>
> Knowing the prefix relative to the root is helpful in other programs
> so export it to built-ins as well.
>
> Signed-off-by: David Aguilar <davvid@gmail.com>
> ---
>
> The original version of this patch did not set GIT_PREFIX
> to an empty string when prefix returns NULL.
> Now we do.

Thanks.  Should I add "Helped-by: MJG"?

```

## David Aguilar, 2011-05-26 21:49

Subject: Re: [PATCH v2 1/3] setup: Provide GIT_PREFIX to built-ins
Message-ID: <20110526214918.GA22623@gmail.com>
URL: https://gitlist.dev/e/20110526214918.GA22623%40gmail.com
In-Reply-To: <7vfwo1xsn4.fsf@alter.siamese.dyndns.org>

```
On Thu, May 26, 2011 at 10:45:35AM -0700, Junio C Hamano wrote:
> David Aguilar <davvid@gmail.com> writes:
> 
> > GIT_PREFIX was added in 7cf16a14f5c070f7b14cf28023769450133172ae so that
> > aliases can know the directory from which a !alias was called.
> >
> > Knowing the prefix relative to the root is helpful in other programs
> > so export it to built-ins as well.
> >
> > Signed-off-by: David Aguilar <davvid@gmail.com>
> > ---
> >
> > The original version of this patch did not set GIT_PREFIX
> > to an empty string when prefix returns NULL.
> > Now we do.
> 
> Thanks.  Should I add "Helped-by: MJG"?

Yes, that'd be great.
-- 
					David

```
