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 533252572SNico Schottelius# control systems git, mercurial (hg) and subversion (svn). 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 13117a93dbSRene Scharfeusage() { 14f6e09b07SMasahiro Yamada echo "Usage: $0 [srctree]" >&2 15117a93dbSRene Scharfe exit 1 16aaebf433SRyan Anderson} 17aaebf433SRyan Anderson 1809155120SMichal Mareksrctree=. 1909155120SMichal Marekif test $# -gt 0; then 2009155120SMichal Marek srctree=$1 2109155120SMichal Marek shift 2209155120SMichal Marekfi 2309155120SMichal Marekif test $# -gt 0 -o ! -d "$srctree"; then 2409155120SMichal Marek usage 2509155120SMichal Marekfi 2609155120SMichal Marek 2709155120SMichal Marekscm_version() 2809155120SMichal Marek{ 296dc0c2f3SMichał Górny local short 306dc0c2f3SMichał Górny short=false 3109155120SMichal Marek 3209155120SMichal Marek cd "$srctree" 3309155120SMichal Marek if test "$1" = "--short"; then 3409155120SMichal Marek short=true 3509155120SMichal Marek fi 36aaebf433SRyan Anderson 3775280bdfSMasahiro Yamada if test -n "$(git rev-parse --show-cdup 2>/dev/null)"; then 3875280bdfSMasahiro Yamada return 3975280bdfSMasahiro Yamada fi 4033252572SNico Schottelius 4175280bdfSMasahiro Yamada if ! head=$(git rev-parse --verify HEAD 2>/dev/null); then 4275280bdfSMasahiro Yamada return 4375280bdfSMasahiro Yamada fi 4475280bdfSMasahiro Yamada 4575280bdfSMasahiro Yamada # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore it 4675280bdfSMasahiro Yamada # because this version is defined in the top level Makefile. 473c96bdd0SBhaskar Chowdhury if [ -z "$(git describe --exact-match 2>/dev/null)" ]; then 4833252572SNico Schottelius 4909155120SMichal Marek # If only the short version is requested, don't bother 5009155120SMichal Marek # running further git commands 5109155120SMichal Marek if $short; then 5209155120SMichal Marek echo "+" 5309155120SMichal Marek return 5409155120SMichal Marek fi 5509155120SMichal Marek # If we are past a tagged commit (like 5609155120SMichal Marek # "v2.6.30-rc5-302-g72357d5"), we pretty print it. 57630ff0faSMasahiro Yamada if atag="$(git describe 2>/dev/null)"; then 58630ff0faSMasahiro Yamada echo "$atag" | awk -F- '{printf("-%05d", $(NF-1))}' 5956b2f070SSebastian Siewior fi 60630ff0faSMasahiro Yamada 61630ff0faSMasahiro Yamada # Add -g and exactly 12 hex chars. 62630ff0faSMasahiro Yamada printf '%s%s' -g "$(echo $head | cut -c1-12)" 6333252572SNico Schottelius fi 64aaebf433SRyan Anderson 65ff64dd48SBrian Norris # Check for uncommitted changes. 6675280bdfSMasahiro Yamada # This script must avoid any write attempt to the source tree, which 6775280bdfSMasahiro Yamada # might be read-only. 6875280bdfSMasahiro Yamada # You cannot use 'git describe --dirty' because it tries to create 6975280bdfSMasahiro Yamada # .git/index.lock . 7075280bdfSMasahiro Yamada # First, with git-status, but --no-optional-locks is only supported in 7175280bdfSMasahiro Yamada # git >= 2.14, so fall back to git-diff-index if it fails. Note that 7275280bdfSMasahiro Yamada # git-diff-index does not refresh the index, so it may give misleading 7375280bdfSMasahiro Yamada # results. 7475280bdfSMasahiro Yamada # See git-update-index(1), git-diff-index(1), and git-status(1). 75ff64dd48SBrian Norris if { 76ff64dd48SBrian Norris git --no-optional-locks status -uno --porcelain 2>/dev/null || 77ff64dd48SBrian Norris git diff-index --name-only HEAD 78a2be76a3SMasahiro Yamada } | read dummy; then 7924d49756SRyan Anderson printf '%s' -dirty 80117a93dbSRene Scharfe fi 8109155120SMichal Marek} 8209155120SMichal Marek 8309155120SMichal Marekcollect_files() 8409155120SMichal Marek{ 857a82e3faSMasahiro Yamada local file res= 8609155120SMichal Marek 8709155120SMichal Marek for file; do 8809155120SMichal Marek case "$file" in 8909155120SMichal Marek *\~*) 9009155120SMichal Marek continue 9109155120SMichal Marek ;; 9209155120SMichal Marek esac 9309155120SMichal Marek if test -e "$file"; then 9409155120SMichal Marek res="$res$(cat "$file")" 9509155120SMichal Marek fi 9609155120SMichal Marek done 9709155120SMichal Marek echo "$res" 9809155120SMichal Marek} 9909155120SMichal Marek 1007d153696SMasahiro Yamadaif ! test -e include/config/auto.conf; then 10178283edfSWolfram Sang echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2 10209155120SMichal Marek exit 1 10309155120SMichal Marekfi 10409155120SMichal Marek 105*ec31f868SMasahiro Yamadaif [ -z "${KERNELVERSION}" ]; then 106*ec31f868SMasahiro Yamada echo "KERNELVERSION is not set" >&2 107*ec31f868SMasahiro Yamada exit 1 108*ec31f868SMasahiro Yamadafi 109*ec31f868SMasahiro Yamada 11009155120SMichal Marek# localversion* files in the build and source directory 11109155120SMichal Marekres="$(collect_files localversion*)" 11209155120SMichal Marekif test ! "$srctree" -ef .; then 11309155120SMichal Marek res="$res$(collect_files "$srctree"/localversion*)" 11409155120SMichal Marekfi 11509155120SMichal Marek 11609155120SMichal Marek# CONFIG_LOCALVERSION and LOCALVERSION (if set) 117129ab0d2SMasahiro Yamadaconfig_localversion=$(sed -n 's/^CONFIG_LOCALVERSION=\(.*\)$/\1/p' include/config/auto.conf) 1187d153696SMasahiro Yamadares="${res}${config_localversion}${LOCALVERSION}" 11909155120SMichal Marek 12009155120SMichal Marek# scm version string if not at a tagged commit 1217d153696SMasahiro Yamadaif grep -q "^CONFIG_LOCALVERSION_AUTO=y$" include/config/auto.conf; then 12209155120SMichal Marek # full scm version string 12309155120SMichal Marek res="$res$(scm_version)" 1245df99becSMikulas Patockaelif [ "${LOCALVERSION+set}" != "set" ]; then 1255df99becSMikulas Patocka # If the variable LOCALVERSION is not set, append a plus 1265df99becSMikulas Patocka # sign if the repository is not in a clean annotated or 1275df99becSMikulas Patocka # signed tagged state (as git describe only looks at signed 1285df99becSMikulas Patocka # or annotated tags - git tag -a/-s). 1295df99becSMikulas Patocka # 1305df99becSMikulas Patocka # If the variable LOCALVERSION is set (including being set 1315df99becSMikulas Patocka # to an empty string), we don't want to append a plus sign. 132992ebfabSMasahiro Yamada res="$res$(scm_version --short)" 13309155120SMichal Marekfi 13409155120SMichal Marek 135*ec31f868SMasahiro Yamadaecho "${KERNELVERSION}${res}" 136