From de684f1b05da46118ec757c9ec267babde1769ba Mon Sep 17 00:00:00 2001 From: Sven Steinbauer Date: Wed, 6 Mar 2024 09:40:13 +0000 Subject: [PATCH 1/5] feat: filter unmodified files by default 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 --- src/darker/git.py | 1 + src/darker/tests/test_git.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/darker/git.py b/src/darker/git.py index e89ca8636..d2649a04f 100644 --- a/src/darker/git.py +++ b/src/darker/git.py @@ -145,6 +145,7 @@ def _git_diff_name_only( "diff", "--name-only", "--relative", + "--diff-filter=M", rev1, # rev2 is inserted here if not WORKTREE "--", diff --git a/src/darker/tests/test_git.py b/src/darker/tests/test_git.py index 8b65ed855..5b60d2855 100644 --- a/src/darker/tests/test_git.py +++ b/src/darker/tests/test_git.py @@ -214,16 +214,16 @@ 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()`` includes added/modified, skips 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")} From cc9317f04d2fc9f3dc560af342ea42e13eeda885 Mon Sep 17 00:00:00 2001 From: Sven Steinbauer Date: Wed, 6 Mar 2024 13:53:18 +0000 Subject: [PATCH 2/5] feat: add added files to filter Adding ``--diff-filter=M` will only show modified files, but darker should also check newly added files. Add the `A` option to the diff call to include these --- src/darker/git.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/darker/git.py b/src/darker/git.py index d2649a04f..20bd96259 100644 --- a/src/darker/git.py +++ b/src/darker/git.py @@ -145,7 +145,7 @@ def _git_diff_name_only( "diff", "--name-only", "--relative", - "--diff-filter=M", + "--diff-filter=MA", rev1, # rev2 is inserted here if not WORKTREE "--", From c55fb885d855050668a9627c63f78222a13718cd Mon Sep 17 00:00:00 2001 From: Sven Steinbauer Date: Fri, 8 Mar 2024 11:47:14 +0000 Subject: [PATCH 3/5] test: fix failing tests With the added diff filter some tests needed updating to reflect the change --- src/darker/tests/test_git.py | 3 --- src/darker/tests/test_main_revision.py | 4 +++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/darker/tests/test_git.py b/src/darker/tests/test_git.py index 5b60d2855..812ba7df1 100644 --- a/src/darker/tests/test_git.py +++ b/src/darker/tests/test_git.py @@ -337,7 +337,6 @@ def test_git_get_modified_python_files(git_repo, modify_paths, paths, expect): _description="from master to worktree and index on branch", revrange="master..", expect={ - "del_master.py", "mod_master.py", "mod_both.py", "mod_branch.py", @@ -353,7 +352,6 @@ def test_git_get_modified_python_files(git_repo, modify_paths, paths, expect): ), revrange="master..HEAD", expect={ - "del_master.py", "mod_master.py", "mod_both.py", "mod_branch.py", @@ -363,7 +361,6 @@ def test_git_get_modified_python_files(git_repo, modify_paths, paths, expect): _description="from master to branch, excluding worktree and index", revrange="master..branch", expect={ - "del_master.py", "mod_master.py", "mod_both.py", "mod_branch.py", diff --git a/src/darker/tests/test_main_revision.py b/src/darker/tests/test_main_revision.py index 7d885f866..029f8b8c5 100644 --- a/src/darker/tests/test_main_revision.py +++ b/src/darker/tests/test_main_revision.py @@ -99,7 +99,9 @@ worktree_content=b"USERMOD=1\n", expect={"+1", "+1M0", "+2-1", "+2", "+2M1-0", "+2M1"}, ), - dict(revision="HEAD~2", worktree_content=b"ORIGINAL=1\n", expect={"+1", "+1M0"}), + # These are empty because git diff reports these are renamed files. + # We only care about added or modified files. See PR #454 + dict(revision="HEAD~2", worktree_content=b"ORIGINAL=1\n", expect=set()), dict( revision="HEAD~2", worktree_content=b"MODIFIED=1\n", From 8df77a19db1e10c05a712eb6863eae8e0de25520 Mon Sep 17 00:00:00 2001 From: Sven Steinbauer Date: Fri, 15 Mar 2024 08:44:00 +0000 Subject: [PATCH 4/5] style: address review comments * New line in test to separate test from verification --- src/darker/tests/test_git.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/darker/tests/test_git.py b/src/darker/tests/test_git.py index 812ba7df1..6c9f84967 100644 --- a/src/darker/tests/test_git.py +++ b/src/darker/tests/test_git.py @@ -219,11 +219,13 @@ def test_git_diff_name_only(git_repo): 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")} From 8daad200b18be8c3d2eaef231f1880e6c63808fc Mon Sep 17 00:00:00 2001 From: Antti Kaihola <13725+akaihola@users.noreply.github.com> Date: Thu, 1 Aug 2024 17:40:56 +0300 Subject: [PATCH 5/5] docs: update the change log --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index 4d2af1432..4eeaef028 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,6 +8,7 @@ Added - New exit codes 2 for file not found, 3 for invalid command line arguments, 4 for missing dependencies and 123 for unknown failures. - Display exit code in parentheses after error message. +- Do not reformat renamed files. Fixed -----