]> ToastFreeware Gitweb - gregoa/movein.git/blobdiff - movein
Merge branch 'master' of git://git.vireo.org/movein
[gregoa/movein.git] / movein
diff --git a/movein b/movein
index f6000fbd36d0cae3336d53b61fb828aea605737c..d2013da5db48de9b63500ca3b821de9c30707b73 100755 (executable)
--- a/movein
+++ b/movein
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 #
 # Copyright © 2008 Mike O'Connor <stew@vireo.org>
 #
@@ -17,7 +17,6 @@
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 # USA.
 
-
 set -e
 set -u
 
@@ -35,20 +34,30 @@ SYNOPSIS
   $bn init
   $bn ls
   $bn list
+  $bn ls-r
+  $bn list-remote
   $bn add repository_name
   $bn new repository_name file1 [file2 file3...]
   $bn login repository_name
+  $bn exec repository_name command [arg1 arg2...]
 
 COMMANDS
   init
-      create ~/.mrconfig
+      create ~/.moveinrc, create/update  ~/.mrconfig
 
   ls
   list
-      show a list of repositories
+      show a list of local repositories
+
+  ls-r
+  list-remote
+      show a list of remote repositories
+
+  locate pattern
+      locate which repositories contain files matching pattern
 
   add repository_name
-      checkout the repository from the remote host and add it to
+      checkout one or more repositories from the remote host and add it to
       mr's configuration
 
   new repository_name file1 [file2 file3...]
@@ -59,13 +68,19 @@ COMMANDS
   login repository_name
       start a subshell in repository_name
 
+  exec repository_name command [arg1 arg2...]
+      execute a command in the context of repository_name, for example:
+        $bn exec myrepo git status
+        $bn exec myrepo git ls-files
+
 EOF
 
     exit 1
 }
 
