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