Skip to content

Commit

Permalink
Merge branch 'pre-commit:main' into 2486-prepush-staged-files
Browse files Browse the repository at this point in the history
  • Loading branch information
aless10 committed Feb 26, 2024
2 parents 793eaef + 7b868c3 commit 4868d89
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repos:
hooks:
- id: add-trailing-comma
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
rev: v3.15.1
hooks:
- id: pyupgrade
args: [--py39-plus]
Expand Down
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
3.6.2 - 2024-02-18
==================

### Fixes
- Fix building golang hooks during `git commit --all`.
- #3130 PR by @asottile.
- #2722 issue by @pestanko and @matthewhughes934.

3.6.1 - 2024-02-10
==================

### Fixes
- Remove `PYTHONEXECUTABLE` from environment when running.
- #3110 PR by @untitaker.
- Handle staged-files-only with only a crlf diff.
- #3126 PR by @asottile.
- issue by @tyyrok.

3.6.0 - 2023-12-09
==================

Expand Down
3 changes: 2 additions & 1 deletion pre_commit/languages/golang.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from pre_commit.envcontext import envcontext
from pre_commit.envcontext import PatchesT
from pre_commit.envcontext import Var
from pre_commit.git import no_git_env
from pre_commit.prefix import Prefix
from pre_commit.util import cmd_output
from pre_commit.util import rmtree
Expand Down Expand Up @@ -141,7 +142,7 @@ def install_environment(
else:
gopath = env_dir

env = dict(os.environ, GOPATH=gopath)
env = no_git_env(dict(os.environ, GOPATH=gopath))
env.pop('GOBIN', None)
if version != 'system':
env['GOROOT'] = os.path.join(env_dir, '.go')
Expand Down
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
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = pre_commit
version = 3.6.0
version = 3.6.2
description = A framework for managing and maintaining multi-language pre-commit hooks.
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
31 changes: 31 additions & 0 deletions tests/languages/golang_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@

import pre_commit.constants as C
from pre_commit import lang_base
from pre_commit.commands.install_uninstall import install
from pre_commit.envcontext import envcontext
from pre_commit.languages import golang
from pre_commit.store import _make_local_repo
from pre_commit.util import cmd_output
from testing.fixtures import add_config_to_repo
from testing.fixtures import make_config_from_repo
from testing.language_helpers import run_language
from testing.util import cmd_output_mocked_pre_commit_home
from testing.util import git_commit


ACTUAL_GET_DEFAULT_VERSION = golang.get_default_version.__wrapped__
Expand Down Expand Up @@ -134,3 +140,28 @@ def test_local_golang_additional_deps(tmp_path):
def test_golang_hook_still_works_when_gobin_is_set(tmp_path):
with envcontext((('GOBIN', str(tmp_path.joinpath('gobin'))),)):
test_golang_system(tmp_path)


def test_during_commit_all(tmp_path, tempdir_factory, store, in_git_dir):
hook_dir = tmp_path.joinpath('hook')
hook_dir.mkdir()
_make_hello_world(hook_dir)
hook_dir.joinpath('.pre-commit-hooks.yaml').write_text(
'- id: hello-world\n'
' name: hello world\n'
' entry: golang-hello-world\n'
' language: golang\n'
' always_run: true\n',
)
cmd_output('git', 'init', hook_dir)
cmd_output('git', 'add', '.', cwd=hook_dir)
git_commit(cwd=hook_dir)

add_config_to_repo(in_git_dir, make_config_from_repo(hook_dir))

assert not install(C.CONFIG_FILE, store, hook_types=['pre-commit'])

git_commit(
fn=cmd_output_mocked_pre_commit_home,
tempdir_factory=tempdir_factory,
)
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 4868d89

Please sign in to comment.