]> ToastFreeware Gitweb - gregoa/movein.git/blob - movein
Require an argument to exec
[gregoa/movein.git] / movein
1 #!/bin/sh
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)
84 REMOTE_REPOS=~/git
85 LOCAL_REPOS=~/.movein
86 MRCONFIG=~/.mrconfig
87 MOVEINRC=~/.moveinrc
88
89 [ -e "$MOVEINRC" ] && . "$MOVEINRC"
90
91 init() {
92
93     if [ $# -ne 0 ]; then
94         usage
95     fi
96
97     if [ -e $MOVEINRC ]; then
98         echo $MOVEINRC already exists
99         exit 1
100     fi
101
102     echo -n "git server hostname? [${GIT_HOST}] "
103     read GIT_HOST
104     if [ -z "$GIT_HOST" ]; then
105         GIT_HOST=git.$(hostname -d)
106     fi
107
108     echo -n "path to remote repositories? [~/git] "
109     read REMOTE_REPOS
110     if [ -z "$REMOTE_REPOS" ]; then
111         REMOTE_REPOS=~/git
112     fi
113
114     echo -n "Local repository directory? [~/.movein] "
115     read LOCAL_REPOS
116     if [ -z "$LOCAL_REPOS" ]; then
117         LOCAL_REPOS=~/.movein
118     fi
119
120     echo -n "Location of .mrconfig file? [~/.mrconfig] "
121     read MRCONFIG
122     if [ -z "$MRCONFIG" ]; then
123         MRCONFIG=~/.mrconfig
124     fi
125
126     cat <<EOF > $MOVEINRC
127 GIT_HOST=$GIT_HOST
128 REMOTE_REPOS=$REMOTE_REPOS
129 LOCAL_REPOS=$LOCAL_REPOS
130 MRCONFIG=$MRCONFIG
131 EOF
132
133     if [ ! -d "$LOCAL_REPOS" ]; then
134        mkdir -p "$LOCAL_REPOS"
135     fi
136
137     mr -c "$MRCONFIG" config DEFAULT include="cat /usr/share/mr/git-fake-bare"
138
139 }
140
141 login() {
142     if [ $# -ne 1 ]; then
143        usage
144     fi
145
146     export GIT_DIR="$LOCAL_REPOS/${1}.git"
147     export GIT_WORK_TREE="$GIT_DIR/$(git config --get core.worktree)"
148
149     GIT_PS1_SHOWUNTRACKEDFILES= PSMOVEIN="movein:${1}" $SHELL -i || :
150 }
151
152 execin() {
153     local REPO
154     if [ $# -lt 2 ]; then
155        usage
156     fi
157
158     REPO=$1;shift
159
160     export GIT_DIR="$LOCAL_REPOS/${REPO}.git"
161     export GIT_WORK_TREE="$GIT_DIR/$(git config --get core.worktree)"
162
163     "$@"
164 }
165
166 add() {
167     if [ $# -lt 1 ]; then
168        usage
169     fi
170
171     for REPO in "$@"; do
172         REPO_NAME=$REPO.git
173         LOCAL_REPO=$LOCAL_REPOS/$REPO_NAME
174         REPO_URL=ssh://$GIT_HOST/$REMOTE_REPOS/$REPO_NAME
175
176         if [ -e "$LOCAL_REPO" ]; then
177             echo $LOCAL_REPO already exists
178             exit 1
179         else
180             trap "rm -rf $LOCAL_REPO" 0
181             mkdir "$LOCAL_REPO"
182             export GIT_DIR="$LOCAL_REPO"
183             git init --bare
184             git remote add origin $REPO_URL
185             git config branch.master.remote origin
186             git config branch.master.merge refs/heads/master
187             git config core.bare false
188             git config core.worktree ../../
189             git config status.showUntrackedFiles no
190             GIT_WORK_TREE="$LOCAL_REPO/../../" git pull
191             trap - 0
192
193             mr -c "$MRCONFIG" config "$LOCAL_REPO" checkout="git_fake_bare_checkout '$REPO_URL' 'REPO_NAME' '../../'"
194         fi
195     done
196 }
197
198 list() {
199     find "${LOCAL_REPOS}" -mindepth 1 -maxdepth 1 -type d | sed 's,^.*/\([^/]*\).git$,\1,'
200 }
201
202 listremote() {
203     ssh $GIT_HOST "
204         find '${REMOTE_REPOS}' -mindepth 1 -maxdepth 1 -type d | sed 's,^.*/\([^/]*\).git$,\1,'
205     " </dev/null
206 }
207
208 locate() {
209     local REPO
210     for REPO in $($0 list); do
211         (cd /; $0 exec "$REPO" git ls-files | sed -nr "/$1/{s/^/$REPO:/p}")
212     done
213 }
214
215 new() {
216     if [ $# -lt 2 ]; then
217         usage
218     fi
219     REPO_NAME=$1.git ; shift
220     LOCAL_REPO="$LOCAL_REPOS/$REPO_NAME"
221     REPO_URL="ssh://$GIT_HOST/$REMOTE_REPOS/$REPO_NAME"
222
223     if [ ! -e "$1" ]; then
224         echo $1 not found
225         exit 1
226     fi
227
228     if [ -e "$LOCAL_REPO" ]; then
229         echo $LOCAL_REPO already exists
230         exit 1
231     else
232         trap "rm -rf $LOCAL_REPO" 0
233         mkdir -p "$LOCAL_REPO"
234
235         ssh $GIT_HOST "
236             GIT_DIR=$REMOTE_REPOS/$REPO_NAME git --bare init
237         " </dev/null
238
239         export GIT_DIR="$LOCAL_REPO"
240         git init --bare
241         git remote add origin $REPO_URL
242         git config branch.master.remote origin
243         git config branch.master.merge refs/heads/master
244         git config core.bare false
245         git config core.worktree ../../
246         git config status.showUntrackedFiles no
247         export GIT_WORK_TREE="$LOCAL_REPO/../../"
248         git add "$@"
249         git commit -m "initial checkin"
250         git push --all
251
252         trap - 0
253
254         mr -c "$MRCONFIG" config "$LOCAL_REPO" checkout="git_fake_bare_checkout '$REPO_URL' 'REPO_NAME' '../../'"
255     fi
256
257 }
258
259 preflight() {
260     # Check a few requirements before doing any work
261     errors=0
262     set +e
263     for binary in mr git; do
264         bin=$(which ${binary})
265         if [ -z "${bin}" ]; then
266             echo "Missing required program: ${binary}"
267             errors=$(( errors + 1 ))
268         fi
269     done
270     set -e
271     if [ $errors -ne 0 ]; then
272         echo "Errors found, exiting"
273         exit 2
274     fi
275 }
276
277 if [ $# -lt 1 ]; then
278     usage
279     exit 1
280 fi
281
282 command=$1 ; shift
283 case "$command" in
284     init)
285        preflight
286        init "$@"
287        ;;
288     add)
289        preflight
290        add "$@"
291        ;;
292     new)
293        preflight
294        new "$@"
295        ;;
296     login)
297        login "$@"
298        ;;
299     exec)
300        execin "$@"
301        ;;
302     ls)
303        list
304        ;;
305     list)
306        list
307        ;;
308     ls-r)
309        listremote
310        ;;
311     list-remote)
312        listremote
313        ;;
314     locate)
315        locate "$@"
316        ;;
317
318     *)
319        usage
320        exit 1
321        ;;
322 esac