xref: /linux/scripts/setlocalversion (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1117a93dbSRene Scharfe#!/bin/sh
2*b2441318SGreg 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)" &&
487593e090SFranck Bui-Huu	   head=`git rev-parse --verify --short 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.
5233252572SNico Schottelius		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.
6233252572SNico Schottelius			if atag="`git describe 2>/dev/null`"; then
63a182ad3dSNico Schottelius				echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),$(NF))}'
6433252572SNico Schottelius
6533252572SNico Schottelius			# If we don't have a tag at all we print -g{commitish}.
66f03b283fSTrent Piepho			else
67f03b283fSTrent Piepho				printf '%s%s' -g $head
6856b2f070SSebastian Siewior			fi
6933252572SNico Schottelius		fi
70aaebf433SRyan Anderson
71ff80aa97SPeter Korsgaard		# Is this git on svn?
72ff80aa97SPeter Korsgaard		if git config --get svn-remote.svn.url >/dev/null; then
734774bb1cSPeter Korsgaard			printf -- '-svn%s' "`git svn find-rev $head`"
74ff80aa97SPeter Korsgaard		fi
75ff80aa97SPeter Korsgaard
76a2bb90a0SNico Schottelius		# Check for uncommitted changes
777f3bd6c9SRoland Dreier		if git diff-index --name-only HEAD | grep -qv "^scripts/package"; then
7824d49756SRyan Anderson			printf '%s' -dirty
79117a93dbSRene Scharfe		fi
803dce174cSAron Griffis
813dce174cSAron Griffis		# All done with git
8209155120SMichal Marek		return
833dce174cSAron Griffis	fi
843dce174cSAron Griffis
853dce174cSAron Griffis	# Check for mercurial and a mercurial repo.
868558f59eSMichal Marek	if test -d .hg && hgid=`hg id 2>/dev/null`; then
8738b3439dSMike Crowe		# Do we have an tagged version?  If so, latesttagdistance == 1
8838b3439dSMike Crowe		if [ "`hg log -r . --template '{latesttagdistance}'`" == "1" ]; then
8938b3439dSMike Crowe			id=`hg log -r . --template '{latesttag}'`
9038b3439dSMike Crowe			printf '%s%s' -hg "$id"
9138b3439dSMike Crowe		else
9238b3439dSMike Crowe			tag=`printf '%s' "$hgid" | cut -d' ' -f2`
933dce174cSAron Griffis			if [ -z "$tag" -o "$tag" = tip ]; then
943dce174cSAron Griffis				id=`printf '%s' "$hgid" | sed 's/[+ ].*//'`
953dce174cSAron Griffis				printf '%s%s' -hg "$id"
963dce174cSAron Griffis			fi
9738b3439dSMike Crowe		fi
983dce174cSAron Griffis
993dce174cSAron Griffis		# Are there uncommitted changes?
1003dce174cSAron Griffis		# These are represented by + after the changeset id.
1013dce174cSAron Griffis		case "$hgid" in
1023dce174cSAron Griffis			*+|*+\ *) printf '%s' -dirty ;;
1033dce174cSAron Griffis		esac
1043dce174cSAron Griffis
1053dce174cSAron Griffis		# All done with mercurial
10609155120SMichal Marek		return
107117a93dbSRene Scharfe	fi
108ba3d05fbSBryan Wu
109ba3d05fbSBryan Wu	# Check for svn and a svn repo.
110f893bfb6SChristophe Leroy	if rev=`LANG= LC_ALL= LC_MESSAGES=C svn info 2>/dev/null | grep '^Last Changed Rev'`; then
111ba3d05fbSBryan Wu		rev=`echo $rev | awk '{print $NF}'`
112ba3d05fbSBryan Wu		printf -- '-svn%s' "$rev"
113ba3d05fbSBryan Wu
114ba3d05fbSBryan Wu		# All done with svn
11509155120SMichal Marek		return
11609155120SMichal Marek	fi
11709155120SMichal Marek}
11809155120SMichal Marek
11909155120SMichal Marekcollect_files()
12009155120SMichal Marek{
12109155120SMichal Marek	local file res
12209155120SMichal Marek
12309155120SMichal Marek	for file; do
12409155120SMichal Marek		case "$file" in
12509155120SMichal Marek		*\~*)
12609155120SMichal Marek			continue
12709155120SMichal Marek			;;
12809155120SMichal Marek		esac
12909155120SMichal Marek		if test -e "$file"; then
13009155120SMichal Marek			res="$res$(cat "$file")"
13109155120SMichal Marek		fi
13209155120SMichal Marek	done
13309155120SMichal Marek	echo "$res"
13409155120SMichal Marek}
13509155120SMichal Marek
13609155120SMichal Marekif $scm_only; then
137b003afe3SMichal Marek	if test ! -e .scmversion; then
138b003afe3SMichal Marek		res=$(scm_version)
139b003afe3SMichal Marek		echo "$res" >.scmversion
140b003afe3SMichal Marek	fi
141ba3d05fbSBryan Wu	exit
142ba3d05fbSBryan Wufi
14309155120SMichal Marek
14409155120SMichal Marekif test -e include/config/auto.conf; then
1456dc0c2f3SMichał Górny	. include/config/auto.conf
14609155120SMichal Marekelse
14778283edfSWolfram Sang	echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
14809155120SMichal Marek	exit 1
14909155120SMichal Marekfi
15009155120SMichal Marek
15109155120SMichal Marek# localversion* files in the build and source directory
15209155120SMichal Marekres="$(collect_files localversion*)"
15309155120SMichal Marekif test ! "$srctree" -ef .; then
15409155120SMichal Marek	res="$res$(collect_files "$srctree"/localversion*)"
15509155120SMichal Marekfi
15609155120SMichal Marek
15709155120SMichal Marek# CONFIG_LOCALVERSION and LOCALVERSION (if set)
15809155120SMichal Marekres="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"
15909155120SMichal Marek
16009155120SMichal Marek# scm version string if not at a tagged commit
16109155120SMichal Marekif test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
16209155120SMichal Marek	# full scm version string
16309155120SMichal Marek	res="$res$(scm_version)"
16409155120SMichal Marekelse
165c3e2f196SMichael Prokop	# append a plus sign if the repository is not in a clean
166c3e2f196SMichael Prokop	# annotated or signed tagged state (as git describe only
167c3e2f196SMichael Prokop	# looks at signed or annotated tags - git tag -a/-s) and
168c3e2f196SMichael Prokop	# LOCALVERSION= is not specified
16909155120SMichal Marek	if test "${LOCALVERSION+set}" != "set"; then
17009155120SMichal Marek		scm=$(scm_version --short)
17109155120SMichal Marek		res="$res${scm:++}"
17209155120SMichal Marek	fi
17309155120SMichal Marekfi
17409155120SMichal Marek
17509155120SMichal Marekecho "$res"
176