xref: /linux/scripts/setlocalversion (revision 630ff0faf84eac6448c851961d4865471a792160)
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
533252572SNico Schottelius# control systems git, mercurial (hg) and subversion (svn).
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() {
14b003afe3SMichal Marek	echo "Usage: $0 [--save-scmversion] [srctree]" >&2
15117a93dbSRene Scharfe	exit 1
16aaebf433SRyan Anderson}
17aaebf433SRyan Anderson
1809155120SMichal Marekscm_only=false
1909155120SMichal Mareksrctree=.
20b003afe3SMichal Marekif test "$1" = "--save-scmversion"; then
2109155120SMichal Marek	scm_only=true
2209155120SMichal Marek	shift
2309155120SMichal Marekfi
2409155120SMichal Marekif test $# -gt 0; then
2509155120SMichal Marek	srctree=$1
2609155120SMichal Marek	shift
2709155120SMichal Marekfi
2809155120SMichal Marekif test $# -gt 0 -o ! -d "$srctree"; then
2909155120SMichal Marek	usage
3009155120SMichal Marekfi
3109155120SMichal Marek
3209155120SMichal Marekscm_version()
3309155120SMichal Marek{
346dc0c2f3SMichał Górny	local short
356dc0c2f3SMichał Górny	short=false
3609155120SMichal Marek
3709155120SMichal Marek	cd "$srctree"
3809155120SMichal Marek	if test -e .scmversion; then
396dc0c2f3SMichał Górny		cat .scmversion
4009155120SMichal Marek		return
4109155120SMichal Marek	fi
4209155120SMichal Marek	if test "$1" = "--short"; then
4309155120SMichal Marek		short=true
4409155120SMichal Marek	fi
45aaebf433SRyan Anderson
46117a93dbSRene Scharfe	# Check for git and a git repo.
477593e090SFranck Bui-Huu	if test -z "$(git rev-parse --show-cdup 2>/dev/null)" &&
48548b8b51SRasmus Villemoes	   head=$(git rev-parse --verify HEAD 2>/dev/null); then
4933252572SNico Schottelius
5009155120SMichal Marek		# If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
5109155120SMichal Marek		# it, because this version is defined in the top level Makefile.
523c96bdd0SBhaskar Chowdhury		if [ -z "$(git describe --exact-match 2>/dev/null)" ]; then
5333252572SNico Schottelius
5409155120SMichal Marek			# If only the short version is requested, don't bother
5509155120SMichal Marek			# running further git commands
5609155120SMichal Marek			if $short; then
5709155120SMichal Marek				echo "+"
5809155120SMichal Marek				return
5909155120SMichal Marek			fi
6009155120SMichal Marek			# If we are past a tagged commit (like
6109155120SMichal Marek			# "v2.6.30-rc5-302-g72357d5"), we pretty print it.
62*630ff0faSMasahiro Yamada			if atag="$(git describe 2>/dev/null)"; then
63*630ff0faSMasahiro Yamada				echo "$atag" | awk -F- '{printf("-%05d", $(NF-1))}'
6456b2f070SSebastian Siewior			fi
65*630ff0faSMasahiro Yamada
66*630ff0faSMasahiro Yamada			# Add -g and exactly 12 hex chars.
67*630ff0faSMasahiro Yamada			printf '%s%s' -g "$(echo $head | cut -c1-12)"
6833252572SNico Schottelius		fi
69aaebf433SRyan Anderson
70ff64dd48SBrian Norris		# Check for uncommitted changes.
71ffaf62a8SMasahiro Yamada		# This script must avoid any write attempt to the source tree,
72ffaf62a8SMasahiro Yamada		# which might be read-only.
73ffaf62a8SMasahiro Yamada		# You cannot use 'git describe --dirty' because it tries to
74ffaf62a8SMasahiro Yamada		# create .git/index.lock .
75ff64dd48SBrian Norris		# First, with git-status, but --no-optional-locks is only
76ff64dd48SBrian Norris		# supported in git >= 2.14, so fall back to git-diff-index if
77ff64dd48SBrian Norris		# it fails. Note that git-diff-index does not refresh the
78ff64dd48SBrian Norris		# index, so it may give misleading results. See
79ff64dd48SBrian Norris		# git-update-index(1), git-diff-index(1), and git-status(1).
80ff64dd48SBrian Norris		if {
81ff64dd48SBrian Norris			git --no-optional-locks status -uno --porcelain 2>/dev/null ||
82ff64dd48SBrian Norris			git diff-index --name-only HEAD
83a2be76a3SMasahiro Yamada		} | read dummy; then
8424d49756SRyan Anderson			printf '%s' -dirty
85117a93dbSRene Scharfe		fi
8609155120SMichal Marek	fi
8709155120SMichal Marek}
8809155120SMichal Marek
8909155120SMichal Marekcollect_files()
9009155120SMichal Marek{
917a82e3faSMasahiro Yamada	local file res=
9209155120SMichal Marek
9309155120SMichal Marek	for file; do
9409155120SMichal Marek		case "$file" in
9509155120SMichal Marek		*\~*)
9609155120SMichal Marek			continue
9709155120SMichal Marek			;;
9809155120SMichal Marek		esac
9909155120SMichal Marek		if test -e "$file"; then
10009155120SMichal Marek			res="$res$(cat "$file")"
10109155120SMichal Marek		fi
10209155120SMichal Marek	done
10309155120SMichal Marek	echo "$res"
10409155120SMichal Marek}
10509155120SMichal Marek
10609155120SMichal Marekif $scm_only; then
107b003afe3SMichal Marek	if test ! -e .scmversion; then
108b003afe3SMichal Marek		res=$(scm_version)
109b003afe3SMichal Marek		echo "$res" >.scmversion
110b003afe3SMichal Marek	fi
111ba3d05fbSBryan Wu	exit
112ba3d05fbSBryan Wufi
11309155120SMichal Marek
11409155120SMichal Marekif test -e include/config/auto.conf; then
1156dc0c2f3SMichał Górny	. include/config/auto.conf
11609155120SMichal Marekelse
11778283edfSWolfram Sang	echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
11809155120SMichal Marek	exit 1
11909155120SMichal Marekfi
12009155120SMichal Marek
12109155120SMichal Marek# localversion* files in the build and source directory
12209155120SMichal Marekres="$(collect_files localversion*)"
12309155120SMichal Marekif test ! "$srctree" -ef .; then
12409155120SMichal Marek	res="$res$(collect_files "$srctree"/localversion*)"
12509155120SMichal Marekfi
12609155120SMichal Marek
12709155120SMichal Marek# CONFIG_LOCALVERSION and LOCALVERSION (if set)
12809155120SMichal Marekres="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"
12909155120SMichal Marek
13009155120SMichal Marek# scm version string if not at a tagged commit
13109155120SMichal Marekif test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
13209155120SMichal Marek	# full scm version string
13309155120SMichal Marek	res="$res$(scm_version)"
13409155120SMichal Marekelse
135c3e2f196SMichael Prokop	# append a plus sign if the repository is not in a clean
136c3e2f196SMichael Prokop	# annotated or signed tagged state (as git describe only
137c3e2f196SMichael Prokop	# looks at signed or annotated tags - git tag -a/-s) and
138c3e2f196SMichael Prokop	# LOCALVERSION= is not specified
13909155120SMichal Marek	if test "${LOCALVERSION+set}" != "set"; then
14009155120SMichal Marek		scm=$(scm_version --short)
14109155120SMichal Marek		res="$res${scm:++}"
14209155120SMichal Marek	fi
14309155120SMichal Marekfi
14409155120SMichal Marek
14509155120SMichal Marekecho "$res"
146