diff --git a/pre_commit_hooks/prepare_commit_msg.py b/pre_commit_hooks/prepare_commit_msg.py index ef91ad2..f9613ae 100644 --- a/pre_commit_hooks/prepare_commit_msg.py +++ b/pre_commit_hooks/prepare_commit_msg.py @@ -47,7 +47,7 @@ def _get_default_template() -> str: def get_rendered_template( template_file: str, - variables: dict[str, str], + variables: dict[str, Sequence[str]], ) -> str: with open(template_file) as f: template = Template(f.read()) @@ -63,6 +63,8 @@ def update_commit_file( with open(commit_msg_file) as f: data = f.readlines() + commented = list(filter(lambda line: line.startswith('#'), data)) + original = list(filter(lambda line: not line.startswith('#'), data)) data_as_str = ''.join([item for item in data]) # if message already contain ticket number means # it is under git commit --amend or rebase or alike @@ -73,6 +75,8 @@ def update_commit_file( variables = { 'ticket': ticket, 'content': data_as_str, + 'commented': commented, + 'original_msg': original, } content = get_rendered_template( diff --git a/templates/prepare_commit_msg_append.j2 b/templates/prepare_commit_msg_append.j2 index 506bf53..b77d84c 100644 --- a/templates/prepare_commit_msg_append.j2 +++ b/templates/prepare_commit_msg_append.j2 @@ -1,3 +1,5 @@ -{{ content }} +{{ original_msg | join('') | trim() }} -Relates: {{ ticket }} +Relates: #{{ ticket }} + +{{ commented | join('') }} diff --git a/templates/prepare_commit_msg_prepend.j2 b/templates/prepare_commit_msg_prepend.j2 index 61c6614..e04d887 100644 --- a/templates/prepare_commit_msg_prepend.j2 +++ b/templates/prepare_commit_msg_prepend.j2 @@ -1 +1 @@ -[{{ ticket }}] {{ content }} +[{{ ticket }}] {{ content | join('') }} diff --git a/tests/prepare_commit_msg_test.py b/tests/prepare_commit_msg_test.py index d2a4c7e..f991b21 100644 --- a/tests/prepare_commit_msg_test.py +++ b/tests/prepare_commit_msg_test.py @@ -52,13 +52,13 @@ TESTS = ( ), ( b'', - b'\n\nRelates: AA-01', + b'\n\nRelates: #AA-01\n\n', 'feature/AA-01', get_template_path('prepare_commit_msg_append.j2'), ), ( b'Initial message', - b'Initial message\n\nRelates: AA-02', + b'Initial message\n\nRelates: #AA-02\n\n', 'feature/AA-02', get_template_path('prepare_commit_msg_append.j2'), ), @@ -110,3 +110,44 @@ def test_main( ], ) == 0 assert path.read_binary() == expected_val + + +TESTS_TEMPLATES = ( + ( + b'Initial Message\n\n# Git commented\n# output simulated', + b'[1.0.0] Initial Message\n\n# Git commented\n# output simulated', + 'release/1.0.0', # but this should + get_template_path('prepare_commit_msg_prepend.j2'), + ), + ( + b'Initial Message\n# Git commented\n# output simulated', + b'Initial Message\n\nRelates: #1.0.0\n\n' + b'# Git commented\n# output simulated', + 'release/1.0.0', # but this should + get_template_path('prepare_commit_msg_append.j2'), + ), +) + + +@pytest.mark.parametrize( + ('input_s', 'expected_val', 'branch_name', 'template'), + TESTS_TEMPLATES, +) +def test_main_separating_content( + input_s, expected_val, branch_name, template, + temp_git_dir, +): + with temp_git_dir.as_cwd(): + path = temp_git_dir.join('file.txt') + path.write_binary(input_s) + assert path.read_binary() == input_s + + cmd_output('git', 'checkout', '-b', branch_name) + assert main( + argv=[ + '-t', template, + '-p', '(?<=release/).*', + str(path), + ], + ) == 0 + assert path.read_binary() == expected_val