How to get relative file path in git aliases with parameters?

I have the following aliases in mine .gitconfig:

[alias]
  lg = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
  log-between = "!f() { git lgl `git merge-base $2 $1`..$2 -- $3 ; }; f"
  log-between-p = "!g() { git lgl -p $(git merge-base $2 $1)..$2 -- $3 ; }; g"
  diff-between = "!h() { echo $(pwd); git diff $(git merge-base $2 $1) $2 -- $3 ; }; h"

Despite the fact that in most cases it works fine, the problem occurs when I want to get the log of a file (or path) passing in the third parameter ( $3) in order to differentiate.

The problem is that the command ( hin this example) is executed in the root directory of this repository (so I added pwdprint for verification) and I pass the file name or path with the name, but not always the full path.

eg.

When I run this alias (command print added to check which command will be expanded):

  diff-between = "!h() { echo $(pwd); echo "git diff $(git merge-base $2 $1) $2 -- $3" ; }; h"

under repo_root_dir/src/so:

git diff-between origin/master my_branch

I will get:

git diff 66efa91ba8c74aa624d05d9ec0b13cc93cbfb6d3 my_branch --

what well. But when I run it like this (with file):

git diff-between origin/master my_branch some_other_subdir/src/Base.cpp

I will get:

git diff 66efa91ba8c74aa624d05d9ec0b13cc93cbfb6d3 my_branch -- some_other_subdir/src/Base.cpp

, ( : , root_dir )


, :

  • root_dir/src/some_other_subdir/Base.cpp
  • root_dir/src some_other_subdir/Base.cpp,
  • : pwd git alias root_dir
+4
1

Git aliases . git , ( !). , dir .

${GIT_PREFIX:-./}. .

diff-between = "!h() { git diff $(git merge-base $2 $1) $2 -- ${GIT_PREFIX:-./}$3 ; }; h"
+5

All Articles