#!/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 "The filesystem must support hardlinks." 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" ZIT_DIR=".zit.$ZIT_FILE" } zit_init() { zit_setup $1 test -e $ZIT_DIR && abort "$ZIT_DIR exists, is $ZIT_FILE tracked already?" mkdir $ZIT_DIR && echo "Initializing Zit repository in $ZIT_DIR" test -d $ZIT_DIR || abort "Failed to create $ZIT_DIR" cd $ZIT_DIR git init || abort "Failed to initialize Git repository in $ZIT_DIR" ln ../$ZIT_FILE $ZIT_FILE || abort "Failed to link $ZIT_FILE into $ZIT_DIR" git add $ZIT_FILE || abort "Failed to add $ZIT_FILE" git commit "$@" || abort "Failed to make first commit for $ZIT_FILE" } zit_list() { for file in `find -path ./.zit.\*/.git`; do export GIT_DIR="$file" file="${file#./.zit.}" 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 cd $ZIT_DIR git $cmd "$@" $1 ;; esac