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