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:
- having file
root_dir/src/some_other_subdir/Base.cpp
- when I'm in
root_dir/src
and run my alias withsome_other_subdir/Base.cpp
parameter I get the following result - NOTE:
pwd
inside git alias will returnroot_dir
diff-between
alias into something like this?"!h() { echo $(pwd); git diff $(git merge-base $2 $1) $2 -- $(git rev-parse --show-toplevel)\/$3 ; }; h"
– Rascality$(git rev-parse --show-toplevel)
this will show the absolute path to root dir of repo which is easy to get e.g. by$(pwd)
since this will be the directory from which the commands will be executed. – Sheba