From 859fd7052a97cd107a8c822503f7585cf5edf981 Mon Sep 17 00:00:00 2001 From: smckown Date: Mon, 23 Nov 2009 20:30:01 +0000 Subject: [PATCH] Add the git-local utility. --- git-local | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100755 git-local diff --git a/git-local b/git-local new file mode 100755 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 -- 2.39.2