1117a93dbSRene Scharfe#!/bin/sh 2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0 333252572SNico Schottelius# 433252572SNico Schottelius# This scripts adds local version information from the version 53354c64dSRasmus Villemoes# control system git. 633252572SNico Schottelius# 733252572SNico Schottelius# If something goes wrong, send a mail the kernel build mailinglist 833252572SNico Schottelius# (see MAINTAINERS) and CC Nico Schottelius 933252572SNico Schottelius# <nico-linuxsetlocalversion -at- schottelius.org>. 1033252572SNico Schottelius# 1133252572SNico Schottelius# 12aaebf433SRyan Anderson 13*e2ff1219SMasahiro Yamadaset -e 14*e2ff1219SMasahiro Yamada 15117a93dbSRene Scharfeusage() { 1605e96e96SMasahiro Yamada echo "Usage: $0 [--no-local] [srctree]" >&2 17117a93dbSRene Scharfe exit 1 18aaebf433SRyan Anderson} 19aaebf433SRyan Anderson 2005e96e96SMasahiro Yamadano_local=false 2105e96e96SMasahiro Yamadaif test "$1" = "--no-local"; then 2205e96e96SMasahiro Yamada no_local=true 2305e96e96SMasahiro Yamada shift 2405e96e96SMasahiro Yamadafi 2505e96e96SMasahiro Yamada 2609155120SMichal Mareksrctree=. 2709155120SMichal Marekif test $# -gt 0; then 2809155120SMichal Marek srctree=$1 2909155120SMichal Marek shift 3009155120SMichal Marekfi 3109155120SMichal Marekif test $# -gt 0 -o ! -d "$srctree"; then 3209155120SMichal Marek usage 3309155120SMichal Marekfi 3409155120SMichal Marek 35523f3dbcSRasmus Villemoestry_tag() { 36523f3dbcSRasmus Villemoes tag="$1" 37523f3dbcSRasmus Villemoes 38523f3dbcSRasmus Villemoes # Is $tag an annotated tag? 39*e2ff1219SMasahiro Yamada if [ "$(git cat-file -t "$tag" 2> /dev/null)" != tag ]; then 40*e2ff1219SMasahiro Yamada return 41*e2ff1219SMasahiro Yamada fi 42523f3dbcSRasmus Villemoes 43523f3dbcSRasmus Villemoes # Is it an ancestor of HEAD, and if so, how many commits are in $tag..HEAD? 44523f3dbcSRasmus Villemoes # shellcheck disable=SC2046 # word splitting is the point here 45523f3dbcSRasmus Villemoes set -- $(git rev-list --count --left-right "$tag"...HEAD 2> /dev/null) 46523f3dbcSRasmus Villemoes 47523f3dbcSRasmus Villemoes # $1 is 0 if and only if $tag is an ancestor of HEAD. Use 48523f3dbcSRasmus Villemoes # string comparison, because $1 is empty if the 'git rev-list' 49523f3dbcSRasmus Villemoes # command somehow failed. 50*e2ff1219SMasahiro Yamada if [ "$1" != 0 ]; then 51*e2ff1219SMasahiro Yamada return 52*e2ff1219SMasahiro Yamada fi 53523f3dbcSRasmus Villemoes 54523f3dbcSRasmus Villemoes # $2 is the number of commits in the range $tag..HEAD, possibly 0. 55523f3dbcSRasmus Villemoes count="$2" 56523f3dbcSRasmus Villemoes} 57523f3dbcSRasmus Villemoes 5809155120SMichal Marekscm_version() 5909155120SMichal Marek{ 6005e96e96SMasahiro Yamada local short=false 6105e96e96SMasahiro Yamada local no_dirty=false 626ab7e1f9SMasahiro Yamada local tag 6305e96e96SMasahiro Yamada 6405e96e96SMasahiro Yamada while [ $# -gt 0 ]; 6505e96e96SMasahiro Yamada do 6605e96e96SMasahiro Yamada case "$1" in 6705e96e96SMasahiro Yamada --short) 6805e96e96SMasahiro Yamada short=true;; 6905e96e96SMasahiro Yamada --no-dirty) 7005e96e96SMasahiro Yamada no_dirty=true;; 7105e96e96SMasahiro Yamada esac 7205e96e96SMasahiro Yamada shift 7305e96e96SMasahiro Yamada done 7409155120SMichal Marek 7509155120SMichal Marek cd "$srctree" 76aaebf433SRyan Anderson 7775280bdfSMasahiro Yamada if test -n "$(git rev-parse --show-cdup 2>/dev/null)"; then 7875280bdfSMasahiro Yamada return 7975280bdfSMasahiro Yamada fi 8033252572SNico Schottelius 8175280bdfSMasahiro Yamada if ! head=$(git rev-parse --verify HEAD 2>/dev/null); then 8275280bdfSMasahiro Yamada return 8375280bdfSMasahiro Yamada fi 8475280bdfSMasahiro Yamada 856ab7e1f9SMasahiro Yamada # mainline kernel: 6.2.0-rc5 -> v6.2-rc5 866ab7e1f9SMasahiro Yamada # stable kernel: 6.1.7 -> v6.1.7 8701e89a4aSRasmus Villemoes version_tag=v$(echo "${KERNELVERSION}" | sed -E 's/^([0-9]+\.[0-9]+)\.0(.*)$/\1\2/') 8801e89a4aSRasmus Villemoes 89523f3dbcSRasmus Villemoes # try_tag initializes count if the tag is usable. 90523f3dbcSRasmus Villemoes count= 91523f3dbcSRasmus Villemoes 9201e89a4aSRasmus Villemoes # If a localversion* file exists, and the corresponding 9301e89a4aSRasmus Villemoes # annotated tag exists and is an ancestor of HEAD, use 9401e89a4aSRasmus Villemoes # it. This is the case in linux-next. 95523f3dbcSRasmus Villemoes if [ -n "${file_localversion#-}" ] ; then 96523f3dbcSRasmus Villemoes try_tag "${file_localversion#-}" 9701e89a4aSRasmus Villemoes fi 9801e89a4aSRasmus Villemoes 9901e89a4aSRasmus Villemoes # Otherwise, if a localversion* file exists, and the tag 10001e89a4aSRasmus Villemoes # obtained by appending it to the tag derived from 10101e89a4aSRasmus Villemoes # KERNELVERSION exists and is an ancestor of HEAD, use 10201e89a4aSRasmus Villemoes # it. This is e.g. the case in linux-rt. 103523f3dbcSRasmus Villemoes if [ -z "${count}" ] && [ -n "${file_localversion}" ]; then 104523f3dbcSRasmus Villemoes try_tag "${version_tag}${file_localversion}" 10501e89a4aSRasmus Villemoes fi 10601e89a4aSRasmus Villemoes 10701e89a4aSRasmus Villemoes # Otherwise, default to the annotated tag derived from KERNELVERSION. 108523f3dbcSRasmus Villemoes if [ -z "${count}" ]; then 109523f3dbcSRasmus Villemoes try_tag "${version_tag}" 1106ab7e1f9SMasahiro Yamada fi 1116ab7e1f9SMasahiro Yamada 112523f3dbcSRasmus Villemoes # If we are at the tagged commit, we ignore it because the 113523f3dbcSRasmus Villemoes # version is well-defined. If none of the attempted tags exist 114523f3dbcSRasmus Villemoes # or were usable, $count is still empty. 115523f3dbcSRasmus Villemoes if [ -z "${count}" ] || [ "${count}" -gt 0 ]; then 11633252572SNico Schottelius 11709155120SMichal Marek # If only the short version is requested, don't bother 11809155120SMichal Marek # running further git commands 11909155120SMichal Marek if $short; then 12009155120SMichal Marek echo "+" 12109155120SMichal Marek return 12209155120SMichal Marek fi 123523f3dbcSRasmus Villemoes 1246ab7e1f9SMasahiro Yamada # If we are past the tagged commit, we pretty print it. 1256ab7e1f9SMasahiro Yamada # (like 6.1.0-14595-g292a089d78d3) 126523f3dbcSRasmus Villemoes if [ -n "${count}" ]; then 127523f3dbcSRasmus Villemoes printf "%s%05d" "-" "${count}" 12856b2f070SSebastian Siewior fi 129630ff0faSMasahiro Yamada 130630ff0faSMasahiro Yamada # Add -g and exactly 12 hex chars. 131523f3dbcSRasmus Villemoes printf '%s%.12s' -g "$head" 13233252572SNico Schottelius fi 133aaebf433SRyan Anderson 13405e96e96SMasahiro Yamada if ${no_dirty}; then 13505e96e96SMasahiro Yamada return 13605e96e96SMasahiro Yamada fi 13705e96e96SMasahiro Yamada 138ff64dd48SBrian Norris # Check for uncommitted changes. 13975280bdfSMasahiro Yamada # This script must avoid any write attempt to the source tree, which 14075280bdfSMasahiro Yamada # might be read-only. 14175280bdfSMasahiro Yamada # You cannot use 'git describe --dirty' because it tries to create 14275280bdfSMasahiro Yamada # .git/index.lock . 14375280bdfSMasahiro Yamada # First, with git-status, but --no-optional-locks is only supported in 14475280bdfSMasahiro Yamada # git >= 2.14, so fall back to git-diff-index if it fails. Note that 14575280bdfSMasahiro Yamada # git-diff-index does not refresh the index, so it may give misleading 14675280bdfSMasahiro Yamada # results. 14775280bdfSMasahiro Yamada # See git-update-index(1), git-diff-index(1), and git-status(1). 148ff64dd48SBrian Norris if { 149ff64dd48SBrian Norris git --no-optional-locks status -uno --porcelain 2>/dev/null || 150ff64dd48SBrian Norris git diff-index --name-only HEAD 151a2be76a3SMasahiro Yamada } | read dummy; then 15224d49756SRyan Anderson printf '%s' -dirty 153117a93dbSRene Scharfe fi 15409155120SMichal Marek} 15509155120SMichal Marek 15609155120SMichal Marekcollect_files() 15709155120SMichal Marek{ 1587a82e3faSMasahiro Yamada local file res= 15909155120SMichal Marek 16009155120SMichal Marek for file; do 16109155120SMichal Marek case "$file" in 16209155120SMichal Marek *\~*) 16309155120SMichal Marek continue 16409155120SMichal Marek ;; 16509155120SMichal Marek esac 16609155120SMichal Marek if test -e "$file"; then 16709155120SMichal Marek res="$res$(cat "$file")" 16809155120SMichal Marek fi 16909155120SMichal Marek done 17009155120SMichal Marek echo "$res" 17109155120SMichal Marek} 17209155120SMichal Marek 173ec31f868SMasahiro Yamadaif [ -z "${KERNELVERSION}" ]; then 174ec31f868SMasahiro Yamada echo "KERNELVERSION is not set" >&2 175ec31f868SMasahiro Yamada exit 1 176ec31f868SMasahiro Yamadafi 177ec31f868SMasahiro Yamada 17809155120SMichal Marek# localversion* files in the build and source directory 179eed36d77SMasahiro Yamadafile_localversion="$(collect_files localversion*)" 18009155120SMichal Marekif test ! "$srctree" -ef .; then 181eed36d77SMasahiro Yamada file_localversion="${file_localversion}$(collect_files "$srctree"/localversion*)" 18209155120SMichal Marekfi 18309155120SMichal Marek 18405e96e96SMasahiro Yamadaif ${no_local}; then 18505e96e96SMasahiro Yamada echo "${KERNELVERSION}$(scm_version --no-dirty)" 18605e96e96SMasahiro Yamada exit 0 18705e96e96SMasahiro Yamadafi 18805e96e96SMasahiro Yamada 18905e96e96SMasahiro Yamadaif ! test -e include/config/auto.conf; then 19005e96e96SMasahiro Yamada echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2 19105e96e96SMasahiro Yamada exit 1 19205e96e96SMasahiro Yamadafi 19305e96e96SMasahiro Yamada 194eed36d77SMasahiro Yamada# version string from CONFIG_LOCALVERSION 195129ab0d2SMasahiro Yamadaconfig_localversion=$(sed -n 's/^CONFIG_LOCALVERSION=\(.*\)$/\1/p' include/config/auto.conf) 19609155120SMichal Marek 1976ab7e1f9SMasahiro Yamada# scm version string if not at the kernel version tag or at the file_localversion 1987d153696SMasahiro Yamadaif grep -q "^CONFIG_LOCALVERSION_AUTO=y$" include/config/auto.conf; then 19909155120SMichal Marek # full scm version string 200eed36d77SMasahiro Yamada scm_version="$(scm_version)" 2015df99becSMikulas Patockaelif [ "${LOCALVERSION+set}" != "set" ]; then 2025df99becSMikulas Patocka # If the variable LOCALVERSION is not set, append a plus 2035df99becSMikulas Patocka # sign if the repository is not in a clean annotated or 2045df99becSMikulas Patocka # signed tagged state (as git describe only looks at signed 2055df99becSMikulas Patocka # or annotated tags - git tag -a/-s). 2065df99becSMikulas Patocka # 2075df99becSMikulas Patocka # If the variable LOCALVERSION is set (including being set 2085df99becSMikulas Patocka # to an empty string), we don't want to append a plus sign. 209eed36d77SMasahiro Yamada scm_version="$(scm_version --short)" 21009155120SMichal Marekfi 21109155120SMichal Marek 212eed36d77SMasahiro Yamadaecho "${KERNELVERSION}${file_localversion}${config_localversion}${LOCALVERSION}${scm_version}" 213