-
Notifications
You must be signed in to change notification settings - Fork 2
/
gitver
executable file
·42 lines (37 loc) · 1.25 KB
/
gitver
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
#!/usr/bin/env bash
set -euo pipefail
# get current git sha of a provided script (full path or in $PATH) or directory
# looks like 78341f2:2023-07-21:1,1(+),3(-)
#
# EXAMPLE: (by (1) directory (2) fullpath (3+4) app in path )
# gitver /opt/ni_tools/lncdtools # 4d99a09:2023-07-24:2,8(+),5(-)
# gitver /opt/ni_tools/lncdtools/gitver # dc2e48c:2021-08-09:1,7(+),2(-)
# gitver gitver # dc2e48c:2021-08-09:1,7(+),2(-)
# gitver dryrun # cc2097f:2022-10-11:
# gitver /tmp # git:NA
# 20200909WF - init
# 20230804WF - given file, ouput log+diff specific to file
# what to spit out when error/no git repo
nover=git:NA
file=""
if [ $# -eq 1 ]; then
repo="$1"
# try script if not file
[ ! -r "$repo" ] && repo="$(which "$repo")" || :
# if given a file, use directory
if [ -r "$repo" -a ! -d "$repo" ]; then
file="-- $repo"
repo="$(dirname "$repo")"
fi
if [ ! -d "$repo" ]; then
echo "# ERROR: gitver given bad dir/file/app: '$1'" >&2
echo $nover
exit 1
fi
cd "$repo"
fi
if git rev-parse --short HEAD >/dev/null 2>&1; then
echo $(git log -1 --pretty=format:%h:%as $file):$(git diff --shortstat $file|tr -d '[a-z ]')
else
echo $nover
fi