Skip to content

Commit 7cf16a1

Browse files
Michael J Grubergitster
authored andcommitted
handle_alias: provide GIT_PREFIX to !alias
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0daed41 commit 7cf16a1

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Documentation/config.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,8 @@ it will be treated as a shell command. For example, defining
589589
"gitk --all --not ORIG_HEAD". Note that shell commands will be
590590
executed from the top-level directory of a repository, which may
591591
not necessarily be the current directory.
592+
'GIT_PREFIX' is set as returned by running 'git rev-parse --show-prefix'
593+
from the original current directory. See linkgit:git-rev-parse[1].
592594

593595
am.keepcr::
594596
If true, git-am will call git-mailsplit for patches in mbox format

git.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ static int handle_alias(int *argcp, const char ***argv)
179179
if (alias_string[0] == '!') {
180180
const char **alias_argv;
181181
int argc = *argcp, i;
182+
struct strbuf sb = STRBUF_INIT;
183+
const char *env[2];
182184

183185
commit_pager_choice();
184186

@@ -189,7 +191,13 @@ static int handle_alias(int *argcp, const char ***argv)
189191
alias_argv[i] = (*argv)[i];
190192
alias_argv[argc] = NULL;
191193

192-
ret = run_command_v_opt(alias_argv, RUN_USING_SHELL);
194+
strbuf_addstr(&sb, "GIT_PREFIX=");
195+
if (subdir)
196+
strbuf_addstr(&sb, subdir);
197+
env[0] = sb.buf;
198+
env[1] = NULL;
199+
ret = run_command_v_opt_cd_env(alias_argv, RUN_USING_SHELL, NULL, env);
200+
strbuf_release(&sb);
193201
if (ret >= 0) /* normal exit */
194202
exit(ret);
195203

t/t1020-subdirectory.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ test_expect_success '!alias expansion' '
129129
test_cmp expect actual
130130
'
131131

132+
test_expect_success 'GIT_PREFIX for !alias' '
133+
printf "dir/" >expect &&
134+
(
135+
git config alias.test "!sh -c \"printf \$GIT_PREFIX\"" &&
136+
cd dir &&
137+
git test >../actual
138+
) &&
139+
test_cmp expect actual
140+
'
141+
132142
test_expect_success 'no file/rev ambiguity check inside .git' '
133143
git commit -a -m 1 &&
134144
(

0 commit comments

Comments
 (0)