Git rm works on a relative path to a symbolic link, but not to an absolute path

I am in the git root folder whose absolute path is /path/project/ . Folder structure:

 /path/project ---- libs/alib (actual library folder) ---- exec/alib_link (symbolic link to the actual alib folder) 

I can remove the symlink with git rm using the relative path: git rm exec/alib_link

But using an absolute path causes git to try to delete the original folder instead

 git rm /path/project/alib_link fatal: not removing /path/project/libs/alib recursively without -r 

How can I get git to remove a symlink using an absolute path without forcing it to try to delete my source directory?

+6
source share
1 answer

The best I could come up with for this is the Git alias using Perl:

 rma = "!f() { r=`echo $1 | perl -e 'use Path::Class; $a=file(<>); $r=$a->relative(\".\"); print $r;'`; git rm $r; }; f" 

Then you can do

 git rma /path/project/alib_link 

from anywhere in your repository with the desired effect.


How it works:

  • Alias ​​Git rma calls the shell function f .
  • This thread passes $1 (the git rma argument) to Perl using echo . Perl reads it when using <> .
  • The Perl $a variable is used to refer to the file you are trying to delete using its absolute path.
  • The Perl $r variable is used to refer to the file you are trying to delete using its path relative to the working directory. Note that the Git aliases that run shell commands use the repository root as the working directory.
  • The relative path "comes back" from Perl, printing it. Then it is stored in the shell variable $r .
  • Finally, we run git rm $r to actually delete the file. Note that this also works in the working directory (i.e., the Root repository).

A shorter option might be:

 rma = "!f() { git rm `perl -e 'use Path::Class; print file($ARGV[0])->relative(\".\");' $1`; }; f" 
+3
source

Source: https://habr.com/ru/post/957731/


All Articles