From: smckown Date: Mon, 23 Nov 2009 19:28:27 +0000 (+0000) Subject: Add git utils X-Git-Url: https://oss.titaniummirror.com/gitweb?p=git-utils.git;a=commitdiff_plain;h=ee12535d2368579f06ca52b9cadbe23ca0ea4b7f Add git utils --- ee12535d2368579f06ca52b9cadbe23ca0ea4b7f diff --git a/git-publish-branch b/git-publish-branch new file mode 100755 index 0000000..fb38886 --- /dev/null +++ b/git-publish-branch @@ -0,0 +1,65 @@ +#!/bin/sh +# git-publish-branch [-d] [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 [] " >&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