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