-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-version
executable file
·82 lines (69 loc) · 1.69 KB
/
git-version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2021 Robin Vobruba <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# See the output of "$0 -h" for details.
# Exit immediately on each error and unset variable;
# see: https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
#set -Eeuo pipefail
set -Eeu
script_dir=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
APP_NAME="Git Version printer"
empty_on_error=false
print_help() {
script_name="$(basename "$0")"
echo "$APP_NAME - Compiles a git based version string for the project."
echo "It is based on 'git describe'"
echo
echo "Usage:"
echo " $script_name [OPTIONS] [repo-dir]"
echo "Options:"
echo " -h, --help Print this usage help and exit"
echo " -e, --empty-on-error Return an empty string, instead of an error, when the supplied dir is not a valid git repo"
echo "Examples:"
echo " $script_name"
echo " $script_name rel/path/to/repo"
echo " $script_name --help"
echo " $script_name -e"
}
# read command-line args
POSITIONAL=()
while [[ $# -gt 0 ]]
do
arg="$1"
shift # $2 -> $1, $3 -> $2, ...
case "$arg" in
-h|--help)
print_help
exit 0
;;
-e|--empty-on-error)
empty_on_error=true
;;
*) # non-/unknown option
POSITIONAL+=("${arg}") # save it in an array for later
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
repo_dir="${1:-.}"
if [ ! -e "$repo_dir/.git" ]
then
if $empty_on_error
then
exit 0
fi
>&2 echo "ERROR: Not a valid git repo: '$repo_dir'"
print_help
exit 1
fi
repo_dir_abs="$(cd "$repo_dir"; pwd)"
git \
-C "$repo_dir_abs" \
describe \
--long \
--dirty \
--candidates=99 \
--always \
--first-parent 2> /dev/null