From 655ba99b57fd6c9d92447c5db25436bca4e3729f Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Thu, 29 Aug 2013 15:09:16 +0200 Subject: [PATCH] Add git-{{local,remote}-branch,tag}-exists. --- README.md | 17 ++++++++++++++++- git-local-branch-exists | 17 +++++++++++++++++ git-remote-branch-exists | 21 +++++++++++++++++++++ git-tag-exists | 17 +++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100755 git-local-branch-exists create mode 100755 git-remote-branch-exists create mode 100755 git-tag-exists diff --git a/README.md b/README.md index c3f16abd..59ea6066 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,21 @@ Accepts options, too, so you can use to force-push. +### git is-headless + +Tests if `HEAD` is pointing to a branch head, or is detached. + + +### git local-branch-exists / git remote-branch-exists / git tag-exists + +Tests if the given local branch, remote branch, or tag exists. + + +### git is-headless + +Tests if `HEAD` is pointing to a branch head, or is detached. + + ### git is-merged-into Tests if X is merged into Y: @@ -125,7 +140,7 @@ conflict.) * mybranch -### git has-local-changes +### git has-local-changes / git is-clean / git is-dirty Helper function that determines whether there are local changes in the working tree, by returning a 0 (local changes) or 1 (no local changes) exit code. diff --git a/git-local-branch-exists b/git-local-branch-exists new file mode 100755 index 00000000..3d6b7ca1 --- /dev/null +++ b/git-local-branch-exists @@ -0,0 +1,17 @@ +#!/bin/sh +set -e + +usage () { + echo "usage: git local-branch-exists " >&2 + echo >&2 + echo "Will return with an exit code of 0 or 1." >&2 +} + +if [ $# -eq 1 ]; then + branch=$1 +else + usage + exit 2 +fi + +git show-ref --heads --quiet --verify -- "refs/heads/$branch" diff --git a/git-remote-branch-exists b/git-remote-branch-exists new file mode 100755 index 00000000..6a51f203 --- /dev/null +++ b/git-remote-branch-exists @@ -0,0 +1,21 @@ +#!/bin/sh +set -e + +usage () { + echo "usage: git remote-branch-exists [] " >&2 + echo >&2 + echo "Will return with an exit code of 0 or 1." >&2 +} + +if [ $# -eq 1 ]; then + remote=origin + branch=$1 +elif [ $# -eq 2 ]; then + remote=$1 + branch=$2 +else + usage + exit 2 +fi + +git show-ref --quiet --verify -- "refs/remotes/$remote/$branch" diff --git a/git-tag-exists b/git-tag-exists new file mode 100755 index 00000000..2da3b20e --- /dev/null +++ b/git-tag-exists @@ -0,0 +1,17 @@ +#!/bin/sh +set -e + +usage () { + echo "usage: git tag-exists " >&2 + echo >&2 + echo "Will return with an exit code of 0 or 1." >&2 +} + +if [ $# -eq 1 ]; then + tag=$1 +else + usage + exit 2 +fi + +git show-ref --tags --quiet --verify -- "refs/tags/$tag"