From d77b3df240768c6d2ede4b99dbe09fb661327ff8 Mon Sep 17 00:00:00 2001 From: Konstantin Ryabitsev Date: Fri, 17 Apr 2020 15:03:40 -0400 Subject: Expand templates with ${branch} and ${treename} You can use ${branch} and ${treename} (if you set b4.thanks-treename). See .example files on some hints on how to use them. Suggested-by: Mark Brown Signed-off-by: Konstantin Ryabitsev --- b4/ty.py | 79 ++++++++++++++++++++++++++++++++++++++++++++-- thanks-am-template.example | 14 +++++++- thanks-pr-template.example | 14 +++++++- 3 files changed, 103 insertions(+), 4 deletions(-) diff --git a/b4/ty.py b/b4/ty.py index 629fa30..008da09 100644 --- a/b4/ty.py +++ b/b4/ty.py @@ -47,6 +47,8 @@ ${signature} # Used to track commits created by current user MY_COMMITS = None +# Used to track additional branch info +BRANCH_INFO = None def git_get_merge_id(gitdir, commit_id): @@ -212,8 +214,41 @@ def read_template(tptfile): return tpt -def generate_pr_thanks(gitdir, jsondata): +def set_branch_details(gitdir, branch, jsondata, config): + binfo = get_branch_info(gitdir, branch) + jsondata['branch'] = branch + for key, val in binfo.items(): + if key == 'b4-treename': + config['thanks-treename'] = val + elif key == 'b4-commit-url-mask': + config['thanks-commit-url-mask'] = val + elif key == 'b4-pr-template': + config['thanks-pr-template'] = val + elif key == 'b4-am-template': + config['thanks-am-template'] = val + elif key == 'branch': + jsondata['branch'] = val + + if 'thanks-treename' in config: + jsondata['treename'] = config['thanks-treename'] + elif 'url' in binfo: + # noinspection PyBroadException + try: + # Try to grab the last two chunks of the path + purl = Path(binfo['url']) + jsondata['treename'] = os.path.join(purl.parts[-2], purl.parts[-1]) + except: + # Something went wrong... just use the whole URL + jsondata['treename'] = binfo['url'] + else: + jsondata['treename'] = 'undefined' + + return jsondata, config + + +def generate_pr_thanks(gitdir, jsondata, branch): config = b4.get_main_config() + jsondata, config = set_branch_details(gitdir, branch, jsondata, config) thanks_template = DEFAULT_PR_TEMPLATE if config['thanks-pr-template']: # Try to load this template instead @@ -242,6 +277,7 @@ def generate_pr_thanks(gitdir, jsondata): def generate_am_thanks(gitdir, jsondata, branch, since): config = b4.get_main_config() + jsondata, config = set_branch_details(gitdir, branch, jsondata, config) thanks_template = DEFAULT_AM_TEMPLATE if config['thanks-am-template']: # Try to load this template instead @@ -350,7 +386,7 @@ def send_messages(listing, gitdir, outdir, branch, since='1.week'): jsondata['signature'] = signature if 'pr_commit_id' in jsondata: # This is a pull request - msg = generate_pr_thanks(gitdir, jsondata) + msg = generate_pr_thanks(gitdir, jsondata, branch) else: # This is a patch series msg = generate_am_thanks(gitdir, jsondata, branch, since) @@ -489,6 +525,7 @@ def check_stale_thanks(outdir): def get_wanted_branch(cmdargs): + global BRANCH_INFO gitdir = cmdargs.gitdir if not cmdargs.branch: # Find out our current branch @@ -514,6 +551,44 @@ def get_wanted_branch(cmdargs): return wantbranch +def get_branch_info(gitdir, branch): + global BRANCH_INFO + if BRANCH_INFO is not None: + return BRANCH_INFO + + BRANCH_INFO = dict() + + if branch.find('/') < 0: + # Not a remote branch + return BRANCH_INFO + + # Get a list of all remotes + gitargs = ['remote', 'show'] + lines = b4.git_get_command_lines(gitdir, gitargs) + if not len(lines): + # No remotes? Hmm... + return BRANCH_INFO + + remote = None + for entry in lines: + if branch.find('%s/' % entry) == 0: + remote = entry + break + + if remote is None: + # Not found any matching remotes + return BRANCH_INFO + + BRANCH_INFO['remote'] = remote + BRANCH_INFO['branch'] = branch.replace('%s/' % remote, '') + + # Grab template overrides + remotecfg = b4.get_config_from_git('remote\\.%s\\..*' % remote) + BRANCH_INFO.update(remotecfg) + + return BRANCH_INFO + + def main(cmdargs): usercfg = b4.get_user_config() if 'email' not in usercfg: diff --git a/thanks-am-template.example b/thanks-am-template.example index 72a8759..40239f3 100644 --- a/thanks-am-template.example +++ b/thanks-am-template.example @@ -9,7 +9,19 @@ On ${sentdate}, ${fromname} wrote: # quote will be limited to 5-6 lines, respecting paragraphs ${quote} -Applied, thanks! +# You can also use ${branch} and ${treename} if you set +# b4.thanks-treename in your config, e.g.: +#Applied to ${treename} (${branch}), thanks! +# +# If you track multiple remotes in the same repo, then you can add +# the following values to [remote], to be loaded when you run +# b4 ty -b foo/branchname: +# [remote "foo"] +# url = https://... +# fetch = ... +# b4-treename = uname/sound.git +# b4-commit-url-mask = https://git.kernel.org/uname/sound/c/%.8s +Applied to ${branch}, thanks! # for patch series, the summary is a list of each patch with a link # to the commit id in your tree, so you probably want to set diff --git a/thanks-pr-template.example b/thanks-pr-template.example index 4994b29..bb146f5 100644 --- a/thanks-pr-template.example +++ b/thanks-pr-template.example @@ -9,7 +9,19 @@ On ${sentdate}, ${fromname} wrote: # quote will be limited to 5-6 lines, respecting paragraphs ${quote} -Merged, thanks! +# You can also use ${branch} and ${treename} if you set +# b4.thanks-treename in your config, e.g.: +#Merged into ${treename} (${branch}), thanks! +# +# If you track multiple remotes in the same repo, then you can add +# the following values to [remote], to be loaded when you run +# b4 ty -b foo/branchname: +# [remote "foo"] +# url = https://... +# fetch = ... +# b4-treename = uname/sound.git +# b4-commit-url-mask = https://git.kernel.org/uname/sound/c/%.8s +Merged into ${branch}, thanks! # for pull requests, the summary is a one-liner with the merge commit, # so you probably want to set b4.thanks-commit-url-mask in gitconfig -- cgit v1.2.3