1dd88ab32SMasahiro Yamada#!/bin/sh 2dd88ab32SMasahiro Yamada# 3dd88ab32SMasahiro Yamada# This scripts adds local version information from the version 4dd88ab32SMasahiro Yamada# control systems git, mercurial (hg) and subversion (svn). 5dd88ab32SMasahiro Yamada# 6a6f47d24SMasahiro Yamada# If something goes wrong, send a mail the kernel build mailinglist 7a6f47d24SMasahiro Yamada# (see MAINTAINERS) and CC Nico Schottelius 8a6f47d24SMasahiro Yamada# <nico-linuxsetlocalversion -at- schottelius.org>. 9a6f47d24SMasahiro Yamada# 10dd88ab32SMasahiro Yamada# 11dd88ab32SMasahiro Yamada 12dd88ab32SMasahiro Yamadausage() { 13dd88ab32SMasahiro Yamada echo "Usage: $0 [--save-scmversion] [srctree]" >&2 14dd88ab32SMasahiro Yamada exit 1 15dd88ab32SMasahiro Yamada} 16dd88ab32SMasahiro Yamada 17dd88ab32SMasahiro Yamadascm_only=false 18dd88ab32SMasahiro Yamadasrctree=. 19dd88ab32SMasahiro Yamadaif test "$1" = "--save-scmversion"; then 20dd88ab32SMasahiro Yamada scm_only=true 21dd88ab32SMasahiro Yamada shift 22dd88ab32SMasahiro Yamadafi 23dd88ab32SMasahiro Yamadaif test $# -gt 0; then 24dd88ab32SMasahiro Yamada srctree=$1 25dd88ab32SMasahiro Yamada shift 26dd88ab32SMasahiro Yamadafi 27dd88ab32SMasahiro Yamadaif test $# -gt 0 -o ! -d "$srctree"; then 28dd88ab32SMasahiro Yamada usage 29dd88ab32SMasahiro Yamadafi 30dd88ab32SMasahiro Yamada 31dd88ab32SMasahiro Yamadascm_version() 32dd88ab32SMasahiro Yamada{ 33dd88ab32SMasahiro Yamada local short 34dd88ab32SMasahiro Yamada short=false 35dd88ab32SMasahiro Yamada 36dd88ab32SMasahiro Yamada cd "$srctree" 37dd88ab32SMasahiro Yamada if test -e .scmversion; then 38dd88ab32SMasahiro Yamada cat .scmversion 39dd88ab32SMasahiro Yamada return 40dd88ab32SMasahiro Yamada fi 41dd88ab32SMasahiro Yamada if test "$1" = "--short"; then 42dd88ab32SMasahiro Yamada short=true 43dd88ab32SMasahiro Yamada fi 44dd88ab32SMasahiro Yamada 45dd88ab32SMasahiro Yamada # Check for git and a git repo. 46a6f47d24SMasahiro Yamada if test -z "$(git rev-parse --show-cdup 2>/dev/null)" && 47a6f47d24SMasahiro Yamada head=`git rev-parse --verify --short HEAD 2>/dev/null`; then 48dd88ab32SMasahiro Yamada 49dd88ab32SMasahiro Yamada # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore 50dd88ab32SMasahiro Yamada # it, because this version is defined in the top level Makefile. 51dd88ab32SMasahiro Yamada if [ -z "`git describe --exact-match 2>/dev/null`" ]; then 52dd88ab32SMasahiro Yamada 53dd88ab32SMasahiro Yamada # If only the short version is requested, don't bother 54dd88ab32SMasahiro Yamada # running further git commands 55dd88ab32SMasahiro Yamada if $short; then 56dd88ab32SMasahiro Yamada echo "+" 57dd88ab32SMasahiro Yamada return 58dd88ab32SMasahiro Yamada fi 59dd88ab32SMasahiro Yamada # If we are past a tagged commit (like 60dd88ab32SMasahiro Yamada # "v2.6.30-rc5-302-g72357d5"), we pretty print it. 61dd88ab32SMasahiro Yamada if atag="`git describe 2>/dev/null`"; then 62*56ed6b51SJoseph Chen echo "$atag" | awk -F- '{printf("-%s", $(NF))}' 63*56ed6b51SJoseph Chen date=`git log -1 --author='@rock-chips' --date=format:%y%m%d | sed -n '/Date:/p' | awk '{ print "-"$2 }'` 64*56ed6b51SJoseph Chen printf '%s' $date 65dd88ab32SMasahiro Yamada 66dd88ab32SMasahiro Yamada # If we don't have a tag at all we print -g{commitish}. 67dd88ab32SMasahiro Yamada else 68dd88ab32SMasahiro Yamada printf '%s%s' -g $head 69dd88ab32SMasahiro Yamada fi 70dd88ab32SMasahiro Yamada fi 71dd88ab32SMasahiro Yamada 72dd88ab32SMasahiro Yamada # Is this git on svn? 73dd88ab32SMasahiro Yamada if git config --get svn-remote.svn.url >/dev/null; then 74dd88ab32SMasahiro Yamada printf -- '-svn%s' "`git svn find-rev $head`" 75dd88ab32SMasahiro Yamada fi 76dd88ab32SMasahiro Yamada 77dd88ab32SMasahiro Yamada # Check for uncommitted changes 78a6f47d24SMasahiro Yamada if git diff-index --name-only HEAD | grep -qv "^scripts/package"; then 79dd88ab32SMasahiro Yamada printf '%s' -dirty 80dd88ab32SMasahiro Yamada fi 81dd88ab32SMasahiro Yamada 82*56ed6b51SJoseph Chen printf ' \#%s' $USER 83*56ed6b51SJoseph Chen 84dd88ab32SMasahiro Yamada # All done with git 85dd88ab32SMasahiro Yamada return 86dd88ab32SMasahiro Yamada fi 87dd88ab32SMasahiro Yamada 88dd88ab32SMasahiro Yamada # Check for mercurial and a mercurial repo. 89dd88ab32SMasahiro Yamada if test -d .hg && hgid=`hg id 2>/dev/null`; then 90dd88ab32SMasahiro Yamada # Do we have an tagged version? If so, latesttagdistance == 1 91dd88ab32SMasahiro Yamada if [ "`hg log -r . --template '{latesttagdistance}'`" == "1" ]; then 92dd88ab32SMasahiro Yamada id=`hg log -r . --template '{latesttag}'` 93dd88ab32SMasahiro Yamada printf '%s%s' -hg "$id" 94dd88ab32SMasahiro Yamada else 95dd88ab32SMasahiro Yamada tag=`printf '%s' "$hgid" | cut -d' ' -f2` 96dd88ab32SMasahiro Yamada if [ -z "$tag" -o "$tag" = tip ]; then 97dd88ab32SMasahiro Yamada id=`printf '%s' "$hgid" | sed 's/[+ ].*//'` 98dd88ab32SMasahiro Yamada printf '%s%s' -hg "$id" 99dd88ab32SMasahiro Yamada fi 100dd88ab32SMasahiro Yamada fi 101dd88ab32SMasahiro Yamada 102dd88ab32SMasahiro Yamada # Are there uncommitted changes? 103dd88ab32SMasahiro Yamada # These are represented by + after the changeset id. 104dd88ab32SMasahiro Yamada case "$hgid" in 105dd88ab32SMasahiro Yamada *+|*+\ *) printf '%s' -dirty ;; 106dd88ab32SMasahiro Yamada esac 107dd88ab32SMasahiro Yamada 108dd88ab32SMasahiro Yamada # All done with mercurial 109dd88ab32SMasahiro Yamada return 110dd88ab32SMasahiro Yamada fi 111dd88ab32SMasahiro Yamada 112dd88ab32SMasahiro Yamada # Check for svn and a svn repo. 113a6f47d24SMasahiro Yamada if rev=`LANG= LC_ALL= LC_MESSAGES=C svn info 2>/dev/null | grep '^Last Changed Rev'`; then 114dd88ab32SMasahiro Yamada rev=`echo $rev | awk '{print $NF}'` 115dd88ab32SMasahiro Yamada printf -- '-svn%s' "$rev" 116dd88ab32SMasahiro Yamada 117dd88ab32SMasahiro Yamada # All done with svn 118dd88ab32SMasahiro Yamada return 119dd88ab32SMasahiro Yamada fi 120dd88ab32SMasahiro Yamada} 121dd88ab32SMasahiro Yamada 122dd88ab32SMasahiro Yamadacollect_files() 123dd88ab32SMasahiro Yamada{ 124dd88ab32SMasahiro Yamada local file res 125dd88ab32SMasahiro Yamada 126dd88ab32SMasahiro Yamada for file; do 127dd88ab32SMasahiro Yamada case "$file" in 128dd88ab32SMasahiro Yamada *\~*) 129dd88ab32SMasahiro Yamada continue 130dd88ab32SMasahiro Yamada ;; 131dd88ab32SMasahiro Yamada esac 132dd88ab32SMasahiro Yamada if test -e "$file"; then 133dd88ab32SMasahiro Yamada res="$res$(cat "$file")" 134dd88ab32SMasahiro Yamada fi 135dd88ab32SMasahiro Yamada done 136dd88ab32SMasahiro Yamada echo "$res" 137dd88ab32SMasahiro Yamada} 138dd88ab32SMasahiro Yamada 139dd88ab32SMasahiro Yamadaif $scm_only; then 140dd88ab32SMasahiro Yamada if test ! -e .scmversion; then 141dd88ab32SMasahiro Yamada res=$(scm_version) 142dd88ab32SMasahiro Yamada echo "$res" >.scmversion 143dd88ab32SMasahiro Yamada fi 144dd88ab32SMasahiro Yamada exit 145dd88ab32SMasahiro Yamadafi 146dd88ab32SMasahiro Yamada 147a6f47d24SMasahiro Yamadaif test -e include/config/auto.conf; then 14881630a3bSPhilipp Tomsich # We are interested only in CONFIG_LOCALVERSION and 14981630a3bSPhilipp Tomsich # CONFIG_LOCALVERSION_AUTO, so extract these in a safe 15081630a3bSPhilipp Tomsich # way (i.e. w/o sourcing auto.conf) 15181630a3bSPhilipp Tomsich CONFIG_LOCALVERSION=`cat include/config/auto.conf | awk -F '=' '/^CONFIG_LOCALVERSION=/ {print $2}'` 15281630a3bSPhilipp Tomsich CONFIG_LOCALVERSION_AUTO=`cat include/config/auto.conf | awk -F '=' '/^CONFIG_LOCALVERSION_AUTO=/ {print $2}'` 153a6f47d24SMasahiro Yamadaelse 154a6f47d24SMasahiro Yamada echo "Error: kernelrelease not valid - run 'make prepare' to update it" 155a6f47d24SMasahiro Yamada exit 1 156a6f47d24SMasahiro Yamadafi 157dd88ab32SMasahiro Yamada 158dd88ab32SMasahiro Yamada# localversion* files in the build and source directory 159dd88ab32SMasahiro Yamadares="$(collect_files localversion*)" 160dd88ab32SMasahiro Yamadaif test ! "$srctree" -ef .; then 161dd88ab32SMasahiro Yamada res="$res$(collect_files "$srctree"/localversion*)" 162dd88ab32SMasahiro Yamadafi 163dd88ab32SMasahiro Yamada 164dd88ab32SMasahiro Yamada# CONFIG_LOCALVERSION and LOCALVERSION (if set) 165dd88ab32SMasahiro Yamadares="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}" 166dd88ab32SMasahiro Yamada 167dd88ab32SMasahiro Yamada# scm version string if not at a tagged commit 168dd88ab32SMasahiro Yamadaif test "$CONFIG_LOCALVERSION_AUTO" = "y"; then 169dd88ab32SMasahiro Yamada # full scm version string 170dd88ab32SMasahiro Yamada res="$res$(scm_version)" 171dd88ab32SMasahiro Yamadaelse 172dd88ab32SMasahiro Yamada # append a plus sign if the repository is not in a clean 173dd88ab32SMasahiro Yamada # annotated or signed tagged state (as git describe only 174dd88ab32SMasahiro Yamada # looks at signed or annotated tags - git tag -a/-s) and 175dd88ab32SMasahiro Yamada # LOCALVERSION= is not specified 176dd88ab32SMasahiro Yamada if test "${LOCALVERSION+set}" != "set"; then 177dd88ab32SMasahiro Yamada scm=$(scm_version --short) 178dd88ab32SMasahiro Yamada res="$res${scm:++}" 179dd88ab32SMasahiro Yamada fi 180dd88ab32SMasahiro Yamadafi 181dd88ab32SMasahiro Yamada 182dd88ab32SMasahiro Yamadaecho "$res" 183