@@ -73,7 +73,8 @@ def test_install_is_executable_idempotent_and_uninstallable(tmp_path: Path) -> N
7373 path = install_hook (repo )
7474 original = path .read_text (encoding = "utf-8" )
7575 assert path .stat ().st_mode & 0o111
76- assert original .startswith (f"#!{ sys .executable } \n " )
76+ assert original .startswith ("#!/bin/sh\n " )
77+ assert sys .executable in original
7778 assert HOOK_MARKER in original
7879
7980 assert install_hook (repo ) == path
@@ -133,7 +134,7 @@ def fake_run(command: list[str], **kwargs: object) -> None:
133134 monkeypatch .setattr (sys , "argv" , ["gitmate-hook" , str (message_file )])
134135 monkeypatch .setenv ("PRE_COMMIT_COMMIT_MSG_SOURCE" , "message" )
135136 monkeypatch .setenv ("PRE_COMMIT_COMMIT_OBJECT_NAME" , "deadbeef" )
136- monkeypatch .setattr (hooks_mod . subprocess , " run" , fake_run )
137+ monkeypatch .setattr ("gitmate.hooks. subprocess. run" , fake_run )
137138
138139 assert hooks_mod .main () == 0
139140 assert commands [0 ][- 3 :] == [str (message_file ), "message" , "deadbeef" ]
@@ -238,3 +239,48 @@ def test_worker_failure_and_timeout_do_not_block_real_commit(
238239 assert result .returncode == 0 , result .stderr
239240 assert _git (repo , "log" , "-1" , "--format=%s" ).stdout .strip () == "Use the repository template"
240241 assert elapsed < 8
242+
243+
244+ def test_commit_preserves_template_and_prepends_message (
245+ tmp_path : Path , hook_env : dict [str , str ]
246+ ) -> None :
247+ repo = tmp_path / "repo"
248+ _init_repo (repo )
249+ install_hook (repo )
250+ template = tmp_path / "commit-template.txt"
251+ template .write_text ("Ticket: JIRA-123\n Reviewed-by: Lead\n " , encoding = "utf-8" )
252+ _git (repo , "config" , "commit.template" , str (template ))
253+
254+ (repo / "auth.py" ).write_text ("def auth(): pass\n " , encoding = "utf-8" )
255+ _git (repo , "add" , "auth.py" )
256+
257+ result = _commit (repo , env = hook_env )
258+ assert result .returncode == 0 , result .stderr
259+ full_message = _git (repo , "log" , "-1" , "--format=%B" ).stdout
260+ assert full_message .startswith ("feat: add auth.py" )
261+ assert "Ticket: JIRA-123" in full_message
262+ assert "Reviewed-by: Lead" in full_message
263+
264+
265+ def test_cli_commit_hook_mode (tmp_path : Path , monkeypatch : pytest .MonkeyPatch ) -> None :
266+ repo = tmp_path / "repo"
267+ _init_repo (repo )
268+ monkeypatch .chdir (repo )
269+
270+ (repo / "new_module.py" ).write_text ("val = 100\n " , encoding = "utf-8" )
271+ _git (repo , "add" , "new_module.py" )
272+
273+ msg_file = tmp_path / "COMMIT_EDITMSG"
274+ msg_file .write_text ("# Please enter the commit message\n # On branch main\n " , encoding = "utf-8" )
275+
276+ runner = CliRunner ()
277+ res = runner .invoke (app , ["commit" , "--hook-mode" , str (msg_file )])
278+ assert res .exit_code == 0 , res .output
279+ content = msg_file .read_text (encoding = "utf-8" )
280+ assert content .startswith ("feat: add new_module.py" )
281+ assert "# Please enter the commit message" in content
282+
283+ # Missing file argument returns error
284+ res_err = runner .invoke (app , ["commit" , "--hook-mode" ])
285+ assert res_err .exit_code == 1
286+ assert "error" in res_err .output .lower ()
0 commit comments