Skip to content

Commit

Permalink
Merge pull request #3126 from pre-commit/crlf-only-diff
Browse files Browse the repository at this point in the history
staged_files_only can handle a crlf-only diff
  • Loading branch information
asottile committed Feb 10, 2024
2 parents 7384838 + 032d8e2 commit 92678c3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pre_commit/staged_files_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ def _unstaged_changes_cleared(patch_dir: str) -> Generator[None, None, None]:
# There weren't any staged files so we don't need to do anything
# special
yield
elif retcode == 1 and not diff_stdout.strip():
# due to behaviour (probably a bug?) in git with crlf endings and
# autocrlf set to either `true` or `input` sometimes git will refuse
# to show a crlf-only diff to us :(
yield
elif retcode == 1 and diff_stdout.strip():
patch_filename = f'patch{int(time.time())}-{os.getpid()}'
patch_filename = os.path.join(patch_dir, patch_filename)
Expand Down
15 changes: 15 additions & 0 deletions tests/staged_files_only_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,21 @@ def test_crlf(in_git_dir, patch_dir, crlf_before, crlf_after, autocrlf):
assert_no_diff()


@pytest.mark.parametrize('autocrlf', ('true', 'input'))
def test_crlf_diff_only(in_git_dir, patch_dir, autocrlf):
# due to a quirk (?) in git -- a diff only in crlf does not show but
# still results in an exit code of `1`
# we treat this as "no diff" -- though ideally it would discard the diff
# while committing
cmd_output('git', 'config', '--local', 'core.autocrlf', autocrlf)

_write(b'1\r\n2\r\n3\r\n')
cmd_output('git', 'add', 'foo')
_write(b'1\n2\n3\n')
with staged_files_only(patch_dir):
pass


def test_whitespace_errors(in_git_dir, patch_dir):
cmd_output('git', 'config', '--local', 'apply.whitespace', 'error')
test_crlf(in_git_dir, patch_dir, True, True, 'true')
Expand Down

0 comments on commit 92678c3

Please sign in to comment.