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