Clarify license terms and up version
[zit] / zit
1 #!/bin/bash
2
3 # Zit: the git-based single-file content tracker
4
5 # Copyright (C) 2008-2014 Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
6 # Licensed under the GNU GPL v2
7 # A full copy of the license text is not included with zit, because the
8 # program is self-contained.
9 # Refer to http://www.gnu.org/licenses/gpl-2.0.html for the full license terms
10
11 abort() {
12         echo $1 >&2
13         exit 1
14 }
15
16 me=$(basename "$0")
17 if test "$me" = zit ; then
18         cmd="$1"
19         shift
20 else
21         cmd="$me"
22 fi
23
24 USAGE="usage: zit COMMAND FILE [ARGS...]"
25 VERSION=0.2014.10.23
26
27 zit_help() {
28         cmd="$1"
29         shift
30         case "$cmd" in
31         git)
32                 git help "$@"
33                 ;;
34         list|tracked)
35                 echo "usage: zit list"
36                 echo "Show tracked files, with a one-letter prefix indicating their status:"
37                 echo "   H   cached"
38                 echo "   M   unmerged"
39                 echo "   R   removed/deleted"
40                 echo "   C   modified/changed"
41                 echo "   K   to be killed"
42                 echo "   ?   other"
43                 ;;
44         import)
45                 echo "usage: zit import FILE"
46                 echo "Import history from an RCS-tracked file. Requires rcs-fast-export."
47                 ;;
48         view)
49                 echo "usage: zit view FILE"
50                 echo "Browse FILE's history with gitk (if possible) or tig."
51                 ;;
52         with)
53                 echo "usage: zit with FILE COMMAND..."
54                 echo "Run COMMAND after setting up the git environment for FILE."
55                 ;;
56         clone)
57                 echo "usage: zit clone REPO [FILE]"
58                 echo "Create and track FILE, retrieving its history from repository REPO."
59                 echo "FILE is guessed by REPO by stripping the '.git' suffix from the last path component"
60                 ;;
61         *)
62                 echo $USAGE
63                 echo ""
64                 echo "Set up a git repository under .FILE.git to track changes for FILE."
65                 echo "File must be a regular file and in the current directory."
66                 echo ""
67                 echo "Zit commands:"
68                 echo "   add            Stage changes or start tracking changes to FILE"
69                 echo "   clone  Clone and track a remote file"
70                 echo "   import Import RCS history for FILE"
71                 echo "   init           Synonym for track"
72                 echo "   list           Synonym for tracked"
73                 echo "   track  Start tracking changes to FILE"
74                 echo "   tracked        List tracked files in current directory"
75                 echo "   view           Browse FILE's history with gitk or tig"
76                 echo "   with           Run a command in FILE's context"
77                 echo ""
78                 echo "See 'zit help git' or 'git help' for git commands."
79                 ;;
80         zig)
81                 echo "usage: zig FILE"
82                 echo "Browse FILE's history with tig."
83                 ;;
84         zik)
85                 echo "usage: zik FILE"
86                 echo "Browse FILE's history with gitk."
87                 ;;
88         esac
89 }
90
91 ZIT_DIR=.zit
92
93 zit_setup() {
94         ZIT_FILE="$1"
95         test "$ZIT_FILE" || abort "Please specify a file"
96         test -f "$ZIT_FILE" || abort "No such file $ZIT_FILE"
97         test "$ZIT_FILE" = $(basename "$ZIT_FILE") || abort "Sorry, Zit only works on files in the current directory"
98
99         export GIT_WORK_TREE=$(pwd)
100
101         # first, check if a repo exists already, looking for
102         # .zit/file.git or .file.git, in that order
103         # if neither is found, and .zit exists, set the repo dir
104         # to .zit/file.git, otherwise set it to .file.git
105         GIT_DIR="$ZIT_DIR/$ZIT_FILE.git"
106         if ! test -d "$GIT_DIR"; then
107                 GIT_DIR=".$ZIT_FILE.git"
108                 if ! test -d "$GIT_DIR"; then
109                         test -d "$ZIT_DIR" && GIT_DIR="$ZIT_DIR/$ZIT_FILE.git"
110                 fi
111         fi
112         export GIT_DIR
113 }
114
115 # initialize the zitdir, without actually making the first commit
116 zitdir_init() {
117         zit_setup $1
118         test -e "$GIT_DIR" && abort "$GIT_DIR exists, is $ZIT_FILE tracked already?"
119         mkdir "$GIT_DIR" && echo "Initializing Zit repository in $GIT_DIR"
120         test -d "$GIT_DIR" || abort "Failed to create $GIT_DIR"
121         git init || abort "Failed to initialize Git repository in $GIT_DIR"
122         rm -rf "$GIT_DIR"/{hooks,info,branches,refs/tags,objects/pack,description}
123         if test -d "$ZIT_DIR"; then
124                 ZIT_EXCLUDE="$ZIT_DIR/exclude"
125         else
126                 ZIT_EXCLUDE="$GIT_DIR/exclude"
127         fi
128         if ! test -f "$ZIT_EXCLUDE"; then
129                 touch "$ZIT_EXCLUDE" || abort "Cannot create $ZIT_EXCLUDE file"
130                 echo "# Ignore patterns used by Zit repositories in the parent worktree." > "$ZIT_EXCLUDE"
131                 echo "# By default it's the single '*' glob, since we want to ignore all" >> "$ZIT_EXCLUDE"
132                 echo "# non-tracked files in the work-tree." >> "$ZIT_EXCLUDE"
133                 echo "# This file is autogenerated and there's usually no need to edit it." >> "$ZIT_EXCLUDE"
134                 echo "*" >> "$ZIT_EXCLUDE"
135         fi
136         git config core.excludesfile "$ZIT_EXCLUDE"
137 }
138
139 zitdir_check() {
140         if test -f "$ZIT_DIR"; then
141                 abort "$ZIT_DIR exists but it's not a directory, cannot continue"
142         fi
143 }
144
145 zit_init() {
146         if test -n "$1"; then
147                 zitdir_init "$1"
148                 git add -f "$ZIT_FILE" || abort "Failed to add $ZIT_FILE"
149                 git commit "$@" || abort "Failed to make first commit for $ZIT_FILE"
150         else
151                 if test -d "$ZIT_DIR"; then
152                         echo "$ZIT_DIR exists already"
153                         exit
154                 fi
155                 zitdir_check
156                 mkdir "$ZIT_DIR"
157         fi
158 }
159
160 zit_add() {
161         # add the file if it is tracked, otherwise zit init
162         zitdir_check
163         zit_setup "$1"
164
165         if test -d "$GIT_DIR"; then
166                 git add "$1"
167         else
168                 zit_init "$1"
169         fi
170 }
171
172 zit_list() {
173         export GIT_WORK_TREE="$(pwd)"
174         GIT_DIR=""
175         for file in "$ZIT_DIR"/*.git .*.git; do
176                 if ! test -e $file; then
177                         continue
178                 fi
179                 export GIT_DIR="$file"
180                 file="${file#.}"
181                 file="${file#zit/}"
182                 file="${file%.git}"
183                 (git ls-files -m -d -t; git ls-files -t) | uniq -f 1
184         done;
185         # if $GIT_DIR is empty, no files were found
186         test "$GIT_DIR" || echo "(no files tracked by zit)"
187 }
188
189 # import an RCS-tracked file using rcs-fast-export, if found
190 zit_import() {
191         which rcs-fast-export || abort "rcs-fast-export not found, I can't import RCS-tracked files, sorry"
192         zitdir_init "$1"
193         # git-fast-import creates a pack file, so (re)build the objects/pack dir
194         mkdir -p "$GIT_DIR/objects/pack"
195         rcs-fast-export "$1" | git-fast-import
196         # for some reason, rcs-fast-export | git-fast-import leaves the original
197         # file in 'deleted' state, a situation which is easily fixed by adding
198         # it back
199         git add -f "$1"
200 }
201
202 zit_clone() {
203         SRC="$1"
204         test -n "$SRC" || abort "Where do you want to clone from?"
205         if [ -n "$2" ]; then
206                 ZIT_FILE="$2"
207         else
208                 ZIT_FILE=$(basename "$SRC" .git)
209         fi
210         test -e "$ZIT_FILE" && abort "File $ZIT_FILE exists already"
211         test "$ZIT_FILE" = $(basename $ZIT_FILE) || abort "Sorry, Zit only works on files in the current directory"
212         touch "$ZIT_FILE" # to make zit_setup happy
213         zitdir_init "$ZIT_FILE"
214
215         git remote add origin "$SRC"
216         git remote update
217         rm "$ZIT_FILE"
218         git checkout -b master origin/master
219 }
220
221 case "$cmd" in
222 "")
223         echo $USAGE
224         ;;
225 version|--version)
226         echo "zit version $VERSION"
227         git --version
228         ;;
229 help|--help|-h|-?)
230         zit_help "$@"
231         ;;
232 add)
233         zit_add "$1"
234         ;;
235 init|track)
236         zit_init "$1"
237         ;;
238 clone)
239         zit_clone "$@"
240         ;;
241 list|ls|tracked)
242         zit_list
243         ;;
244 import)
245         zit_import "$1"
246         ;;
247 view)
248         zit_setup "$1"
249         shift
250         if test -n "$DISPLAY" -a -n "$(which gitk)" ; then
251                 gitk "$@"
252         elif test -n "$(which tig)" ; then
253                 tig "$@"
254         else
255                 abort "Neither gitk or tig could be launched"
256         fi
257         ;;
258 with)
259         zit_setup "$1"
260         shift
261         "$@"
262         ;;
263 zig)
264         zit_setup "$1"
265         shift
266         tig "$@"
267         ;;
268 zik)
269         zit_setup "$1"
270         shift
271         gitk "$@"
272         ;;
273 # Most commands will work with the generic catch-all mechanism used
274 # below, but some of them require a more thorough analysis of the
275 # parameters to decide whether $ZIT_FILE should be put back into the
276 # parameter list or not. For example,
277 # $ zit commit somefile
278 # wouldn't do what one expects it to do, unless 'add' is run first, however
279 # $ zit commit somefile -a
280 # would work correctly. So we handle some commands separately (for the
281 # moment just commit)
282 commit)
283         zit_setup "$1"
284         shift
285         git commit "$@" "$ZIT_FILE"
286         ;;
287         # the raw<command> method can be used to not replicate $ZIT_FILE in the
288         # parameter list
289 raw*)
290         zit_setup "$1"
291         shift
292         git ${cmd#raw} "$@"
293         ;;
294 *)
295         zit_setup "$1"
296         shift
297         git $cmd "$@"
298         ;;
299 esac