]> oss.titaniummirror.com Git - git-utils.git/commitdiff
Add git utils
authorsmckown <smckown@986fd584-583e-0410-b54d-b9fe63dff8e5>
Mon, 23 Nov 2009 19:28:27 +0000 (19:28 +0000)
committersmckown <smckown@986fd584-583e-0410-b54d-b9fe63dff8e5>
Mon, 23 Nov 2009 19:28:27 +0000 (19:28 +0000)
git-publish-branch [new file with mode: 0755]

diff --git a/git-publish-branch b/git-publish-branch
new file mode 100755 (executable)
index 0000000..fb38886
--- /dev/null
@@ -0,0 +1,65 @@
+#!/bin/sh
+# git-publish-branch [-d] <branch> [repository]
+
+unset delete
+if [ "$1" = "-d" ]; then
+    delete=1
+    shift
+fi
+
+unset branch
+unset remote
+
+if [ $# -eq 1 ]; then
+    branch=$1
+elif [ $# -eq 2 ]; then
+    remote=$1
+    branch=$2
+elif [ $# -gt 2 ]; then
+    echo "Usage: $0 [<repository>] <branch>" >&2
+    exit 127
+fi
+
+if [ -z "$branch" ]; then
+    branch=$(git symbolic-ref HEAD)
+    branch=${branch//refs\/heads\//}
+fi
+
+if [ -z "$remote" ]; then
+    remote=origin
+fi
+
+local_ref=$(git show-ref "heads/$branch")
+remote_ref=$(git show-ref "remotes/$remote/$branch")
+remote_config=$(git config "branch.$branch.merge")
+
+if [ $delete ]; then
+    # Delete the remote branch
+    if [ -n "$remote_ref" ]; then
+       git push "$remote" ":refs/heads/$branch"
+    else
+       echo "$remote has to remote branch $branch" >&2
+    fi
+
+    # Remove local configs for remote branch
+    if [ -n "$local_ref" ]; then
+       git config --unset "branch.$branch.remote"
+       git config --unset "branch.$branch.merge"
+       git config --unset "branch.$branch"
+    fi
+else
+    if [ -z "$local_ref" ]; then
+       echo "$0: no local branch $branch" >&2
+       exit 127
+    elif [ -n "$remote_ref" ]; then
+       echo "$0: $remote already has branch $branch" >&2
+       exit 127
+    elif [ -n "$remote_config" ]; then
+       echo "$0: local branch $branch is already a tracking branch" >&2
+       exit 127
+    fi
+
+    git push "$remote" "$branch:refs/heads/$branch"
+    git config "branch.$branch.remote" "$remote"
+    git config "branch.$branch.merge" "refs/heads/$branch"
+fi