blob: d2690c3a1c186736bc23ab540858af23569a11e0 [file] [log] [blame]
Rene Scharfe117a93d2006-01-04 20:42:03 +01001#!/bin/sh
Nico Schottelius33252572009-05-16 14:00:56 +02002#
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#
Ryan Andersonaaebf432005-07-31 04:57:49 -040011
Rene Scharfe117a93d2006-01-04 20:42:03 +010012usage() {
Michal Marekb003afe2010-07-15 10:36:37 +020013 echo "Usage: $0 [--save-scmversion] [srctree]" >&2
Rene Scharfe117a93d2006-01-04 20:42:03 +010014 exit 1
Ryan Andersonaaebf432005-07-31 04:57:49 -040015}
16
Michal Marek09155122010-06-17 15:14:58 +020017scm_only=false
18srctree=.
Michal Marekb003afe2010-07-15 10:36:37 +020019if test "$1" = "--save-scmversion"; then
Michal Marek09155122010-06-17 15:14:58 +020020 scm_only=true
21 shift
22fi
23if test $# -gt 0; then
24 srctree=$1
25 shift
26fi
27if test $# -gt 0 -o ! -d "$srctree"; then
28 usage
29fi
Ryan Andersonaaebf432005-07-31 04:57:49 -040030
Michal Marek09155122010-06-17 15:14:58 +020031scm_version()
32{
Michał Górny6dc0c2f2010-07-18 10:26:40 +020033 local short
34 short=false
Nico Schottelius33252572009-05-16 14:00:56 +020035
Michal Marek09155122010-06-17 15:14:58 +020036 cd "$srctree"
37 if test -e .scmversion; then
Michał Górny6dc0c2f2010-07-18 10:26:40 +020038 cat .scmversion
Michal Marek09155122010-06-17 15:14:58 +020039 return
40 fi
41 if test "$1" = "--short"; then
42 short=true
43 fi
Nico Schottelius33252572009-05-16 14:00:56 +020044
Michal Marek09155122010-06-17 15:14:58 +020045 # Check for git and a git repo.
Michal Marek8558f592010-08-16 17:09:52 +020046 if test -d .git && head=`git rev-parse --verify --short HEAD 2>/dev/null`; then
Nico Schottelius33252572009-05-16 14:00:56 +020047
Michal Marek09155122010-06-17 15:14:58 +020048 # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
49 # it, because this version is defined in the top level Makefile.
Stephen Boydcd7c0ee2012-03-08 13:50:46 -080050 if atag="`git describe --exact-match --abbrev=0 2>/dev/null`"; then
51 # Make sure we're at the tag that matches the Makefile.
52 # If not place the hash of the tag as well for
53 # v2.6.30-rc5-g314aef
54 if [ "x$atag" -ne "x$VERSION" ]; then
55 # If only the short version is requested,
56 # don't bother running further git commands
57 if $short; then
58 echo "+"
59 return
60 fi
61 printf '%s%s' -g "`git show-ref -s --abbrev $atag 2>/dev/null`"
62 fi
63 else
Michal Marek09155122010-06-17 15:14:58 +020064
65 # If only the short version is requested, don't bother
66 # running further git commands
67 if $short; then
68 echo "+"
69 return
70 fi
Stephen Boydcd7c0ee2012-03-08 13:50:46 -080071 # If we are past a tagged commit (like
72 # "v2.6.30-rc5-302-g72357d5"), we pretty print it and
73 # include the hash of any new tag on top.
74 if atag="`git describe 2>/dev/null`"; then
75 tag="`git describe --abbrev=0 2>/dev/null`"
76 commit="`echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),$(NF))}'`"
77 printf '%s%s%s' -g "`git show-ref -s --abbrev $tag 2>/dev/null`" $commit
78 # If we don't have a tag at all we print -g{commitish}.
79 else
80 printf '%s%s' -g $head
81 fi
Nico Schottelius33252572009-05-16 14:00:56 +020082 fi
Michal Marek09155122010-06-17 15:14:58 +020083
84 # Is this git on svn?
85 if git config --get svn-remote.svn.url >/dev/null; then
86 printf -- '-svn%s' "`git svn find-rev $head`"
87 fi
88
89 # Update index only on r/w media
90 [ -w . ] && git update-index --refresh --unmerged > /dev/null
91
92 # Check for uncommitted changes
93 if git diff-index --name-only HEAD | grep -v "^scripts/package" \
94 | read dummy; then
95 printf '%s' -dirty
96 fi
97
98 # All done with git
99 return
Rene Scharfe117a93d2006-01-04 20:42:03 +0100100 fi
Ryan Andersonaaebf432005-07-31 04:57:49 -0400101
Michal Marek09155122010-06-17 15:14:58 +0200102 # Check for mercurial and a mercurial repo.
Michal Marek8558f592010-08-16 17:09:52 +0200103 if test -d .hg && hgid=`hg id 2>/dev/null`; then
Mike Crowe38b34392011-01-12 00:53:52 -0500104 # Do we have an tagged version? If so, latesttagdistance == 1
105 if [ "`hg log -r . --template '{latesttagdistance}'`" == "1" ]; then
106 id=`hg log -r . --template '{latesttag}'`
Michal Marek09155122010-06-17 15:14:58 +0200107 printf '%s%s' -hg "$id"
Mike Crowe38b34392011-01-12 00:53:52 -0500108 else
109 tag=`printf '%s' "$hgid" | cut -d' ' -f2`
110 if [ -z "$tag" -o "$tag" = tip ]; then
111 id=`printf '%s' "$hgid" | sed 's/[+ ].*//'`
112 printf '%s%s' -hg "$id"
113 fi
Michal Marek09155122010-06-17 15:14:58 +0200114 fi
115
116 # Are there uncommitted changes?
117 # These are represented by + after the changeset id.
118 case "$hgid" in
119 *+|*+\ *) printf '%s' -dirty ;;
120 esac
121
122 # All done with mercurial
123 return
Peter Korsgaardff80aa92008-12-02 21:58:06 +0100124 fi
125
Michal Marek09155122010-06-17 15:14:58 +0200126 # Check for svn and a svn repo.
127 if rev=`svn info 2>/dev/null | grep '^Last Changed Rev'`; then
128 rev=`echo $rev | awk '{print $NF}'`
129 printf -- '-svn%s' "$rev"
Nico Schotteliusa2bb90a2009-06-12 09:59:52 +0200130
Michal Marek09155122010-06-17 15:14:58 +0200131 # All done with svn
132 return
Rene Scharfe117a93d2006-01-04 20:42:03 +0100133 fi
Michal Marek09155122010-06-17 15:14:58 +0200134}
Aron Griffis3dce1742007-11-28 16:55:44 -0500135
Michal Marek09155122010-06-17 15:14:58 +0200136collect_files()
137{
138 local file res
139
140 for file; do
141 case "$file" in
142 *\~*)
143 continue
144 ;;
145 esac
146 if test -e "$file"; then
147 res="$res$(cat "$file")"
148 fi
149 done
150 echo "$res"
151}
152
153if $scm_only; then
Michal Marekb003afe2010-07-15 10:36:37 +0200154 if test ! -e .scmversion; then
155 res=$(scm_version)
156 echo "$res" >.scmversion
157 fi
Aron Griffis3dce1742007-11-28 16:55:44 -0500158 exit
159fi
160
Michal Marek09155122010-06-17 15:14:58 +0200161if test -e include/config/auto.conf; then
Michał Górny6dc0c2f2010-07-18 10:26:40 +0200162 . include/config/auto.conf
Michal Marek09155122010-06-17 15:14:58 +0200163else
164 echo "Error: kernelrelease not valid - run 'make prepare' to update it"
165 exit 1
166fi
Aron Griffis3dce1742007-11-28 16:55:44 -0500167
Michal Marek09155122010-06-17 15:14:58 +0200168# localversion* files in the build and source directory
169res="$(collect_files localversion*)"
170if test ! "$srctree" -ef .; then
171 res="$res$(collect_files "$srctree"/localversion*)"
172fi
173
174# CONFIG_LOCALVERSION and LOCALVERSION (if set)
175res="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"
176
177# scm version string if not at a tagged commit
178if test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
179 # full scm version string
180 res="$res$(scm_version)"
181else
Michael Prokopc3e2f192010-09-06 11:57:19 +0200182 # append a plus sign if the repository is not in a clean
183 # annotated or signed tagged state (as git describe only
184 # looks at signed or annotated tags - git tag -a/-s) and
185 # LOCALVERSION= is not specified
Michal Marek09155122010-06-17 15:14:58 +0200186 if test "${LOCALVERSION+set}" != "set"; then
187 scm=$(scm_version --short)
188 res="$res${scm:++}"
Aron Griffis3dce1742007-11-28 16:55:44 -0500189 fi
Rene Scharfe117a93d2006-01-04 20:42:03 +0100190fi
Bryan Wuba3d05f2008-02-03 14:13:26 +0800191
Michal Marek09155122010-06-17 15:14:58 +0200192echo "$res"