From 3db3dd6b87a9e4531ca43c491c88c69cf9bbb0c3 Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Mon, 5 Feb 2024 11:26:00 +0100 Subject: [PATCH] Use as few digits of the year as possible --- tagger | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/tagger b/tagger index 3dc5991..d44dbc8 100755 --- a/tagger +++ b/tagger @@ -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