From: Frantisek Hrbata Date: Tue, 15 Dec 2020 11:43:53 +0000 (+0100) Subject: git-mr-comment-show: initial implementation X-Git-Tag: v0.2~48 X-Git-Url: http://www.hrbata.com/gitweb/?a=commitdiff_plain;h=59a562bd0d99271a1dd9304d3ec27218195ed785;p=gimrr.git git-mr-comment-show: initial implementation --- diff --git a/git-mr-comment-show b/git-mr-comment-show new file mode 100755 index 0000000..0463245 --- /dev/null +++ b/git-mr-comment-show @@ -0,0 +1,94 @@ +#!/usr/bin/env python3 + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 + +""" +Copyright © 2020 Frantisek Hrbata +This program is free software. It comes without any warranty, to +the extent permitted by applicable law. You can redistribute it +and/or modify it under the terms of the Do What The Fuck You Want +To Public License, Version 2, as published by Sam Hocevar. See +http://www.wtfpl.net/ for more details. +""" + +import argparse +import string +import fcntl +import sys +import subprocess +import mrlib +import os + +parser = argparse.ArgumentParser(description="Show comments.") +parser.add_argument("comment", nargs="*", metavar="COMMENT", + help="Show comments specified by COMMENT revisions.") +parser.add_argument("-A", "--all", action="store_true", + help="Show all comments. Comments entered as positional arguments or on stdin are ignored.") +parser.add_argument("-", "--stdin", action="store_true", + help="Show comments for revisions read from stdin. Comments entered as positional arguments are ignored.") +parser.add_argument("-w", "--write-revs", metavar="FILE", + help="Append revs for read comments to FILE (default: $HOME/.gimrr/comment.revs).") + +args = parser.parse_args() +refs = mrlib.load_refs() + +if args.all: + mboxes = mrlib.filter_revs(refs, types=["mbox"]) + stdin = "\n".join(mboxes) + if not stdin: + sys.exit() + rv, out, err = mrlib.run(["git", "rev-list", "--stdin"], stdin=stdin) + comments = out.strip().split("\n") +else: + if args.stdin: + comments = sys.stdin.readlines() + else: + comments = args.comment + + comments = mrlib.verify_comments(comments, refs) + +if not comments: + sys.exit() + +c2m = mrlib.comments2mrs(refs) + +fmt = "{}%n\ +From: %an <%ae>%n\ +Reply-To: %an <%ae>%n\ +Subject: %s%n\ +Date: %aD%n\ +DI-Egassem: %H%n\ +MIME-Version: 1.0%n\ +Content-Type: text/plain; charset=UTF-8%n\ +Content-Transfer-Encoding: 8bit%n\ +%b".format(mrlib.from_line) +rv, out, err = mrlib.run(["git", "show", "--format={}".format(fmt), "--stdin", "--no-walk"], + stdin="\n".join(comments)) + +for line in out.split("\n"): + if not line.startswith("DI-Egassem:"): + print(line) + continue + c = line.split()[1] + print("Message-ID: <{}.{}@localhost>".format(c2m[c], c)) + +if args.write_revs: + revs_path = args.read_revs +else: + revs_path = os.environ.get('HOME') + "/" + ".gimrr/comment.revs" + +fd = open(revs_path, "a+") +fd.seek(0) +fcntl.lockf(fd.fileno(), fcntl.LOCK_SH | fcntl.LOCK_NB) +oldrevs = {r for r in fd.read().split("\n") if r} +currevs = set(comments) +newrevs = currevs - oldrevs + +try: + pos = fd.tell() + for r in newrevs: + fd.write("{}\n".format(r)) + fd.flush() +except: + fd.truncate(pos) + raise