-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathautoversion
More file actions
executable file
·79 lines (67 loc) · 2.26 KB
/
autoversion
File metadata and controls
executable file
·79 lines (67 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python3
# Copyright (c) TurnKey GNU/Linux - http://www.turnkeylinux.org
#
# This file is part of AutoVersion
#
# AutoVersion is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
import os
import re
import sys
import argparse
from typing import NoReturn
import autoversion_lib as autoversion
from autoversion_lib import AutoverError
EXAMPLES = """Example usage:
autoversion # print latest version (uses 'HEAD' \
by default)
autoversion 9497a28 # print version at commit ID 9497a28
autoversion -r v1.0 # print commit ID of version v1.0
autoversion $(git-rev-list --all) # print all versions
"""
def fatal(msg: str | AutoverError) -> NoReturn:
print(f"error: {msg}", file=sys.stderr)
sys.exit(1)
def resolve_committish(git: autoversion.Git,
committish: str) -> str:
# skip expensive git-rev-parse if given a full commit id
if re.match("[0-9a-f]{40}$", committish):
return committish
commit = git.rev_parse(committish)
if commit is None:
fatal(f"invalid committish `{committish}'")
return commit
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description="Map git commits to auto-versions and vice versa",
epilog=EXAMPLES,
)
parser.add_argument(
"-r",
"--reverse",
action="store_true",
default=False,
help="map version to git commit",
)
parser.add_argument(
"commit",
nargs="?",
default="HEAD",
help="any revision supported by git (e.g. commit ids, tags, refs,"
" etc.) Default if not set: HEAD",
)
args = parser.parse_args()
try:
auto_ver = autoversion.Autoversion(os.getcwd(), precache=args.reverse)
except AutoverError as e:
fatal(e)
if args.reverse:
print(auto_ver.version2commit(args.commit))
else:
print(auto_ver.commit2version(
resolve_committish(auto_ver.git, args.commit)))
if __name__ == "__main__":
main()