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() { 14*f6e09b07SMasahiro 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 37117a93dbSRene Scharfe # Check for git and a git repo. 387593e090SFranck Bui-Huu if test -z "$(git rev-parse --show-cdup 2>/dev/null)" && 39548b8b51SRasmus Villemoes head=$(git rev-parse --verify HEAD 2>/dev/null); then 4033252572SNico Schottelius 4109155120SMichal Marek # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore 4209155120SMichal Marek # it, because this version is defined in the top level Makefile. 433c96bdd0SBhaskar Chowdhury if [ -z "$(git describe --exact-match 2>/dev/null)" ]; then 4433252572SNico Schottelius 4509155120SMichal Marek # If only the short version is requested, don't bother 4609155120SMichal Marek # running further git commands 4709155120SMichal Marek if $short; then 4809155120SMichal Marek echo "+" 4909155120SMichal Marek return 5009155120SMichal Marek fi 5109155120SMichal Marek # If we are past a tagged commit (like 5209155120SMichal Marek # "v2.6.30-rc5-302-g72357d5"), we pretty print it. 53630ff0faSMasahiro Yamada if atag="$(git describe 2>/dev/null)"; then 54630ff0faSMasahiro Yamada echo "$atag" | awk -F- '{printf("-%05d", $(NF-1))}' 5556b2f070SSebastian Siewior fi 56630ff0faSMasahiro Yamada 57630ff0faSMasahiro Yamada # Add -g and exactly 12 hex chars. 58630ff0faSMasahiro Yamada printf '%s%s' -g "$(echo $head | cut -c1-12)" 5933252572SNico Schottelius fi 60aaebf433SRyan Anderson 61ff64dd48SBrian Norris # Check for uncommitted changes. 62ffaf62a8SMasahiro Yamada # This script must avoid any write attempt to the source tree, 63ffaf62a8SMasahiro Yamada # which might be read-only. 64ffaf62a8SMasahiro Yamada # You cannot use 'git describe --dirty' because it tries to 65ffaf62a8SMasahiro Yamada # create .git/index.lock . 66ff64dd48SBrian Norris # First, with git-status, but --no-optional-locks is only 67ff64dd48SBrian Norris # supported in git >= 2.14, so fall back to git-diff-index if 68ff64dd48SBrian Norris # it fails. Note that git-diff-index does not refresh the 69ff64dd48SBrian Norris # index, so it may give misleading results. See 70ff64dd48SBrian Norris # git-update-index(1), git-diff-index(1), and git-status(1). 71ff64dd48SBrian Norris if { 72ff64dd48SBrian Norris git --no-optional-locks status -uno --porcelain 2>/dev/null || 73ff64dd48SBrian Norris git diff-index --name-only HEAD 74a2be76a3SMasahiro Yamada } | read dummy; then 7524d49756SRyan Anderson printf '%s' -dirty 76117a93dbSRene Scharfe fi 7709155120SMichal Marek fi 7809155120SMichal Marek} 7909155120SMichal Marek 8009155120SMichal Marekcollect_files() 8109155120SMichal Marek{ 827a82e3faSMasahiro Yamada local file res= 8309155120SMichal Marek 8409155120SMichal Marek for file; do 8509155120SMichal Marek case "$file" in 8609155120SMichal Marek *\~*) 8709155120SMichal Marek continue 8809155120SMichal Marek ;; 8909155120SMichal Marek esac 9009155120SMichal Marek if test -e "$file"; then 9109155120SMichal Marek res="$res$(cat "$file")" 9209155120SMichal Marek fi 9309155120SMichal Marek done 9409155120SMichal Marek echo "$res" 9509155120SMichal Marek} 9609155120SMichal Marek 977d153696SMasahiro Yamadaif ! test -e include/config/auto.conf; then 9878283edfSWolfram Sang echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2 9909155120SMichal Marek exit 1 10009155120SMichal Marekfi 10109155120SMichal Marek 10209155120SMichal Marek# localversion* files in the build and source directory 10309155120SMichal Marekres="$(collect_files localversion*)" 10409155120SMichal Marekif test ! "$srctree" -ef .; then 10509155120SMichal Marek res="$res$(collect_files "$srctree"/localversion*)" 10609155120SMichal Marekfi 10709155120SMichal Marek 10809155120SMichal Marek# CONFIG_LOCALVERSION and LOCALVERSION (if set) 109129ab0d2SMasahiro Yamadaconfig_localversion=$(sed -n 's/^CONFIG_LOCALVERSION=\(.*\)$/\1/p' include/config/auto.conf) 1107d153696SMasahiro Yamadares="${res}${config_localversion}${LOCALVERSION}" 11109155120SMichal Marek 11209155120SMichal Marek# scm version string if not at a tagged commit 1137d153696SMasahiro Yamadaif grep -q "^CONFIG_LOCALVERSION_AUTO=y$" include/config/auto.conf; then 11409155120SMichal Marek # full scm version string 11509155120SMichal Marek res="$res$(scm_version)" 1165df99becSMikulas Patockaelif [ "${LOCALVERSION+set}" != "set" ]; then 1175df99becSMikulas Patocka # If the variable LOCALVERSION is not set, append a plus 1185df99becSMikulas Patocka # sign if the repository is not in a clean annotated or 1195df99becSMikulas Patocka # signed tagged state (as git describe only looks at signed 1205df99becSMikulas Patocka # or annotated tags - git tag -a/-s). 1215df99becSMikulas Patocka # 1225df99becSMikulas Patocka # If the variable LOCALVERSION is set (including being set 1235df99becSMikulas Patocka # to an empty string), we don't want to append a plus sign. 12409155120SMichal Marek scm=$(scm_version --short) 12509155120SMichal Marek res="$res${scm:++}" 12609155120SMichal Marekfi 12709155120SMichal Marek 12809155120SMichal Marekecho "$res" 129