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() { 14b003afe3SMichal Marek echo "Usage: $0 [--save-scmversion] [srctree]" >&2 15117a93dbSRene Scharfe exit 1 16aaebf433SRyan Anderson} 17aaebf433SRyan Anderson 1809155120SMichal Marekscm_only=false 1909155120SMichal Mareksrctree=. 20b003afe3SMichal Marekif test "$1" = "--save-scmversion"; then 2109155120SMichal Marek scm_only=true 2209155120SMichal Marek shift 2309155120SMichal Marekfi 2409155120SMichal Marekif test $# -gt 0; then 2509155120SMichal Marek srctree=$1 2609155120SMichal Marek shift 2709155120SMichal Marekfi 2809155120SMichal Marekif test $# -gt 0 -o ! -d "$srctree"; then 2909155120SMichal Marek usage 3009155120SMichal Marekfi 3109155120SMichal Marek 3209155120SMichal Marekscm_version() 3309155120SMichal Marek{ 346dc0c2f3SMichał Górny local short 356dc0c2f3SMichał Górny short=false 3609155120SMichal Marek 3709155120SMichal Marek cd "$srctree" 3809155120SMichal Marek if test -e .scmversion; then 396dc0c2f3SMichał Górny cat .scmversion 4009155120SMichal Marek return 4109155120SMichal Marek fi 4209155120SMichal Marek if test "$1" = "--short"; then 4309155120SMichal Marek short=true 4409155120SMichal Marek fi 45aaebf433SRyan Anderson 46117a93dbSRene Scharfe # Check for git and a git repo. 477593e090SFranck Bui-Huu if test -z "$(git rev-parse --show-cdup 2>/dev/null)" && 48*548b8b51SRasmus Villemoes head=$(git rev-parse --verify HEAD 2>/dev/null); then 4933252572SNico Schottelius 5009155120SMichal Marek # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore 5109155120SMichal Marek # it, because this version is defined in the top level Makefile. 523c96bdd0SBhaskar Chowdhury if [ -z "$(git describe --exact-match 2>/dev/null)" ]; then 5333252572SNico Schottelius 5409155120SMichal Marek # If only the short version is requested, don't bother 5509155120SMichal Marek # running further git commands 5609155120SMichal Marek if $short; then 5709155120SMichal Marek echo "+" 5809155120SMichal Marek return 5909155120SMichal Marek fi 6009155120SMichal Marek # If we are past a tagged commit (like 6109155120SMichal Marek # "v2.6.30-rc5-302-g72357d5"), we pretty print it. 62*548b8b51SRasmus Villemoes # 63*548b8b51SRasmus Villemoes # Ensure the abbreviated sha1 has exactly 12 64*548b8b51SRasmus Villemoes # hex characters, to make the output 65*548b8b51SRasmus Villemoes # independent of git version, local 66*548b8b51SRasmus Villemoes # core.abbrev settings and/or total number of 67*548b8b51SRasmus Villemoes # objects in the current repository - passing 68*548b8b51SRasmus Villemoes # --abbrev=12 ensures a minimum of 12, and the 69*548b8b51SRasmus Villemoes # awk substr() then picks the 'g' and first 12 70*548b8b51SRasmus Villemoes # hex chars. 71*548b8b51SRasmus Villemoes if atag="$(git describe --abbrev=12 2>/dev/null)"; then 72*548b8b51SRasmus Villemoes echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),substr($(NF),0,13))}' 7333252572SNico Schottelius 74*548b8b51SRasmus Villemoes # If we don't have a tag at all we print -g{commitish}, 75*548b8b51SRasmus Villemoes # again using exactly 12 hex chars. 76f03b283fSTrent Piepho else 77*548b8b51SRasmus Villemoes head="$(echo $head | cut -c1-12)" 78f03b283fSTrent Piepho printf '%s%s' -g $head 7956b2f070SSebastian Siewior fi 8033252572SNico Schottelius fi 81aaebf433SRyan Anderson 82ff80aa97SPeter Korsgaard # Is this git on svn? 83ff80aa97SPeter Korsgaard if git config --get svn-remote.svn.url >/dev/null; then 843c96bdd0SBhaskar Chowdhury printf -- '-svn%s' "$(git svn find-rev $head)" 85ff80aa97SPeter Korsgaard fi 86ff80aa97SPeter Korsgaard 87ff64dd48SBrian Norris # Check for uncommitted changes. 88ff64dd48SBrian Norris # First, with git-status, but --no-optional-locks is only 89ff64dd48SBrian Norris # supported in git >= 2.14, so fall back to git-diff-index if 90ff64dd48SBrian Norris # it fails. Note that git-diff-index does not refresh the 91ff64dd48SBrian Norris # index, so it may give misleading results. See 92ff64dd48SBrian Norris # git-update-index(1), git-diff-index(1), and git-status(1). 93ff64dd48SBrian Norris if { 94ff64dd48SBrian Norris git --no-optional-locks status -uno --porcelain 2>/dev/null || 95ff64dd48SBrian Norris git diff-index --name-only HEAD 96ff64dd48SBrian Norris } | grep -qvE '^(.. )?scripts/package'; then 9724d49756SRyan Anderson printf '%s' -dirty 98117a93dbSRene Scharfe fi 993dce174cSAron Griffis 1003dce174cSAron Griffis # All done with git 10109155120SMichal Marek return 1023dce174cSAron Griffis fi 1033dce174cSAron Griffis 1043dce174cSAron Griffis # Check for mercurial and a mercurial repo. 1053c96bdd0SBhaskar Chowdhury if test -d .hg && hgid=$(hg id 2>/dev/null); then 10638b3439dSMike Crowe # Do we have an tagged version? If so, latesttagdistance == 1 1073c96bdd0SBhaskar Chowdhury if [ "$(hg log -r . --template '{latesttagdistance}')" = "1" ]; then 1083c96bdd0SBhaskar Chowdhury id=$(hg log -r . --template '{latesttag}') 10938b3439dSMike Crowe printf '%s%s' -hg "$id" 11038b3439dSMike Crowe else 1113c96bdd0SBhaskar Chowdhury tag=$(printf '%s' "$hgid" | cut -d' ' -f2) 1123dce174cSAron Griffis if [ -z "$tag" -o "$tag" = tip ]; then 1133c96bdd0SBhaskar Chowdhury id=$(printf '%s' "$hgid" | sed 's/[+ ].*//') 1143dce174cSAron Griffis printf '%s%s' -hg "$id" 1153dce174cSAron Griffis fi 11638b3439dSMike Crowe fi 1173dce174cSAron Griffis 1183dce174cSAron Griffis # Are there uncommitted changes? 1193dce174cSAron Griffis # These are represented by + after the changeset id. 1203dce174cSAron Griffis case "$hgid" in 1213dce174cSAron Griffis *+|*+\ *) printf '%s' -dirty ;; 1223dce174cSAron Griffis esac 1233dce174cSAron Griffis 1243dce174cSAron Griffis # All done with mercurial 12509155120SMichal Marek return 126117a93dbSRene Scharfe fi 127ba3d05fbSBryan Wu 128ba3d05fbSBryan Wu # Check for svn and a svn repo. 1293c96bdd0SBhaskar Chowdhury if rev=$(LANG= LC_ALL= LC_MESSAGES=C svn info 2>/dev/null | grep '^Last Changed Rev'); then 1303c96bdd0SBhaskar Chowdhury rev=$(echo $rev | awk '{print $NF}') 131ba3d05fbSBryan Wu printf -- '-svn%s' "$rev" 132ba3d05fbSBryan Wu 133ba3d05fbSBryan Wu # All done with svn 13409155120SMichal Marek return 13509155120SMichal Marek fi 13609155120SMichal Marek} 13709155120SMichal Marek 13809155120SMichal Marekcollect_files() 13909155120SMichal Marek{ 1407a82e3faSMasahiro Yamada local file res= 14109155120SMichal Marek 14209155120SMichal Marek for file; do 14309155120SMichal Marek case "$file" in 14409155120SMichal Marek *\~*) 14509155120SMichal Marek continue 14609155120SMichal Marek ;; 14709155120SMichal Marek esac 14809155120SMichal Marek if test -e "$file"; then 14909155120SMichal Marek res="$res$(cat "$file")" 15009155120SMichal Marek fi 15109155120SMichal Marek done 15209155120SMichal Marek echo "$res" 15309155120SMichal Marek} 15409155120SMichal Marek 15509155120SMichal Marekif $scm_only; then 156b003afe3SMichal Marek if test ! -e .scmversion; then 157b003afe3SMichal Marek res=$(scm_version) 158b003afe3SMichal Marek echo "$res" >.scmversion 159b003afe3SMichal Marek fi 160ba3d05fbSBryan Wu exit 161ba3d05fbSBryan Wufi 16209155120SMichal Marek 16309155120SMichal Marekif test -e include/config/auto.conf; then 1646dc0c2f3SMichał Górny . include/config/auto.conf 16509155120SMichal Marekelse 16678283edfSWolfram Sang echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2 16709155120SMichal Marek exit 1 16809155120SMichal Marekfi 16909155120SMichal Marek 17009155120SMichal Marek# localversion* files in the build and source directory 17109155120SMichal Marekres="$(collect_files localversion*)" 17209155120SMichal Marekif test ! "$srctree" -ef .; then 17309155120SMichal Marek res="$res$(collect_files "$srctree"/localversion*)" 17409155120SMichal Marekfi 17509155120SMichal Marek 17609155120SMichal Marek# CONFIG_LOCALVERSION and LOCALVERSION (if set) 17709155120SMichal Marekres="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}" 17809155120SMichal Marek 17909155120SMichal Marek# scm version string if not at a tagged commit 18009155120SMichal Marekif test "$CONFIG_LOCALVERSION_AUTO" = "y"; then 18109155120SMichal Marek # full scm version string 18209155120SMichal Marek res="$res$(scm_version)" 18309155120SMichal Marekelse 184c3e2f196SMichael Prokop # append a plus sign if the repository is not in a clean 185c3e2f196SMichael Prokop # annotated or signed tagged state (as git describe only 186c3e2f196SMichael Prokop # looks at signed or annotated tags - git tag -a/-s) and 187c3e2f196SMichael Prokop # LOCALVERSION= is not specified 18809155120SMichal Marek if test "${LOCALVERSION+set}" != "set"; then 18909155120SMichal Marek scm=$(scm_version --short) 19009155120SMichal Marek res="$res${scm:++}" 19109155120SMichal Marek fi 19209155120SMichal Marekfi 19309155120SMichal Marek 19409155120SMichal Marekecho "$res" 195