Thanks to visit codestin.com
Credit goes to github.com

Skip to content

perform autoupdate without Store contention #2861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions pre_commit/commands/autoupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from pre_commit.clientlib import LOCAL
from pre_commit.clientlib import META
from pre_commit.commands.migrate_config import migrate_config
from pre_commit.store import Store
from pre_commit.util import CalledProcessError
from pre_commit.util import cmd_output
from pre_commit.util import cmd_output_b
Expand All @@ -27,11 +26,12 @@
class RevInfo(NamedTuple):
repo: str
rev: str
frozen: str | None
frozen: str | None = None
hook_ids: frozenset[str] = frozenset()

@classmethod
def from_config(cls, config: dict[str, Any]) -> RevInfo:
return cls(config['repo'], config['rev'], None)
return cls(config['repo'], config['rev'])

def update(self, tags_only: bool, freeze: bool) -> RevInfo:
with tempfile.TemporaryDirectory() as tmp:
Expand Down Expand Up @@ -63,7 +63,19 @@ def update(self, tags_only: bool, freeze: bool) -> RevInfo:
exact = cmd_output(*_git, 'rev-parse', rev)[1].strip()
if exact != rev:
rev, frozen = exact, rev
return self._replace(rev=rev, frozen=frozen)

try:
cmd_output(*_git, 'checkout', rev, '--', C.MANIFEST_FILE)
except CalledProcessError:
pass # this will be caught by manifest validating code
try:
manifest = load_manifest(os.path.join(tmp, C.MANIFEST_FILE))
except InvalidManifestError as e:
raise RepositoryCannotBeUpdatedError(str(e))
else:
hook_ids = frozenset(hook['id'] for hook in manifest)

return self._replace(rev=rev, frozen=frozen, hook_ids=hook_ids)


class RepositoryCannotBeUpdatedError(RuntimeError):
Expand All @@ -73,17 +85,10 @@ class RepositoryCannotBeUpdatedError(RuntimeError):
def _check_hooks_still_exist_at_rev(
repo_config: dict[str, Any],
info: RevInfo,
store: Store,
) -> None:
try:
path = store.clone(repo_config['repo'], info.rev)
manifest = load_manifest(os.path.join(path, C.MANIFEST_FILE))
except InvalidManifestError as e:
raise RepositoryCannotBeUpdatedError(str(e))

# See if any of our hooks were deleted with the new commits
hooks = {hook['id'] for hook in repo_config['hooks']}
hooks_missing = hooks - {hook['id'] for hook in manifest}
hooks_missing = hooks - info.hook_ids
if hooks_missing:
raise RepositoryCannotBeUpdatedError(
f'Cannot update because the update target is missing these '
Expand Down Expand Up @@ -139,7 +144,6 @@ def _write_new_config(path: str, rev_infos: list[RevInfo | None]) -> None:

def autoupdate(
config_file: str,
store: Store,
tags_only: bool,
freeze: bool,
repos: Sequence[str] = (),
Expand All @@ -161,9 +165,9 @@ def autoupdate(
continue

output.write(f'Updating {info.repo} ... ')
new_info = info.update(tags_only=tags_only, freeze=freeze)
try:
_check_hooks_still_exist_at_rev(repo_config, new_info, store)
new_info = info.update(tags_only=tags_only, freeze=freeze)
_check_hooks_still_exist_at_rev(repo_config, new_info)
except RepositoryCannotBeUpdatedError as error:
output.write_line(error.args[0])
rev_infos.append(None)
Expand Down
2 changes: 1 addition & 1 deletion pre_commit/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def _add_cmd(name: str, *, help: str) -> argparse.ArgumentParser:

if args.command == 'autoupdate':
return autoupdate(
args.config, store,
args.config,
tags_only=not args.bleeding_edge,
freeze=args.freeze,
repos=args.repos,
Expand Down
Loading