diff mbox

[v2,2/4] ndctl: version from git and annotated tags

Message ID 20150909230131.4976.3541.stgit@dwillia2-desk3.amr.corp.intel.com (mailing list archive)
State Accepted
Commit a2fbfb610730
Headers show

Commit Message

Dan Williams Sept. 9, 2015, 11:01 p.m. UTC
Arrange for "VERSION" in config.h to append the HEAD git commit when the
build is not being done on a released tag.  This also appends ".dirty"
when the build was done while local changes were pending in the tree.
When a released tag is present the VERSION string collapses to just the
base version number.

Signed-off-by: Dan Williams <dan.j.williams@intel.com>
---
 Makefile.am           |    6 ++++++
 autogen.sh            |    1 +
 configure.ac          |    3 ++-
 contrib/genspec.c     |    4 +++-
 contrib/ndctl.spec.in |    8 +++-----
 git-version-gen       |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 ndctl.c               |    4 +---
 7 files changed, 63 insertions(+), 10 deletions(-)
 create mode 100755 git-version-gen
diff mbox

Patch

diff --git a/Makefile.am b/Makefile.am
index d3b0c5274d94..f2ebeea58720 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -23,6 +23,12 @@  AM_LDFLAGS = \
 	-Wl,--gc-sections \
 	-Wl,--as-needed
 
+BUILT_SOURCES = $(top_srcdir)/version.m4
+$(top_srcdir)/version.m4: FORCE
+	$(AM_V_GEN)$(top_srcdir)/git-version-gen
+
+FORCE:
+
 SED_PROCESS = \
 	$(AM_V_GEN)$(MKDIR_P) $(dir $@) && $(SED) \
 	-e 's,@VERSION\@,$(VERSION),g' \
diff --git a/autogen.sh b/autogen.sh
index 0d60b0a96e9a..2970a3b9bff7 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -6,6 +6,7 @@  if [ -f .git/hooks/pre-commit.sample -a ! -f .git/hooks/pre-commit ] ; then
         echo "Activated pre-commit hook."
 fi
 
+$(dirname $0)/git-version-gen
 autoreconf --install --symlink
 
 libdir() {
diff --git a/configure.ac b/configure.ac
index 4b83e7444770..deaa94b0338b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,6 +1,7 @@ 
 AC_PREREQ(2.60)
+m4_include([version.m4])
 AC_INIT([ndctl],
-        [41],
+        GIT_VERSION,
         [linux-nvdimm@lists.01.org],
         [ndctl],
         [https://github.com/pmem/ndctl])
diff --git a/contrib/genspec.c b/contrib/genspec.c
index 14f27c4b2224..b3e85da3c9ff 100644
--- a/contrib/genspec.c
+++ b/contrib/genspec.c
@@ -34,7 +34,7 @@  int main(int argc, char **argv)
 
 	while (fgets(buf, sizeof(buf), stdin)) {
 		if (strncmp("Version:", buf, 8) == 0)
-			fprintf(stdout, "Version:        %s\n", VERSION);
+			fprintf(stdout, "Version:        %s\n", &VERSION[1]);
 		else if (strncmp("%global gitcommit", buf, 17) == 0)
 			fprintf(stdout, "%%global gitcommit %s\n", commit);
 		else if (strncmp("%define lname", buf, 12) == 0)
@@ -43,6 +43,8 @@  int main(int argc, char **argv)
 			fprintf(stdout, "%%define dname %s\n", dname[os]);
 		else if (strncmp("%license", buf, 8) == 0 && !license[os])
 			/* skip */;
+		else if (strncmp("echo \"\" > version", buf, 17) == 0)
+			fprintf(stdout, "echo \"%s\" > version\n", VERSION);
 		else
 			fprintf(stdout, "%s", buf);
 	}
diff --git a/contrib/ndctl.spec.in b/contrib/ndctl.spec.in
index 9e64a44e97c6..61fa525d1e87 100644
--- a/contrib/ndctl.spec.in
+++ b/contrib/ndctl.spec.in
@@ -4,7 +4,7 @@ 
 
 Name:           ndctl
 Version:
-Release:        1%{?gitcommit:.git%{gitcommit}}%{?dist}
+Release:        1%{?dist}
 Summary:	Manage "libnvdimm" subsystem devices (Non-volatile Memory)
 License:        GPL-2.0
 URL:            https://github.com/pmem/ndctl
@@ -52,11 +52,9 @@  Libraries for %{name}
 %prep
 %setup -q %{?gitcommit:-n %{name}-git%{gitcommit}}
 
-
 %build
-%if %{defined gitcommit}
-    ./autogen.sh
-%endif
+echo "" > version
+./autogen.sh
 %configure --disable-static
 make %{?_smp_mflags}
 
diff --git a/git-version-gen b/git-version-gen
new file mode 100755
index 000000000000..782a5f214c04
--- /dev/null
+++ b/git-version-gen
@@ -0,0 +1,47 @@ 
+#!/bin/sh
+
+dirty() {
+	git update-index -q --refresh
+	if test -z "$(git diff-index --name-only HEAD --)"; then
+		echo "$1"
+	else
+		echo "${1}.dirty"
+	fi
+}
+
+GVF=version.m4
+DEF_VER=v41
+
+LF='
+'
+
+# First see if there is a version file (included in release tarballs),
+# then try git-describe, then default.
+if test -f version; then
+	VN=$(cat version) || VN="$DEF_VER"
+elif test -d ${GIT_DIR:-.git} -o -f .git &&
+	VN=$(git describe --match "v[0-9]*" --abbrev=7 HEAD 2>/dev/null) &&
+	case "$VN" in
+	*$LF*) (exit 1) ;;
+	v[0-9]*)
+		VN="$(dirty $VN)"
+	esac; then
+	VN=$(echo "$VN" | sed -e 's/-/./g');
+else
+	read COMMIT COMMIT_SUBJECT <<EOF
+	$(git log --oneline --abbrev=8 -n1 HEAD)
+EOF
+	VN="$(dirty ${DEF_VER}.git$COMMIT)"
+fi
+
+if test -r $GVF; then
+	VC=$(sed -e 's/m4_define(\[GIT_VERSION], \[//' <$GVF)
+		VC=$(echo $VC | sed -e 's/\])//')
+else
+	VC=unset
+fi
+test "$VN" = "$VC" || {
+	echo >&2 "GIT_VERSION = $VN"
+	echo "m4_define([GIT_VERSION], [$VN])" >$GVF
+	exit 0
+}
diff --git a/ndctl.c b/ndctl.c
index 93cc25e1d564..8d806d3adb30 100644
--- a/ndctl.c
+++ b/ndctl.c
@@ -22,9 +22,7 @@  struct cmd_struct {
 
 static int cmd_version(int argc, const char **argv)
 {
-	static const char ndctl_version_string[] = "ndctl " VERSION;
-
-	printf("%s\n", ndctl_version_string);
+	printf("%s\n", VERSION);
 	return 0;
 }