xref: /linux/scripts/setlocalversion (revision 09155120cf0ffe6f0c8aba3aa625831405b65996)
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() {
13*09155120SMichal Marek	echo "Usage: $0 [--scm-only] [srctree]" >&2
14117a93dbSRene Scharfe	exit 1
15aaebf433SRyan Anderson}
16aaebf433SRyan Anderson
17*09155120SMichal Marekscm_only=false
18*09155120SMichal Mareksrctree=.
19*09155120SMichal Marekif test "$1" = "--scm-only"; then
20*09155120SMichal Marek	scm_only=true
21*09155120SMichal Marek	shift
22*09155120SMichal Marekfi
23*09155120SMichal Marekif test $# -gt 0; then
24*09155120SMichal Marek	srctree=$1
25*09155120SMichal Marek	shift
26*09155120SMichal Marekfi
27*09155120SMichal Marekif test $# -gt 0 -o ! -d "$srctree"; then
28*09155120SMichal Marek	usage
29*09155120SMichal Marekfi
30*09155120SMichal Marek
31*09155120SMichal Marekscm_version()
32*09155120SMichal Marek{
33*09155120SMichal Marek	local short=false
34*09155120SMichal Marek
35*09155120SMichal Marek	cd "$srctree"
36*09155120SMichal Marek	if test -e .scmversion; then
37*09155120SMichal Marek		cat "$_"
38*09155120SMichal Marek		return
39*09155120SMichal Marek	fi
40*09155120SMichal Marek	if test "$1" = "--short"; then
41*09155120SMichal Marek		short=true
42*09155120SMichal Marek	fi
43aaebf433SRyan Anderson
44117a93dbSRene Scharfe	# Check for git and a git repo.
45f03b283fSTrent Piepho	if head=`git rev-parse --verify --short HEAD 2>/dev/null`; then
4633252572SNico Schottelius
47*09155120SMichal Marek		# If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
48*09155120SMichal Marek		# it, because this version is defined in the top level Makefile.
4933252572SNico Schottelius		if [ -z "`git describe --exact-match 2>/dev/null`" ]; then
5033252572SNico Schottelius
51*09155120SMichal Marek			# If only the short version is requested, don't bother
52*09155120SMichal Marek			# running further git commands
53*09155120SMichal Marek			if $short; then
54*09155120SMichal Marek				echo "+"
55*09155120SMichal Marek				return
56*09155120SMichal Marek			fi
57*09155120SMichal Marek			# If we are past a tagged commit (like
58*09155120SMichal Marek			# "v2.6.30-rc5-302-g72357d5"), we pretty print it.
5933252572SNico Schottelius			if atag="`git describe 2>/dev/null`"; then
60a182ad3dSNico Schottelius				echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),$(NF))}'
6133252572SNico Schottelius
6233252572SNico Schottelius			# If we don't have a tag at all we print -g{commitish}.
63f03b283fSTrent Piepho			else
64f03b283fSTrent Piepho				printf '%s%s' -g $head
6556b2f070SSebastian Siewior			fi
6633252572SNico Schottelius		fi
67aaebf433SRyan Anderson
68ff80aa97SPeter Korsgaard		# Is this git on svn?
69ff80aa97SPeter Korsgaard		if git config --get svn-remote.svn.url >/dev/null; then
704774bb1cSPeter Korsgaard			printf -- '-svn%s' "`git svn find-rev $head`"
71ff80aa97SPeter Korsgaard		fi
72ff80aa97SPeter Korsgaard
73a2bb90a0SNico Schottelius		# Update index only on r/w media
74a2bb90a0SNico Schottelius		[ -w . ] && git update-index --refresh --unmerged > /dev/null
75a2bb90a0SNico Schottelius
76a2bb90a0SNico Schottelius		# Check for uncommitted changes
77b052ce4cSTheodore Ts'o		if git diff-index --name-only HEAD | grep -v "^scripts/package" \
78b052ce4cSTheodore Ts'o		    | read dummy; then
7924d49756SRyan Anderson			printf '%s' -dirty
80117a93dbSRene Scharfe		fi
813dce174cSAron Griffis
823dce174cSAron Griffis		# All done with git
83*09155120SMichal Marek		return
843dce174cSAron Griffis	fi
853dce174cSAron Griffis
863dce174cSAron Griffis	# Check for mercurial and a mercurial repo.
873dce174cSAron Griffis	if hgid=`hg id 2>/dev/null`; then
883dce174cSAron Griffis		tag=`printf '%s' "$hgid" | cut -d' ' -f2`
893dce174cSAron Griffis
903dce174cSAron Griffis		# Do we have an untagged version?
913dce174cSAron Griffis		if [ -z "$tag" -o "$tag" = tip ]; then
923dce174cSAron Griffis			id=`printf '%s' "$hgid" | sed 's/[+ ].*//'`
933dce174cSAron Griffis			printf '%s%s' -hg "$id"
943dce174cSAron Griffis		fi
953dce174cSAron Griffis
963dce174cSAron Griffis		# Are there uncommitted changes?
973dce174cSAron Griffis		# These are represented by + after the changeset id.
983dce174cSAron Griffis		case "$hgid" in
993dce174cSAron Griffis			*+|*+\ *) printf '%s' -dirty ;;
1003dce174cSAron Griffis		esac
1013dce174cSAron Griffis
1023dce174cSAron Griffis		# All done with mercurial
103*09155120SMichal Marek		return
104117a93dbSRene Scharfe	fi
105ba3d05fbSBryan Wu
106ba3d05fbSBryan Wu	# Check for svn and a svn repo.
107167d6a02SPeter Korsgaard	if rev=`svn info 2>/dev/null | grep '^Last Changed Rev'`; then
108ba3d05fbSBryan Wu		rev=`echo $rev | awk '{print $NF}'`
109ba3d05fbSBryan Wu		printf -- '-svn%s' "$rev"
110ba3d05fbSBryan Wu
111ba3d05fbSBryan Wu		# All done with svn
112*09155120SMichal Marek		return
113*09155120SMichal Marek	fi
114*09155120SMichal Marek}
115*09155120SMichal Marek
116*09155120SMichal Marekcollect_files()
117*09155120SMichal Marek{
118*09155120SMichal Marek	local file res
119*09155120SMichal Marek
120*09155120SMichal Marek	for file; do
121*09155120SMichal Marek		case "$file" in
122*09155120SMichal Marek		*\~*)
123*09155120SMichal Marek			continue
124*09155120SMichal Marek			;;
125*09155120SMichal Marek		esac
126*09155120SMichal Marek		if test -e "$file"; then
127*09155120SMichal Marek			res="$res$(cat "$file")"
128*09155120SMichal Marek		fi
129*09155120SMichal Marek	done
130*09155120SMichal Marek	echo "$res"
131*09155120SMichal Marek}
132*09155120SMichal Marek
133*09155120SMichal Marekif $scm_only; then
134*09155120SMichal Marek	scm_version
135ba3d05fbSBryan Wu	exit
136ba3d05fbSBryan Wufi
137*09155120SMichal Marek
138*09155120SMichal Marekif test -e include/config/auto.conf; then
139*09155120SMichal Marek	source "$_"
140*09155120SMichal Marekelse
141*09155120SMichal Marek	echo "Error: kernelrelease not valid - run 'make prepare' to update it"
142*09155120SMichal Marek	exit 1
143*09155120SMichal Marekfi
144*09155120SMichal Marek
145*09155120SMichal Marek# localversion* files in the build and source directory
146*09155120SMichal Marekres="$(collect_files localversion*)"
147*09155120SMichal Marekif test ! "$srctree" -ef .; then
148*09155120SMichal Marek	res="$res$(collect_files "$srctree"/localversion*)"
149*09155120SMichal Marekfi
150*09155120SMichal Marek
151*09155120SMichal Marek# CONFIG_LOCALVERSION and LOCALVERSION (if set)
152*09155120SMichal Marekres="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"
153*09155120SMichal Marek
154*09155120SMichal Marek# scm version string if not at a tagged commit
155*09155120SMichal Marekif test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
156*09155120SMichal Marek	# full scm version string
157*09155120SMichal Marek	res="$res$(scm_version)"
158*09155120SMichal Marekelse
159*09155120SMichal Marek	# apped a plus sign if the repository is not in a clean tagged
160*09155120SMichal Marek	# state and  LOCALVERSION= is not specified
161*09155120SMichal Marek	if test "${LOCALVERSION+set}" != "set"; then
162*09155120SMichal Marek		scm=$(scm_version --short)
163*09155120SMichal Marek		res="$res${scm:++}"
164*09155120SMichal Marek	fi
165*09155120SMichal Marekfi
166*09155120SMichal Marek
167*09155120SMichal Marekecho "$res"
168