18c2ecf20Sopenharmony_ci#!/bin/sh 28c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0 38c2ecf20Sopenharmony_ci# 48c2ecf20Sopenharmony_ci# This scripts adds local version information from the version 58c2ecf20Sopenharmony_ci# control systems git, mercurial (hg) and subversion (svn). 68c2ecf20Sopenharmony_ci# 78c2ecf20Sopenharmony_ci# If something goes wrong, send a mail the kernel build mailinglist 88c2ecf20Sopenharmony_ci# (see MAINTAINERS) and CC Nico Schottelius 98c2ecf20Sopenharmony_ci# <nico-linuxsetlocalversion -at- schottelius.org>. 108c2ecf20Sopenharmony_ci# 118c2ecf20Sopenharmony_ci# 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ciusage() { 148c2ecf20Sopenharmony_ci echo "Usage: $0 [--save-scmversion] [srctree]" >&2 158c2ecf20Sopenharmony_ci exit 1 168c2ecf20Sopenharmony_ci} 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ciscm_only=false 198c2ecf20Sopenharmony_cisrctree=. 208c2ecf20Sopenharmony_ciif test "$1" = "--save-scmversion"; then 218c2ecf20Sopenharmony_ci scm_only=true 228c2ecf20Sopenharmony_ci shift 238c2ecf20Sopenharmony_cifi 248c2ecf20Sopenharmony_ciif test $# -gt 0; then 258c2ecf20Sopenharmony_ci srctree=$1 268c2ecf20Sopenharmony_ci shift 278c2ecf20Sopenharmony_cifi 288c2ecf20Sopenharmony_ciif test $# -gt 0 -o ! -d "$srctree"; then 298c2ecf20Sopenharmony_ci usage 308c2ecf20Sopenharmony_cifi 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ciscm_version() 338c2ecf20Sopenharmony_ci{ 348c2ecf20Sopenharmony_ci local short 358c2ecf20Sopenharmony_ci short=false 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci cd "$srctree" 388c2ecf20Sopenharmony_ci if test -e .scmversion; then 398c2ecf20Sopenharmony_ci cat .scmversion 408c2ecf20Sopenharmony_ci return 418c2ecf20Sopenharmony_ci fi 428c2ecf20Sopenharmony_ci if test "$1" = "--short"; then 438c2ecf20Sopenharmony_ci short=true 448c2ecf20Sopenharmony_ci fi 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci # Check for git and a git repo. 478c2ecf20Sopenharmony_ci if test -z "$(git rev-parse --show-cdup 2>/dev/null)" && 488c2ecf20Sopenharmony_ci head=$(git rev-parse --verify HEAD 2>/dev/null); then 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore 518c2ecf20Sopenharmony_ci # it, because this version is defined in the top level Makefile. 528c2ecf20Sopenharmony_ci if [ -z "$(git describe --exact-match 2>/dev/null)" ]; then 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci # If only the short version is requested, don't bother 558c2ecf20Sopenharmony_ci # running further git commands 568c2ecf20Sopenharmony_ci if $short; then 578c2ecf20Sopenharmony_ci echo "+" 588c2ecf20Sopenharmony_ci return 598c2ecf20Sopenharmony_ci fi 608c2ecf20Sopenharmony_ci # If we are past a tagged commit (like 618c2ecf20Sopenharmony_ci # "v2.6.30-rc5-302-g72357d5"), we pretty print it. 628c2ecf20Sopenharmony_ci # 638c2ecf20Sopenharmony_ci # Ensure the abbreviated sha1 has exactly 12 648c2ecf20Sopenharmony_ci # hex characters, to make the output 658c2ecf20Sopenharmony_ci # independent of git version, local 668c2ecf20Sopenharmony_ci # core.abbrev settings and/or total number of 678c2ecf20Sopenharmony_ci # objects in the current repository - passing 688c2ecf20Sopenharmony_ci # --abbrev=12 ensures a minimum of 12, and the 698c2ecf20Sopenharmony_ci # awk substr() then picks the 'g' and first 12 708c2ecf20Sopenharmony_ci # hex chars. 718c2ecf20Sopenharmony_ci if atag="$(git describe --abbrev=12 2>/dev/null)"; then 728c2ecf20Sopenharmony_ci echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),substr($(NF),0,13))}' 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci # If we don't have a tag at all we print -g{commitish}, 758c2ecf20Sopenharmony_ci # again using exactly 12 hex chars. 768c2ecf20Sopenharmony_ci else 778c2ecf20Sopenharmony_ci head="$(echo $head | cut -c1-12)" 788c2ecf20Sopenharmony_ci printf '%s%s' -g $head 798c2ecf20Sopenharmony_ci fi 808c2ecf20Sopenharmony_ci fi 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci # Is this git on svn? 838c2ecf20Sopenharmony_ci if git config --get svn-remote.svn.url >/dev/null; then 848c2ecf20Sopenharmony_ci printf -- '-svn%s' "$(git svn find-rev $head)" 858c2ecf20Sopenharmony_ci fi 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci # Check for uncommitted changes. 888c2ecf20Sopenharmony_ci # First, with git-status, but --no-optional-locks is only 898c2ecf20Sopenharmony_ci # supported in git >= 2.14, so fall back to git-diff-index if 908c2ecf20Sopenharmony_ci # it fails. Note that git-diff-index does not refresh the 918c2ecf20Sopenharmony_ci # index, so it may give misleading results. See 928c2ecf20Sopenharmony_ci # git-update-index(1), git-diff-index(1), and git-status(1). 938c2ecf20Sopenharmony_ci if { 948c2ecf20Sopenharmony_ci git --no-optional-locks status -uno --porcelain 2>/dev/null || 958c2ecf20Sopenharmony_ci git diff-index --name-only HEAD 968c2ecf20Sopenharmony_ci } | grep -qvE '^(.. )?scripts/package'; then 978c2ecf20Sopenharmony_ci printf '%s' -dirty 988c2ecf20Sopenharmony_ci fi 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci # All done with git 1018c2ecf20Sopenharmony_ci return 1028c2ecf20Sopenharmony_ci fi 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci # Check for mercurial and a mercurial repo. 1058c2ecf20Sopenharmony_ci if test -d .hg && hgid=$(hg id 2>/dev/null); then 1068c2ecf20Sopenharmony_ci # Do we have an tagged version? If so, latesttagdistance == 1 1078c2ecf20Sopenharmony_ci if [ "$(hg log -r . --template '{latesttagdistance}')" = "1" ]; then 1088c2ecf20Sopenharmony_ci id=$(hg log -r . --template '{latesttag}') 1098c2ecf20Sopenharmony_ci printf '%s%s' -hg "$id" 1108c2ecf20Sopenharmony_ci else 1118c2ecf20Sopenharmony_ci tag=$(printf '%s' "$hgid" | cut -d' ' -f2) 1128c2ecf20Sopenharmony_ci if [ -z "$tag" -o "$tag" = tip ]; then 1138c2ecf20Sopenharmony_ci id=$(printf '%s' "$hgid" | sed 's/[+ ].*//') 1148c2ecf20Sopenharmony_ci printf '%s%s' -hg "$id" 1158c2ecf20Sopenharmony_ci fi 1168c2ecf20Sopenharmony_ci fi 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci # Are there uncommitted changes? 1198c2ecf20Sopenharmony_ci # These are represented by + after the changeset id. 1208c2ecf20Sopenharmony_ci case "$hgid" in 1218c2ecf20Sopenharmony_ci *+|*+\ *) printf '%s' -dirty ;; 1228c2ecf20Sopenharmony_ci esac 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci # All done with mercurial 1258c2ecf20Sopenharmony_ci return 1268c2ecf20Sopenharmony_ci fi 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci # Check for svn and a svn repo. 1298c2ecf20Sopenharmony_ci if rev=$(LANG= LC_ALL= LC_MESSAGES=C svn info 2>/dev/null | grep '^Last Changed Rev'); then 1308c2ecf20Sopenharmony_ci rev=$(echo $rev | awk '{print $NF}') 1318c2ecf20Sopenharmony_ci printf -- '-svn%s' "$rev" 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci # All done with svn 1348c2ecf20Sopenharmony_ci return 1358c2ecf20Sopenharmony_ci fi 1368c2ecf20Sopenharmony_ci} 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_cicollect_files() 1398c2ecf20Sopenharmony_ci{ 1408c2ecf20Sopenharmony_ci local file res= 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci for file; do 1438c2ecf20Sopenharmony_ci case "$file" in 1448c2ecf20Sopenharmony_ci *\~*) 1458c2ecf20Sopenharmony_ci continue 1468c2ecf20Sopenharmony_ci ;; 1478c2ecf20Sopenharmony_ci esac 1488c2ecf20Sopenharmony_ci if test -e "$file"; then 1498c2ecf20Sopenharmony_ci res="$res$(cat "$file")" 1508c2ecf20Sopenharmony_ci fi 1518c2ecf20Sopenharmony_ci done 1528c2ecf20Sopenharmony_ci echo "$res" 1538c2ecf20Sopenharmony_ci} 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ciif $scm_only; then 1568c2ecf20Sopenharmony_ci if test ! -e .scmversion; then 1578c2ecf20Sopenharmony_ci res=$(scm_version) 1588c2ecf20Sopenharmony_ci echo "$res" >.scmversion 1598c2ecf20Sopenharmony_ci fi 1608c2ecf20Sopenharmony_ci exit 1618c2ecf20Sopenharmony_cifi 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ciif test -e include/config/auto.conf; then 1648c2ecf20Sopenharmony_ci . include/config/auto.conf 1658c2ecf20Sopenharmony_cielse 1668c2ecf20Sopenharmony_ci echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2 1678c2ecf20Sopenharmony_ci exit 1 1688c2ecf20Sopenharmony_cifi 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci# localversion* files in the build and source directory 1718c2ecf20Sopenharmony_cires="$(collect_files localversion*)" 1728c2ecf20Sopenharmony_ciif test ! "$srctree" -ef .; then 1738c2ecf20Sopenharmony_ci res="$res$(collect_files "$srctree"/localversion*)" 1748c2ecf20Sopenharmony_cifi 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci# CONFIG_LOCALVERSION and LOCALVERSION (if set) 1778c2ecf20Sopenharmony_cires="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}" 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci# scm version string if not at a tagged commit 1808c2ecf20Sopenharmony_ciif test "$CONFIG_LOCALVERSION_AUTO" = "y"; then 1818c2ecf20Sopenharmony_ci # full scm version string 1828c2ecf20Sopenharmony_ci res="$res$(scm_version)" 1838c2ecf20Sopenharmony_cielse 1848c2ecf20Sopenharmony_ci # append a plus sign if the repository is not in a clean 1858c2ecf20Sopenharmony_ci # annotated or signed tagged state (as git describe only 1868c2ecf20Sopenharmony_ci # looks at signed or annotated tags - git tag -a/-s) and 1878c2ecf20Sopenharmony_ci # LOCALVERSION= is not specified 1888c2ecf20Sopenharmony_ci if test "${LOCALVERSION+set}" != "set"; then 1898c2ecf20Sopenharmony_ci scm=$(scm_version --short) 1908c2ecf20Sopenharmony_ci res="$res${scm:++}" 1918c2ecf20Sopenharmony_ci fi 1928c2ecf20Sopenharmony_cifi 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ciecho "$res" 195