Zit list command
[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         shift
14         test -f $ZIT_FILE || abort "No such file $ZIT_FILE"
15         test $ZIT_FILE = "`basename $ZIT_FILE`" || abort "Sorry, Zit only works on files in the current directory"
16         ZIT_DIR=".zit.$ZIT_FILE"
17 }
18
19 zit_init() {
20         zit_setup $1
21         test -e $ZIT_DIR && abort "$ZIT_DIR exists, is $ZIT_FILE tracked already?"
22         mkdir $ZIT_DIR && echo "Initializing Zit repository in $ZIT_DIR"
23         test -d $ZIT_DIR || abort "Failed to create $ZIT_DIR"
24         cd $ZIT_DIR
25         git init || abort "Failed to initialize Git repository in $ZIT_DIR" 
26         ln ../$ZIT_FILE $ZIT_FILE || abort "Failed to link $ZIT_FILE into $ZIT_DIR"
27         git add $ZIT_FILE || abort "Failed to add $ZIT_FILE"
28         git commit "$@" || abort "Failed to make first commit for $ZIT_FILE"
29 }
30
31 zit_list() {
32         for file in `find -path ./.zit.\*/.git`; do
33                 export GIT_DIR="$file"
34                 file="${file#./.zit.}"
35                 file="${file%/.git}"
36                 # TODO show actual file status
37                 git ls-files -t $file
38         done;
39 }
40
41 case $cmd in
42         init)
43         zit_init $1
44         ;;
45         list)
46         zit_list
47         ;;
48 esac