Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- The ggshield pre-commit hook no longer crashes when merging files with spaces in their names (#991).
4 changes: 2 additions & 2 deletions ggshield/core/scan/commit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def get_file_sha_in_ref(
"""
output = git(["ls-tree", "-z", ref] + files, cwd=cwd)
for line in output.split("\0")[:-1]:
_, _, sha, path = line.split()
_, _, sha, path = line.split(maxsplit=3)
yield (path, sha)


Expand All @@ -381,7 +381,7 @@ def get_file_sha_stage(
"""
output = git(["ls-files", "--stage", "-z"] + files, cwd=cwd)
for line in output.split("\0")[:-1]:
_, sha, _, path = line.split()
_, sha, _, path = line.split(maxsplit=3)
yield (path, sha)


Expand Down
38 changes: 38 additions & 0 deletions tests/unit/core/scan/test_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,41 @@ def test_from_merge(tmp_path):
(Path("conflict.md"), Filemode.MODIFY),
(Path("new.md"), Filemode.NEW),
]


def test_from_merge_filename_with_spaces(tmp_path):
"""
GIVEN two commits on different branches with a conflict
involving a filename with spaces
WHEN Commit.from_merge() is called
THEN it returns successfully
AND get_files returns the correct filename
"""
repo = Repository.create(tmp_path, initial_branch="master")

Path(tmp_path / "inital.md").write_text("Initial")
repo.add(".")
repo.create_commit("Initial commit on master")

repo.create_branch("feature_branch")
repo.checkout("master")
conflict_file = tmp_path / "file with spaces.md"
conflict_file.write_text("Hello")
repo.add(".")
repo.create_commit("Commit on master")

repo.checkout("feature_branch")
conflict_file.write_text("World")
repo.add(".")
repo.create_commit("Commit on feature_branch")

# Create merge commit with conflict
with pytest.raises(subprocess.CalledProcessError):
repo.git("merge", "master")

conflict_file.write_text("Hello World !")
repo.add(".")
commit = Commit.from_merge(cwd=tmp_path)
files = list(commit.get_files())
assert len(files) == 1
assert files[0].path == Path("file with spaces.md")
Loading