File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ #! /usr/bin/env bash
2+ usage () {
3+ echo -n " git-ref [OPTION]... [FILE]...
4+
5+ Prints the branch or tag of the current git repository.
6+
7+ -t, --type Print the git reference type ('branch' or 'tag').
8+
9+ If PWD is tracking a branch, print " branch" .
10+ If PWD is in a DETACHED HEAD state, and there is a tag for the SHA, print " tag" .
11+ If the PWD is in a DETACHED HEAD state, and there is no tag, exit with an error.
12+ "
13+ }
14+
15+ BRANCH=` git symbolic-ref --quiet --short HEAD 2> /dev/null`
16+ TAG=` git describe --tags --exact-match 2> /dev/null`
17+
18+ # Set up options for --type and --help.
19+ while [ $# -gt 0 ]; do
20+ case " $1 " in
21+ -t|--type)
22+ if [[ -n $BRANCH ]]; then
23+ echo " branch"
24+ elif [[ -n $TAG ]]; then
25+ echo " tag"
26+ else
27+ exit 1
28+ fi
29+
30+ exit 0
31+ ;;
32+ -h|--help) usage >&2 ; exit 0;;
33+ * )
34+ echo " Invalid option: $1 "
35+ exit 1
36+ esac
37+ shift
38+ done
39+
40+ # Print the branch or tag name
41+ if [[ -n $BRANCH ]]; then
42+ echo $BRANCH
43+ elif [[ -n $TAG ]]; then
44+ echo $TAG
45+ else
46+ exit 1
47+ fi
You can’t perform that action at this time.
0 commit comments