From 0f46d4c8bf35c7762e96271e7061d42e8dae79e4 Mon Sep 17 00:00:00 2001 From: Konstantin Ryabitsev Date: Thu, 30 Sep 2021 10:35:10 -0400 Subject: shazam: implement custom merge message templates Allow people to set up their own preferred merge templates, using the netdev standard as default. Signed-off-by: Konstantin Ryabitsev --- b4/__init__.py | 17 ++++++++++++- b4/mbox.py | 55 +++++++++++++++++++++++++++++++++---------- b4/ty.py | 19 ++------------- shazam-merge-template.example | 19 +++++++++++++++ 4 files changed, 79 insertions(+), 31 deletions(-) create mode 100644 shazam-merge-template.example diff --git a/b4/__init__.py b/b4/__init__.py index 47272bb..b352d6a 100644 --- a/b4/__init__.py +++ b/b4/__init__.py @@ -1895,7 +1895,7 @@ def git_temp_worktree(gitdir=None, commitish=None): yield dfn finally: if dfn is not None: - git_run_command(gitdir, ['worktree', 'remove', dfn]) + git_run_command(gitdir, ['worktree', 'remove', '--force', dfn]) @contextmanager @@ -2461,3 +2461,18 @@ def get_mailinfo(bmsg: bytes, scissors: bool = False) -> Tuple[dict, bytes, byte with open(p_out, 'rb') as pfh: p = pfh.read() return i, m, p + + +def read_template(tptfile): + # bubbles up FileNotFound + tpt = '' + if tptfile.find('~') >= 0: + tptfile = os.path.expanduser(tptfile) + if tptfile.find('$') >= 0: + tptfile = os.path.expandvars(tptfile) + with open(tptfile, 'r', encoding='utf-8') as fh: + for line in fh: + if len(line) and line[0] == '#': + continue + tpt += line + return tpt diff --git a/b4/mbox.py b/b4/mbox.py index 00b683d..50dd2c3 100644 --- a/b4/mbox.py +++ b/b4/mbox.py @@ -27,9 +27,19 @@ import xml.etree.ElementTree import b4 from typing import Optional, Tuple +from string import Template logger = b4.logger +DEFAULT_MERGE_TEMPLATE = """Merge ${patch_or_series} "${seriestitle}" + +${authorname} <${authoremail}> says: +==================== +${coverletter} +==================== +Link: ${midurl} +""" + def make_am(msgs, cmdargs, msgid): config = b4.get_main_config() @@ -309,27 +319,46 @@ def make_am(msgs, cmdargs, msgid): new_contents = contents.replace(gwt, mmsg) if new_contents != contents: with open(fhf, 'w') as fhh: - fhh.write(contents) + fhh.write(new_contents) + + mmf = os.path.join(topdir, '.git', 'b4-cover') if lser.has_cover: + merge_template = DEFAULT_MERGE_TEMPLATE + if config.get('shazam-merge-template'): + # Try to load this template instead + try: + merge_template = b4.read_template(config['shazam-merge-template']) + except FileNotFoundError: + logger.critical('ERROR: shazam-merge-template says to use %s, but it does not exist', + config['shazam-merge-template']) + sys.exit(2) + # Write out a sample merge message using the cover letter - mmf = os.path.join(topdir, '.git', 'b4-cover') cmsg = lser.patches[0] parts = b4.LoreMessage.get_body_parts(cmsg.body) + tptvals = { + 'seriestitle': cmsg.subject, + 'authorname': cmsg.fromname, + 'authoremail': cmsg.fromemail, + 'coverletter': parts[1], + 'midurl': linkurl, + } + if len(am_msgs) > 1: + tptvals['patch_or_series'] = 'patch series' + else: + tptvals['patch_or_series'] = 'patch' + + body = Template(merge_template).safe_substitute(tptvals) with open(mmf, 'w') as mmh: - mmh.write('Merge %s\n\n' % mmsg) - if len(am_msgs) > 1: - mmh.write('Accept %d patches from %s <%s>\n\n' % (len(am_msgs), cmsg.fromname, cmsg.fromemail)) - else: - mmh.write('Accept patch from %s <%s>\n\n' % (cmsg.fromname, cmsg.fromemail)) - mmh.write('%s\n' % cmsg.subject) - mmh.write('=' * len(cmsg.subject) + '\n') - mmh.write(parts[1]) - mmh.write('=' * len(cmsg.subject) + '\n') - mmh.write('Link: %s\n' % linkurl) + mmh.write(body) + + elif os.path.exists(mmf): + # Make sure any old cover letters don't confuse anyone + os.unlink(mmf) logger.info('You can now merge or checkout FETCH_HEAD') if lser.has_cover: - logger.info(' e.g.: git merge -F .git/b4-cover --edit FETCH_HEAD') + logger.info(' e.g.: git merge -F .git/b4-cover --signoff --edit FETCH_HEAD') thanks_record_am(lser, cherrypick=cherrypick) return diff --git a/b4/ty.py b/b4/ty.py index c6d13cb..fbae20c 100644 --- a/b4/ty.py +++ b/b4/ty.py @@ -228,21 +228,6 @@ def auto_locate_series(gitdir, jsondata, branch, since='1.week'): return found -def read_template(tptfile): - # bubbles up FileNotFound - tpt = '' - if tptfile.find('~') >= 0: - tptfile = os.path.expanduser(tptfile) - if tptfile.find('$') >= 0: - tptfile = os.path.expandvars(tptfile) - with open(tptfile, 'r', encoding='utf-8') as fh: - for line in fh: - if len(line) and line[0] == '#': - continue - tpt += line - return tpt - - def set_branch_details(gitdir, branch, jsondata, config): binfo = get_branch_info(gitdir, branch) jsondata['branch'] = branch @@ -282,7 +267,7 @@ def generate_pr_thanks(gitdir, jsondata, branch): if config['thanks-pr-template']: # Try to load this template instead try: - thanks_template = read_template(config['thanks-pr-template']) + thanks_template = b4.read_template(config['thanks-pr-template']) except FileNotFoundError: logger.critical('ERROR: thanks-pr-template says to use %s, but it does not exist', config['thanks-pr-template']) @@ -311,7 +296,7 @@ def generate_am_thanks(gitdir, jsondata, branch, since): if config['thanks-am-template']: # Try to load this template instead try: - thanks_template = read_template(config['thanks-am-template']) + thanks_template = b4.read_template(config['thanks-am-template']) except FileNotFoundError: logger.critical('ERROR: thanks-am-template says to use %s, but it does not exist', config['thanks-am-template']) diff --git a/shazam-merge-template.example b/shazam-merge-template.example new file mode 100644 index 0000000..ab735b4 --- /dev/null +++ b/shazam-merge-template.example @@ -0,0 +1,19 @@ +# Lines starting with '#' will be removed before invoking git-merge +# This is the first line (title) of the merge +# ${seriestitle}: will be a cleaned up subject of the cover +# letter or the first patch in the series. +# ${patch_or_series}: will say "patch" if a single patch or +# "patch series" if more than one +Merge ${patch_or_series} "${seriestitle}" + +${authorname} <${authoremail}> says: +==================== +# This will be the entirety of the cover letter minus anything +# below the "-- \n" signature line. You will almost certainly +# want to edit it down to only include the relevant info. +${coverletter} +==================== +# This will contain a lore link to the patches in question +Link: ${midurl} +# git-merge will append any additional information here, depending +# on the flags you used to invoke it (e.g. --log, --signoff, etc) -- cgit v1.2.3