Skip to content

Commit

Permalink
cmd/version, etc: fix version number detection stuff.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
apenwarr committed Apr 24, 2010
1 parent 0e3565a commit 7949755
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,5 +1,6 @@
/bup
/cmd/bup-*
/lib/bup/_version.py
randomgen
memtest
*.o
Expand Down
4 changes: 2 additions & 2 deletions Documentation/Makefile
Expand Up @@ -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

Expand Down
17 changes: 13 additions & 4 deletions Makefile
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
35 changes: 35 additions & 0 deletions cmd/version-cmd.py
@@ -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]
25 changes: 25 additions & 0 deletions format-subst.pl
@@ -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;
}
1 change: 1 addition & 0 deletions lib/bup/.gitattributes
@@ -0,0 +1 @@
_version.py.pre export-subst
4 changes: 4 additions & 0 deletions lib/bup/_version.py.pre
@@ -0,0 +1,4 @@

COMMIT='$Format:%H$'
NAMES='$Format:%d$'
DATE='$Format:%ci$'
6 changes: 4 additions & 2 deletions main.py
@@ -1,5 +1,4 @@
#!/usr/bin/env python

import sys, os, subprocess, signal, getopt

argv = sys.argv
Expand Down Expand Up @@ -65,7 +64,8 @@ def usage():

# 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()
Expand All @@ -76,6 +76,8 @@ def usage():
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:
Expand Down

0 comments on commit 7949755

Please sign in to comment.