setlocalversion: Prevent tags from overflowing version string

Some post Linus tags are very long and they exceed the character
limit on the version string. This leads to compile errors like

 3.0.8-insert-your-reallllly-long-tag-name-here-13-g4b4e960-dirty
 exceeds 64 characters

Instead of putting the pretty printed name of the closest post
Linus tag, place the tag's object hash in the version string.
This should allow developers to easily run a git show on the
first hash to see what tag the build is based on. The version
will look like:

	3.0.8-gb080168-00006-g41f3bb3-dirty

meaning the kernel is based on v3.0.8 at the tag b080168 with 6
patches applied on top of that tag resulting in a commit with the
hash 41f3bb3 plus a dirty tree. Running "git show b080168"
should show the closest tag the tree was based on.

Change-Id: I8a26532f76aadf31654cb420ab789e90bd2fe828
Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>
diff --git a/scripts/setlocalversion b/scripts/setlocalversion
index 2c52866..d2690c3 100755
--- a/scripts/setlocalversion
+++ b/scripts/setlocalversion
@@ -47,7 +47,20 @@
 
 		# If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
 		# it, because this version is defined in the top level Makefile.
-		if [ -z "`git describe --exact-match 2>/dev/null`" ]; then
+		if atag="`git describe --exact-match --abbrev=0 2>/dev/null`"; then
+			# Make sure we're at the tag that matches the Makefile.
+			# If not place the hash of the tag as well for
+			# v2.6.30-rc5-g314aef
+			if [ "x$atag" -ne "x$VERSION" ]; then
+				# If only the short version is requested,
+				# don't bother running further git commands
+				if $short; then
+					echo "+"
+					return
+				fi
+				printf '%s%s' -g "`git show-ref -s --abbrev $atag 2>/dev/null`"
+			fi
+		else
 
 			# If only the short version is requested, don't bother
 			# running further git commands
@@ -55,17 +68,18 @@
 				echo "+"
 				return
 			fi
+			# If we are past a tagged commit (like
+			# "v2.6.30-rc5-302-g72357d5"), we pretty print it and
+			# include the hash of any new tag on top.
+			if atag="`git describe 2>/dev/null`"; then
+				tag="`git describe --abbrev=0 2>/dev/null`"
+				commit="`echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),$(NF))}'`"
+				printf '%s%s%s' -g "`git show-ref -s --abbrev $tag 2>/dev/null`" $commit
+			# If we don't have a tag at all we print -g{commitish}.
+			else
+				printf '%s%s' -g $head
+			fi
 		fi
-		# If we are past a tagged commit (like
-		# "v2.6.30-rc5-302-g72357d5"), we pretty print it but strip
-		# off the v2.6.30-rc5 part because that's in the Makefile.
-		if atag="`git describe 2>/dev/null`"; then
-			atag="-${atag/v$KERNELVERSION-/}"
-		# If we don't have a tag at all we print -g{commitish}.
-		else
-			atag="-g$head"
-		fi
-		printf '%s' "$atag"
 
 		# Is this git on svn?
 		if git config --get svn-remote.svn.url >/dev/null; then