-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_version.sh
More file actions
executable file
·41 lines (33 loc) · 835 Bytes
/
git_version.sh
File metadata and controls
executable file
·41 lines (33 loc) · 835 Bytes
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
#!/bin/bash
# 1. Find the closest tag
TAG=$(git describe --tags --abbrev=0 2>/dev/null)
if [ -z "$TAG" ]; then
TAG="0.0.0"
fi
# 2. Get current commit hash
HASH=$(git rev-parse --short HEAD 2>/dev/null)
if [ -z "$HASH" ]; then
echo "0.0.0-unknown"
exit 0
fi
# 3. Calculate distance (number of commits since tag)
if [ "$TAG" != "0.0.0" ]; then
DISTANCE=$(git rev-list --count ${TAG}..HEAD 2>/dev/null)
else
DISTANCE=$(git rev-list --count HEAD 2>/dev/null)
fi
# 4. Check for uncommitted changes (dirty state)
if [ -n "$(git status --porcelain)" ]; then
DIRTY="-dirty"
else
DIRTY=""
fi
# 5. Construct version string
if [ "$DISTANCE" -eq "0" ]; then
# Exact tag match
VERSION="${TAG}${DIRTY}"
else
# Tag + distance + hash
VERSION="${TAG}-${DISTANCE}-g${HASH}${DIRTY}"
fi
echo "$VERSION"