diff --git a/commitizen/git.py b/commitizen/git.py index 900ca9298e..53e7335a3f 100644 --- a/commitizen/git.py +++ b/commitizen/git.py @@ -112,7 +112,7 @@ def commit( f.write(message.encode("utf-8")) f.close() - command = f"git commit {args} -F {f.name}" + command = f'git commit {args} -F "{f.name}"' if committer_date and os.name == "nt": # pragma: no cover # Using `cmd /v /c "{command}"` sets environment variables only for that command diff --git a/tests/test_git.py b/tests/test_git.py index 3b7a08f94a..8af332d214 100644 --- a/tests/test_git.py +++ b/tests/test_git.py @@ -293,3 +293,37 @@ def test_create_tag_with_message(tmp_commitizen_project): assert git.get_tag_message(tag_name) == ( tag_message if platform.system() != "Windows" else f"'{tag_message}'" ) + + +@pytest.mark.parametrize( + "file_path,expected_cmd", + [ + ( + "/tmp/temp file", + 'git commit --signoff -F "/tmp/temp file"', + ), + ( + "/tmp dir/temp file", + 'git commit --signoff -F "/tmp dir/temp file"', + ), + ( + "/tmp/tempfile", + 'git commit --signoff -F "/tmp/tempfile"', + ), + ], + ids=[ + "File contains spaces", + "Path contains spaces", + "Path does not contain spaces", + ], +) +def test_commit_with_spaces_in_path(mocker, file_path, expected_cmd): + mock_run = mocker.patch("commitizen.cmd.run", return_value=FakeCommand()) + mock_unlink = mocker.patch("os.unlink") + mock_temp_file = mocker.patch("commitizen.git.NamedTemporaryFile") + mock_temp_file.return_value.name = file_path + + git.commit("feat: new feature", "--signoff") + + mock_run.assert_called_once_with(expected_cmd) + mock_unlink.assert_called_once_with(file_path)