Skip to content

Commit dc7ba1d

Browse files
fix(precommit): handle filenames with spaces in Commit.from_merge
1 parent 118ca6d commit dc7ba1d

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

ggshield/core/scan/commit_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def get_file_sha_in_ref(
369369
"""
370370
output = git(["ls-tree", "-z", ref] + files, cwd=cwd)
371371
for line in output.split("\0")[:-1]:
372-
_, _, sha, path = line.split()
372+
_, _, sha, path = line.split(maxsplit=3)
373373
yield (path, sha)
374374

375375

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

387387

tests/unit/core/scan/test_commit.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,3 +504,41 @@ def test_from_merge(tmp_path):
504504
(Path("conflict.md"), Filemode.MODIFY),
505505
(Path("new.md"), Filemode.NEW),
506506
]
507+
508+
509+
def test_from_merge_filename_with_spaces(tmp_path):
510+
"""
511+
GIVEN two commits on different branches with a conflict
512+
involving a filename with spaces
513+
WHEN Commit.from_merge() is called
514+
THEN it returns successfully
515+
AND get_files returns the correct filename
516+
"""
517+
repo = Repository.create(tmp_path, initial_branch="master")
518+
519+
Path(tmp_path / "inital.md").write_text("Initial")
520+
repo.add(".")
521+
repo.create_commit("Initial commit on master")
522+
523+
repo.create_branch("feature_branch")
524+
repo.checkout("master")
525+
conflict_file = tmp_path / "file with spaces.md"
526+
conflict_file.write_text("Hello")
527+
repo.add(".")
528+
repo.create_commit("Commit on master")
529+
530+
repo.checkout("feature_branch")
531+
conflict_file.write_text("World")
532+
repo.add(".")
533+
repo.create_commit("Commit on feature_branch")
534+
535+
# Create merge commit with conflict
536+
with pytest.raises(subprocess.CalledProcessError):
537+
repo.git("merge", "master")
538+
539+
conflict_file.write_text("Hello World !")
540+
repo.add(".")
541+
commit = Commit.from_merge(cwd=tmp_path)
542+
files = list(commit.get_files())
543+
assert len(files) == 1
544+
assert files[0].path == Path("file with spaces.md")

0 commit comments

Comments
 (0)