xref: /aosp_15_r20/external/erofs-utils/scripts/get-version-number (revision 33b1fccf6a0fada2c2875d400ed01119b7676ee5)
1*33b1fccfSAndroid Build Coastguard Worker#!/bin/sh
2*33b1fccfSAndroid Build Coastguard Worker# SPDX-License-Identifier: GPL-2.0
3*33b1fccfSAndroid Build Coastguard Worker
4*33b1fccfSAndroid Build Coastguard Workerscm_version()
5*33b1fccfSAndroid Build Coastguard Worker{
6*33b1fccfSAndroid Build Coastguard Worker	# Check for git and a git repo.
7*33b1fccfSAndroid Build Coastguard Worker	if test -z "$(git rev-parse --show-cdup 2>/dev/null)" &&
8*33b1fccfSAndroid Build Coastguard Worker	   head="$(git rev-parse --verify HEAD 2>/dev/null)"; then
9*33b1fccfSAndroid Build Coastguard Worker		# If we are at a tagged commit, we ignore it.
10*33b1fccfSAndroid Build Coastguard Worker		if [ -z "$(git describe --exact-match 2>/dev/null)" ]; then
11*33b1fccfSAndroid Build Coastguard Worker			# Add -g and 8 hex chars.
12*33b1fccfSAndroid Build Coastguard Worker			printf '%s%s' -g "$(echo $head | cut -c1-8)"
13*33b1fccfSAndroid Build Coastguard Worker		fi
14*33b1fccfSAndroid Build Coastguard Worker		# Check for uncommitted changes.
15*33b1fccfSAndroid Build Coastguard Worker		# This script must avoid any write attempt to the source tree,
16*33b1fccfSAndroid Build Coastguard Worker		# which might be read-only.
17*33b1fccfSAndroid Build Coastguard Worker		# You cannot use 'git describe --dirty' because it tries to
18*33b1fccfSAndroid Build Coastguard Worker		# create .git/index.lock .
19*33b1fccfSAndroid Build Coastguard Worker		# First, with git-status, but --no-optional-locks is only
20*33b1fccfSAndroid Build Coastguard Worker		# supported in git >= 2.14, so fall back to git-diff-index if
21*33b1fccfSAndroid Build Coastguard Worker		# it fails. Note that git-diff-index does not refresh the
22*33b1fccfSAndroid Build Coastguard Worker		# index, so it may give misleading results. See
23*33b1fccfSAndroid Build Coastguard Worker		# git-update-index(1), git-diff-index(1), and git-status(1).
24*33b1fccfSAndroid Build Coastguard Worker		if {
25*33b1fccfSAndroid Build Coastguard Worker			git --no-optional-locks status -uno --porcelain 2>/dev/null ||
26*33b1fccfSAndroid Build Coastguard Worker			git diff-index --name-only HEAD
27*33b1fccfSAndroid Build Coastguard Worker		} | read dummy; then
28*33b1fccfSAndroid Build Coastguard Worker			printf '%s' -dirty
29*33b1fccfSAndroid Build Coastguard Worker		fi
30*33b1fccfSAndroid Build Coastguard Worker	fi
31*33b1fccfSAndroid Build Coastguard Worker}
32*33b1fccfSAndroid Build Coastguard Worker
33*33b1fccfSAndroid Build Coastguard Workerecho $(sed -n '1p' VERSION | tr -d '\n')$(scm_version)
34