diff --git a/README.rst b/README.rst index 1e969b3..c2f5524 100644 --- a/README.rst +++ b/README.rst @@ -43,17 +43,38 @@ By default it's ``'{ticket} {commit_msg}``, where ``ticket`` is replaced with th Pass ``--mode=`` or update ``args: [--mode=regex_match]`` in your .yaml file to extract ticket by the regex rather than relying on branch name convention. With this mode you can also make use of ``{tickets}`` placeholder in ``format`` argument value to put multiple comma-separated tickets in the commit message in case your branch contains more than one ticket. +Pass ``--capitalize`` or ``--capitalize=N`` to capitalize the first ``N`` characters of the resulting commit message. When passed without a value, defaults to capitalizing the first character (``--capitalize=1``). + +Pass ``--to_trailer`` to append the ticket(s) as a `git trailer `_ at the bottom of the commit message instead of modifying the subject line. For example, committing on branch ``JIRA-1234_awesome_feature`` will append:: + + Refs: JIRA-1234 + +Pass ``--trailer_token=`` to customise the trailer key written by ``--to_trailer``. Defaults to ``Refs``. For example, ``--trailer_token=Fixes`` produces:: + + Fixes: JIRA-1234 + It is best used along with pre-commit_. You can use it along with pre-commit by adding the following hook in your ``.pre-commit-config.yaml`` file. :: repos: - repo: https://github.com/milin/giticket - rev: v1.4 + rev: v1.92 hooks: - id: giticket args: ['--regex=PROJ-[0-9]', '--format={ticket} {commit_msg}'] # Optional +Alternatively, to append tickets as a trailer instead of modifying the subject line: + +:: + + repos: + - repo: https://github.com/milin/giticket + rev: v1.92 + hooks: + - id: giticket + args: ['--to_trailer', '--trailer_token=Refs'] # --trailer_token is optional, defaults to Refs + You need to have precommit setup to use this hook. -------------------------------------------------- diff --git a/giticket/giticket.py b/giticket/giticket.py index 3709a51..c3a4ba3 100644 --- a/giticket/giticket.py +++ b/giticket/giticket.py @@ -19,7 +19,7 @@ def capitalize(text, capitalize_spaces): return text[:capitalize_spaces].upper() + text[capitalize_spaces:] if text else text -def update_commit_message(filename, regex, mode, format_string, conventionalcommits=False, capitalize_spaces=0, to_trailer=False): +def update_commit_message(filename, regex, mode, format_string, conventionalcommits=False, capitalize_spaces=0, to_trailer=False, trailer_token='Refs'): with io.open(filename, 'r+') as fd: contents = fd.readlines() commit_msg = contents[0].rstrip('\r\n') @@ -37,7 +37,7 @@ def update_commit_message(filename, regex, mode, format_string, conventionalcomm tickets = [t.strip() for t in tickets] if to_trailer: - trailer = 'Refs: {tickets}\n'.format(tickets=', '.join(tickets)) + trailer = '{token}: {tickets}\n'.format(token=trailer_token, tickets=', '.join(tickets)) if contents and not contents[-1].endswith('\n'): contents[-1] += '\n' if not contents or contents[-1].strip() != '': @@ -99,6 +99,7 @@ def main(argv=None): parser.add_argument('--conventionalcommits', action='store_true') parser.add_argument('--capitalize', nargs='?', const=1, type=int, default=0) parser.add_argument('--to_trailer', action='store_true') + parser.add_argument('--trailer_token', nargs='?', default='Refs') parser.add_argument('--regex') parser.add_argument('--format', nargs='?') parser.add_argument('--mode', nargs='?', const=underscore_split_mode, @@ -110,7 +111,7 @@ def main(argv=None): return 1 regex = args.regex or r'[A-Z]+-\d+' # noqa format_string = args.format or '{ticket} {commit_msg}' # noqa - update_commit_message(args.filenames[0], regex, args.mode, format_string, args.conventionalcommits, args.capitalize, args.to_trailer) + update_commit_message(args.filenames[0], regex, args.mode, format_string, args.conventionalcommits, args.capitalize, args.to_trailer, args.trailer_token) if __name__ == '__main__': diff --git a/tests/test_giticket.py b/tests/test_giticket.py index 5298b15..c1f1a01 100644 --- a/tests/test_giticket.py +++ b/tests/test_giticket.py @@ -214,6 +214,17 @@ def test_update_commit_message_to_trailer_multiple_tickets(mock_branch_name, tmp assert path.read() == "Subject line\n\nRefs: JIRA-1234, JIRA-5678\n" +@mock.patch(TESTING_MODULE + '.get_branch_name') +def test_update_commit_message_to_trailer_custom_token(mock_branch_name, tmpdir): + mock_branch_name.return_value = "JIRA-1234_new_feature" + path = tmpdir.join('file.txt') + path.write("Subject line") + update_commit_message(six.text_type(path), r'[A-Z]+-\d+', + 'regex_match', '{ticket}: {commit_msg}', + to_trailer=True, trailer_token='Fixes') + assert path.read() == "Subject line\n\nFixes: JIRA-1234\n" + + @mock.patch(TESTING_MODULE + '.get_branch_name') def test_update_commit_message_capitalize_disabled_by_default(mock_branch_name, tmpdir): mock_branch_name.return_value = "JIRA-1234_new_feature" @@ -280,6 +291,7 @@ def test_main(mock_update_commit_message, mock_argparse): mock_args.conventionalcommits = True mock_args.capitalize = 0 mock_args.to_trailer = False + mock_args.trailer_token = 'Refs' mock_argparse.ArgumentParser.return_value.parse_args.return_value = mock_args main() mock_update_commit_message.assert_called_once_with('foo.txt', r'[A-Z]+-\d+', @@ -287,7 +299,8 @@ def test_main(mock_update_commit_message, mock_argparse): '{ticket} {commit_msg}', True, 0, - False) + False, + 'Refs') @mock.patch(TESTING_MODULE + '.update_commit_message') @@ -298,7 +311,8 @@ def test_main_to_trailer_only_is_allowed(mock_update_commit_message): '{ticket} {commit_msg}', False, 0, - True) + True, + 'Refs') def test_main_errors_without_format_or_conventionalcommits_or_to_trailer():