Rene Scharfe | 117a93d | 2006-01-04 20:42:03 +0100 | [diff] [blame] | 1 | #!/bin/sh |
Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0 |
Nico Schottelius | 3325257 | 2009-05-16 14:00:56 +0200 | [diff] [blame] | 3 | # |
| 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 Anderson | aaebf43 | 2005-07-31 04:57:49 -0400 | [diff] [blame] | 12 | |
Rene Scharfe | 117a93d | 2006-01-04 20:42:03 +0100 | [diff] [blame] | 13 | usage() { |
Will McVicker | f0bec7a | 2020-07-09 15:33:45 -0700 | [diff] [blame^] | 14 | echo "Usage: $0 [--save-scmversion] [srctree] [branch] [kmi-generation]" >&2 |
Rene Scharfe | 117a93d | 2006-01-04 20:42:03 +0100 | [diff] [blame] | 15 | exit 1 |
Ryan Anderson | aaebf43 | 2005-07-31 04:57:49 -0400 | [diff] [blame] | 16 | } |
| 17 | |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 18 | scm_only=false |
| 19 | srctree=. |
Will McVicker | f0bec7a | 2020-07-09 15:33:45 -0700 | [diff] [blame^] | 20 | android_release= |
| 21 | kmi_generation= |
Michal Marek | b003afe | 2010-07-15 10:36:37 +0200 | [diff] [blame] | 22 | if test "$1" = "--save-scmversion"; then |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 23 | scm_only=true |
| 24 | shift |
| 25 | fi |
| 26 | if test $# -gt 0; then |
| 27 | srctree=$1 |
| 28 | shift |
| 29 | fi |
Will McVicker | f0bec7a | 2020-07-09 15:33:45 -0700 | [diff] [blame^] | 30 | if 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 |
| 47 | fi |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 48 | if test $# -gt 0 -o ! -d "$srctree"; then |
| 49 | usage |
| 50 | fi |
Ryan Anderson | aaebf43 | 2005-07-31 04:57:49 -0400 | [diff] [blame] | 51 | |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 52 | scm_version() |
| 53 | { |
Michał Górny | 6dc0c2f | 2010-07-18 10:26:40 +0200 | [diff] [blame] | 54 | local short |
| 55 | short=false |
Nico Schottelius | 3325257 | 2009-05-16 14:00:56 +0200 | [diff] [blame] | 56 | |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 57 | cd "$srctree" |
| 58 | if test -e .scmversion; then |
Michał Górny | 6dc0c2f | 2010-07-18 10:26:40 +0200 | [diff] [blame] | 59 | cat .scmversion |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 60 | return |
| 61 | fi |
| 62 | if test "$1" = "--short"; then |
| 63 | short=true |
| 64 | fi |
Nico Schottelius | 3325257 | 2009-05-16 14:00:56 +0200 | [diff] [blame] | 65 | |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 66 | # Check for git and a git repo. |
Franck Bui-Huu | 7593e09 | 2013-12-02 16:34:29 +0100 | [diff] [blame] | 67 | if test -z "$(git rev-parse --show-cdup 2>/dev/null)" && |
Bhaskar Chowdhury | 3c96bdd | 2019-10-23 07:24:27 +0530 | [diff] [blame] | 68 | head=$(git rev-parse --verify --short HEAD 2>/dev/null); then |
Nico Schottelius | 3325257 | 2009-05-16 14:00:56 +0200 | [diff] [blame] | 69 | |
Will McVicker | f0bec7a | 2020-07-09 15:33:45 -0700 | [diff] [blame^] | 70 | if [ -n "$android_release" ] && [ -n "$kmi_generation" ]; then |
| 71 | printf '%s' "-$android_release-$kmi_generation" |
| 72 | fi |
| 73 | |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 74 | # 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. |
Bhaskar Chowdhury | 3c96bdd | 2019-10-23 07:24:27 +0530 | [diff] [blame] | 76 | if [ -z "$(git describe --exact-match 2>/dev/null)" ]; then |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 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. |
Bhaskar Chowdhury | 3c96bdd | 2019-10-23 07:24:27 +0530 | [diff] [blame] | 86 | if atag="$(git describe 2>/dev/null)"; then |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 87 | echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),$(NF))}' |
| 88 | |
| 89 | # If we don't have a tag at all we print -g{commitish}. |
| 90 | else |
| 91 | printf '%s%s' -g $head |
| 92 | fi |
Nico Schottelius | 3325257 | 2009-05-16 14:00:56 +0200 | [diff] [blame] | 93 | fi |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 94 | |
| 95 | # Is this git on svn? |
| 96 | if git config --get svn-remote.svn.url >/dev/null; then |
Bhaskar Chowdhury | 3c96bdd | 2019-10-23 07:24:27 +0530 | [diff] [blame] | 97 | printf -- '-svn%s' "$(git svn find-rev $head)" |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 98 | fi |
| 99 | |
Brian Norris | ff64dd4 | 2018-11-14 18:11:18 -0800 | [diff] [blame] | 100 | # Check for uncommitted changes. |
| 101 | # First, with git-status, but --no-optional-locks is only |
| 102 | # supported in git >= 2.14, so fall back to git-diff-index if |
| 103 | # it fails. Note that git-diff-index does not refresh the |
| 104 | # index, so it may give misleading results. See |
| 105 | # git-update-index(1), git-diff-index(1), and git-status(1). |
| 106 | if { |
| 107 | git --no-optional-locks status -uno --porcelain 2>/dev/null || |
| 108 | git diff-index --name-only HEAD |
| 109 | } | grep -qvE '^(.. )?scripts/package'; then |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 110 | printf '%s' -dirty |
| 111 | fi |
| 112 | |
| 113 | # All done with git |
| 114 | return |
Rene Scharfe | 117a93d | 2006-01-04 20:42:03 +0100 | [diff] [blame] | 115 | fi |
Ryan Anderson | aaebf43 | 2005-07-31 04:57:49 -0400 | [diff] [blame] | 116 | |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 117 | # Check for mercurial and a mercurial repo. |
Bhaskar Chowdhury | 3c96bdd | 2019-10-23 07:24:27 +0530 | [diff] [blame] | 118 | if test -d .hg && hgid=$(hg id 2>/dev/null); then |
Mike Crowe | 38b3439 | 2011-01-12 00:53:52 -0500 | [diff] [blame] | 119 | # Do we have an tagged version? If so, latesttagdistance == 1 |
Bhaskar Chowdhury | 3c96bdd | 2019-10-23 07:24:27 +0530 | [diff] [blame] | 120 | if [ "$(hg log -r . --template '{latesttagdistance}')" = "1" ]; then |
| 121 | id=$(hg log -r . --template '{latesttag}') |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 122 | printf '%s%s' -hg "$id" |
Mike Crowe | 38b3439 | 2011-01-12 00:53:52 -0500 | [diff] [blame] | 123 | else |
Bhaskar Chowdhury | 3c96bdd | 2019-10-23 07:24:27 +0530 | [diff] [blame] | 124 | tag=$(printf '%s' "$hgid" | cut -d' ' -f2) |
Mike Crowe | 38b3439 | 2011-01-12 00:53:52 -0500 | [diff] [blame] | 125 | if [ -z "$tag" -o "$tag" = tip ]; then |
Bhaskar Chowdhury | 3c96bdd | 2019-10-23 07:24:27 +0530 | [diff] [blame] | 126 | id=$(printf '%s' "$hgid" | sed 's/[+ ].*//') |
Mike Crowe | 38b3439 | 2011-01-12 00:53:52 -0500 | [diff] [blame] | 127 | printf '%s%s' -hg "$id" |
| 128 | fi |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 129 | fi |
| 130 | |
| 131 | # Are there uncommitted changes? |
| 132 | # These are represented by + after the changeset id. |
| 133 | case "$hgid" in |
| 134 | *+|*+\ *) printf '%s' -dirty ;; |
| 135 | esac |
| 136 | |
| 137 | # All done with mercurial |
| 138 | return |
Peter Korsgaard | ff80aa9 | 2008-12-02 21:58:06 +0100 | [diff] [blame] | 139 | fi |
| 140 | |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 141 | # Check for svn and a svn repo. |
Bhaskar Chowdhury | 3c96bdd | 2019-10-23 07:24:27 +0530 | [diff] [blame] | 142 | if rev=$(LANG= LC_ALL= LC_MESSAGES=C svn info 2>/dev/null | grep '^Last Changed Rev'); then |
| 143 | rev=$(echo $rev | awk '{print $NF}') |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 144 | printf -- '-svn%s' "$rev" |
Nico Schottelius | a2bb90a | 2009-06-12 09:59:52 +0200 | [diff] [blame] | 145 | |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 146 | # All done with svn |
| 147 | return |
Rene Scharfe | 117a93d | 2006-01-04 20:42:03 +0100 | [diff] [blame] | 148 | fi |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 149 | } |
Aron Griffis | 3dce174 | 2007-11-28 16:55:44 -0500 | [diff] [blame] | 150 | |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 151 | collect_files() |
| 152 | { |
Masahiro Yamada | 7a82e3f | 2019-10-01 21:17:24 +0900 | [diff] [blame] | 153 | local file res= |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 154 | |
| 155 | for file; do |
| 156 | case "$file" in |
| 157 | *\~*) |
| 158 | continue |
| 159 | ;; |
| 160 | esac |
| 161 | if test -e "$file"; then |
| 162 | res="$res$(cat "$file")" |
| 163 | fi |
| 164 | done |
| 165 | echo "$res" |
| 166 | } |
| 167 | |
| 168 | if $scm_only; then |
Michal Marek | b003afe | 2010-07-15 10:36:37 +0200 | [diff] [blame] | 169 | if test ! -e .scmversion; then |
| 170 | res=$(scm_version) |
| 171 | echo "$res" >.scmversion |
| 172 | fi |
Aron Griffis | 3dce174 | 2007-11-28 16:55:44 -0500 | [diff] [blame] | 173 | exit |
| 174 | fi |
| 175 | |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 176 | if test -e include/config/auto.conf; then |
Michał Górny | 6dc0c2f | 2010-07-18 10:26:40 +0200 | [diff] [blame] | 177 | . include/config/auto.conf |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 178 | else |
Wolfram Sang | 78283edf | 2016-06-06 21:00:38 +0200 | [diff] [blame] | 179 | echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2 |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 180 | exit 1 |
| 181 | fi |
Aron Griffis | 3dce174 | 2007-11-28 16:55:44 -0500 | [diff] [blame] | 182 | |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 183 | # localversion* files in the build and source directory |
| 184 | res="$(collect_files localversion*)" |
| 185 | if test ! "$srctree" -ef .; then |
| 186 | res="$res$(collect_files "$srctree"/localversion*)" |
| 187 | fi |
| 188 | |
| 189 | # CONFIG_LOCALVERSION and LOCALVERSION (if set) |
| 190 | res="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}" |
| 191 | |
| 192 | # scm version string if not at a tagged commit |
| 193 | if test "$CONFIG_LOCALVERSION_AUTO" = "y"; then |
| 194 | # full scm version string |
| 195 | res="$res$(scm_version)" |
| 196 | else |
Michael Prokop | c3e2f19 | 2010-09-06 11:57:19 +0200 | [diff] [blame] | 197 | # append a plus sign if the repository is not in a clean |
| 198 | # annotated or signed tagged state (as git describe only |
| 199 | # looks at signed or annotated tags - git tag -a/-s) and |
| 200 | # LOCALVERSION= is not specified |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 201 | if test "${LOCALVERSION+set}" != "set"; then |
| 202 | scm=$(scm_version --short) |
| 203 | res="$res${scm:++}" |
Aron Griffis | 3dce174 | 2007-11-28 16:55:44 -0500 | [diff] [blame] | 204 | fi |
Rene Scharfe | 117a93d | 2006-01-04 20:42:03 +0100 | [diff] [blame] | 205 | fi |
Bryan Wu | ba3d05f | 2008-02-03 14:13:26 +0800 | [diff] [blame] | 206 | |
Michal Marek | 0915512 | 2010-06-17 15:14:58 +0200 | [diff] [blame] | 207 | echo "$res" |