]> ToastFreeware Gitweb - gregoa/movein.git/blob - movein
e2d0e9085b06cb5cddfd3cccae0eb5af2efbb89c
[gregoa/movein.git] / movein
1 #!/bin/bash
2 #
3 # Copyright © 2008 Mike O'Connor <stew@vireo.org>
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 2, or (at your option) any
8 # later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 # USA.
19
20 set -e
21 set -u
22
23 usage() {
24     bn=$(basename $0)
25     cat <<EOF
26 $0: manage home directory using mr
27
28 NAME
29   $bn - a command to manage detached git repositories using mr
30
31 SYNOPSIS
32   $bn command [options]
33
34   $bn init
35   $bn ls
36   $bn list
37   $bn ls-r
38   $bn list-remote
39   $bn add repository_name
40   $bn new repository_name file1 [file2 file3...]
41   $bn login repository_name
42   $bn exec repository_name command [arg1 arg2...]
43
44 COMMANDS
45   init
46       create ~/.moveinrc, create/update  ~/.mrconfig
47
48   ls
49   list
50       show a list of local repositories
51
52   ls-r
53   list-remote
54       show a list of remote repositories
55
56   locate pattern
57       locate which repositories contain files matching pattern
58
59   add repository_name
60       checkout one or more repositories from the remote host and add it to
61       mr's configuration
62
63   new repository_name file1 [file2 file3...]
64       create a new repository on the remote host, and checkin the
65       listed files to the new repository.   Add the new repository
66       to mr's configuration
67
68   login repository_name
69       start a subshell in repository_name
70
71   exec repository_name command [arg1 arg2...]
72       execute a command in the context of repository_name, for example:
73         $bn exec myrepo git status
74         $bn exec myrepo git ls-files
75
76 EOF
77
78     exit 1
79 }
80
81 [ $# -ge 1 ] || usage
82
83 GIT_HOST=git.$(hostname -d || echo "example.com")
84 REMOTE_REPOS=~/git
85 LOCAL_REPOS=~/.movein
86 MRCONFIG=~/.mrconfig
87 MOVEINRC=~/.moveinrc
88 CREATE_REMOTE=1
89
90 [ -e "$MOVEINRC" ] && . "$MOVEINRC"
91
92 init() {
93
94     if [ $# -ne 0 ]; then
95         usage
96     fi
97
98     if [ -e $MOVEINRC ]; then
99         echo $MOVEINRC already exists
100         exit 1
101     fi
102
103     echo -n "git server hostname? [${GIT_HOST}] "
104     read GIT_HOST
105     if [ -z "$GIT_HOST" ]; then
106         GIT_HOST=git.$(hostname -d)
107     fi
108
109     echo -n "path to remote repositories? [~/git] "
110     read REMOTE_REPOS
111     if [ -z "$REMOTE_REPOS" ]; then
112         REMOTE_REPOS=~/git
113     fi
114
115     case $GIT_HOST in
116         *github*)
117             DEFAULT_CREATE_REMOTE=0
118             CREATE_REMOTE_OPTIONS="[yN]"
119             ;;
120         *)
121             DEFAULT_CREATE_REMOTE=1
122             CREATE_REMOTE_OPTIONS="[Yn]"
123             ;;
124     esac
125
126     echo -n "should \"movein new\" run \"git init\" on the remote host? (github users should say \"no\") $CREATE_REMOTE_OPTIONS"
127     read CREATE_REMOTE_SELECTION
128     case "$CREATE_REMOTE_SELECTION" in
129         [yY]) CREATE_REMOTE=1 ;;
130         [nN]) CREATE_REMOTE=0 ;;
131         *) CREATE_REMOTE=$DEFAULT_CREATE_REMOTE
132     esac
133
134     echo -n "Local repository directory? [~/.movein] "
135     read LOCAL_REPOS
136     if [ -z "$LOCAL_REPOS" ]; then
137         LOCAL_REPOS=~/.movein
138     fi
139
140     echo -n "Location of .mrconfig file? [~/.mrconfig] "
141     read MRCONFIG
142     if [ -z "$MRCONFIG" ]; then
143         MRCONFIG=~/.mrconfig
144     fi
145
146     cat <<EOF > $MOVEINRC
147 GIT_HOST=$GIT_HOST
148 REMOTE_REPOS=$REMOTE_REPOS
149 LOCAL_REPOS=$LOCAL_REPOS
150 MRCONFIG=$MRCONFIG
151 CREATE_REMOTE=$CREATE_REMOTE
152 EOF
153
154     if [ ! -d "$LOCAL_REPOS" ]; then
155        mkdir -p "$LOCAL_REPOS"
156     fi
157
158     mr -c "$MRCONFIG" config DEFAULT include="cat /usr/share/mr/git-fake-bare"
159
160 }
161
162 login() {
163     if [ $# -ne 1 ]; then
164        usage
165     fi
166
167     export GIT_DIR="$LOCAL_REPOS/${1}.git"
168     export GIT_WORK_TREE="$GIT_DIR/$(git config --get core.worktree)"
169
170     GIT_PS1_SHOWUNTRACKEDFILES= PSMOVEIN="movein:${1}" $SHELL -i || :
171 }
172
173 execin() {
174     local REPO
175     if [ $# -lt 2 ]; then
176        usage
177     fi
178
179     REPO=$1;shift
180
181     export GIT_DIR="$LOCAL_REPOS/${REPO}.git"
182     export GIT_WORK_TREE="$GIT_DIR/$(git config --get core.worktree)"
183
184     "$@"
185 }
186
187 add() {
188     if [ $# -lt 1 ]; then
189        usage
190     fi
191
192     for REPO in "$@"; do
193         REPO_NAME=$REPO.git
194         LOCAL_REPO=$LOCAL_REPOS/$REPO_NAME
195         REPO_URL=ssh://$GIT_HOST/$REMOTE_REPOS/$REPO_NAME
196
197         if [ -e "$LOCAL_REPO" ]; then
198             echo $LOCAL_REPO already exists
199             exit 1
200         else
201             trap "unset GIT_DIR; unset GIT_WORK_TREE; rm -rf $LOCAL_REPO" 0
202             mkdir "$LOCAL_REPO"
203             export GIT_DIR="$LOCAL_REPO"
204             git init --bare
205             git remote add origin $REPO_URL
206             git config branch.master.remote origin
207             git config branch.master.merge refs/heads/master
208             git config core.bare false
209             git config core.worktree ../../
210             git config status.showUntrackedFiles no
211             GIT_WORK_TREE="$LOCAL_REPO/../../" git pull
212             trap - 0
213
214             mr -c "$MRCONFIG" config "$LOCAL_REPO" checkout="git_fake_bare_checkout '$REPO_URL' 'REPO_NAME' '../../'"
215         fi
216     done
217 }
218
219 list() {
220     find "${LOCAL_REPOS}" -mindepth 1 -maxdepth 1 -type d | sed 's,^.*/\([^/]*\).git$,\1,'
221 }
222
223 listremote() {
224     ssh $GIT_HOST "
225         find '${REMOTE_REPOS}' -mindepth 1 -maxdepth 1 -type d | sed 's,^.*/\([^/]*\).git$,\1,'
226     " </dev/null
227 }
228
229 locate() {
230     local REPO
231     if [ $# -ne 1 ]; then
232         usage
233     fi
234     for REPO in $($0 list); do
235         (cd /; $0 exec "$REPO" git ls-files | sed -nr "/$1/{s/^/$REPO:/p}")
236     done
237 }
238
239 new() {
240     if [ $# -lt 2 ]; then
241         usage
242     fi
243     REPO_NAME=$1.git ; shift
244     LOCAL_REPO="$LOCAL_REPOS/$REPO_NAME"
245     REPO_URL="ssh://$GIT_HOST/$REMOTE_REPOS/$REPO_NAME"
246
247     if [ ! -e "$1" ]; then
248         echo $1 not found
249         exit 1
250     fi
251
252     if [ -e "$LOCAL_REPO" ]; then
253         echo $LOCAL_REPO already exists
254         exit 1
255     else
256         trap "unset GIT_DIR; unset GIT_WORK_TREE; rm -rf $LOCAL_REPO" 0
257         mkdir -p "$LOCAL_REPO"
258
259         if [ $CREATE_REMOTE -ne 0 ]; then
260             ssh $GIT_HOST "
261                 GIT_DIR=$REMOTE_REPOS/$REPO_NAME git --bare init
262             " </dev/null
263         fi
264
265         export GIT_DIR="$LOCAL_REPO"
266         git init --bare
267         git remote add origin $REPO_URL
268         git config branch.master.remote origin
269         git config branch.master.merge refs/heads/master
270         git config core.bare false
271         git config core.worktree ../../
272         git config status.showUntrackedFiles no
273         export GIT_WORK_TREE="$LOCAL_REPO/../../"
274         git add "$@"
275         git commit -m "initial checkin"
276         git push --all
277
278         trap - 0
279
280         mr -c "$MRCONFIG" config "$LOCAL_REPO" checkout="git_fake_bare_checkout '$REPO_URL' 'REPO_NAME' '../../'"
281     fi
282
283 }
284
285 preflight() {
286     # Check a few requirements before doing any work
287     errors=0
288     set +e
289     for binary in mr git; do
290         bin=$(which ${binary})
291         if [ -z "${bin}" ]; then
292             echo "Missing required program: ${binary}"
293             errors=$(( errors + 1 ))
294         fi
295     done
296     set -e
297     if [ $errors -ne 0 ]; then
298         echo "Errors found, exiting"
299         exit 2
300     fi
301 }
302
303 if [ $# -lt 1 ]; then
304     usage
305     exit 1
306 fi
307
308 command=$1 ; shift
309 case "$command" in
310     init)
311        preflight
312        init "$@"
313        ;;
314     add)
315        preflight
316        add "$@"
317        ;;
318     new)
319        preflight
320        new "$@"
321        ;;
322     login)
323        login "$@"
324        ;;
325     exec)
326        execin "$@"
327        ;;
328     ls)
329        list
330        ;;
331     list)
332        list
333        ;;
334     ls-r)
335        listremote
336        ;;
337     list-remote)
338        listremote
339        ;;
340     locate)
341        locate "$@"
342        ;;
343
344     *)
345        usage
346        exit 1
347        ;;
348 esac