xref: /OK3568_Linux_fs/buildroot/support/scripts/setlocalversion (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#!/bin/sh
2*4882a593Smuzhiyun#
3*4882a593Smuzhiyun# This scripts adds local version information from the version
4*4882a593Smuzhiyun# control systems git, mercurial (hg) and subversion (svn).
5*4882a593Smuzhiyun#
6*4882a593Smuzhiyun# If something goes wrong, send a mail the kernel build mailinglist
7*4882a593Smuzhiyun# (see MAINTAINERS) and CC Nico Schottelius
8*4882a593Smuzhiyun# <nico-linuxsetlocalversion -at- schottelius.org>.
9*4882a593Smuzhiyun#
10*4882a593Smuzhiyun#
11*4882a593Smuzhiyun
12*4882a593Smuzhiyunusage() {
13*4882a593Smuzhiyun	echo "Usage: $0 [srctree]" >&2
14*4882a593Smuzhiyun	exit 1
15*4882a593Smuzhiyun}
16*4882a593Smuzhiyun
17*4882a593Smuzhiyuncd "${1:-.}" || usage
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun# Check for git and a git repo.
20*4882a593Smuzhiyunif head=`git rev-parse --verify --short HEAD 2>/dev/null`; then
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun	atag="`git describe 2>/dev/null`"
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun	# Show -g<commit> if we have no tag, or just the tag
25*4882a593Smuzhiyun	# otherwise.
26*4882a593Smuzhiyun	if [ -z "${atag}" ] ; then
27*4882a593Smuzhiyun		printf "%s%s" -g ${head}
28*4882a593Smuzhiyun	else
29*4882a593Smuzhiyun		printf ${atag}
30*4882a593Smuzhiyun	fi
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun	# Is this git on svn?
33*4882a593Smuzhiyun	if git config --get svn-remote.svn.url >/dev/null; then
34*4882a593Smuzhiyun	        printf -- '-svn%s' "`git svn find-rev $head`"
35*4882a593Smuzhiyun	fi
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun	# Update index only on r/w media
38*4882a593Smuzhiyun	[ -w . ] && git update-index --refresh --unmerged > /dev/null
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun	# Check for uncommitted changes
41*4882a593Smuzhiyun	if git diff-index --name-only HEAD | grep -v "^scripts/package" \
42*4882a593Smuzhiyun	    | read dummy; then
43*4882a593Smuzhiyun		printf '%s' -dirty
44*4882a593Smuzhiyun	fi
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun	# All done with git
47*4882a593Smuzhiyun	exit
48*4882a593Smuzhiyunfi
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun# Check for mercurial and a mercurial repo.
51*4882a593Smuzhiyun# In the git case, 'git describe' will show the latest tag, and unless we are
52*4882a593Smuzhiyun# exactly on that tag, the number of commits since then, and last commit id.
53*4882a593Smuzhiyun# Mimic something similar in the Mercurial case.
54*4882a593Smuzhiyunif hgid=`HGRCPATH= hg id --id --tags 2>/dev/null`; then
55*4882a593Smuzhiyun	tag=`printf '%s' "$hgid" | cut -d' ' -f2 --only-delimited`
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun	# Do we have an untagged version?
58*4882a593Smuzhiyun	if [ -z "$tag" -o "$tag" = tip ]; then
59*4882a593Smuzhiyun		# current revision is not tagged, determine latest tag
60*4882a593Smuzhiyun		latesttag=`HGRCPATH= hg log -r. -T '{latesttag}' 2>/dev/null`
61*4882a593Smuzhiyun		# In case there is more than one tag on the latest tagged commit,
62*4882a593Smuzhiyun		# 'latesttag' will separate them by colon (:). We'll retain this.
63*4882a593Smuzhiyun		# In case there is no tag at all, 'null' will be returned.
64*4882a593Smuzhiyun		if [ "$latesttag" = "null" ]; then
65*4882a593Smuzhiyun			latesttag=''
66*4882a593Smuzhiyun		fi
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun		# add the commit id
69*4882a593Smuzhiyun		id=`printf '%s' "$hgid" | sed 's/[+ ].*//'`
70*4882a593Smuzhiyun		printf '%s%s%s' "${latesttag}" -hg "$id"
71*4882a593Smuzhiyun	else
72*4882a593Smuzhiyun		# current revision is tagged, just print the tag
73*4882a593Smuzhiyun		printf ${tag}
74*4882a593Smuzhiyun	fi
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun	# Are there uncommitted changes?
77*4882a593Smuzhiyun	# These are represented by + after the changeset id.
78*4882a593Smuzhiyun	case "$hgid" in
79*4882a593Smuzhiyun		*+|*+\ *) printf '%s' -dirty ;;
80*4882a593Smuzhiyun	esac
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun	# All done with mercurial
83*4882a593Smuzhiyun	exit
84*4882a593Smuzhiyunfi
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun# Check for svn and a svn repo.
87*4882a593Smuzhiyunif rev=`LC_ALL=C svn info 2>/dev/null | grep '^Last Changed Rev'`; then
88*4882a593Smuzhiyun	rev=`echo $rev | awk '{print $NF}'`
89*4882a593Smuzhiyun	printf -- '-svn%s' "$rev"
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun	# All done with svn
92*4882a593Smuzhiyun	exit
93*4882a593Smuzhiyunfi
94