Skip to content

blurb: get BPO# from branch, add commit template - update the PR #308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions blurb/blurb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -913,24 +913,12 @@ 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 if the branch name incidates the BPO number
branch_bpo, branch_suffix = get_bpo_git_branch()

init_tmp_with_template()
with open(tmp_path, "wt", encoding="utf-8") as f:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be

Suggested change
with open(tmp_path, "wt", encoding="utf-8") as f:
pathlib.Path(tmp_path).write_text(template.format(bpo=branch_bpo))

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
Expand Down Expand Up @@ -976,6 +964,17 @@ 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. 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]
with open(msgfile, 'wt', encoding="utf-8") as f:
f.write("bpo-{}: {}\n".format(metadata['bpo'], branch_suffix))
f.write("\n")
f.write(text)

print("Ready for commit.")


Expand Down Expand Up @@ -1156,6 +1155,28 @@ 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 None:
return '', branch
return mo.group(1), mo.group(2)


# @subcommand
# def noop():
# "Do-nothing command. Used for blurb smoke-testing."
Expand Down