Skip to contentSkip to conversation

handle_alias: provide GIT_CWD to !alias

RFC/PATCH In review Michael J Gruber 3 participants 12 messages Jun 15, 2016 1 message not in the archive Documentation

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive

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.

---
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(-)
git.c +9 -1
index ef598c3..c8b9189 100644
@@ -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);
 
Signature · 1 line
1.7.5.rc0.365.g23929a
#2 lore raw
Quoted 10 lines · Michael J Gruber

-                       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.

Signature · 1 line
Duy
#3 lore raw

Nguyen Thai Ngoc Duy venit, vidit, dixit 13.04.2011 15:12:

Quoted 13 lines

git.c static int handle_alias(int *argcp, const char ***argv) · line 202

Quoted 10 lines · Michael J Gruber
git.c +7 -1
-                       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);
Quoted 1 line

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...

Quoted 2 lines

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

#4 lore raw
Quoted 14 lines · Michael J Gruber

Nguyen Thai Ngoc Duy venit, vidit, dixit 13.04.2011 15:12:

Quoted 13 lines
Quoted 10 lines · Michael J Gruber

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

Quoted 3 lines
Quoted 1 line

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.

Quoted 7 lines
Quoted 2 lines

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.

Signature · 1 line
Duy
#5 lore raw
In reply to Michael J Gruber
Quoted 3 lines · Michael J Gruber

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?

#6 lore raw

Junio C Hamano venit, vidit, dixit 27.04.2011 00:22:

Quoted 7 lines
Quoted 3 lines · Michael J Gruber

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

#7 lore raw
Quoted 3 lines · Michael J Gruber

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.

Signature · 1 line
Duy
#8 lore raw

Nguyen Thai Ngoc Duy venit, vidit, dixit 27.04.2011 09:40:

Quoted 8 lines
Quoted 3 lines · Michael J Gruber

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

#9 lore raw

PATCH 1/2 t1020: test !alias in subdirectory

---
 t/t1020-subdirectory.sh |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
t/t1020-subdirectory.sh +11 -0
index 1fd187c..66e40d3 100755
@@ -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 &&
 	(
Signature · 1 line
1.7.5.270.gafca7
#10 lore raw

PATCH 2/2 handle_alias: provide GIT_PREFIX to !alias

In reply to Michael J Gruber

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.

---
 Documentation/config.txt |    2 ++
 git.c                    |   10 +++++++++-
 t/t1020-subdirectory.sh  |   10 ++++++++++
 3 files changed, 21 insertions(+), 1 deletions(-)
Documentation/config.txt +2 -0
index 750c86d..66281dd 100644
@@ -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
git.c +9 -1
index ef598c3..ed89951 100644
@@ -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);
 
t/t1020-subdirectory.sh +10 -0
index 66e40d3..ddc3921 100755
@@ -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 &&
 	(
Signature · 1 line
1.7.5.270.gafca7
#11 lore raw

Re: [PATCH 2/2] handle_alias: provide GIT_PREFIX to !alias

2011/4/27 Michael J Gruber <[email protected]>:

Quoted 6 lines
(quoted diff) +0 -0
@@ -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

Quoted 2 lines

+'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].

?

Signature · 1 line
Duy
#12 lore raw

Re: [PATCH 2/2] handle_alias: provide GIT_PREFIX to !alias

Nguyen Thai Ngoc Duy venit, vidit, dixit 27.04.2011 11:06:

Quoted 12 lines

2011/4/27 Michael J Gruber <[email protected]>:

Documentation/config.txt it will be treated as a shell command. For example, defining · line 593

Quoted 6 lines
Documentation/config.txt +2 -0
@@ -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

Quoted 4 lines

Review it will be treated as a shell command. For example, defining · line 595

Quoted 2 lines
(quoted diff) +2 -0
+'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