If an external process is started via Git alias, then it is executed from the root of the workspace (the one where .git / usually lies and which is returned by the git rev-parse --show-toplevel )

What if the path from which alias starts is important? How can the process being started know where alias came from?

  • from the root directory - the root directory is not there. maybe better: from the root of a working copy ? - aleksandr barakin

1 answer 1

For this, the running process is available environment variable (environment variable) GIT_PREFIX .

Starting with Git 1.7.6 :

Processes running via " [alias] <name> = !process " can use the GIT_PREFIX environment GIT_PREFIX

git config :

The variable GIT_PREFIX assigned the value obtained from the expression ' git rev-parse --show-prefix ', executed in the original current directory.

Note that GIT_PREFIX contains a relative path, i.e. from the root of the workspace to the original directory. If you pass this value to the Git command, then this is just enough.

If alias is executed from the workspace root, then GIT_PREFIX is undefined. To guarantee a valid path, it is better to use it with the expression ${GIT_PREFIX:-.} : When started from the root directory, this expression is equal to ' . '

An example of using tests from Git, t/t1020-subdirectory.sh :

 test_expect_success 'GIT_PREFIX for !alias' ' printf "dir/" >expect && ( git config alias.test-alias-directory "!sh -c \"printf \$GIT_PREFIX\"" && cd dir && git test-alias-directory >../actual ) && test_cmp expect actual ' 

This is a translation of the answer from EN.SO