xref: /openbmc/linux/scripts/setlocalversion (revision b003afe3)
1117a93dbSRene Scharfe#!/bin/sh
233252572SNico Schottelius#
333252572SNico Schottelius# This scripts adds local version information from the version
433252572SNico Schottelius# control systems git, mercurial (hg) and subversion (svn).
533252572SNico Schottelius#
633252572SNico Schottelius# If something goes wrong, send a mail the kernel build mailinglist
733252572SNico Schottelius# (see MAINTAINERS) and CC Nico Schottelius
833252572SNico Schottelius# <nico-linuxsetlocalversion -at- schottelius.org>.
933252572SNico Schottelius#
1033252572SNico Schottelius#
11aaebf433SRyan Anderson
12117a93dbSRene Scharfeusage() {
13b003afe3SMichal Marek	echo "Usage: $0 [--save-scmversion] [srctree]" >&2
14117a93dbSRene Scharfe	exit 1
15aaebf433SRyan Anderson}
16aaebf433SRyan Anderson
1709155120SMichal Marekscm_only=false
1809155120SMichal Mareksrctree=.
19b003afe3SMichal Marekif test "$1" = "--save-scmversion"; then
2009155120SMichal Marek	scm_only=true
2109155120SMichal Marek	shift
2209155120SMichal Marekfi
2309155120SMichal Marekif test $# -gt 0; then
2409155120SMichal Marek	srctree=$1
2509155120SMichal Marek	shift
2609155120SMichal Marekfi
2709155120SMichal Marekif test $# -gt 0 -o ! -d "$srctree"; then
2809155120SMichal Marek	usage
2909155120SMichal Marekfi
3009155120SMichal Marek
3109155120SMichal Marekscm_version()
3209155120SMichal Marek{
336dc0c2f3SMichał Górny	local short
346dc0c2f3SMichał Górny	short=false
3509155120SMichal Marek
3609155120SMichal Marek	cd "$srctree"
3709155120SMichal Marek	if test -e .scmversion; then
386dc0c2f3SMichał Górny		cat .scmversion
3909155120SMichal Marek		return
4009155120SMichal Marek	fi
4109155120SMichal Marek	if test "$1" = "--short"; then
4209155120SMichal Marek		short=true
4309155120SMichal Marek	fi
44aaebf433SRyan Anderson
45117a93dbSRene Scharfe	# Check for git and a git repo.
46f03b283fSTrent Piepho	if head=`git rev-parse --verify --short HEAD 2>/dev/null`; then
4733252572SNico Schottelius
4809155120SMichal Marek		# If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
4909155120SMichal Marek		# it, because this version is defined in the top level Makefile.
5033252572SNico Schottelius		if [ -z "`git describe --exact-match 2>/dev/null`" ]; then
5133252572SNico Schottelius
5209155120SMichal Marek			# If only the short version is requested, don't bother
5309155120SMichal Marek			# running further git commands
5409155120SMichal Marek			if $short; then
5509155120SMichal Marek				echo "+"
5609155120SMichal Marek				return
5709155120SMichal Marek			fi
5809155120SMichal Marek			# If we are past a tagged commit (like
5909155120SMichal Marek			# "v2.6.30-rc5-302-g72357d5"), we pretty print it.
6033252572SNico Schottelius			if atag="`git describe 2>/dev/null`"; then
61a182ad3dSNico Schottelius				echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),$(NF))}'
6233252572SNico Schottelius
6333252572SNico Schottelius			# If we don't have a tag at all we print -g{commitish}.
64f03b283fSTrent Piepho			else
65f03b283fSTrent Piepho				printf '%s%s' -g $head
6656b2f070SSebastian Siewior			fi
6733252572SNico Schottelius		fi
68aaebf433SRyan Anderson
69ff80aa97SPeter Korsgaard		# Is this git on svn?
70ff80aa97SPeter Korsgaard		if git config --get svn-remote.svn.url >/dev/null; then
714774bb1cSPeter Korsgaard			printf -- '-svn%s' "`git svn find-rev $head`"
72ff80aa97SPeter Korsgaard		fi
73ff80aa97SPeter Korsgaard
74a2bb90a0SNico Schottelius		# Update index only on r/w media
75a2bb90a0SNico Schottelius		[ -w . ] && git update-index --refresh --unmerged > /dev/null
76a2bb90a0SNico Schottelius
77a2bb90a0SNico Schottelius		# Check for uncommitted changes
78b052ce4cSTheodore Ts'o		if git diff-index --name-only HEAD | grep -v "^scripts/package" \
79b052ce4cSTheodore Ts'o		    | read dummy; then
8024d49756SRyan Anderson			printf '%s' -dirty
81117a93dbSRene Scharfe		fi
823dce174cSAron Griffis
833dce174cSAron Griffis		# All done with git
8409155120SMichal Marek		return
853dce174cSAron Griffis	fi
863dce174cSAron Griffis
873dce174cSAron Griffis	# Check for mercurial and a mercurial repo.
883dce174cSAron Griffis	if hgid=`hg id 2>/dev/null`; then
893dce174cSAron Griffis		tag=`printf '%s' "$hgid" | cut -d' ' -f2`
903dce174cSAron Griffis
913dce174cSAron Griffis		# Do we have an untagged version?
923dce174cSAron Griffis		if [ -z "$tag" -o "$tag" = tip ]; then
933dce174cSAron Griffis			id=`printf '%s' "$hgid" | sed 's/[+ ].*//'`
943dce174cSAron Griffis			printf '%s%s' -hg "$id"
953dce174cSAron Griffis		fi
963dce174cSAron Griffis
973dce174cSAron Griffis		# Are there uncommitted changes?
983dce174cSAron Griffis		# These are represented by + after the changeset id.
993dce174cSAron Griffis		case "$hgid" in
1003dce174cSAron Griffis			*+|*+\ *) printf '%s' -dirty ;;
1013dce174cSAron Griffis		esac
1023dce174cSAron Griffis
1033dce174cSAron Griffis		# All done with mercurial
10409155120SMichal Marek		return
105117a93dbSRene Scharfe	fi
106ba3d05fbSBryan Wu
107ba3d05fbSBryan Wu	# Check for svn and a svn repo.
108167d6a02SPeter Korsgaard	if rev=`svn info 2>/dev/null | grep '^Last Changed Rev'`; then
109ba3d05fbSBryan Wu		rev=`echo $rev | awk '{print $NF}'`
110ba3d05fbSBryan Wu		printf -- '-svn%s' "$rev"
111ba3d05fbSBryan Wu
112ba3d05fbSBryan Wu		# All done with svn
11309155120SMichal Marek		return
11409155120SMichal Marek	fi
11509155120SMichal Marek}
11609155120SMichal Marek
11709155120SMichal Marekcollect_files()
11809155120SMichal Marek{
11909155120SMichal Marek	local file res
12009155120SMichal Marek
12109155120SMichal Marek	for file; do
12209155120SMichal Marek		case "$file" in
12309155120SMichal Marek		*\~*)
12409155120SMichal Marek			continue
12509155120SMichal Marek			;;
12609155120SMichal Marek		esac
12709155120SMichal Marek		if test -e "$file"; then
12809155120SMichal Marek			res="$res$(cat "$file")"
12909155120SMichal Marek		fi
13009155120SMichal Marek	done
13109155120SMichal Marek	echo "$res"
13209155120SMichal Marek}
13309155120SMichal Marek
13409155120SMichal Marekif $scm_only; then
135b003afe3SMichal Marek	if test ! -e .scmversion; then
136b003afe3SMichal Marek		res=$(scm_version)
137b003afe3SMichal Marek		echo "$res" >.scmversion
138b003afe3SMichal Marek	fi
139ba3d05fbSBryan Wu	exit
140ba3d05fbSBryan Wufi
14109155120SMichal Marek
14209155120SMichal Marekif test -e include/config/auto.conf; then
1436dc0c2f3SMichał Górny	. include/config/auto.conf
14409155120SMichal Marekelse
14509155120SMichal Marek	echo "Error: kernelrelease not valid - run 'make prepare' to update it"
14609155120SMichal Marek	exit 1
14709155120SMichal Marekfi
14809155120SMichal Marek
14909155120SMichal Marek# localversion* files in the build and source directory
15009155120SMichal Marekres="$(collect_files localversion*)"
15109155120SMichal Marekif test ! "$srctree" -ef .; then
15209155120SMichal Marek	res="$res$(collect_files "$srctree"/localversion*)"
15309155120SMichal Marekfi
15409155120SMichal Marek
15509155120SMichal Marek# CONFIG_LOCALVERSION and LOCALVERSION (if set)
15609155120SMichal Marekres="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"
15709155120SMichal Marek
15809155120SMichal Marek# scm version string if not at a tagged commit
15909155120SMichal Marekif test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
16009155120SMichal Marek	# full scm version string
16109155120SMichal Marek	res="$res$(scm_version)"
16209155120SMichal Marekelse
16309155120SMichal Marek	# apped a plus sign if the repository is not in a clean tagged
16409155120SMichal Marek	# state and  LOCALVERSION= is not specified
16509155120SMichal Marek	if test "${LOCALVERSION+set}" != "set"; then
16609155120SMichal Marek		scm=$(scm_version --short)
16709155120SMichal Marek		res="$res${scm:++}"
16809155120SMichal Marek	fi
16909155120SMichal Marekfi
17009155120SMichal Marek
17109155120SMichal Marekecho "$res"
172