From 35abf83604f3b9f10dd07ac4e8edbaae409a541a Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Wed, 20 Sep 2017 21:31:55 +0200 Subject: [PATCH 1/3] blurb: get BPO# from branch, add commit template blurb now gets the branch name from git and auto-fills the bpo number if the branch name looks like "bpo-1234-whatever". "bpo1234" or "bpo_1234_whatever" are also supported. Further more blurb now writes .git/blurb commit template. The first line of the template is "bpo-{bpo}: suffix of branch name". The body of the template is the blurb text. $ git config commit.template .git/blurb $ git checkout -b bpo-1234-confuse-a-cat $ blurb git ci --- bpo-1234: confuse a cat blurb text --- Signed-off-by: Christian Heimes --- blurb/blurb.py | 61 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/blurb/blurb.py b/blurb/blurb.py index cd975ba..78076e1 100755 --- a/blurb/blurb.py +++ b/blurb/blurb.py @@ -76,7 +76,7 @@ # # Please enter the relevant bugs.python.org issue number here: # -.. bpo: +.. bpo: {bpo} # # Uncomment one of these "section:" lines to specify which section @@ -913,24 +913,14 @@ def add(): os.close(handle) atexit.register(lambda : os.unlink(tmp_path)) - def init_tmp_with_template(): - with open(tmp_path, "wt", encoding="utf-8") as file: - # hack: - # my editor likes to strip trailing whitespace from lines. - # normally this is a good idea. but in the case of the template - # it's unhelpful. - # so, manually ensure there's a space at the end of the bpo line. - text = template - - bpo_line = ".. bpo:" - without_space = "\n" + bpo_line + "\n" - with_space = "\n" + bpo_line + " \n" - if without_space not in text: - sys.exit("Can't find BPO line to ensure there's a space on the end!") - text = text.replace(without_space, with_space) - file.write(text) + # see of the branch name indicated BPO number + # makes the 'editor strips trailing whitespace from template lines' + # hack obsolete, too. + branch_bpo, branch_suffix = get_bpo_git_branch() - init_tmp_with_template() + with open(tmp_path, "wt", encoding="utf-8") as f: + text = template.format(bpo=branch_bpo) + f.write(text) # We need to be clever about EDITOR. # On the one hand, it might be a legitimate path to an @@ -976,6 +966,18 @@ def init_tmp_with_template(): path = blurb.save_next() git_add_files.append(path) flush_git_add_files() + + # For convenience, write a preformatted commit template. Enable template + # with ``git config commit.template .git/blurb``. + msgfile = os.path.join(root, '.git', 'blurb') + if os.path.isdir(os.path.dirname(msgfile)): + metadata, text = blurb[0] + title = branch_suffix.replace('-', ' ').replace('_', ' ') + with open(msgfile, 'wt', encoding="utf-8") as f: + f.write(f"bpo-{metadata['bpo']}: {title}\n") + f.write("\n") + f.write(text) + print("Ready for commit.") @@ -1156,6 +1158,29 @@ def flush_git_rm_files(): git_rm_files.clear() +def get_bpo_git_branch(): + """Try to detect bpo number from branch name + + Supports: + bpo1234 + bpo_1234 + bpo-1234 + + returns: branch number, branch suffix + """ + try: + p = subprocess.run(["git", "rev-parse", "--abbrev-ref", "HEAD"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + p.check_returncode() + except subprocess.CalledProcessError: + return '', '' + branch = p.stdout.decode('utf-8').strip() + mo = re.match("^bpo[-_]?(\d+)[-_]?(.*)", branch) + if mo is not None: + return mo.group(1), mo.group(2) + else: + return '', branch + + # @subcommand # def noop(): # "Do-nothing command. Used for blurb smoke-testing." From 7e049a6a239145cb1e7f28a06707d084ec6f1c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Fri, 15 Feb 2019 13:25:35 +0100 Subject: [PATCH 2/3] Update with the remarks of Larry Hastings --- blurb/blurb.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/blurb/blurb.py b/blurb/blurb.py index 78076e1..5019001 100755 --- a/blurb/blurb.py +++ b/blurb/blurb.py @@ -913,9 +913,7 @@ def add(): os.close(handle) atexit.register(lambda : os.unlink(tmp_path)) - # see of the branch name indicated BPO number - # makes the 'editor strips trailing whitespace from template lines' - # hack obsolete, too. + # See if the branch name incidates the BPO number branch_bpo, branch_suffix = get_bpo_git_branch() with open(tmp_path, "wt", encoding="utf-8") as f: @@ -967,14 +965,13 @@ def add(): git_add_files.append(path) flush_git_add_files() - # For convenience, write a preformatted commit template. Enable template - # with ``git config commit.template .git/blurb``. + # For convenience, write a preformatted commit template. You can tell git to + # use this template with "git config.template .git/blurb". msgfile = os.path.join(root, '.git', 'blurb') if os.path.isdir(os.path.dirname(msgfile)): metadata, text = blurb[0] - title = branch_suffix.replace('-', ' ').replace('_', ' ') with open(msgfile, 'wt', encoding="utf-8") as f: - f.write(f"bpo-{metadata['bpo']}: {title}\n") + f.write(f"bpo-{metadata['bpo']}: {branch_suffix}\n") f.write("\n") f.write(text) @@ -1175,10 +1172,9 @@ def get_bpo_git_branch(): return '', '' branch = p.stdout.decode('utf-8').strip() mo = re.match("^bpo[-_]?(\d+)[-_]?(.*)", branch) - if mo is not None: - return mo.group(1), mo.group(2) - else: + if mo is None: return '', branch + return mo.group(1), mo.group(2) # @subcommand From 9024ae70c261ff4180b35d46f7002ce53968609d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Fri, 15 Feb 2019 13:33:04 +0100 Subject: [PATCH 3/3] Compatible with 3.5 --- blurb/blurb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blurb/blurb.py b/blurb/blurb.py index 5019001..11393bf 100755 --- a/blurb/blurb.py +++ b/blurb/blurb.py @@ -971,7 +971,7 @@ def add(): if os.path.isdir(os.path.dirname(msgfile)): metadata, text = blurb[0] with open(msgfile, 'wt', encoding="utf-8") as f: - f.write(f"bpo-{metadata['bpo']}: {branch_suffix}\n") + f.write("bpo-{}: {}\n".format(metadata['bpo'], branch_suffix)) f.write("\n") f.write(text)