git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC/PATCH] handle_alias: provide GIT_CWD to !alias
       [not found] <4DA59B27.50506@ge.infn.it>
@ 2011-04-13 13:05 ` Michael J Gruber
  2011-04-13 13:12   ` Nguyen Thai Ngoc Duy
  2011-04-26 22:22   ` Junio C Hamano
  0 siblings, 2 replies; 12+ messages in thread
From: Michael J Gruber @ 2011-04-13 13:05 UTC (permalink / raw)
  To: git; +Cc: Matej Batic

Provide an environment variable GIT_CWD which contains the subdirectory
from which a !alias was called since these cd to the to level directory
before they are executed.

Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
---
RFC for lack of tests and doc.

I would say this is something realistic and useful which can be done now, no
matter how our efforts with !(top) and the like go.

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

diff --git a/git.c b/git.c
index ef598c3..c8b9189 100644
--- a/git.c
+++ b/git.c
@@ -179,6 +179,8 @@ 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();
 
@@ -189,7 +191,13 @@ static int handle_alias(int *argcp, const char ***argv)
 				alias_argv[i] = (*argv)[i];
 			alias_argv[argc] = NULL;
 
-			ret = run_command_v_opt(alias_argv, RUN_USING_SHELL);
+			strbuf_addstr(&sb, "GIT_CWD=");
+			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);
 			if (ret >= 0)   /* normal exit */
 				exit(ret);
 
-- 
1.7.5.rc0.365.g23929a

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [RFC/PATCH] handle_alias: provide GIT_CWD to !alias
  2011-04-13 13:05 ` [RFC/PATCH] handle_alias: provide GIT_CWD to !alias Michael J Gruber
@ 2011-04-13 13:12   ` Nguyen Thai Ngoc Duy
  2011-04-13 13:16     ` Michael J Gruber
  2011-04-26 22:22   ` Junio C Hamano
  1 sibling, 1 reply; 12+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2011-04-13 13:12 UTC (permalink / raw)
  To: Michael J Gruber; +Cc: git, Matej Batic

On Wed, Apr 13, 2011 at 8:05 PM, Michael J Gruber
<git@drmicha.warpmail.net> wrote:
> -                       ret = run_command_v_opt(alias_argv, RUN_USING_SHELL);
> +                       strbuf_addstr(&sb, "GIT_CWD=");
> +                       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);
>                        if (ret >= 0)   /* normal exit */
>                                exit(ret);

subdir can be NULL. I'm not sure if it can be empty string though. May
need a check and set it to '.' so "cd $GIT_CWD" does not go back to
$HOME.
-- 
Duy

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [RFC/PATCH] handle_alias: provide GIT_CWD to !alias
  2011-04-13 13:12   ` Nguyen Thai Ngoc Duy
@ 2011-04-13 13:16     ` Michael J Gruber
  2011-04-13 13:24       ` Nguyen Thai Ngoc Duy
  0 siblings, 1 reply; 12+ messages in thread
From: Michael J Gruber @ 2011-04-13 13:16 UTC (permalink / raw)
  To: Nguyen Thai Ngoc Duy; +Cc: git, Matej Batic

Nguyen Thai Ngoc Duy venit, vidit, dixit 13.04.2011 15:12:
> On Wed, Apr 13, 2011 at 8:05 PM, Michael J Gruber
> <git@drmicha.warpmail.net> wrote:
>> -                       ret = run_command_v_opt(alias_argv, RUN_USING_SHELL);
>> +                       strbuf_addstr(&sb, "GIT_CWD=");
>> +                       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);
>>                        if (ret >= 0)   /* normal exit */
>>                                exit(ret);
> 
> subdir can be NULL. I'm not sure if it can be empty string though. May

I'm pretty sure that my "if (subdir)" would catch that...

> need a check and set it to '.' so "cd $GIT_CWD" does not go back to
> $HOME.

Well, with the current implementation you get empty or the subdir so
that you can easily "test -n $GIT_CWD". If you want to cd around you can
do "cd ./$GIT_CWD" unconditionally. I think this is more useful than
having a "." there.

Michael

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [RFC/PATCH] handle_alias: provide GIT_CWD to !alias
  2011-04-13 13:16     ` Michael J Gruber
@ 2011-04-13 13:24       ` Nguyen Thai Ngoc Duy
  0 siblings, 0 replies; 12+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2011-04-13 13:24 UTC (permalink / raw)
  To: Michael J Gruber; +Cc: git, Matej Batic

