Zit track = init, tracked = list
[zit] / zit
1 #!/bin/bash
2
3 abort() {
4         echo $1
5         exit 1
6 }
7
8 cmd="$1"
9 shift
10
11 zit_setup() {
12         ZIT_FILE="$1"
13         test $ZIT_FILE || abort "Please specify a file"
14         shift
15         test -f $ZIT_FILE || abort "No such file $ZIT_FILE"
16         test $ZIT_FILE = "`basename $ZIT_FILE`" || abort "Sorry, Zit only works on files in the current directory"
17         ZIT_DIR=".zit.$ZIT_FILE"
18 }
19
20 zit_init() {
21         zit_setup $1
22         test -e $ZIT_DIR && abort "$ZIT_DIR exists, is $ZIT_FILE tracked already?"
23         mkdir $ZIT_DIR && echo "Initializing Zit repository in $ZIT_DIR"
24         test -d $ZIT_DIR || abort "Failed to create $ZIT_DIR"
25         cd $ZIT_DIR
26         git init || abort "Failed to initialize Git repository in $ZIT_DIR" 
27         ln ../$ZIT_FILE $ZIT_FILE || abort "Failed to link $ZIT_FILE into $ZIT_DIR"
28         git add $ZIT_FILE || abort "Failed to add $ZIT_FILE"
29         git commit "$@" || abort "Failed to make first commit for $ZIT_FILE"
30 }
31
32 zit_list() {
33         for file in `find -path ./.zit.\*/.git`; do
34                 export GIT_DIR="$file"
35                 file="${file#./.zit.}"
36                 file="${file%/.git}"
37                 # TODO show actual file status
38                 git ls-files -t $file
39         done;
40 }
41
42 case $cmd in
43         init|track)
44         zit_init $1
45         ;;
46         list|tracked)
47         zit_list
48         ;;
49         *)
50         zit_setup $1
51         cd $ZIT_DIR
52         git $cmd "$@" $1
53         ;;
54 esac