Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

I am on the git root folder, whose absolute path is /path/project/. The folder structure is:

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

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

But using absolute path causes git to try deleting my original folder instead

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

How can I force git to remove the symbolic link using absolute path without causing it to try deleting my original directory?

like image 977
Lim H. Avatar asked Nov 09 '13 10:11

Lim H.


1 Answers

The best I've been able to come up with for this is a 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"

You can then do

git rma /path/project/alib_link

from anywhere within your repository, with the desired effect.


How it works:

  • The Git alias rma calls the shell function f.
  • This streams $1 (the argument to git rma) to Perl using echo. Perl reads it in using <>.
  • The Perl variable $a is used to refer to the file you're trying to remove using its absolute path.
  • The Perl variable $r is used to refer to the file you're trying to remove using its path relative to the working directory. Note that Git aliases that run shell commands use the repository root as their working directory.
  • The relative path is "returned" from Perl by printing it. It is then stored in the shell variable $r.
  • Finally, we run git rm $r to actually remove the file. Note that this also runs in the working directory (i.e. the repository root).

A more concise version might be:

rma = "!f() { git rm `perl -e 'use Path::Class; print file($ARGV[0])->relative(\".\");' $1`; }; f"
like image 103
Stuart Golodetz Avatar answered Oct 06 '22 16:10

Stuart Golodetz