From 8efbb57822417204689e51b42a24d251705c18c7 Mon Sep 17 00:00:00 2001 From: chrisjbillington Date: Fri, 7 Feb 2020 18:24:04 -0500 Subject: [PATCH] Add additional options to branch_name_in_commit plugin - Allow skipping writing the branch name if the branch is 'master'. - Allow writing the branch name on the same line as the first line of the commit message separated by a colon, instead of it having its own line. --- plugins/branch_name_in_commit/README.md | 12 ++++++++++- plugins/branch_name_in_commit/__init__.py | 25 +++++++++++++++-------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/plugins/branch_name_in_commit/README.md b/plugins/branch_name_in_commit/README.md index b11982b..17e76e4 100644 --- a/plugins/branch_name_in_commit/README.md +++ b/plugins/branch_name_in_commit/README.md @@ -6,5 +6,15 @@ during the migration to Git. You can use this plugin to either prepend or append the branch name from the mercurial commit into the commit message in Git. +Valid arguments are: + +- `start`: write the branch name at the start of the commit +- `end`: write the branch name at the end of the commit +- `sameline`: if `start` specified, put a colon and a space + after the branch name, such that the commit message reads + `branch_name: first line of commit message`. Otherwise, the + branch name is on the first line of the commit message by itself. +- `skipmaster`: Don't write the branch name if the branch is `master`. + To use the plugin, add -`--plugin branch_name_in_commit=(start|end)`. +`--plugin branch_name_in_commit=`. diff --git a/plugins/branch_name_in_commit/__init__.py b/plugins/branch_name_in_commit/__init__.py index 20abe5b..910a446 100644 --- a/plugins/branch_name_in_commit/__init__.py +++ b/plugins/branch_name_in_commit/__init__.py @@ -3,12 +3,21 @@ def build_filter(args): class Filter: def __init__(self, args): - if not args in ['start','end']: - raise Exception('Cannot have branch name anywhere but start and end') - self.pos = args + args = {arg: True for arg in args.split(',')} + self.start = args.pop('start', False) + self.end = args.pop('end', False) + self.sameline = args.pop('sameline', False) + self.skip_master = args.pop('skipmaster', False) - def commit_message_filter(self,commit_data): - if self.pos == 'start': - commit_data['desc'] = commit_data['branch'] + '\n' + commit_data['desc'] - if self.pos == 'end': - commit_data['desc'] = commit_data['desc'] + '\n' + commit_data['branch'] + if self.sameline and not self.start: + raise ValueError("sameline option only allowed if 'start' given") + if args: + raise ValueError("Unknown args: " + ','.join(args)) + + def commit_message_filter(self, commit_data): + if not (self.skip_master and commit_data['branch'] == 'master'): + if self.start: + sep = ': ' if self.sameline else '\n' + commit_data['desc'] = commit_data['branch'] + sep + commit_data['desc'] + if self.end: + commit_data['desc'] = commit_data['desc'] + '\n' + commit_data['branch']