Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 with some_other_subdir/Base.cpp parameter I get the following result
  • NOTE: pwd inside git alias will return root_dir
like image 379
Patryk Avatar asked Sep 01 '15 08:09

Patryk


1 Answers

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"
like image 86
René Link Avatar answered Nov 14 '22 23:11

René Link