On Wed, Apr 13, 2011 at 8:16 PM, Michael J Gruber
<git@drmicha.warpmail.net> wrote:
> Nguyen Thai Ngoc Duy venit, vidit, dixit 13.04.2011 15:12:
>> On Wed, Apr 13, 2011 at 8:05 PM, Michael J Gruber
>> <git@drmicha.warpmail.net> wrote:
>>> -                       ret = run_command_v_opt(alias_argv, RUN_USING_SHELL);
>>> +                       strbuf_addstr(&sb, "GIT_CWD=");
>>> +                       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);
>>>                        if (ret >= 0)   /* normal exit */
>>>                                exit(ret);
>>
>> subdir can be NULL. I'm not sure if it can be empty string though. May
>
> I'm pretty sure that my "if (subdir)" would catch that...

Yes. I skimmed too quick and realized after sending my email.

>> need a check and set it to '.' so "cd $GIT_CWD" does not go back to
>> $HOME.
>
> Well, with the current implementation you get empty or the subdir so
> that you can easily "test -n $GIT_CWD". If you want to cd around you can
> do "cd ./$GIT_CWD" unconditionally. I think this is more useful than
> having a "." there.

In that case should the variable have a another name? CWD/PWD is
usually absolute path. "cd ./$GIT_CWD" looks odd to me. Or you can
getcwd() and set $OLDPWD.
-- 
Duy

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [RFC/PATCH] handle_alias: provide GIT_CWD to !alias
  2011-04-13 13:05 ` [RFC/PATCH] handle_alias: provide GIT_CWD to !alias Michael J Gruber
  2011-04-13 13:12   ` Nguyen Thai Ngoc Duy
@ 2011-04-26 22:22   ` Junio C Hamano
  2011-04-27  6:47     ` Michael J Gruber
  1 sibling, 1 reply; 12+ messages in thread
From: Junio C Hamano @ 2011-04-26 22:22 UTC (permalink / raw)
  To: Michael J Gruber; +Cc: git, Matej Batic

Michael J Gruber <git@drmicha.warpmail.net> writes:

> Provide an environment variable GIT_CWD which contains the subdirectory
> from which a !alias was called since these cd to the to level directory
> before they are executed.

Isn't this what we usually call "prefix", though?

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [RFC/PATCH] handle_alias: provide GIT_CWD to !alias
  2011-04-26 22:22   ` Junio C Hamano
@ 2011-04-27  6:47     ` Michael J Gruber
  2011-04-27  7:40       ` Nguyen Thai Ngoc Duy
  0 siblings, 1 reply; 12+ messages in thread
From: Michael J Gruber @ 2011-04-27  6:47 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Matej Batic

Junio C Hamano venit, vidit, dixit 27.04.2011 00:22:
> Michael J Gruber <git@drmicha.warpmail.net> writes:
> 
>> Provide an environment variable GIT_CWD which contains the subdirectory
>> from which a !alias was called since these cd to the to level directory
>> before they are executed.
> 
> Isn't this what we usually call "prefix", though?

In code it is called "prefix". But I have to ask: So what? Are you
suggesting GIT_PREFIX? User expectation is probably more tied to CWD,
unless a user knows "rev-parse --show-prefix". I don't care about the
name, and GIT_PREFIX may be more systematic.

A more important point is the prefix-less case (i.e. we are in topdir):
Should GIT_YOUNAMEIT be empty then? I would say yes (just like
"rev-parse --show-prefix"), Duy said no. We need a third vote :)

Also, we may want to scrap this altogether and do !!-aliases instead
(without chdir).

Michael

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [RFC/PATCH] handle_alias: provide GIT_CWD to !alias
  2011-04-27  6:47     ` Michael J Gruber
@ 2011-04-27  7:40       ` Nguyen Thai Ngoc Duy
  2011-04-27  8:10         ` Michael J Gruber
  0 siblings, 1 reply; 12+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2011-04-27  7:40 UTC (permalink / raw)
  To: Michael J Gruber; +Cc: Junio C Hamano, git, Matej Batic

On Wed, Apr 27, 2011 at 1:47 PM, Michael J Gruber
<git@drmicha.warpmail.net> wrote:
> A more important point is the prefix-less case (i.e. we are in topdir):
> Should GIT_YOUNAMEIT be empty then? I would say yes (just like
> "rev-parse --show-prefix"), Duy said no. We need a third vote :)

