Skip to content

Instantly share code, notes, and snippets.

@xkr47
Last active February 14, 2016 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xkr47/63661c098f13d7a2bc21 to your computer and use it in GitHub Desktop.
Save xkr47/63661c098f13d7a2bc21 to your computer and use it in GitHub Desktop.
Git subcommand for moving/renaming paths using sed/perl expressions

Save file as git-mvs in your path and then:

find -name '*file' -print0 | xargs -r0 git mvs 's/foo/bar/g'

or in zsh:

git mvs 's/foo/bar/g' **/*file

When run against a git tree like this:

/
├ /foodir
│ ├ /foofile
│ └ /otherfile
└ /otherdir
  ├ /foofile
  └ /otherfile

then the result is:

/
├ /bardir
│ ├ /barfile
│ └ /otherfile
└ /otherdir
  ├ /barfile
  └ /otherfile
#!/bin/bash
if [ $# -lt 1 ]; then
echo usage: "$0 <perl> [<file1> ...]"
exit 1
fi
sed="$1"
shift
try () {
if ! "$@" ; then
echo ERROR!!
exit 1
fi
}
for old ; do
new="`echo -n $old | perl -pe "$sed"`"
if [ "$old" = "$new" ]; then
echo "Filename unchanged: $old"
else
echo "$old -> $new"
mkdir -p "`dirname \"$new\"`"
try git mv "$old" "$new"
rmdir -p "`dirname \"$old\"`"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment