From 7c81f11d6381e94b41dc17ba1a11a4cbf33e5634 Mon Sep 17 00:00:00 2001 From: Daniel Bast <2790401+dbast@users.noreply.github.com> Date: Tue, 18 Mar 2025 15:32:23 +0100 Subject: [PATCH] Add freeze config option for autoupdate command This commit adds a new 'freeze' option to the CONFIG_SCHEMA, allowing users to set 'freeze: true' in their pre-commit configuration file. The intend are two effects, when setting `freeze: True`: * Enables freeze automatically when running `pre-commit autoupdate` (no need then anymore to specify --freeze). * Enables https://pre-commit.ci/ to freeze hooks as part of update PRs. --- pre_commit/clientlib.py | 2 ++ pre_commit/commands/autoupdate.py | 2 ++ tests/commands/autoupdate_test.py | 11 +++++++++++ 3 files changed, 15 insertions(+) diff --git a/pre_commit/clientlib.py b/pre_commit/clientlib.py index c0f736d9..bee23579 100644 --- a/pre_commit/clientlib.py +++ b/pre_commit/clientlib.py @@ -466,6 +466,7 @@ CONFIG_SCHEMA = cfgv.Map( cfgv.Optional('files', check_string_regex, ''), cfgv.Optional('exclude', check_string_regex, '^$'), cfgv.Optional('fail_fast', cfgv.check_bool, False), + cfgv.Optional('freeze', cfgv.check_bool, False), cfgv.WarnAdditionalKeys( ( 'repos', @@ -477,6 +478,7 @@ CONFIG_SCHEMA = cfgv.Map( 'fail_fast', 'minimum_pre_commit_version', 'ci', + 'freeze', ), warn_unknown_keys_root, ), diff --git a/pre_commit/commands/autoupdate.py b/pre_commit/commands/autoupdate.py index aa0c5e25..ceaec9b7 100644 --- a/pre_commit/commands/autoupdate.py +++ b/pre_commit/commands/autoupdate.py @@ -176,6 +176,8 @@ def autoupdate( if repo['repo'] not in {LOCAL, META} ] + freeze = freeze or load_config(config_file)['freeze'] + rev_infos: list[RevInfo | None] = [None] * len(config_repos) jobs = jobs or xargs.cpu_count() # 0 => number of cpus jobs = min(jobs, len(repos) or len(config_repos)) # max 1-per-thread diff --git a/tests/commands/autoupdate_test.py b/tests/commands/autoupdate_test.py index 71bd0444..11aed2b4 100644 --- a/tests/commands/autoupdate_test.py +++ b/tests/commands/autoupdate_test.py @@ -139,6 +139,17 @@ def test_rev_info_update_does_not_freeze_if_already_sha(out_of_date): assert new_info.frozen is None +def test_autoupdate_freeze_from_config(tagged, tmpdir): + git_commit(cwd=tagged.path) + repo = make_config_from_repo(tagged.path, rev=tagged.original_rev) + contents = {'repos': [repo], 'freeze': True} + cfg = tmpdir.join(C.CONFIG_FILE) + write_config(str(tmpdir), contents) + + assert autoupdate(str(cfg), freeze=False, tags_only=True) == 0 + assert f'rev: {tagged.head_rev} # frozen: v1.2.3' in cfg.read() + + def test_autoupdate_up_to_date_repo(up_to_date, tmpdir): contents = ( f'repos:\n'