Created
March 22, 2016 14:27
-
-
Save replsv/8d227cd2b6f84223af3a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# git-mv-with-history -- move/rename file or folder, with history. | |
# https://git.wiki.kernel.org/index.php/GitFaq#Why_does_Git_not_.22track.22_renames.3F | |
function usage() { | |
echo "How to use it: `basename $0` [-d/--dry-run] [-v/--verbose] <srcname>=<destname> <...> <...>" | |
[ -z "$1" ] || echo $1 | |
exit 1 | |
} | |
[ ! -d .git ] && usage "ERROR : No git dir found. Bye." | |
dryrun=0 | |
filter="" | |
verbose="" | |
repo=$(basename `git rev-parse --show-toplevel`) | |
while [[ $1 =~ ^\- ]]; do | |
case $1 in | |
-d|--dry-run) | |
dryrun=1 | |
;; | |
-v|--verbose) | |
verbose="-v" | |
;; | |
*) | |
usage "Invalid argument: $1" | |
esac | |
shift | |
done | |
for arg in $@; do | |
val=`echo $arg | grep -q '=' && echo 1 || echo 0` | |
src=`echo $arg | sed 's/\(.*\)=\(.*\)/\1/'` | |
dst=`echo $arg | sed 's/\(.*\)=\(.*\)/\2/'` | |
dir=`echo $dst | grep -q '/$' && echo $dst || dirname $dst` | |
[ "$val" -ne 1 ] && usage | |
[ ! -e "$src" ] && usage "error: $src does not exist" | |
filter="$filter \n\ | |
if [ -e \"$src\" ]; then \n\ | |
echo \n\ | |
if [ ! -e \"$dir\" ]; then \n\ | |
mkdir -p ${verbose} \"$dir\" && echo \n\ | |
fi \n\ | |
mv $verbose \"$src\" \"$dst\" \n\ | |
fi \n\ | |
" | |
done | |
[ -z "$filter" ] && usage | |
if [[ $dryrun -eq 1 || ! -z $verbose ]]; then | |
echo | |
echo "tree-filter to execute against $repo:" | |
echo -e "$filter" | |
fi | |
[ $dryrun -eq 0 ] && git filter-branch -f --tree-filter "`echo -e $filter`" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment