zit-to-git command delegation fixes
[zit] / zit
1 #!/bin/bash
2
3 abort() {
4         echo $1
5         exit 1
6 }
7
8 cmd="$1"
9 shift
10
11 USAGE="usage: zit COMMAND FILE [ARGS...]"
12
13 zit_help() {
14         cmd="$1"
15         shift
16         case $cmd in
17                 git)
18                 git help "$@"
19                 ;;
20                 *)
21                 echo $USAGE
22                 echo ""
23                 echo "Set up a git repository under .zit.FILE to track changes for FILE."
24                 echo "File must be a regular file and in the current directory."
25                 echo ""
26                 echo "Zit commands:"
27                 echo "   init           Synonym for track"
28                 echo "   list           Synonym for tracked"
29                 echo "   track  Start tracking changes to FILE"
30                 echo "   tracked        List tracked files in current directory"
31                 echo ""
32                 echo "See 'zit help git' or 'git help' for git commands."
33                 ;;
34         esac
35 }
36
37 zit_setup() {
38         ZIT_FILE="$1"
39         test $ZIT_FILE || abort "Please specify a file"
40         test -f $ZIT_FILE || abort "No such file $ZIT_FILE"
41         test $ZIT_FILE = "`basename $ZIT_FILE`" || abort "Sorry, Zit only works on files in the current directory"
42         export GIT_DIR=".$ZIT_FILE.git"
43         export GIT_WORK_TREE="`pwd`"
44 }
45
46 zit_init() {
47         zit_setup $1
48         test -e $GIT_DIR && abort "$GIT_DIR exists, is $ZIT_FILE tracked already?"
49         mkdir $GIT_DIR && echo "Initializing Zit repository in $GIT_DIR"
50         test -d $GIT_DIR || abort "Failed to create $GIT_DIR"
51         git init || abort "Failed to initialize Git repository in $GIT_DIR" 
52         echo "*" > $GIT_DIR/info/exclude
53         git add -f $ZIT_FILE || abort "Failed to add $ZIT_FILE"
54         git commit "$@" || abort "Failed to make first commit for $ZIT_FILE"
55 }
56
57 zit_list() {
58         export GIT_WORK_TREE="`pwd`"
59         for file in .*.git; do
60                 if ! test -e $file; then
61                         echo "(no files tracked by zit)"
62                         break
63                 fi
64                 export GIT_DIR="$file"
65                 file="${file#.}"
66                 file="${file%.git}"
67                 # TODO show actual file status
68                 git ls-files -t $file
69         done;
70 }
71
72 case $cmd in
73         "")
74         echo $USAGE
75         ;;
76         help)
77         zit_help "$@"
78         ;;
79         init|track)
80         zit_init $1
81         ;;
82         list|tracked)
83         zit_list
84         ;;
85         # Most commands will work with the generic catch-all mechanism used
86         # below, but some of them require a more thorough analysis of the
87         # parameters to decide whether $ZIT_FILE should be put back into the
88         # parameter list or not. For example,
89         # $ zit commit somefile
90         # wouldn't do what one expects it to do, unless 'add' is run first,
91         # (except that
92         # $ zit add somefile
93         # wouldn't work either), however
94         # $ zit commit somefile -a
95         # would work correctly. So we handle some commands separately (for the
96         # moment just add and commit)
97         add|commit)
98         zit_setup $1
99         shift
100         git $cmd "$@" "$ZIT_FILE"
101         ;;
102         # the raw<command> method can be used to not replicate $ZIT_FILE in the
103         # parameter list
104         raw*)
105         zit_setup $1
106         shift
107         git ${cmd#raw} "$@"
108         ;;
109         *)
110         zit_setup $1
111         shift
112         git $cmd "$@"
113         ;;
114 esac