blob: 842936656b846bf95e2b84ed55a0c13857558aa0 [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 McVickerf0bec7a2020-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 McVickerf0bec7a2020-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 McVickerf0bec7a2020-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
Will McVickerf0bec7a2020-07-09 15:33:45 -070044 fi
45fi
Michal Marek09155122010-06-17 15:14:58 +020046if test $# -gt 0 -o ! -d "$srctree"; then
47 usage
48fi
Ryan Andersonaaebf432005-07-31 04:57:49 -040049
Michal Marek09155122010-06-17 15:14:58 +020050scm_version()
51{
Michał Górny6dc0c2f2010-07-18 10:26:40 +020052 local short
53 short=false
Nico Schottelius33252572009-05-16 14:00:56 +020054
Michal Marek09155122010-06-17 15:14:58 +020055 cd "$srctree"
56 if test -e .scmversion; then
Michał Górny6dc0c2f2010-07-18 10:26:40 +020057 cat .scmversion
Michal Marek09155122010-06-17 15:14:58 +020058 return
59 fi
60 if test "$1" = "--short"; then
61 short=true
62 fi
Nico Schottelius33252572009-05-16 14:00:56 +020063
Michal Marek09155122010-06-17 15:14:58 +020064 # Check for git and a git repo.
Will McVicker8c7a2fec2021-02-11 11:33:42 -080065 if head=$(git rev-parse --verify HEAD 2>/dev/null); then
Nico Schottelius33252572009-05-16 14:00:56 +020066
Will McVickerf0bec7a2020-07-09 15:33:45 -070067 if [ -n "$android_release" ] && [ -n "$kmi_generation" ]; then
68 printf '%s' "-$android_release-$kmi_generation"
Alistair Delvae41b1162021-11-17 09:58:10 -080069 elif [ -n "$android_release" ]; then
70 printf '%s' "-$android_release"
Will McVickerf0bec7a2020-07-09 15:33:45 -070071 fi
72
Michal Marek09155122010-06-17 15:14:58 +020073 # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
74 # it, because this version is defined in the top level Makefile.
Bhaskar Chowdhury3c96bdd2019-10-23 07:24:27 +053075 if [ -z "$(git describe --exact-match 2>/dev/null)" ]; then
Michal Marek09155122010-06-17 15:14:58 +020076
77 # If only the short version is requested, don't bother
78 # running further git commands
79 if $short; then
80 echo "+"
81 return
82 fi
83 # If we are past a tagged commit (like
84 # "v2.6.30-rc5-302-g72357d5"), we pretty print it.
Rasmus Villemoes548b8b52020-09-17 08:56:11 +020085 #
86 # Ensure the abbreviated sha1 has exactly 12
87 # hex characters, to make the output
88 # independent of git version, local
89 # core.abbrev settings and/or total number of
90 # objects in the current repository - passing
91 # --abbrev=12 ensures a minimum of 12, and the
92 # awk substr() then picks the 'g' and first 12
93 # hex chars.
94 if atag="$(git describe --abbrev=12 2>/dev/null)"; then
95 echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),substr($(NF),0,13))}'
Michal Marek09155122010-06-17 15:14:58 +020096
Rasmus Villemoes548b8b52020-09-17 08:56:11 +020097 # If we don't have a tag at all we print -g{commitish},
98 # again using exactly 12 hex chars.
Michal Marek09155122010-06-17 15:14:58 +020099 else
Rasmus Villemoes548b8b52020-09-17 08:56:11 +0200100 head="$(echo $head | cut -c1-12)"
Michal Marek09155122010-06-17 15:14:58 +0200101 printf '%s%s' -g $head
102 fi
Nico Schottelius33252572009-05-16 14:00:56 +0200103 fi
Michal Marek09155122010-06-17 15:14:58 +0200104
105 # Is this git on svn?
106 if git config --get svn-remote.svn.url >/dev/null; then
Bhaskar Chowdhury3c96bdd2019-10-23 07:24:27 +0530107 printf -- '-svn%s' "$(git svn find-rev $head)"
Michal Marek09155122010-06-17 15:14:58 +0200108 fi
109
Brian Norrisff64dd42018-11-14 18:11:18 -0800110 # Check for uncommitted changes.
111 # First, with git-status, but --no-optional-locks is only
112 # supported in git >= 2.14, so fall back to git-diff-index if
113 # it fails. Note that git-diff-index does not refresh the
114 # index, so it may give misleading results. See
115 # git-update-index(1), git-diff-index(1), and git-status(1).
116 if {
117 git --no-optional-locks status -uno --porcelain 2>/dev/null ||
118 git diff-index --name-only HEAD
119 } | grep -qvE '^(.. )?scripts/package'; then
Michal Marek09155122010-06-17 15:14:58 +0200120 printf '%s' -dirty
121 fi
122
123 # All done with git
124 return
Rene Scharfe117a93d2006-01-04 20:42:03 +0100125 fi
Ryan Andersonaaebf432005-07-31 04:57:49 -0400126
Michal Marek09155122010-06-17 15:14:58 +0200127 # Check for mercurial and a mercurial repo.
Will McVicker8c7a2fec2021-02-11 11:33:42 -0800128 if hgid=$(hg id 2>/dev/null); then
Mike Crowe38b34392011-01-12 00:53:52 -0500129 # Do we have an tagged version? If so, latesttagdistance == 1
Bhaskar Chowdhury3c96bdd2019-10-23 07:24:27 +0530130 if [ "$(hg log -r . --template '{latesttagdistance}')" = "1" ]; then
131 id=$(hg log -r . --template '{latesttag}')
Michal Marek09155122010-06-17 15:14:58 +0200132 printf '%s%s' -hg "$id"
Mike Crowe38b34392011-01-12 00:53:52 -0500133 else
Bhaskar Chowdhury3c96bdd2019-10-23 07:24:27 +0530134 tag=$(printf '%s' "$hgid" | cut -d' ' -f2)
Mike Crowe38b34392011-01-12 00:53:52 -0500135 if [ -z "$tag" -o "$tag" = tip ]; then
Bhaskar Chowdhury3c96bdd2019-10-23 07:24:27 +0530136 id=$(printf '%s' "$hgid" | sed 's/[+ ].*//')
Mike Crowe38b34392011-01-12 00:53:52 -0500137 printf '%s%s' -hg "$id"
138 fi
Michal Marek09155122010-06-17 15:14:58 +0200139 fi
140
141 # Are there uncommitted changes?
142 # These are represented by + after the changeset id.
143 case "$hgid" in
144 *+|*+\ *) printf '%s' -dirty ;;
145 esac
146
147 # All done with mercurial
148 return
Peter Korsgaardff80aa92008-12-02 21:58:06 +0100149 fi
150
Michal Marek09155122010-06-17 15:14:58 +0200151 # Check for svn and a svn repo.
Bhaskar Chowdhury3c96bdd2019-10-23 07:24:27 +0530152 if rev=$(LANG= LC_ALL= LC_MESSAGES=C svn info 2>/dev/null | grep '^Last Changed Rev'); then
153 rev=$(echo $rev | awk '{print $NF}')
Michal Marek09155122010-06-17 15:14:58 +0200154 printf -- '-svn%s' "$rev"
Nico Schotteliusa2bb90a2009-06-12 09:59:52 +0200155
Michal Marek09155122010-06-17 15:14:58 +0200156 # All done with svn
157 return
Rene Scharfe117a93d2006-01-04 20:42:03 +0100158 fi
Michal Marek09155122010-06-17 15:14:58 +0200159}
Aron Griffis3dce1742007-11-28 16:55:44 -0500160
Michal Marek09155122010-06-17 15:14:58 +0200161collect_files()
162{
Masahiro Yamada7a82e3f2019-10-01 21:17:24 +0900163 local file res=
Michal Marek09155122010-06-17 15:14:58 +0200164
165 for file; do
166 case "$file" in
167 *\~*)
168 continue
169 ;;
170 esac
171 if test -e "$file"; then
172 res="$res$(cat "$file")"
173 fi
174 done
175 echo "$res"
176}
177
178if $scm_only; then
Michal Marekb003afe2010-07-15 10:36:37 +0200179 if test ! -e .scmversion; then
180 res=$(scm_version)
181 echo "$res" >.scmversion
182 fi
Aron Griffis3dce1742007-11-28 16:55:44 -0500183 exit
184fi
185
Michal Marek09155122010-06-17 15:14:58 +0200186if test -e include/config/auto.conf; then
Michał Górny6dc0c2f2010-07-18 10:26:40 +0200187 . include/config/auto.conf
Michal Marek09155122010-06-17 15:14:58 +0200188else
Wolfram Sang78283edf2016-06-06 21:00:38 +0200189 echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
Michal Marek09155122010-06-17 15:14:58 +0200190 exit 1
191fi
Aron Griffis3dce1742007-11-28 16:55:44 -0500192
Michal Marek09155122010-06-17 15:14:58 +0200193# localversion* files in the build and source directory
194res="$(collect_files localversion*)"
195if test ! "$srctree" -ef .; then
196 res="$res$(collect_files "$srctree"/localversion*)"
197fi
198
199# CONFIG_LOCALVERSION and LOCALVERSION (if set)
200res="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"
201
202# scm version string if not at a tagged commit
203if test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
204 # full scm version string
205 res="$res$(scm_version)"
206else
Michael Prokopc3e2f192010-09-06 11:57:19 +0200207 # append a plus sign if the repository is not in a clean
208 # annotated or signed tagged state (as git describe only
209 # looks at signed or annotated tags - git tag -a/-s) and
210 # LOCALVERSION= is not specified
Michal Marek09155122010-06-17 15:14:58 +0200211 if test "${LOCALVERSION+set}" != "set"; then
212 scm=$(scm_version --short)
213 res="$res${scm:++}"
Aron Griffis3dce1742007-11-28 16:55:44 -0500214 fi
Rene Scharfe117a93d2006-01-04 20:42:03 +0100215fi
Bryan Wuba3d05f2008-02-03 14:13:26 +0800216
Michal Marek09155122010-06-17 15:14:58 +0200217echo "$res"