]> arthur.barton.de Git - bup.git/blob - cmd/version-cmd.py
Fix release archives and add tests
[bup.git] / cmd / version-cmd.py
1 #!/bin/sh
2 """": # -*-python-*-
3 bup_python="$(dirname "$0")/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
5 """
6 # end of bup preamble
7 import re, sys
8 from bup import options
9 from bup import version
10
11 version_rx = re.compile(r'^[0-9]+\.[0-9]+(\.[0-9]+)?(-[0-9]+-g[0-9abcdef]+)?$')
12
13 optspec = """
14 bup version [--date|--commit|--tag]
15 --
16 date    display the date this version of bup was created
17 commit  display the git commit id of this version of bup
18 tag     display the tag name of this version.  If no tag is available, display the commit id
19 """
20 o = options.Options(optspec)
21 (opt, flags, extra) = o.parse(sys.argv[1:])
22
23
24 total = (opt.date or 0) + (opt.commit or 0) + (opt.tag or 0)
25 if total > 1:
26     o.fatal('at most one option expected')
27
28
29 def version_date():
30     """Format bup's version date string for output."""
31     return version.DATE.split(' ')[0]
32
33
34 def version_commit():
35     """Get the commit hash of bup's current version."""
36     return version.COMMIT
37
38
39 def version_tag():
40     """Format bup's version tag (the official version number).
41
42     When generated from a commit other than one pointed to with a tag, the
43     returned string will be "unknown-" followed by the first seven positions of
44     the commit hash.
45     """
46     names = version.NAMES.strip()
47     assert(names[0] == '(')
48     assert(names[-1] == ')')
49     names = names[1:-1]
50     l = [n.strip() for n in names.split(',')]
51     for n in l:
52         if n.startswith('tag: ') and version_rx.match(n[5:]):
53             return n[5:]
54     return 'unknown-%s' % version.COMMIT[:7]
55
56
57 if opt.date:
58     print version_date()
59 elif opt.commit:
60     print version_commit()
61 else:
62     print version_tag()