I have the following aliases in my .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"
While it works fine in most of the cases the problem arises when I would like to get a log of a file (or a path) passing in the third parameter ($3
) to diff-between.
The issue is that the command (h
in this example) is being executed in the root directory of this repository (that's why I added pwd
printing for checking) and I am passing in the name of the file or a path with a name but not always the absolute path.
E.g.
When I run this alias (added command printing to check to what command it will expand):
diff-between = "!h() { echo $(pwd); echo "git diff $(git merge-base $2 $1) $2 -- $3" ; }; h"
under repo_root_dir/src/
like so:
git diff-between origin/master my_branch
I will get:
git diff 66efa91ba8c74aa624d05d9ec0b13cc93cbfb6d3 my_branch --
which is fine. But when I run this like so (with a 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
regardless of where this command has been run from (NOTE: bear in mind that this is run from the root_dir of this repo)
How to properly get the current directory so that:
root_dir/src/some_other_subdir/Base.cpp
root_dir/src
and run my alias with some_other_subdir/Base.cpp
parameter I get the following resultpwd
inside git alias will return root_dir
Git aliases are normally executed within the current directory. Except git aliases that execute in a shell (start with !
). If a shell is started the shell's working dir will be the repository root.
Use the ${GIT_PREFIX:-./}
variable. It will resolve to the absolute repository path of the current directory.
diff-between = "!h() { git diff $(git merge-base $2 $1) $2 -- ${GIT_PREFIX:-./}$3 ; }; h"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With