Add CONFIG_LOCALVERSION_AUTO_GIT_TAG_GLOB

"git describe" prints the most recent tag it can find in the git repo.
The Makefile bakes the output into the version string. If the most
recent tag turns out to be a SpaceCast tag, I get version strings that
cointain something like "spacecast-3.8-6-gafe9d1f" even if I build for
GFiber devices.

Add CONFIG_LOCALVERSION_AUTO_GIT_TAG_GLOB which allows us to pass a
"--match" option to git describe. If we set it to "gfiber*", we'll only
get gfiber tags.

Change-Id: Ic751cef172f51b997d1f10aafd22477d027a87c9
diff --git a/Makefile b/Makefile
index a39d8f1..69ca638 100644
--- a/Makefile
+++ b/Makefile
@@ -794,7 +794,9 @@
 # checks as needed.
 ifdef CONFIG_LOCALVERSION_AUTO
 	_localver-auto = $(shell $(CONFIG_SHELL) \
-	                  $(srctree)/scripts/setlocalversion $(srctree))
+			  $(srctree)/scripts/setlocalversion \
+			  -m $(CONFIG_LOCALVERSION_AUTO_GIT_TAG_GLOB) \
+			  $(srctree))
 	localver-auto  = $(LOCALVERSION)$(_localver-auto)
 endif
 
diff --git a/common/Kconfig b/common/Kconfig
index 62ffd14..9df7508 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -62,6 +62,21 @@
 
 	  which is done within the script "scripts/setlocalversion".)
 
+config LOCALVERSION_AUTO_GIT_TAG_GLOB
+	depends on LOCALVERSION_AUTO
+	string "Only consider git tags for version information that match glob pattern"
+	help
+	  The script "scripts/setlocalversion" uses the command "git describe" to
+	  look for a git tag and appends it to localversion. This option allows us
+	  to pass a "--match" parameter to "git describe". The git man page describes
+	  the parameter like this: "Only consider tags matching the given glob(7)
+	  pattern,"
+
+	  Without this parameter, "git describe" returns
+	  "spacecast-3.8-6-gafe9d1f" which might be confusing if we are building
+	  an image for Google Fiber. To avoid this, we can pass "--match gfiber*"
+	  into "git describe" and we will get "gfiber-46-pre3-6-gafe9d1f".
+
 config BOARDINFO
 	string
 
diff --git a/scripts/setlocalversion b/scripts/setlocalversion
index 1313945..332f84d 100755
--- a/scripts/setlocalversion
+++ b/scripts/setlocalversion
@@ -6,13 +6,25 @@
 	exit 1
 }
 
+while getopts "m:" opt; do
+	case $opt in
+		m)
+			LOCALVERSION_AUTO_GIT_TAG_GLOB="$OPTARG"
+			;;
+	esac
+done
+shift $((OPTIND - 1))
 cd "${1:-.}" || usage
 
 # Check for git and a git repo.
 if head=`git rev-parse --verify HEAD 2>/dev/null`; then
 	# Do we have an untagged version?
 	if git name-rev --tags HEAD | grep -E '^HEAD[[:space:]]+(.*~[0-9]*|undefined)$' > /dev/null; then
-		if tag=`git describe 2>/dev/null`; then
+		set --
+		if [ -n "$LOCALVERSION_AUTO_GIT_TAG_GLOB" ]; then
+			set -- --match "$LOCALVERSION_AUTO_GIT_TAG_GLOB"
+		fi
+		if tag=`git describe "$@" 2>/dev/null`; then
 			echo -n "-${tag}"
 		fi
 	fi