Skip to content

Resolve tempfile path spaces issue in git commit function #1039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion commitizen/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,32 @@ 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"',
), # File contains spaces
(
"/tmp dir/temp file",
'git commit --signoff -F "/tmp dir/temp file"',
), # Path contains spaces
(
"/tmp/tempfile",
'git commit --signoff -F "/tmp/tempfile"',
), # Path does not contain spaces
],
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: instead of using commands. ids might be a even better solution

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree! ids is a better way to describe the test cases

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)