]> ToastFreeware Gitweb - gregoa/movein.git/blob - movein
Allow multiple repos to be specified with "add"
[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
21 set -e
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   add repository_name
57       checkout one or more repositories from the remote host and add it to
58       mr's configuration
59
60   new repository_name file1 [file2 file3...]  
61       create a new repository on the remote host, and checkin the
62       listed files to the new repository.   Add the new repository
63       to mr's configuration
64
65   login repository_name
66       start a subshell in repository_name
67
68   exec repository_name command [arg1 arg2...]
69       execute a command in the context of repository_name, for example:
70         $bn exec myrepo git status
71         $bn exec myrepo git ls-files
72
73 EOF
74
75     exit 1
76 }
77
78 [ $# -ge 1 ] || usage
79
80 GIT_HOST=git.vireo.org
81 REMOTE_REPOS=~/git
82 LOCAL_REPOS=~/.movein
83 MRCONFIG=~/.mrconfig
84 MOVEINRC=~/.moveinrc
85
86 [ -e "$MOVEINRC" ] && . "$MOVEINRC"
87
88 init() {
89
90     if [ $# -ne 0 ]; then 
91         usage
92     fi
93     
94     if [ -e $MOVEINRC ]; then
95         echo $MOVEINRC already exists
96         exit 1
97     fi
98
99     if ! which mr > /dev/null; then
100         echo '"mr" not found in the PATH'
101         exit 1
102     fi
103
104     echo -n "git server hostname? [git.vireo.org] " 
105     read GIT_HOST
106     if [ -z "$GIT_HOST" ]; then 
107         GIT_HOST=git.vireo.org
108     fi
109
110     echo -n "path to remote repositories? [~/git] " 
111     read REMOTE_REPOS
112     if [ -z "$REMOTE_REPOS" ]; then 
113         REMOTE_REPOS=~/git
114     fi
115
116     echo -n "Local repository directory? [~/.movein] " 
117     read LOCAL_REPOS
118     if [ -z "$LOCAL_REPOS" ]; then 
119         LOCAL_REPOS=~/.movein
120     fi
121
122     echo -n "Location of .mrconfig file? [~/.mrconfig] " 
123     read MRCONFIG
124     if [ -z "$MRCONFIG" ]; then 
125         MRCONFIG=~/.mrconfig
126     fi
127
128     cat <<EOF > $MOVEINRC
129 GIT_HOST=$GIT_HOST
130 REMOTE_REPOS=$REMOTE_REPOS
131 LOCAL_REPOS=$LOCAL_REPOS
132 MRCONFIG=$MRCONFIG
133 EOF
134
135     if [ ! -d "$LOCAL_REPOS" ]; then 
136         mkdir -p "$LOCAL_REPOS"
137     fi
138
139     mr -c "$MRCONFIG" config DEFAULT include="cat /usr/share/mr/git-fake-bare"
140     
141 }
142
143 login() {
144     if [ $# -ne 1 ]; then 
145         usage
146     fi
147
148     export GIT_DIR="$LOCAL_REPOS/${1}.git"
149     export GIT_WORK_TREE="$GIT_DIR/$(git config --get core.worktree)"
150
151     GIT_PS1_SHOWUNTRACKEDFILES= PSMOVEIN="movein:${1}" $SHELL -i || :
152 }
153
154 execin() {
155     local REPO
156     if [ $# -lt 1 ]; then
157         usage
158     fi
159
160     REPO=$1;shift
161
162     export GIT_DIR="$LOCAL_REPOS/${REPO}.git"
163     export GIT_WORK_TREE="$GIT_DIR/$(git config --get core.worktree)"
164
165     "$@"
166 }
167
168 add() {
169     if [ $# -lt 1 ]; then 
170         usage
171     fi
172
173     for REPO in "$@"; do
174       REPO_NAME=$REPO.git 
175       LOCAL_REPO=$LOCAL_REPOS/$REPO_NAME
176       REPO_URL=ssh://$GIT_HOST/$REMOTE_REPOS/$REPO_NAME
177       if [ -e "$LOCAL_REPO" ]; then
178           echo $LOCAL_REPO already exists
179           exit 1
180       else
181           trap "rm -rf $LOCAL_REPO" 0
182           mkdir "$LOCAL_REPO"
183           export GIT_DIR="$LOCAL_REPO"
184           git init --bare
185           git remote add origin $REPO_URL
186           git config branch.master.remote origin
187           git config branch.master.merge refs/heads/master
188           git config core.bare false
189           git config core.worktree ../../
190           git config status.showUntrackedFiles no
191           GIT_WORK_TREE="$LOCAL_REPO/../../" git pull
192           trap - 0
193
194           mr -c "$MRCONFIG" config "$LOCAL_REPO" checkout="git_fake_bare_checkout '$REPO_URL' 'REPO_NAME' '../../'"
195       fi
196     done
197 }
198
199 list() {
200     find "${LOCAL_REPOS}" -mindepth 1 -maxdepth 1 -type d | sed 's,^.*/\([^/]*\).git$,\1,'
201 }
202
203 listremote() {
204     ssh $GIT_HOST "
205         find '${REMOTE_REPOS}' -mindepth 1 -maxdepth 1 -type d | sed 's,^.*/\([^/]*\).git$,\1,'
206     " </dev/null
207 }
208
209 new() {
210     if [ $# -lt 2 ]; then 
211         usage
212     fi
213     REPO_NAME=$1.git ; shift
214     LOCAL_REPO="$LOCAL_REPOS/$REPO_NAME"
215     REPO_URL="ssh://$GIT_HOST/$REMOTE_REPOS/$REPO_NAME"
216
217     if [ ! -e "$1" ]; then
218         echo $1 not found
219         exit 1
220     fi
221
222     if [ -e "$LOCAL_REPO" ]; then
223         echo $LOCAL_REPO already exists
224         exit 1
225     else
226         trap "rm -rf $LOCAL_REPO" 0
227         mkdir -p "$LOCAL_REPO"
228
229         ssh $GIT_HOST "
230                GIT_DIR=$REMOTE_REPOS/$REPO_NAME git --bare init
231         " </dev/null
232
233         export GIT_DIR="$LOCAL_REPO"
234         git init --bare
235         git remote add origin $REPO_URL
236         git config branch.master.remote origin
237         git config branch.master.merge refs/heads/master
238         git config core.bare false
239         git config core.worktree ../../
240         git config status.showUntrackedFiles no
241         export GIT_WORK_TREE="$LOCAL_REPO/../../"
242         git add "$@"
243         git commit -m "initial checkin"
244         git push --all
245         
246         trap - 0
247
248         mr -c "$MRCONFIG" config "$LOCAL_REPO" checkout="git_fake_bare_checkout '$REPO_URL' 'REPO_NAME' '../../'"
249     fi
250
251 }
252
253
254 command=$1 ; shift
255 case "$command" in 
256     init)
257         init "$@"
258         ;;
259     add)
260         add "$@"
261         ;;
262     new)
263         new "$@"
264         ;;
265     login)
266         login "$@"
267         ;;
268     exec)
269         execin "$@"
270         ;;
271     ls)
272         list
273         ;;
274     list)
275         list
276         ;;
277     ls-r)
278         listremote
279         ;;
280     list-remote)
281         listremote
282         ;;
283
284     *)
285         usage
286         exit 1
287         ;;
288 esac