Skip to content

Instantly share code, notes, and snippets.

@mub
Created November 5, 2012 21:55
Show Gist options
  • Save mub/4020638 to your computer and use it in GitHub Desktop.
Save mub/4020638 to your computer and use it in GitHub Desktop.
Git Trail
#!/usr/bin/zsh
# rename a file from a subdirectory on the Git path.
NAME_FROM="$1"
NAME_TO="$2"
[[ -z $NAME_FROM || -z $NAME_TO ]] && { echo "Need name from and name to"; return 1 }
[[ -f $NAME_FROM ]] || { echo "$NAME_FROM is not a file"; return 2 }
[[ -f $NAME_TO ]] && { echo "File $NAME_TO exists"; return 3 }
function findgitroot {
local ORIG_DIR; ORIG_DIR=$PWD
GIT_ROOT=''
GIT_XTRA=''
local PATH_DIV; PATH_DIV=''
while [[ ! -d .git ]]; do
if [[ "$PWD" == '/' ]]; then
cd $ORIG_DIR
return 1
fi
GIT_XTRA="`basename $PWD`$PATH_DIV$GIT_XTRA"
PATH_DIV='/'
cd ..
done
export GIT_ROOT="$PWD"
cd $ORIG_DIR
}
findgitroot || { echo "Not in a git path"; return 4 }
echo "renaming $GIT_ROOT/$GIT_XTRA/$NAME_FROM -> $GIT_ROOT/$GIT_XTRA/$NAME_TO from the root $GIT_ROOT, xtra $GIT_XTRA"
ORIG_DIR=$PWD
cd $GIT_ROOT
git mv $GIT_XTRA/$NAME_FROM $GIT_XTRA/$NAME_TO
cd $ORIG_DIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment