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?
GIT_PREFIX
is unset if you run from the top-level directory, so you need to catch that condition with something likecd ${GIT_PREFIX:-.}
so it "changes" to the current directory in that case. – Premise