]> arthur.barton.de Git - bup.git/commitdiff
cmd/version, etc: fix version number detection stuff.
authorAvery Pennarun <apenwarr@gmail.com>
Sat, 24 Apr 2010 21:10:02 +0000 (17:10 -0400)
committerAvery Pennarun <apenwarr@gmail.com>
Sat, 24 Apr 2010 21:10:02 +0000 (17:10 -0400)
Gabriel Filion pointed out that bup's version number (which we added to the
man pages automatically) was not detected when you used a bup tarball
generated from 'git archive' (or more likely, you let GitHub call 'git
archive' for you).  That makes sense, since our version detection was based
on having a .git directory around, which the tarball doesn't.

Instead, let's create a .gitattributes file and have it auto-substitute some
version information during 'git archive'.  Of course, if we actually *do*
have a .git directory, continue to use that.

While we're here, add a new 'bup version' command and alias "bup --version"
and "bup -V" to call it, since those options are pretty standard.

.gitignore
Documentation/Makefile
Makefile
cmd/version-cmd.py [new file with mode: 0755]
format-subst.pl [new file with mode: 0755]
lib/bup/.gitattributes [new file with mode: 0644]
lib/bup/_version.py.pre [new file with mode: 0644]
main.py

index 03e92231d24ab0fa829a49a30b60c10422184fb0..d9bfc17437c9e8f73bf34757f72293c852202eff 100644 (file)
@@ -1,5 +1,6 @@
 /bup
 /cmd/bup-*
+/lib/bup/_version.py
 randomgen
 memtest
 *.o
index d69aa88602118d79f81e62608e89ce4548489bff..528b7b7cc7660d9e9913f847e364eb6f0c736580 100644 (file)
@@ -5,8 +5,8 @@ PANDOC:=$(shell \
                echo "Warning: pandoc not installed; can't generate manpages." >&2; \
                echo '@echo Skipping: pandoc'; \
        fi)
-BUP_VERSION=$(shell git describe --match 'bup-*' | sed 's/^bup-//')
-BUP_DATE=$(shell git log --no-walk --pretty='%ci%n' | (read x y && echo $$x))
+BUP_VERSION:=$(shell ../bup version --tag)
+BUP_DATE:=$(shell ../bup version --date)
 
 default: all
 
index 217b85212af08a27040329605a71e15ce1c70ab2..64884963ffcf15af45e315500a773d5a237bcc3c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -19,8 +19,11 @@ endif
 
 default: all
 
-all: cmds bup lib/bup/_hashsplit$(SOEXT) \
-       Documentation/all
+all: bup Documentation/all
+
+bup: lib/bup/_version.py lib/bup/_hashsplit$(SOEXT) cmds
+
+Documentation/all: bup
 
 INSTALL=install
 MANDIR=$(DESTDIR)/usr/share/man
@@ -54,6 +57,12 @@ lib/bup/_hashsplit$(SOEXT): lib/bup/_hashsplit.c lib/bup/csetup.py
        @rm -f $@
        cd lib/bup && python csetup.py build
        cp lib/bup/build/*/_hashsplit$(SOEXT) lib/bup/
+
+.PHONY: lib/bup/_version.py
+lib/bup/_version.py:
+       rm -f $@ $@.new
+       ./format-subst.pl $@.pre >$@.new
+       mv $@.new $@
        
 runtests: all runtests-python runtests-cmdline
 
@@ -95,7 +104,7 @@ bup-%: cmd-%.sh
 
 clean: Documentation/clean
        rm -f *.o *.so */*/*.so *.dll *.exe .*~ *~ */*~ */*/*~ \
-               *.pyc */*.pyc */*/*.pyc\
-               bup bup-* cmd/bup-* randomgen memtest \
+               *.pyc */*.pyc */*/*.pyc \
+               bup bup-* cmd/bup-* lib/bup/_version.py randomgen memtest \
                out[12] out2[tc] tags[12] tags2[tc]
        rm -rf *.tmp build lib/bup/build
diff --git a/cmd/version-cmd.py b/cmd/version-cmd.py
new file mode 100755 (executable)
index 0000000..1964ca0
--- /dev/null
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+import sys, os, glob
+from bup import options, _version
+
+optspec = """
+bup version [--date|--commit|--tag]
+--
+date    display the date this version of bup was created
+commit  display the git commit id of this version of bup
+tag     display the tag name of this version.  If no tag is available, display the commit id
+"""
+o = options.Options('bup version', optspec)
+(opt, flags, extra) = o.parse(sys.argv[1:])
+
+def autoname(names):
+    names = names.strip()
+    assert(names[0] == '(')
+    assert(names[-1] == ')')
+    names = names[1:-1]
+    l = [n.strip() for n in names.split(',')]
+    for n in l:
+        if n.startswith('tag: bup-'):
+            return n[9:]
+
+
+total = (opt.date or 0) + (opt.commit or 0) + (opt.tag or 0)
+if total > 1:
+    o.fatal('at most one option expected')
+
+if opt.date:
+    print _version.DATE.split(' ')[0]
+elif opt.commit:
+    print _version.COMMIT
+else:
+    print autoname(_version.NAMES) or 'unknown-%s' % _version.COMMIT[:7]
diff --git a/format-subst.pl b/format-subst.pl
new file mode 100755 (executable)
index 0000000..e04e123
--- /dev/null
@@ -0,0 +1,25 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+
+sub fix($) {
+    my $s = shift;
+    chomp $s;
+    return $s;
+}
+
+while (<>) {
+    s{
+       \$Format:\%d\$
+    }{
+       my $tag = fix(`git describe --match="bup-*"`);
+       "(tag: $tag)"
+    }ex;
+    
+    s{ 
+       \$Format:([^\$].*)\$
+    }{
+       fix(`git log -1 --format="$1"`)
+    }ex;
+    print;
+}
diff --git a/lib/bup/.gitattributes b/lib/bup/.gitattributes
new file mode 100644 (file)
index 0000000..7410999
--- /dev/null
@@ -0,0 +1 @@
+_version.py.pre  export-subst
diff --git a/lib/bup/_version.py.pre b/lib/bup/_version.py.pre
new file mode 100644 (file)
index 0000000..32b02f4
--- /dev/null
@@ -0,0 +1,4 @@
+
+COMMIT='$Format:%H$'
+NAMES='$Format:%d$'
+DATE='$Format:%ci$'
diff --git a/main.py b/main.py
index 5904018c30854b80f84d87e00e688724d6038d4e..840e3e53e2614f0eed29540d5cc2256f45cd4c45 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -1,5 +1,4 @@
 #!/usr/bin/env python
-
 import sys, os, subprocess, signal, getopt
 
 argv = sys.argv
@@ -65,7 +64,8 @@ if len(argv) < 2:
 
 # Handle global options.
 try:
-    global_args, subcmd = getopt.getopt(argv[1:], '?d:', ['help', 'bup-dir='])
+    global_args, subcmd = getopt.getopt(argv[1:], '?Vd:',
+                                        ['help', 'version', 'bup-dir='])
 except getopt.GetoptError, ex:
     log('error: ' + ex.msg + '\n')
     usage()
@@ -76,6 +76,8 @@ dest_dir = None
 for opt in global_args:
     if opt[0] == '-?' or opt[0] == '--help':
         help_requested = True
+    if opt[0] == '-V' or opt[0] == '--version':
+        subcmd = ['version']
     elif opt[0] == '-d' or opt[0] == '--bup-dir':
         dest_dir = opt[1]
     else: