The script in
using git and vi
together
was a little bit borked, as too many git repo’s were created. Hence a
new and improved version. This one will look up the directory tree to
spot an existing git repository - is nothing found a new one will be
created in $PWD.
code
#!/bin/zsh
# a wrapper around git and vi
# expands $Hash$ to $Hash: file short_hash epoch committer $
# git checkout HEAD $file? when would I need this
# TODO: spaces in filename
[[ ! -x =git ]] && exit 1
who=$SUDO_USER
who=${who:-$LOGNAME}
function search_git_dir {
gpath="$1"
[[ -d "$gpath/.git" ]] && echo "$gpath" && return
[[ -z "$gpath" ]] && echo "" && return
# strip that last path component and try again
search_git_dir "${gpath%/*}"
}
for file in "$@"; do
dir=$(dirname $file); cd $dir
base=$(basename $file)
if [[ -z $(search_git_dir $PWD) ]]; then
# make a new one in $PWD
git init || exit 1
else
#echo FOUND ONE
fi
chmod +w $base 2> /dev/null
if vi $base; then
[[ ! -e $base ]] && exit 0
git add $base
# collapse $Hash: id $ line
sed -i -e 's/\$Hash:.*\$/$Hash$/' $base
git commit $base
fi
id=$(git-show -s --pretty=format:$base\ %h\ %ct\ $who%n -- $base)
[[ -z $id ]] && exit 1
# re-add $Hash: sha1hash$ line
sed -i -e 's/\$Hash\$'/\$Hash:\ $id\ \$/ $base
chmod a-w $base 2> /dev/null
done
manpage And ofcourse the manpage.