]> oss.titaniummirror.com Git - git-utils.git/commitdiff
Add the git-local utility.
authorsmckown <smckown@986fd584-583e-0410-b54d-b9fe63dff8e5>
Mon, 23 Nov 2009 20:30:01 +0000 (20:30 +0000)
committersmckown <smckown@986fd584-583e-0410-b54d-b9fe63dff8e5>
Mon, 23 Nov 2009 20:30:01 +0000 (20:30 +0000)
git-local [new file with mode: 0755]

diff --git a/git-local b/git-local
new file mode 100755 (executable)
index 0000000..3378a2c
--- /dev/null
+++ b/git-local
@@ -0,0 +1,131 @@
+#!/bin/sh
+# git-local <--upload|--download|--remove|--list> [remote] [prefix]
+#
+# Upload (backup) and download (restore) the state of local branches to a remote
+# repository.  This feature is useful at the end of a work session to push
+# changes not ready to publish to others for the purposes of backup, and to
+# provide some desktop independence for the developer.
+#
+# Default remote is 'origin', default prefix is current username.
+#
+# --upload
+#      upload all local branches to remote repository with a namespace prefix
+#      of prefix/.
+#
+# --download
+#      download local branches on remote repository with a namespace prefix of
+#      prefix/.
+#
+# --remove
+#      remove all branches on remote repository with a namespace prefix of
+#      prefix/.
+#
+# --list
+#      list all branches on remote repository with a namespace prefix of
+#      prefix/.
+#
+
+usage()
+{
+    cat <<+EOF+ >&2
+usage: $0 <--upload|--download> [remote] [prefix]
+
+    Back up and restore local branches to a remote repository
+
+    --upload local branches to remote, using prefix prefix
+    --download branches on remote using prefix prefix as local branches
+    --remove remote branches on remote having prefix prefix (CAREFUL!)
+    --list remote branches on remote having prefix prefix (CAREFUL!)
+
+    Default remote is 'origin', default prefix is user's username.
++EOF+
+}
+
+upload()
+{
+    git push "$remote" "+refs/heads/*:refs/$prefix/*"
+}
+
+download()
+{
+    git fetch -uf "$remote" "+refs/$prefix/*:refs/heads/*"
+    git reset --hard
+}
+
+remove()
+{
+    for b in $(git ls-remote $remote refs/$prefix/* | awk '{ print $2 }'); do
+       git push "$remote" ":$b"
+    done
+}
+
+list()
+{
+    git ls-remote $remote "refs/$prefix/*"
+}
+
+# MAIN
+
+unset op
+case "$1" in
+    --upload) op=upload
+       ;;
+
+    --download) op=download
+       ;;
+
+    --remove) op=remove
+       ;;
+
+    --list) op=list
+       ;;
+
+    *) usage
+       exit 0
+       ;;
+esac
+
+unset remote
+unset prefix
+
+shift
+if [ $# -eq 1 ]; then
+    remote=$1
+elif [ $# -eq 2 ]; then
+    remote=$1
+    prefix=$2
+elif [ $# -gt 2 ]; then
+    usage
+    exit 0
+fi
+
+if [ -z "$remote" ]; then
+    remote=origin
+fi
+
+if [ -z "$prefix" ]; then
+    prefix=$(whoami)
+fi
+
+case $op in
+    upload)
+       upload
+       ;;
+
+    download)
+       download
+       ;;
+
+    remove)
+       remove
+       ;;
+
+    list)
+       list
+       ;;
+
+    *)
+       usage
+       exit 0
+       ;;
+esac