I said no because you named it GIT_CWD. If it's GIT_PREFIX, then empty
prefix is normal, no objections.
-- 
Duy

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [RFC/PATCH] handle_alias: provide GIT_CWD to !alias
  2011-04-27  7:40       ` Nguyen Thai Ngoc Duy
@ 2011-04-27  8:10         ` Michael J Gruber
  2011-04-27  8:36           ` [PATCH 1/2] t1020: test !alias in subdirectory Michael J Gruber
  0 siblings, 1 reply; 12+ messages in thread
From: Michael J Gruber @ 2011-04-27  8:10 UTC (permalink / raw)
  To: Nguyen Thai Ngoc Duy; +Cc: Junio C Hamano, git, Matej Batic

Nguyen Thai Ngoc Duy venit, vidit, dixit 27.04.2011 09:40:
> On Wed, Apr 27, 2011 at 1:47 PM, Michael J Gruber
> <git@drmicha.warpmail.net> wrote:
>> A more important point is the prefix-less case (i.e. we are in topdir):
>> Should GIT_YOUNAMEIT be empty then? I would say yes (just like
>> "rev-parse --show-prefix"), Duy said no. We need a third vote :)
> 
> I said no because you named it GIT_CWD. If it's GIT_PREFIX, then empty
> prefix is normal, no objections.

We have a winner then! Patch upcoming.

Michael

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 1/2] t1020: test !alias in subdirectory
  2011-04-27  8:10         ` Michael J Gruber
@ 2011-04-27  8:36           ` Michael J Gruber
  2011-04-27  8:36             ` [PATCH 2/2] handle_alias: provide GIT_PREFIX to !alias Michael J Gruber
  0 siblings, 1 reply; 12+ messages in thread
From: Michael J Gruber @ 2011-04-27  8:36 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy,
	Matej Batic

Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
---
 t/t1020-subdirectory.sh |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)

diff --git a/t/t1020-subdirectory.sh b/t/t1020-subdirectory.sh
index 1fd187c..66e40d3 100755
--- a/t/t1020-subdirectory.sh
+++ b/t/t1020-subdirectory.sh
@@ -118,6 +118,17 @@ test_expect_success 'alias expansion' '
 		git ss
 	)
 '
+
+test_expect_success '!alias expansion' '
+	pwd >expect &&
+	(
+		git config alias.test !pwd &&
+		cd dir &&
+		git test >../actual
+	) &&
+	test_cmp expect actual
+'
+
 test_expect_success 'no file/rev ambiguity check inside .git' '
 	git commit -a -m 1 &&
 	(
-- 
1.7.5.270.gafca7

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 2/2] handle_alias: provide GIT_PREFIX to !alias
  2011-04-27  8:36           ` [PATCH 1/2] t1020: test !alias in subdirectory Michael J Gruber
@ 2011-04-27  8:36             ` Michael J Gruber
  2011-04-27  9:06               ` Nguyen Thai Ngoc Duy
  0 siblings, 1 reply; 12+ messages in thread
From: Michael J Gruber @ 2011-04-27  8:36 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Nguyễn Thái Ngọc Duy,
	Matej Batic

Provide an environment variable GIT_PREFIX which contains the subdirectory
from which a !alias was called (i.e. 'git rev-parse --show-prefix') since
these cd to the to level directory before they are executed.

Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
---
 Documentation/config.txt |    2 ++
 git.c                    |   10 +++++++++-
 t/t1020-subdirectory.sh  |   10 ++++++++++
 3 files changed, 21 insertions(+), 1 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 750c86d..66281dd 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -589,6 +589,8 @@ it will be treated as a shell command.  For example, defining
 "gitk --all --not ORIG_HEAD".  Note that shell commands will be
 executed from the top-level directory of a repository, which may
 not necessarily be the current directory.
+'GIT_PREFIX' is set as returned by running 'git rev-parse --show-prefix'
+from the original current directory. See linkgit:git-rev-parse[1].
 
 am.keepcr::
 	If true, git-am will call git-mailsplit for patches in mbox format
diff --git a/git.c b/git.c
index ef598c3..ed89951 100644
--- a/git.c
+++ b/git.c
@@ -179,6 +179,8 @@ 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();
 
@@ -189,7 +191,13 @@ static int handle_alias(int *argcp, const char ***argv)
 				alias_argv[i] = (*argv)[i];
 			alias_argv[argc] = NULL;
 
-			ret = run_command_v_opt(alias_argv, RUN_USING_SHELL);
+			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);
 			if (ret >= 0)   /* normal exit */
 				exit(ret);
 
diff --git a/t/t1020-subdirectory.sh b/t/t1020-subdirectory.sh
index 66e40d3..ddc3921 100755
--- a/t/t1020-subdirectory.sh
+++ b/t/t1020-subdirectory.sh
@@ -129,6 +129,16 @@ test_expect_success '!alias expansion' '
 	test_cmp expect actual
 '
 
+test_expect_success 'GIT_PREFIX for !alias' '
+	printf "dir/" >expect &&
+	(
+		git config alias.test "!sh -c \"printf \$GIT_PREFIX\"" &&
+		cd dir &&
+		git test >../actual
+	) &&
+	test_cmp expect actual
+'
+
 test_expect_success 'no file/rev ambiguity check inside .git' '
 	git commit -a -m 1 &&
 	(
-- 
1.7.5.270.gafca7

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH 2/2] handle_alias: provide GIT_PREFIX to !alias
  2011-04-27  8:36             ` [PATCH 2/2] handle_alias: provide GIT_PREFIX to !alias Michael J Gruber
