gitgit-config

gitconfig shell-command aliasing runs from top-level rather than current directory


I've written a bash script called git_diff_parent which, when passed a commit handle, performs git diff commit~1 commit; in other words, it diffs with its ancestor without having to specify commit~1 on the command line. I do this by parsing the git command line, finding which argument is the commit string (using git cat-file -t), replacing it with commit~1 commit then passing the new argument list to git diff. I've aliased this script in /etc/gitconfig so I can execute this command as git diff-parent:

[alias]
   diff-parent = !git_diff_parent

This almost works except when I try to diff an individual file from a place other than the top-level repository directory, e.g.:

% pwd
/home/myhome/github/my-branch/rtl
% git diff-parent branch -- mycode.sv
<no output>
% git diff-parent branch -- rtl/mycode.sv
<diff output appears>

To debug this, I added pwd to my script to see the script's working directory. Lo and behold, it always launches from the top-level directory (/home/myhome/github/my-branch) rather than the subdirectory I'm in. I've even tried git -C . diff-parent ... to no avail.

So is there any way to make my alias launch from the current working directory?


Solution

  • https://git-scm.com/docs/git-config#Documentation/git-config.txt-alias

    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 … from the original current directory.

    (Emphasize and cut are mine — phd)

    So you can do cd $GIT_PREFIX in git_diff_parent shell script.