zit import command
[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                 list|tracked)
21                 echo "usage: zit list"
22                 echo "Show tracked files, with a one-letter prefix indicating their status:"
23                 echo "   H   cached"
24                 echo "   M   unmerged"
25                 echo "   R   removed/deleted"
26                 echo "   C   modified/changed"
27                 echo "   K   to be killed"
28                 echo "   ?   other"
29                 ;;
30                 import)
31                 echo "usage: zit import FILE"
32                 echo "Import history from an RCS-tracked file. Requires rcs-fast-export."
33                 ;;
34                 *)
35                 echo $USAGE
36                 echo ""
37                 echo "Set up a git repository under .zit.FILE to track changes for FILE."
38                 echo "File must be a regular file and in the current directory."
39                 echo ""
40                 echo "Zit commands:"
41                 echo "   import Import RCS history for FILE"
42                 echo "   init           Synonym for track"
43                 echo "   list           Synonym for tracked"
44                 echo "   track  Start tracking changes to FILE"
45                 echo "   tracked        List tracked files in current directory"
46                 echo ""
47                 echo "See 'zit help git' or 'git help' for git commands."
48                 ;;
49         esac
50 }
51
52 ZIT_DIR=.zit
53
54 zit_setup() {
55         ZIT_FILE="$1"
56         test $ZIT_FILE || abort "Please specify a file"
57         test -f $ZIT_FILE || abort "No such file $ZIT_FILE"
58         test $ZIT_FILE = "`basename $ZIT_FILE`" || abort "Sorry, Zit only works on files in the current directory"
59
60         export GIT_WORK_TREE="`pwd`"
61
62         # first, check if a repo exists already, looking for
63         # .zit/file.git or .file.git, in that order
64         # if neither is found, and .zit exists, set the repo dir
65         # to .zit/file.git, otherwise set it to .file.git
66         GIT_DIR="$ZIT_DIR/$ZIT_FILE.git"
67         if ! test -d "$GIT_DIR"; then
68                 GIT_DIR=".$ZIT_FILE.git"
69                 if ! test -d "$GIT_DIR"; then
70                         test -d "$ZIT_DIR" && GIT_DIR="$ZIT_DIR/$ZIT_FILE.git"
71                 fi
72         fi
73         export GIT_DIR
74 }
75
76 # initialize the zitdir, without actually making the first commit
77 zitdir_init() {
78         zit_setup $1
79         test -e $GIT_DIR && abort "$GIT_DIR exists, is $ZIT_FILE tracked already?"
80         mkdir $GIT_DIR && echo "Initializing Zit repository in $GIT_DIR"
81         test -d $GIT_DIR || abort "Failed to create $GIT_DIR"
82         git init || abort "Failed to initialize Git repository in $GIT_DIR"
83         rm -rf $GIT_DIR/{hooks,info,branches,refs/tags,objects/pack,description}
84         if test -d "$ZIT_DIR"; then
85                 ZIT_EXCLUDE="$ZIT_DIR/exclude"
86         else
87                 ZIT_EXCLUDE="$GIT_DIR/exclude"
88         fi
89         if ! test -f "$ZIT_EXCLUDE"; then
90                 touch "$ZIT_EXCLUDE" || abort "Cannot create $ZIT_EXCLUDE file"
91                 echo "# Ignore patterns used by Zit repositories in the parent worktree." > "$ZIT_EXCLUDE"
92                 echo "# By default it's the single '*' glob, since we want to ignore all" >> "$ZIT_EXCLUDE"
93                 echo "# non-tracked files in the work-tree." >> "$ZIT_EXCLUDE"
94                 echo "# This file is autogenerated and there's usually no need to edit it." >> "$ZIT_EXCLUDE"
95                 echo "*" >> "$ZIT_EXCLUDE"
96         fi
97         git config core.excludesfile "$ZIT_EXCLUDE"
98 }
99
100 zit_init() {
101         if test "$1"; then
102                 zitdir_init "$1"
103                 git add -f $ZIT_FILE || abort "Failed to add $ZIT_FILE"
104                 git commit "$@" || abort "Failed to make first commit for $ZIT_FILE"
105         else
106                 if test -d "$ZIT_DIR"; then
107                         echo "$ZIT_DIR exists already"
108                         exit
109                 fi
110                 test -e $ZIT_DIR && abort "$ZIT_DIR exists but it's not a directory, cannot continue"
111                 mkdir $ZIT_DIR
112         fi
113 }
114
115 zit_list() {
116         export GIT_WORK_TREE="`pwd`"
117         GIT_DIR=""
118         for file in "$ZIT_DIR"/*.git .*.git; do
119                 if ! test -e $file; then
120                         continue
121                 fi
122                 export GIT_DIR="$file"
123                 file="${file#.}"
124                 file="${file#zit/}"
125                 file="${file%.git}"
126                 (git ls-files -m -d -t; git ls-files -t) | uniq -f 1
127         done;
128         # if $GIT_DIR is empty, no files were found
129         test "$GIT_DIR" || echo "(no files tracked by zit)"
130 }
131
132 # import an RCS-tracked file using rcs-fast-export, if found
133 zit_import() {
134         which rcs-fast-export || abort "rcs-fast-export not found, I can't import RCS-tracked files, sorry"
135         zitdir_init $1
136         # git-fast-import creates a pack file, so (re)build the objects/pack dir
137         mkdir -p $GIT_DIR/objects/pack
138         rcs-fast-export $1 | git-fast-import
139         # for some reason, rcs-fast-export | git-fast-import leaves the original
140         # file in 'deleted' state, a situation which is easily fixed by adding
141         # it back
142         git add -f $1
143 }
144
145 case $cmd in
146         "")
147         echo $USAGE
148         ;;
149         help)
150         zit_help "$@"
151         ;;
152         init|track)
153         zit_init $1
154         ;;
155         list|tracked)
156         zit_list
157         ;;
158         import)
159         zit_import $1
160         ;;
161         # Most commands will work with the generic catch-all mechanism used
162         # below, but some of them require a more thorough analysis of the
163         # parameters to decide whether $ZIT_FILE should be put back into the
164         # parameter list or not. For example,
165         # $ zit commit somefile
166         # wouldn't do what one expects it to do, unless 'add' is run first,
167         # (except that
168         # $ zit add somefile
169         # wouldn't work either), however
170         # $ zit commit somefile -a
171         # would work correctly. So we handle some commands separately (for the
172         # moment just add and commit)
173         add|commit)
174         zit_setup $1
175         shift
176         git $cmd "$@" "$ZIT_FILE"
177         ;;
178         # the raw<command> method can be used to not replicate $ZIT_FILE in the
179         # parameter list
180         raw*)
181         zit_setup $1
182         shift
183         git ${cmd#raw} "$@"
184         ;;
185         *)
186         zit_setup $1
187         shift
188         git $cmd "$@"
189         ;;
190 esac