threads / patch / 15820

patchguilt: add git commit level versioning

Subject: [PATCH] guilt: add git commit level versioning

## tl;dr

3 messages between Oct 8, 2008 and Oct 9, 2008. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Paul Gortmaker· Oct 8, 2008, 02:08 UTC · lore

Shamelessly steal the dynamic versioning goodies from git itself. Now when you do "guilt --version" you can expect to see things like:

	Guilt version 0.31.2.14.gece1.dirty

assuming you've 14 commits since 0.31.2 and also some changes that you've not committed yet.

Signed-off-by: Paul Gortmaker <paul.gortmaker@windriver.com>
---
 .gitignore             |    2 ++
 Documentation/Makefile |    4 ++--
 GUILT-VERSION-GEN      |   42 ++++++++++++++++++++++++++++++++++++++++++
 Makefile               |   21 ++++++++++++++++-----
 guilt                  |    4 +++-
 5 files changed, 65 insertions(+), 8 deletions(-)
 create mode 100755 GUILT-VERSION-GEN
Show changes to 5 files +65 −8

.gitignore, Documentation/Makefile, GUILT-VERSION-GEN, Makefile, guilt

diff --git a/.gitignore b/.gitignore
index c81112b..61161ef 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,3 +7,5 @@
 #
 *~
 *.swp
+guilt.bin
+GUILT-VERSION-FILE
diff --git a/Documentation/Makefile b/Documentation/Makefile
index 9fc1165..4b7d037 100644
--- a/Documentation/Makefile
+++ b/Documentation/Makefile
@@ -7,7 +7,7 @@ DOC_MAN1=$(patsubst %.txt,%.1,$(MAN1_TXT))
 DOC_MAN7=$(patsubst %.txt,%.7,$(MAN7_TXT))
 
 USAGE=$(patsubst %.txt,usage-%.txt,$(MAN1_TXT))
-VERSION=$(shell git describe 2> /dev/null || sed -n -e '/^GUILT_VERSION=/ { s/^GUILT_VERSION="/v/; s/"//; p; q; }' ../guilt)
+include ../GUILT-VERSION-FILE
 
 prefix?=$(PREFIX)
 bindir?=$(prefix)/bin
@@ -51,7 +51,7 @@ doc.dep : $(wildcard *.txt) build-docdep.perl
 -include doc.dep
 
 version.txt:
-	echo "(Generated for Guilt $(VERSION))" > version.txt
+	echo "(Generated for Guilt $(GUILT_VERSION))" > version.txt
 
 cmds.txt: cmd-list.sh $(MAN1_TXT)
 	sh ./cmd-list.sh
diff --git a/GUILT-VERSION-GEN b/GUILT-VERSION-GEN
new file mode 100755
index 0000000..66b40e2
--- /dev/null
+++ b/GUILT-VERSION-GEN
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+GVF=GUILT-VERSION-FILE
+DEF_VER=v0.31.2
+
+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 -o -f .git &&
+	VN=$(git describe --abbrev=4 HEAD 2>/dev/null) &&
+	case "$VN" in
+	*$LF*) (exit 1) ;;
+	v[0-9]*)
+		git update-index -q --refresh
+		test -z "$(git diff-index --name-only HEAD --)" ||
+		VN="$VN-dirty" ;;
+	esac
+then
+	VN=$(echo "$VN" | sed -e 's/-/./g');
+else
+	VN="$DEF_VER"
+fi
+
+VN=$(expr "$VN" : v*'\(.*\)')
+
+if test -r $GVF
+then
+	VC=$(sed -e 's/^GUILT_VERSION = //' <$GVF)
+else
+	VC=unset
+fi
+test "$VN" = "$VC" || {
+	echo >&2 "GUILT_VERSION=$VN"
+	echo "GUILT_VERSION=$VN" >$GVF
+}
+
+
diff --git a/Makefile b/Makefile
index af53fb5..f3cf0b3 100644
--- a/Makefile
+++ b/Makefile
@@ -1,25 +1,35 @@
 PREFIX=/usr/local
 DESTDIR=
 
-SCRIPTS = guilt \
-	  $(filter-out $(wildcard *~),$(wildcard guilt-*))
+SCRIPTS = $(filter-out $(wildcard *~),$(wildcard guilt-*))
 
