Use as few digits of the year as possible

This commit is contained in:
Peter J. Holzer 2024-02-05 11:26:00 +01:00
parent 5f082698ab
commit 3db3dd6b87
1 changed files with 22 additions and 5 deletions

27
tagger
View File

@ -7,17 +7,28 @@ import subprocess
ap = argparse.ArgumentParser()
ap.add_argument("--fullyear", action="store_true")
ap.add_argument("--prefix", default="r")
ap.add_argument("--noop", action="store_true")
args = ap.parse_args()
subprocess.run(["git", "pull"])
result = subprocess.run(["git", "log", "--pretty=format:%H %D", "--date-order"], stdout=subprocess.PIPE, universal_newlines=True)
found_tags = set()
need_tag = False
max_major = 0
for ln in result.stdout.split("\n"):
m = re.match(r"[0-9a-f]+ .*\btag: ([-a-z0-9._]+)", ln)
if m:
found_tags.add(m.group(1))
tag = m.group(1)
if tag.startswith(args.prefix):
found_tags.add(tag)
try:
major = int(tag[len(args.prefix):].split(".")[0])
max_major = max(max_major, major)
except ValueError:
# Silently ignore tags which don't start with a number
pass
if not found_tags:
need_tag = True
@ -26,16 +37,22 @@ print(need_tag, found_tags)
if need_tag:
today = datetime.date.today()
if args.fullyear:
tag = today.strftime("r%Y.%m.%d")
tag = args.prefix + today.strftime("%Y.%m.%d")
else:
tag = today.strftime("r%y.%m.%d")
year = today.year
for m in (10, 100, 1000, 10000):
if year % m >= max_major:
major = str(year % m)
break
tag = args.prefix + major + today.strftime(".%m.%d")
if tag in found_tags:
inc = 1
while f"{tag}.{inc}" in found_tags:
inc += 1
tag = f"{tag}.{inc}"
print(tag)
subprocess.run(["git", "tag", "-a", tag, "-m", "auto-tagged"])
subprocess.run(["git", "push", "--tags"])
if not args.noop:
subprocess.run(["git", "tag", "-a", tag, "-m", "auto-tagged"])
subprocess.run(["git", "push", "--tags"])
# vim: expandtab sw=4