Skip to content

Commit 07bbf8a

Browse files
authored
Add git-ref script to print current git reference
This script determines the current git reference type and prints the branch or tag name based on the repository state. description from copilot
1 parent 086ae15 commit 07bbf8a

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/git-ref

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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

0 commit comments

Comments
 (0)