-.PHONY: all 
-all: doc
+all::
+
+.PHONY: .FORCE-GUILT-VERSION-FILE
+GUILT-VERSION-FILE: .FORCE-GUILT-VERSION-FILE
+	@$(SHELL_PATH) ./GUILT-VERSION-GEN
+-include GUILT-VERSION-FILE
+
+.PHONY: all
+all:: doc guilt.bin
 	@echo "Nothing to build, it is all bash :)"
 	@echo "Try make install"
 
+guilt.bin: GUILT-VERSION-FILE
+	@sed 's/^GUILT_VERSION=$$/GUILT_VERSION="$(GUILT_VERSION)"/' < guilt > guilt.bin
+
 .PHONY: install
 install:
 	install -d $(DESTDIR)$(PREFIX)/bin/
 	install -m 755 $(SCRIPTS) $(DESTDIR)$(PREFIX)/bin/
+	install -m 755 guilt.bin $(DESTDIR)$(PREFIX)/bin/guilt
 
 .PHONY: uninstall
 uninstall:
 	./uninstall $(DESTDIR)$(PREFIX)/bin/ $(SCRIPTS)
 
 .PHONY: doc
-doc:
+doc: GUILT-VERSION-FILE
 	$(MAKE) -C Documentation all
 
 .PHONY: install-doc
@@ -33,3 +43,4 @@ test:
 .PHONY: clean
 clean: 
 	$(MAKE) -C Documentation clean 
+	$(RM) GUILT-VERSION-FILE guilt.bin
diff --git a/guilt b/guilt
index fabee17..7cfa23b 100755
--- a/guilt
+++ b/guilt
@@ -3,7 +3,9 @@
 # Copyright (c) Josef "Jeff" Sipek, 2006-2008
 #
 
-GUILT_VERSION="0.31.2"
+# Version replaced at build time. Could have in separate file, but why
+# do a "source version_file" on every guilt operation?
+GUILT_VERSION=
 GUILT_NAME="Ciuleandra"
 
 # If the first argument is one of the below, display the man page instead of
-- 
1.6.0
Josef 'Jeff' Sipek· Oct 9, 2008, 15:54 UTC · re: Paul Gortmaker · lore

Re: [PATCH] guilt: add git commit level versioning

On Tue, Oct 07, 2008 at 10:08:42PM -0400, Paul Gortmaker wrote:
Show 8 quoted lines
> Shamelessly steal the dynamic versioning goodies from git
> itself.  Now when you do "guilt --version" you can expect to
> see things like:
> 
> 	Guilt version 0.31.2.14.gece1.dirty
> 
> assuming you've 14 commits since 0.31.2 and also some changes
> that you've not committed yet.

Good idea. I've been pondering how to do this a while back, but never got around to actually do it.

One comment: Why not keep the default version in guilt? You're running sed
on it anyway.  This makes releasing identical to what it was before the
patch:
	<edit guilt to reflect new version, commit>
	git tag -u abcdef v0.xy
	git archive --tar v0.xy | gzip -9 > guilt-0.xy.tar.gz

Additionally, things won't "break" for folks running right out of the git repo - I just add the git repo dir to my $PATH.

Josef 'Jeff' Sipek.
-- 
Keyboard not found!
Press F1 to enter Setup
Paul Gortmaker· Oct 9, 2008, 16:13 UTC · re: Josef 'Jeff' Sipek · lore

Re: [PATCH] guilt: add git commit level versioning

Josef 'Jeff' Sipek wrote:
Show 25 quoted lines
> On Tue, Oct 07, 2008 at 10:08:42PM -0400, Paul Gortmaker wrote:
>   
>> Shamelessly steal the dynamic versioning goodies from git
>> itself.  Now when you do "guilt --version" you can expect to
>> see things like:
>>
>> 	Guilt version 0.31.2.14.gece1.dirty
>>
>> assuming you've 14 commits since 0.31.2 and also some changes
>> that you've not committed yet.
>>     
>
> Good idea. I've been pondering how to do this a while back, but never got
> around to actually do it.
>
> One comment: Why not keep the default version in guilt? You're running sed
> on it anyway.  This makes releasing identical to what it was before the
> patch:
> 	<edit guilt to reflect new version, commit>
> 	git tag -u abcdef v0.xy
> 	git archive --tar v0.xy | gzip -9 > guilt-0.xy.tar.gz
>
> Additionally, things won't "break" for folks running right out of the git
> repo - I just add the git repo dir to my $PATH.
>   

Sounds reasonable to me. I'm about to disappear for a 4 day weekend (Cdn Thanksgiving) so I won't be able to do that until next Tuesday at the earliest though.

P.
> Josef 'Jeff' Sipek.
>
>   

← back to recent threads