Skip to content

Commit

Permalink
Filter unmodified files by default
Browse files Browse the repository at this point in the history
Add `--diff-filter=M` to the git command that fetches changed files. The filter will ignore files that have been renamed or moved, but not had their content changed
  • Loading branch information
Svenito committed Mar 6, 2024
1 parent 91afe0f commit 5d07731
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/darker/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ def _git_diff_name_only(
"diff",
"--name-only",
"--relative",
"--diff-filter=M",
rev1,
# rev2 is inserted here if not WORKTREE
"--",
Expand Down
6 changes: 6 additions & 0 deletions src/darker/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ def add(
self._run("commit", "-m", commit)
return absolute_paths

def rename(self, original_path: str, new_path: str, commit: str = None):
"""Move/rename a file"""
self._run("mv", original_path, new_path)
if commit:
self._run("commit", "-m", commit)

def get_hash(self, revision: str = "HEAD") -> str:
"""Return the commit hash at the given revision in the Git repository"""
return self._run_and_get_first_line("rev-parse", revision)
Expand Down
5 changes: 3 additions & 2 deletions src/darker/tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,16 +608,17 @@ def test_get_missing_at_revision_worktree(git_repo):


def test_git_diff_name_only(git_repo):
"""``_git_diff_name_only()`` only returns paths of modified files"""
"""``_git_diff_name_only()`` only returns paths of modified files and ignores
renamed/moved files"""
git_repo.add({"a.py": "a", "b.py": "b", "c.py": "c"}, commit="Initial commit")
first = git_repo.get_hash()
git_repo.add({"a.py": "A", "b.dy": "B"}, commit="only a.py modified")
git_repo.rename("c.py", "x.py", commit="rename c.py to x.py")
second = git_repo.get_hash()

result = git._git_diff_name_only(
first, second, {Path("a.py"), Path("c.py"), Path("Z.py")}, git_repo.root
)

assert result == {Path("a.py")}


Expand Down
1 change: 0 additions & 1 deletion src/darker/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import random
import re
import string
import sys
from argparse import ArgumentError
from io import BytesIO
from pathlib import Path
Expand Down

0 comments on commit 5d07731

Please sign in to comment.