#!/bin/bash abort() { echo $1 exit 1 } cmd="$1" shift USAGE="usage: zit COMMAND FILE [ARGS...]" zit_help() { cmd="$1" shift case $cmd in git) git help "$@" ;; *) echo $USAGE echo "" echo "Set up a git repository under .zit.FILE to track changes for FILE." echo "File must be a regular file and in the current directory." echo "" echo "Zit commands:" echo " init Synonym for track" echo " list Synonym for tracked" echo " track Start tracking changes to FILE" echo " tracked List tracked files in current directory" echo "" echo "See 'zit help git' or 'git help' for git commands." ;; esac } zit_setup() { ZIT_FILE="$1" test $ZIT_FILE || abort "Please specify a file" shift test -f $ZIT_FILE || abort "No such file $ZIT_FILE" test $ZIT_FILE = "`basename $ZIT_FILE`" || abort "Sorry, Zit only works on files in the current directory" export GIT_DIR=".$ZIT_FILE.git" export GIT_WORK_TREE="`pwd`" } zit_init() { zit_setup $1 test -e $GIT_DIR && abort "$GIT_DIR exists, is $ZIT_FILE tracked already?" mkdir $GIT_DIR && echo "Initializing Zit repository in $GIT_DIR" test -d $GIT_DIR || abort "Failed to create $GIT_DIR" git init || abort "Failed to initialize Git repository in $GIT_DIR" echo "*" > $GIT_DIR/info/exclude git add -f $ZIT_FILE || abort "Failed to add $ZIT_FILE" git commit "$@" || abort "Failed to make first commit for $ZIT_FILE" } zit_list() { export GIT_WORK_TREE="`pwd`" for file in .*.git; do if ! test -e $file; then echo "(no files tracked by zit)" break fi export GIT_DIR="$file" file="${file#.}" file="${file%.git}" # TODO show actual file status git ls-files -t $file done; } case $cmd in "") echo $USAGE ;; help) zit_help "$@" ;; init|track) zit_init $1 ;; list|tracked) zit_list ;; *) zit_setup $1 git $cmd "$@" $1 ;; esac