+[ $# -ge 1 ] || usage
 
-GIT_HOST=git.vireo.org
+GIT_HOST=git.$(hostname -d)
 REMOTE_REPOS=~/git
 LOCAL_REPOS=~/.movein
 MRCONFIG=~/.mrconfig
@@ -76,41 +91,36 @@ MOVEINRC=~/.moveinrc
 init() {
 
     if [ $# -ne 0 ]; then
-           usage
-    fi
-
-    if [ -e $MRCONFIG ]; then
-           echo $MRCONFIG already exists
-           exit 1
+        usage
     fi
 
     if [ -e $MOVEINRC ]; then
-           echo $MOVEINRC already exists
-           exit 1
+        echo $MOVEINRC already exists
+        exit 1
     fi
 
-    echo -n "git server hostname? [git.vireo.org] "
+    echo -n "git server hostname? [${GIT_HOST}] "
     read GIT_HOST
     if [ -z "$GIT_HOST" ]; then
-           GIT_HOST=git.vireo.org
+        GIT_HOST=git.$(hostname -d)
     fi
 
     echo -n "path to remote repositories? [~/git] "
     read REMOTE_REPOS
     if [ -z "$REMOTE_REPOS" ]; then
-           REMOTE_REPOS=~/git
+        REMOTE_REPOS=~/git
     fi
 
     echo -n "Local repository directory? [~/.movein] "
     read LOCAL_REPOS
     if [ -z "$LOCAL_REPOS" ]; then
-           LOCAL_REPOS=~/.movein
+        LOCAL_REPOS=~/.movein
     fi
 
     echo -n "Location of .mrconfig file? [~/.mrconfig] "
     read MRCONFIG
     if [ -z "$MRCONFIG" ]; then
-           MRCONFIG=~/.mrconfig
+        MRCONFIG=~/.mrconfig
     fi
 
     cat <<EOF > $MOVEINRC
@@ -121,112 +131,149 @@ MRCONFIG=$MRCONFIG
 EOF
 
     if [ ! -d "$LOCAL_REPOS" ]; then
-           mkdir -p "$LOCAL_REPOS"
+       mkdir -p "$LOCAL_REPOS"
     fi
 
-    cat <<END > $MRCONFIG
-[DEFAULT]
-include = cat /usr/share/mr/git-fake-bare
-END
-
+    mr -c "$MRCONFIG" config DEFAULT include="cat /usr/share/mr/git-fake-bare"
 
 }
 
 login() {
     if [ $# -ne 1 ]; then
-           usage
+       usage
     fi
 
     export GIT_DIR="$LOCAL_REPOS/${1}.git"
     export GIT_WORK_TREE="$GIT_DIR/$(git config --get core.worktree)"
 
-    $SHELL -i || :
+    GIT_PS1_SHOWUNTRACKEDFILES= PSMOVEIN="movein:${1}" $SHELL -i || :
 }
 
-add() {
-    if [ $# -ne 1 ]; then
-           usage
+execin() {
+    local REPO
+    if [ $# -lt 1 ]; then
+       usage
     fi
-    REPO_NAME=$1.git ; shift
-    LOCAL_REPO=$LOCAL_REPOS/$REPO_NAME
-    REPO_URL=ssh://$GIT_HOST/$REMOTE_REPOS/$REPO_NAME
 
-    if [ -e "$LOCAL_REPO" ]; then
-           echo $LOCAL_REPO already exists
-           exit 1
-    else
-           trap "rm -rf $LOCAL_REPO" 0
-           mkdir "$LOCAL_REPO"
-           export GIT_DIR="$LOCAL_REPO"
-           git init --bare
-           git remote add origin $REPO_URL
-           git config branch.master.remote origin
-           git config branch.master.merge refs/heads/master
-           git config core.worktree ../../
-           git config core.bare false
-           GIT_WORK_TREE="$PWD" git pull
-           trap - 0
-           cat <<END >> $MRCONFIG
-
-[$LOCAL_REPO]
-checkout = git_fake_bare_checkout '$REPO_URL' '$REPO_NAME' '../../'
-END
+    REPO=$1;shift
+
+    export GIT_DIR="$LOCAL_REPOS/${REPO}.git"
+    export GIT_WORK_TREE="$GIT_DIR/$(git config --get core.worktree)"
+
+    "$@"
+}
+
+add() {
+    if [ $# -lt 1 ]; then
+       usage
     fi
 
+    for REPO in "$@"; do
+        REPO_NAME=$REPO.git
+        LOCAL_REPO=$LOCAL_REPOS/$REPO_NAME
+        REPO_URL=ssh://$GIT_HOST/$REMOTE_REPOS/$REPO_NAME
+
+        if [ -e "$LOCAL_REPO" ]; then
+            echo $LOCAL_REPO already exists
+            exit 1
+        else
+            trap "rm -rf $LOCAL_REPO" 0
+            mkdir "$LOCAL_REPO"
+            export GIT_DIR="$LOCAL_REPO"
+            git init --bare
+            git remote add origin $REPO_URL
+            git config branch.master.remote origin
+            git config branch.master.merge refs/heads/master
+            git config core.bare false
+            git config core.worktree ../../
+            git config status.showUntrackedFiles no
+            GIT_WORK_TREE="$LOCAL_REPO/../../" git pull
+            trap - 0
+
+            mr -c "$MRCONFIG" config "$LOCAL_REPO" checkout="git_fake_bare_checkout '$REPO_URL' 'REPO_NAME' '../../'"
+        fi
+    done
 }
 
 list() {
     find "${LOCAL_REPOS}" -mindepth 1 -maxdepth 1 -type d | sed 's,^.*/\([^/]*\).git$,\1,'
 }
 
+listremote() {
+    ssh $GIT_HOST "
+        find '${REMOTE_REPOS}' -mindepth 1 -maxdepth 1 -type d | sed 's,^.*/\([^/]*\).git$,\1,'
+    " </dev/null
+}
+
+locate() {
+    local REPO
+    for REPO in $($0 list); do
+        (cd /; $0 exec "$REPO" git ls-files | sed -nr "/$1/{s/^/$REPO:/p}")
+    done
+}
+
 new() {
     if [ $# -lt 2 ]; then
-           usage
+        usage
     fi
     REPO_NAME=$1.git ; shift
     LOCAL_REPO="$LOCAL_REPOS/$REPO_NAME"
     REPO_URL="ssh://$GIT_HOST/$REMOTE_REPOS/$REPO_NAME"
 
     if [ ! -e "$1" ]; then
-           echo $1 not found
-           exit 1
+        echo $1 not found
+        exit 1
     fi
 
     if [ -e "$LOCAL_REPO" ]; then
-           echo $LOCAL_REPO already exists
-           exit 1
+        echo $LOCAL_REPO already exists
+        exit 1
     else
-           trap "rm -rf $LOCAL_REPO" 0
-           mkdir -p "$LOCAL_REPO"
+        trap "rm -rf $LOCAL_REPO" 0
+        mkdir -p "$LOCAL_REPO"
 
-           ssh $GIT_HOST "
-               GIT_DIR=$REMOTE_REPOS/$REPO_NAME git --bare init
+        ssh $GIT_HOST "
+            GIT_DIR=$REMOTE_REPOS/$REPO_NAME git --bare init
         " </dev/null
 
-           export GIT_DIR="$LOCAL_REPO"
-           git init --bare
-           git remote add origin $REPO_URL
-           git config branch.master.remote origin
-           git config branch.master.merge refs/heads/master
-           git config core.worktree ../../
-           git config core.bare false
-       # for file in  ; do
-           export GIT_WORK_TREE="$PWD"
-           git add "$@"
-       # done
-           git commit -m "initial checkin"
-           git push --all
-
-           trap - 0
-           cat <<END >> $MRCONFIG
-
-[$LOCAL_REPO]
-checkout = git_fake_bare_checkout '$REPO_URL' '$REPO_NAME' '../../'
-END
+        export GIT_DIR="$LOCAL_REPO"
+        git init --bare
+        git remote add origin $REPO_URL
+        git config branch.master.remote origin
+        git config branch.master.merge refs/heads/master
+        git config core.bare false
+        git config core.worktree ../../
+        git config status.showUntrackedFiles no
+        export GIT_WORK_TREE="$LOCAL_REPO/../../"
+        git add "$@"
+        git commit -m "initial checkin"
+        git push --all
+
+        trap - 0
+
+        mr -c "$MRCONFIG" config "$LOCAL_REPO" checkout="git_fake_bare_checkout '$REPO_URL' 'REPO_NAME' '../../'"
     fi
 
 }
 
+preflight() {
+    # Check a few requirements before doing any work
+    errors=0
+    set +e
+    for binary in mr git; do
+        bin=$(which ${binary})
+        if [ -z "${bin}" ]; then
+            echo "Missing required program: ${binary}"
+            errors=$(( errors + 1 ))
+        fi
+    done
+    set -e
+    if [ $errors -ne 0 ]; then
+        echo "Errors found, exiting"
+        exit 2
+    fi
+}
+
 if [ $# -lt 1 ]; then
     usage
     exit 1
@@ -235,26 +282,41 @@ fi
 command=$1 ; shift
 case "$command" in
     init)
-           init $@
-           ;;
+       preflight
+       init "$@"
+       ;;
     add)
-        add $@
-           ;;
+       preflight
+       add "$@"
+       ;;
     new)
-        new $@
-           ;;
+       preflight
+       new "$@"
+       ;;
     login)
-        login $@
-           ;;
+       login "$@"
+       ;;
+    exec)
+       execin "$@"
+       ;;
     ls)
-        list
-           ;;
+       list
+       ;;
     list)
-        list
-           ;;
+       list
+       ;;
+    ls-r)
+       listremote
+       ;;
+    list-remote)
+       listremote
+       ;;
+    locate)
+       locate "$@"
+       ;;
 
     *)
-           usage
-           exit 1
-           ;;
+       usage
+       exit 1
+       ;;
 esac