xref: /linux/scripts/setlocalversion (revision 523f3dbc187a9618d4fd80c2b438e4d490705dcd)
1117a93dbSRene Scharfe#!/bin/sh
2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
333252572SNico Schottelius#
433252572SNico Schottelius# This scripts adds local version information from the version
53354c64dSRasmus Villemoes# control system git.
633252572SNico Schottelius#
733252572SNico Schottelius# If something goes wrong, send a mail the kernel build mailinglist
833252572SNico Schottelius# (see MAINTAINERS) and CC Nico Schottelius
933252572SNico Schottelius# <nico-linuxsetlocalversion -at- schottelius.org>.
1033252572SNico Schottelius#
1133252572SNico Schottelius#
12aaebf433SRyan Anderson
13117a93dbSRene Scharfeusage() {
1405e96e96SMasahiro Yamada	echo "Usage: $0 [--no-local] [srctree]" >&2
15117a93dbSRene Scharfe	exit 1
16aaebf433SRyan Anderson}
17aaebf433SRyan Anderson
1805e96e96SMasahiro Yamadano_local=false
1905e96e96SMasahiro Yamadaif test "$1" = "--no-local"; then
2005e96e96SMasahiro Yamada	no_local=true
2105e96e96SMasahiro Yamada	shift
2205e96e96SMasahiro Yamadafi
2305e96e96SMasahiro Yamada
2409155120SMichal Mareksrctree=.
2509155120SMichal Marekif test $# -gt 0; then
2609155120SMichal Marek	srctree=$1
2709155120SMichal Marek	shift
2809155120SMichal Marekfi
2909155120SMichal Marekif test $# -gt 0 -o ! -d "$srctree"; then
3009155120SMichal Marek	usage
3109155120SMichal Marekfi
3209155120SMichal Marek
33*523f3dbcSRasmus Villemoestry_tag() {
34*523f3dbcSRasmus Villemoes	tag="$1"
35*523f3dbcSRasmus Villemoes
36*523f3dbcSRasmus Villemoes	# Is $tag an annotated tag?
37*523f3dbcSRasmus Villemoes	[ "$(git cat-file -t "$tag" 2> /dev/null)" = tag ] || return 1
38*523f3dbcSRasmus Villemoes
39*523f3dbcSRasmus Villemoes	# Is it an ancestor of HEAD, and if so, how many commits are in $tag..HEAD?
40*523f3dbcSRasmus Villemoes	# shellcheck disable=SC2046 # word splitting is the point here
41*523f3dbcSRasmus Villemoes	set -- $(git rev-list --count --left-right "$tag"...HEAD 2> /dev/null)
42*523f3dbcSRasmus Villemoes
43*523f3dbcSRasmus Villemoes	# $1 is 0 if and only if $tag is an ancestor of HEAD. Use
44*523f3dbcSRasmus Villemoes	# string comparison, because $1 is empty if the 'git rev-list'
45*523f3dbcSRasmus Villemoes	# command somehow failed.
46*523f3dbcSRasmus Villemoes	[ "$1" = 0 ] || return 1
47*523f3dbcSRasmus Villemoes
48*523f3dbcSRasmus Villemoes	# $2 is the number of commits in the range $tag..HEAD, possibly 0.
49*523f3dbcSRasmus Villemoes	count="$2"
50*523f3dbcSRasmus Villemoes
51*523f3dbcSRasmus Villemoes	return 0
52*523f3dbcSRasmus Villemoes}
53*523f3dbcSRasmus Villemoes
5409155120SMichal Marekscm_version()
5509155120SMichal Marek{
5605e96e96SMasahiro Yamada	local short=false
5705e96e96SMasahiro Yamada	local no_dirty=false
586ab7e1f9SMasahiro Yamada	local tag
5905e96e96SMasahiro Yamada
6005e96e96SMasahiro Yamada	while [ $# -gt 0 ];
6105e96e96SMasahiro Yamada	do
6205e96e96SMasahiro Yamada		case "$1" in
6305e96e96SMasahiro Yamada		--short)
6405e96e96SMasahiro Yamada			short=true;;
6505e96e96SMasahiro Yamada		--no-dirty)
6605e96e96SMasahiro Yamada			no_dirty=true;;
6705e96e96SMasahiro Yamada		esac
6805e96e96SMasahiro Yamada		shift
6905e96e96SMasahiro Yamada	done
7009155120SMichal Marek
7109155120SMichal Marek	cd "$srctree"
72aaebf433SRyan Anderson
7375280bdfSMasahiro Yamada	if test -n "$(git rev-parse --show-cdup 2>/dev/null)"; then
7475280bdfSMasahiro Yamada		return
7575280bdfSMasahiro Yamada	fi
7633252572SNico Schottelius
7775280bdfSMasahiro Yamada	if ! head=$(git rev-parse --verify HEAD 2>/dev/null); then
7875280bdfSMasahiro Yamada		return
7975280bdfSMasahiro Yamada	fi
8075280bdfSMasahiro Yamada
816ab7e1f9SMasahiro Yamada	# mainline kernel:  6.2.0-rc5  ->  v6.2-rc5
826ab7e1f9SMasahiro Yamada	# stable kernel:    6.1.7      ->  v6.1.7
8301e89a4aSRasmus Villemoes	version_tag=v$(echo "${KERNELVERSION}" | sed -E 's/^([0-9]+\.[0-9]+)\.0(.*)$/\1\2/')
8401e89a4aSRasmus Villemoes
85*523f3dbcSRasmus Villemoes	# try_tag initializes count if the tag is usable.
86*523f3dbcSRasmus Villemoes	count=
87*523f3dbcSRasmus Villemoes
8801e89a4aSRasmus Villemoes	# If a localversion* file exists, and the corresponding
8901e89a4aSRasmus Villemoes	# annotated tag exists and is an ancestor of HEAD, use
9001e89a4aSRasmus Villemoes	# it. This is the case in linux-next.
91*523f3dbcSRasmus Villemoes	if [ -n "${file_localversion#-}" ] ; then
92*523f3dbcSRasmus Villemoes		try_tag "${file_localversion#-}"
9301e89a4aSRasmus Villemoes	fi
9401e89a4aSRasmus Villemoes
9501e89a4aSRasmus Villemoes	# Otherwise, if a localversion* file exists, and the tag
9601e89a4aSRasmus Villemoes	# obtained by appending it to the tag derived from
9701e89a4aSRasmus Villemoes	# KERNELVERSION exists and is an ancestor of HEAD, use
9801e89a4aSRasmus Villemoes	# it. This is e.g. the case in linux-rt.
99*523f3dbcSRasmus Villemoes	if [ -z "${count}" ] && [ -n "${file_localversion}" ]; then
100*523f3dbcSRasmus Villemoes		try_tag "${version_tag}${file_localversion}"
10101e89a4aSRasmus Villemoes	fi
10201e89a4aSRasmus Villemoes
10301e89a4aSRasmus Villemoes	# Otherwise, default to the annotated tag derived from KERNELVERSION.
104*523f3dbcSRasmus Villemoes	if [ -z "${count}" ]; then
105*523f3dbcSRasmus Villemoes		try_tag "${version_tag}"
1066ab7e1f9SMasahiro Yamada	fi
1076ab7e1f9SMasahiro Yamada
108*523f3dbcSRasmus Villemoes	# If we are at the tagged commit, we ignore it because the
109*523f3dbcSRasmus Villemoes	# version is well-defined. If none of the attempted tags exist
110*523f3dbcSRasmus Villemoes	# or were usable, $count is still empty.
111*523f3dbcSRasmus Villemoes	if [ -z "${count}" ] || [ "${count}" -gt 0 ]; then
11233252572SNico Schottelius
11309155120SMichal Marek		# If only the short version is requested, don't bother
11409155120SMichal Marek		# running further git commands
11509155120SMichal Marek		if $short; then
11609155120SMichal Marek			echo "+"
11709155120SMichal Marek			return
11809155120SMichal Marek		fi
119*523f3dbcSRasmus Villemoes
1206ab7e1f9SMasahiro Yamada		# If we are past the tagged commit, we pretty print it.
1216ab7e1f9SMasahiro Yamada		# (like 6.1.0-14595-g292a089d78d3)
122*523f3dbcSRasmus Villemoes		if [ -n "${count}" ]; then
123*523f3dbcSRasmus Villemoes			printf "%s%05d" "-" "${count}"
12456b2f070SSebastian Siewior		fi
125630ff0faSMasahiro Yamada
126630ff0faSMasahiro Yamada		# Add -g and exactly 12 hex chars.
127*523f3dbcSRasmus Villemoes		printf '%s%.12s' -g "$head"
12833252572SNico Schottelius	fi
129aaebf433SRyan Anderson
13005e96e96SMasahiro Yamada	if ${no_dirty}; then
13105e96e96SMasahiro Yamada		return
13205e96e96SMasahiro Yamada	fi
13305e96e96SMasahiro Yamada
134ff64dd48SBrian Norris	# Check for uncommitted changes.
13575280bdfSMasahiro Yamada	# This script must avoid any write attempt to the source tree, which
13675280bdfSMasahiro Yamada	# might be read-only.
13775280bdfSMasahiro Yamada	# You cannot use 'git describe --dirty' because it tries to create
13875280bdfSMasahiro Yamada	# .git/index.lock .
13975280bdfSMasahiro Yamada	# First, with git-status, but --no-optional-locks is only supported in
14075280bdfSMasahiro Yamada	# git >= 2.14, so fall back to git-diff-index if it fails. Note that
14175280bdfSMasahiro Yamada	# git-diff-index does not refresh the index, so it may give misleading
14275280bdfSMasahiro Yamada	# results.
14375280bdfSMasahiro Yamada	# See git-update-index(1), git-diff-index(1), and git-status(1).
144ff64dd48SBrian Norris	if {
145ff64dd48SBrian Norris		git --no-optional-locks status -uno --porcelain 2>/dev/null ||
146ff64dd48SBrian Norris		git diff-index --name-only HEAD
147a2be76a3SMasahiro Yamada	} | read dummy; then
14824d49756SRyan Anderson		printf '%s' -dirty
149117a93dbSRene Scharfe	fi
15009155120SMichal Marek}
15109155120SMichal Marek
15209155120SMichal Marekcollect_files()
15309155120SMichal Marek{
1547a82e3faSMasahiro Yamada	local file res=
15509155120SMichal Marek
15609155120SMichal Marek	for file; do
15709155120SMichal Marek		case "$file" in
15809155120SMichal Marek		*\~*)
15909155120SMichal Marek			continue
16009155120SMichal Marek			;;
16109155120SMichal Marek		esac
16209155120SMichal Marek		if test -e "$file"; then
16309155120SMichal Marek			res="$res$(cat "$file")"
16409155120SMichal Marek		fi
16509155120SMichal Marek	done
16609155120SMichal Marek	echo "$res"
16709155120SMichal Marek}
16809155120SMichal Marek
169ec31f868SMasahiro Yamadaif [ -z "${KERNELVERSION}" ]; then
170ec31f868SMasahiro Yamada	echo "KERNELVERSION is not set" >&2
171ec31f868SMasahiro Yamada	exit 1
172ec31f868SMasahiro Yamadafi
173ec31f868SMasahiro Yamada
17409155120SMichal Marek# localversion* files in the build and source directory
175eed36d77SMasahiro Yamadafile_localversion="$(collect_files localversion*)"
17609155120SMichal Marekif test ! "$srctree" -ef .; then
177eed36d77SMasahiro Yamada	file_localversion="${file_localversion}$(collect_files "$srctree"/localversion*)"
17809155120SMichal Marekfi
17909155120SMichal Marek
18005e96e96SMasahiro Yamadaif ${no_local}; then
18105e96e96SMasahiro Yamada	echo "${KERNELVERSION}$(scm_version --no-dirty)"
18205e96e96SMasahiro Yamada	exit 0
18305e96e96SMasahiro Yamadafi
18405e96e96SMasahiro Yamada
18505e96e96SMasahiro Yamadaif ! test -e include/config/auto.conf; then
18605e96e96SMasahiro Yamada	echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
18705e96e96SMasahiro Yamada	exit 1
18805e96e96SMasahiro Yamadafi
18905e96e96SMasahiro Yamada
190eed36d77SMasahiro Yamada# version string from CONFIG_LOCALVERSION
191129ab0d2SMasahiro Yamadaconfig_localversion=$(sed -n 's/^CONFIG_LOCALVERSION=\(.*\)$/\1/p' include/config/auto.conf)
19209155120SMichal Marek
1936ab7e1f9SMasahiro Yamada# scm version string if not at the kernel version tag or at the file_localversion
1947d153696SMasahiro Yamadaif grep -q "^CONFIG_LOCALVERSION_AUTO=y$" include/config/auto.conf; then
19509155120SMichal Marek	# full scm version string
196eed36d77SMasahiro Yamada	scm_version="$(scm_version)"
1975df99becSMikulas Patockaelif [ "${LOCALVERSION+set}" != "set" ]; then
1985df99becSMikulas Patocka	# If the variable LOCALVERSION is not set, append a plus
1995df99becSMikulas Patocka	# sign if the repository is not in a clean annotated or
2005df99becSMikulas Patocka	# signed tagged state (as git describe only looks at signed
2015df99becSMikulas Patocka	# or annotated tags - git tag -a/-s).
2025df99becSMikulas Patocka	#
2035df99becSMikulas Patocka	# If the variable LOCALVERSION is set (including being set
2045df99becSMikulas Patocka	# to an empty string), we don't want to append a plus sign.
205eed36d77SMasahiro Yamada	scm_version="$(scm_version --short)"
20609155120SMichal Marekfi
20709155120SMichal Marek
208eed36d77SMasahiro Yamadaecho "${KERNELVERSION}${file_localversion}${config_localversion}${LOCALVERSION}${scm_version}"
209