summaryrefslogtreecommitdiffstats
path: root/src/pmdk/utils/version.sh
blob: fd751c9d9cb8826d55c098079a8b081e29b8bb18 (plain)
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
#!/usr/bin/env bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2017-2020, Intel Corporation
#
# utils/version.sh -- determine project's version
#
set -e

if [ -f "$1/VERSION" ]; then
	cat "$1/VERSION"
	exit 0
fi

if [ -f $1/GIT_VERSION ]; then
	echo -n "\$Format:%h\$" | cmp -s $1/GIT_VERSION - && true
	if [ $? -eq 0 ]; then
		PARSE_GIT_VERSION=0
	else
		PARSE_GIT_VERSION=1
	fi
else
	PARSE_GIT_VERSION=0
fi

LATEST_RELEASE=$(cat $1/ChangeLog | grep "* Version" | cut -d " " -f 3 | sort -rd | head -n1)

if [ $PARSE_GIT_VERSION -eq 1 ]; then
	GIT_VERSION_HASH=$(cat $1/GIT_VERSION)

	if [ -n "$GIT_VERSION_HASH" ]; then
		echo "$LATEST_RELEASE+git.$GIT_VERSION_HASH"
		exit 0
	fi
fi

cd "$1"

GIT_DESCRIBE=$(git describe 2>/dev/null) && true
if [ -n "$GIT_DESCRIBE" ]; then
	# 1.5-19-gb8f78a329 -> 1.5+git19.gb8f78a329
	# 1.5-rc1-19-gb8f78a329 -> 1.5-rc1+git19.gb8f78a329
	echo "$GIT_DESCRIBE" | sed "s/\([0-9.]*\)-rc\([0-9]*\)-\([0-9]*\)-\([0-9a-g]*\)/\1-rc\2+git\3.\4/" | sed "s/\([0-9.]*\)-\([0-9]*\)-\([0-9a-g]*\)/\1+git\2.\3/"
	exit 0
fi

# try commit it, git describe can fail when there are no tags (e.g. with shallow clone, like on Travis)
GIT_COMMIT=$(git log -1 --format=%h) && true
if [ -n "$GIT_COMMIT" ]; then
	echo "$LATEST_RELEASE+git.$GIT_COMMIT"
	exit 0
fi

cd - >/dev/null

# If nothing works, try to get version from directory name
VER=$(basename `realpath "$1"` | sed 's/pmdk[-]*\([0-9a-z.+-]*\).*/\1/')
if [ -n "$VER" ]; then
	echo "$VER"
	exit 0
fi

exit 1