@ 2011-04-27  9:06               ` Nguyen Thai Ngoc Duy
  2011-04-27  9:36                 ` Michael J Gruber
  0 siblings, 1 reply; 12+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2011-04-27  9:06 UTC (permalink / raw)
  To: Michael J Gruber; +Cc: git, Junio C Hamano, Matej Batic

2011/4/27 Michael J Gruber <git@drmicha.warpmail.net>:
> @@ -589,6 +589,8 @@ it will be treated as a shell command.  For example, defining
>  "gitk --all --not ORIG_HEAD".  Note that shell commands will be
>  executed from the top-level directory of a repository, which may
>  not necessarily be the current directory.
> +'GIT_PREFIX' is set as returned by running 'git rev-parse --show-prefix'
> +from the original current directory. See linkgit:git-rev-parse[1].

It made me wonder "but then why should not we just do GIT_PREFIX=`git
rev-parse --show-prefix` in the alias?" then realized by the time we
do that, it's already too late. Maybe

> +'GIT_PREFIX' is set as returned by running 'git rev-parse --show-prefix'
> +before current directory is changed to top-level directory. See linkgit:git-rev-parse[1].

?
-- 
Duy

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 2/2] handle_alias: provide GIT_PREFIX to !alias
  2011-04-27  9:06               ` Nguyen Thai Ngoc Duy
@ 2011-04-27  9:36                 ` Michael J Gruber
  0 siblings, 0 replies; 12+ messages in thread
From: Michael J Gruber @ 2011-04-27  9:36 UTC (permalink / raw)
  To: Nguyen Thai Ngoc Duy; +Cc: git, Junio C Hamano, Matej Batic

Nguyen Thai Ngoc Duy venit, vidit, dixit 27.04.2011 11:06:
> 2011/4/27 Michael J Gruber <git@drmicha.warpmail.net>:
>> @@ -589,6 +589,8 @@ it will be treated as a shell command.  For example, defining
>>  "gitk --all --not ORIG_HEAD".  Note that shell commands will be
>>  executed from the top-level directory of a repository, which may
>>  not necessarily be the current directory.
>> +'GIT_PREFIX' is set as returned by running 'git rev-parse --show-prefix'
>> +from the original current directory. See linkgit:git-rev-parse[1].
> 
> It made me wonder "but then why should not we just do GIT_PREFIX=`git
> rev-parse --show-prefix` in the alias?" then realized by the time we
> do that, it's already too late. Maybe
> 
>> +'GIT_PREFIX' is set as returned by running 'git rev-parse --show-prefix'
>> +before current directory is changed to top-level directory. See linkgit:git-rev-parse[1].
> 
> ?

Well, the text says already that the alias is not run from the "original
current directory", so it's two ways of saying the same thing.

Michael

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2011-04-27  9:36 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <4DA59B27.50506@ge.infn.it>
2011-04-13 13:05 ` [RFC/PATCH] handle_alias: provide GIT_CWD to !alias Michael J Gruber
2011-04-13 13:12   ` Nguyen Thai Ngoc Duy
2011-04-13 13:16     ` Michael J Gruber
2011-04-13 13:24       ` Nguyen Thai Ngoc Duy
2011-04-26 22:22   ` Junio C Hamano
2011-04-27  6:47     ` Michael J Gruber
2011-04-27  7:40       ` Nguyen Thai Ngoc Duy
2011-04-27  8:10         ` Michael J Gruber
2011-04-27  8:36           ` [PATCH 1/2] t1020: test !alias in subdirectory Michael J Gruber
2011-04-27  8:36             ` [PATCH 2/2] handle_alias: provide GIT_PREFIX to !alias Michael J Gruber
2011-04-27  9:06               ` Nguyen Thai Ngoc Duy
2011-04-27  9:36                 ` Michael J Gruber

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 NNTP newsgroup(s).