blob: c2e8455b3aebe74778550a56fce286e1355f1717 [file] [log] [blame]
Rene Scharfe117a93d2006-01-04 20:42:03 +01001#!/bin/sh
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01002# SPDX-License-Identifier: GPL-2.0
Nico Schottelius33252572009-05-16 14:00:56 +02003#
4# This scripts adds local version information from the version
5# control systems git, mercurial (hg) and subversion (svn).
6#
7# If something goes wrong, send a mail the kernel build mailinglist
8# (see MAINTAINERS) and CC Nico Schottelius
9# <nico-linuxsetlocalversion -at- schottelius.org>.
10#
11#
Ryan Andersonaaebf432005-07-31 04:57:49 -040012
Rene Scharfe117a93d2006-01-04 20:42:03 +010013usage() {
Will McVicker897439b2020-07-09 15:33:45 -070014 echo "Usage: $0 [--save-scmversion] [srctree] [branch] [kmi-generation]" >&2
Rene Scharfe117a93d2006-01-04 20:42:03 +010015 exit 1
Ryan Andersonaaebf432005-07-31 04:57:49 -040016}
17
Michal Marek09155122010-06-17 15:14:58 +020018scm_only=false
19srctree=.
Will McVicker897439b2020-07-09 15:33:45 -070020android_release=
21kmi_generation=
Michal Marekb003afe2010-07-15 10:36:37 +020022if test "$1" = "--save-scmversion"; then
Michal Marek09155122010-06-17 15:14:58 +020023 scm_only=true
24 shift
25fi
26if test $# -gt 0; then
27 srctree=$1
28 shift
29fi
Will McVicker897439b2020-07-09 15:33:45 -070030if test $# -gt 0; then
31 # Extract the Android release version. If there is no match, then return 255
32 # and clear the var $android_release
33 android_release=`echo "$1" | sed -e '/android[0-9]\{2,\}/!{q255}; \
34 s/^\(android[0-9]\{2,\}\)-.*/\1/'`
35 if test $? -ne 0; then
36 android_release=
37 fi
38 shift
39
40 if test $# -gt 0; then
41 kmi_generation=$1
42 [ $(expr $kmi_generation : '^[0-9]\+$') -eq 0 ] && usage
43 shift
44 else
45 usage
46 fi
47fi
Michal Marek09155122010-06-17 15:14:58 +020048if test $# -gt 0 -o ! -d "$srctree"; then
49 usage
50fi
Ryan Andersonaaebf432005-07-31 04:57:49 -040051
Michal Marek09155122010-06-17 15:14:58 +020052scm_version()
53{
Michał Górny6dc0c2f2010-07-18 10:26:40 +020054 local short
55 short=false
Nico Schottelius33252572009-05-16 14:00:56 +020056
Michal Marek09155122010-06-17 15:14:58 +020057 cd "$srctree"
58 if test -e .scmversion; then
Michał Górny6dc0c2f2010-07-18 10:26:40 +020059 cat .scmversion
Michal Marek09155122010-06-17 15:14:58 +020060 return
61 fi
62 if test "$1" = "--short"; then
63 short=true
64 fi
Nico Schottelius33252572009-05-16 14:00:56 +020065
Michal Marek09155122010-06-17 15:14:58 +020066 # Check for git and a git repo.
Franck Bui-Huu7593e092013-12-02 16:34:29 +010067 if test -z "$(git rev-parse --show-cdup 2>/dev/null)" &&
Rasmus Villemoesaefcff72020-09-17 08:56:11 +020068 head=$(git rev-parse --verify HEAD 2>/dev/null); then
Nico Schottelius33252572009-05-16 14:00:56 +020069
Will McVicker897439b2020-07-09 15:33:45 -070070 if [ -n "$android_release" ] && [ -n "$kmi_generation" ]; then
71 printf '%s' "-$android_release-$kmi_generation"
72 fi
73
Michal Marek09155122010-06-17 15:14:58 +020074 # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
75 # it, because this version is defined in the top level Makefile.
76 if [ -z "`git describe --exact-match 2>/dev/null`" ]; then
77
78 # If only the short version is requested, don't bother
79 # running further git commands
80 if $short; then
81 echo "+"
82 return
83 fi
84 # If we are past a tagged commit (like
85 # "v2.6.30-rc5-302-g72357d5"), we pretty print it.
Rasmus Villemoesaefcff72020-09-17 08:56:11 +020086 #
87 # Ensure the abbreviated sha1 has exactly 12
88 # hex characters, to make the output
89 # independent of git version, local
90 # core.abbrev settings and/or total number of
91 # objects in the current repository - passing
92 # --abbrev=12 ensures a minimum of 12, and the
93 # awk substr() then picks the 'g' and first 12
94 # hex chars.
95 if atag="$(git describe --abbrev=12 2>/dev/null)"; then
96 echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),substr($(NF),0,13))}'
Michal Marek09155122010-06-17 15:14:58 +020097
Rasmus Villemoesaefcff72020-09-17 08:56:11 +020098 # If we don't have a tag at all we print -g{commitish},
99 # again using exactly 12 hex chars.
Michal Marek09155122010-06-17 15:14:58 +0200100 else
Rasmus Villemoesaefcff72020-09-17 08:56:11 +0200101 head="$(echo $head | cut -c1-12)"
Michal Marek09155122010-06-17 15:14:58 +0200102 printf '%s%s' -g $head
103 fi
Nico Schottelius33252572009-05-16 14:00:56 +0200104 fi
Michal Marek09155122010-06-17 15:14:58 +0200105
106 # Is this git on svn?
107 if git config --get svn-remote.svn.url >/dev/null; then
108 printf -- '-svn%s' "`git svn find-rev $head`"
109 fi
110
Brian Norrisb064e272018-11-14 18:11:18 -0800111 # Check for uncommitted changes.
112 # First, with git-status, but --no-optional-locks is only
113 # supported in git >= 2.14, so fall back to git-diff-index if
114 # it fails. Note that git-diff-index does not refresh the
115 # index, so it may give misleading results. See
116 # git-update-index(1), git-diff-index(1), and git-status(1).
117 if {
118 git --no-optional-locks status -uno --porcelain 2>/dev/null ||
119 git diff-index --name-only HEAD
120 } | grep -qvE '^(.. )?scripts/package'; then
Michal Marek09155122010-06-17 15:14:58 +0200121 printf '%s' -dirty
122 fi
123
124 # All done with git
125 return
Rene Scharfe117a93d2006-01-04 20:42:03 +0100126 fi
Ryan Andersonaaebf432005-07-31 04:57:49 -0400127
Michal Marek09155122010-06-17 15:14:58 +0200128 # Check for mercurial and a mercurial repo.
Michal Marek8558f592010-08-16 17:09:52 +0200129 if test -d .hg && hgid=`hg id 2>/dev/null`; then
Mike Crowe38b34392011-01-12 00:53:52 -0500130 # Do we have an tagged version? If so, latesttagdistance == 1
131 if [ "`hg log -r . --template '{latesttagdistance}'`" == "1" ]; then
132 id=`hg log -r . --template '{latesttag}'`
Michal Marek09155122010-06-17 15:14:58 +0200133 printf '%s%s' -hg "$id"
Mike Crowe38b34392011-01-12 00:53:52 -0500134 else
135 tag=`printf '%s' "$hgid" | cut -d' ' -f2`
136 if [ -z "$tag" -o "$tag" = tip ]; then
137 id=`printf '%s' "$hgid" | sed 's/[+ ].*//'`
138 printf '%s%s' -hg "$id"
139 fi
Michal Marek09155122010-06-17 15:14:58 +0200140 fi
141
142 # Are there uncommitted changes?
143 # These are represented by + after the changeset id.
144 case "$hgid" in
145 *+|*+\ *) printf '%s' -dirty ;;
146 esac
147
148 # All done with mercurial
149 return
Peter Korsgaardff80aa92008-12-02 21:58:06 +0100150 fi
151
Michal Marek09155122010-06-17 15:14:58 +0200152 # Check for svn and a svn repo.
Christophe Leroyf893bfb2013-02-22 09:59:18 +0100153 if rev=`LANG= LC_ALL= LC_MESSAGES=C svn info 2>/dev/null | grep '^Last Changed Rev'`; then
Michal Marek09155122010-06-17 15:14:58 +0200154 rev=`echo $rev | awk '{print $NF}'`
155 printf -- '-svn%s' "$rev"
Nico Schotteliusa2bb90a2009-06-12 09:59:52 +0200156
Michal Marek09155122010-06-17 15:14:58 +0200157 # All done with svn
158 return
Rene Scharfe117a93d2006-01-04 20:42:03 +0100159 fi
Michal Marek09155122010-06-17 15:14:58 +0200160}
Aron Griffis3dce1742007-11-28 16:55:44 -0500161
Michal Marek09155122010-06-17 15:14:58 +0200162collect_files()
163{
164 local file res
165
166 for file; do
167 case "$file" in
168 *\~*)
169 continue
170 ;;
171 esac
172 if test -e "$file"; then
173 res="$res$(cat "$file")"
174 fi
175 done
176 echo "$res"
177}
178
179if $scm_only; then
Michal Marekb003afe2010-07-15 10:36:37 +0200180 if test ! -e .scmversion; then
181 res=$(scm_version)
182 echo "$res" >.scmversion
183 fi
Aron Griffis3dce1742007-11-28 16:55:44 -0500184 exit
185fi
186
Michal Marek09155122010-06-17 15:14:58 +0200187if test -e include/config/auto.conf; then
Michał Górny6dc0c2f2010-07-18 10:26:40 +0200188 . include/config/auto.conf
Michal Marek09155122010-06-17 15:14:58 +0200189else
Wolfram Sang78283edf2016-06-06 21:00:38 +0200190 echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
Michal Marek09155122010-06-17 15:14:58 +0200191 exit 1
192fi
Aron Griffis3dce1742007-11-28 16:55:44 -0500193
Michal Marek09155122010-06-17 15:14:58 +0200194# localversion* files in the build and source directory
195res="$(collect_files localversion*)"
196if test ! "$srctree" -ef .; then
197 res="$res$(collect_files "$srctree"/localversion*)"
198fi
199
200# CONFIG_LOCALVERSION and LOCALVERSION (if set)
201res="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"
202
203# scm version string if not at a tagged commit
204if test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
205 # full scm version string
206 res="$res$(scm_version)"
207else
Michael Prokopc3e2f192010-09-06 11:57:19 +0200208 # append a plus sign if the repository is not in a clean
209 # annotated or signed tagged state (as git describe only
210 # looks at signed or annotated tags - git tag -a/-s) and
211 # LOCALVERSION= is not specified
Michal Marek09155122010-06-17 15:14:58 +0200212 if test "${LOCALVERSION+set}" != "set"; then
213 scm=$(scm_version --short)
214 res="$res${scm:++}"
Aron Griffis3dce1742007-11-28 16:55:44 -0500215 fi
Rene Scharfe117a93d2006-01-04 20:42:03 +0100216fi
Bryan Wuba3d05f2008-02-03 14:13:26 +0800217
Michal Marek09155122010-06-17 15:14:58 +0200218echo "$res"