mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-14 06:34:44 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
72ad6dc953
commit
f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions
|
|
@ -1,23 +1,28 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os.path
|
||||
import pathlib
|
||||
import re
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
from typing import List, Optional, Tuple
|
||||
from typing import List
|
||||
from typing import Optional
|
||||
from typing import Tuple
|
||||
|
||||
from pip._internal.exceptions import BadCommand, InstallationError
|
||||
from pip._internal.utils.misc import HiddenText, display_path, hide_url
|
||||
from pip._internal.exceptions import BadCommand
|
||||
from pip._internal.exceptions import InstallationError
|
||||
from pip._internal.utils.misc import display_path
|
||||
from pip._internal.utils.misc import HiddenText
|
||||
from pip._internal.utils.misc import hide_url
|
||||
from pip._internal.utils.subprocess import make_command
|
||||
from pip._internal.vcs.versioncontrol import (
|
||||
AuthInfo,
|
||||
RemoteNotFoundError,
|
||||
RemoteNotValidError,
|
||||
RevOptions,
|
||||
VersionControl,
|
||||
find_path_to_project_root_from_repo_root,
|
||||
vcs,
|
||||
)
|
||||
from pip._internal.vcs.versioncontrol import AuthInfo
|
||||
from pip._internal.vcs.versioncontrol import find_path_to_project_root_from_repo_root
|
||||
from pip._internal.vcs.versioncontrol import RemoteNotFoundError
|
||||
from pip._internal.vcs.versioncontrol import RemoteNotValidError
|
||||
from pip._internal.vcs.versioncontrol import RevOptions
|
||||
from pip._internal.vcs.versioncontrol import vcs
|
||||
from pip._internal.vcs.versioncontrol import VersionControl
|
||||
|
||||
urlsplit = urllib.parse.urlsplit
|
||||
urlunsplit = urllib.parse.urlunsplit
|
||||
|
|
@ -27,14 +32,14 @@ logger = logging.getLogger(__name__)
|
|||
|
||||
|
||||
GIT_VERSION_REGEX = re.compile(
|
||||
r"^git version " # Prefix.
|
||||
r"(\d+)" # Major.
|
||||
r"\.(\d+)" # Dot, minor.
|
||||
r"(?:\.(\d+))?" # Optional dot, patch.
|
||||
r".*$" # Suffix, including any pre- and post-release segments we don't care about.
|
||||
r'^git version ' # Prefix.
|
||||
r'(\d+)' # Major.
|
||||
r'\.(\d+)' # Dot, minor.
|
||||
r'(?:\.(\d+))?' # Optional dot, patch.
|
||||
r'.*$', # Suffix, including any pre- and post-release segments we don't care about.
|
||||
)
|
||||
|
||||
HASH_REGEX = re.compile("^[a-fA-F0-9]{40}$")
|
||||
HASH_REGEX = re.compile('^[a-fA-F0-9]{40}$')
|
||||
|
||||
# SCP (Secure copy protocol) shorthand. e.g. 'git@example.com:foo/bar.git'
|
||||
SCP_REGEX = re.compile(
|
||||
|
|
@ -57,23 +62,23 @@ def looks_like_hash(sha: str) -> bool:
|
|||
|
||||
|
||||
class Git(VersionControl):
|
||||
name = "git"
|
||||
dirname = ".git"
|
||||
repo_name = "clone"
|
||||
name = 'git'
|
||||
dirname = '.git'
|
||||
repo_name = 'clone'
|
||||
schemes = (
|
||||
"git+http",
|
||||
"git+https",
|
||||
"git+ssh",
|
||||
"git+git",
|
||||
"git+file",
|
||||
'git+http',
|
||||
'git+https',
|
||||
'git+ssh',
|
||||
'git+git',
|
||||
'git+file',
|
||||
)
|
||||
# Prevent the user's environment variables from interfering with pip:
|
||||
# https://github.com/pypa/pip/issues/1130
|
||||
unset_environ = ("GIT_DIR", "GIT_WORK_TREE")
|
||||
default_arg_rev = "HEAD"
|
||||
unset_environ = ('GIT_DIR', 'GIT_WORK_TREE')
|
||||
default_arg_rev = 'HEAD'
|
||||
|
||||
@staticmethod
|
||||
def get_base_rev_args(rev: str) -> List[str]:
|
||||
def get_base_rev_args(rev: str) -> list[str]:
|
||||
return [rev]
|
||||
|
||||
def is_immutable_rev_checkout(self, url: str, dest: str) -> bool:
|
||||
|
|
@ -90,10 +95,10 @@ class Git(VersionControl):
|
|||
is_tag_or_branch = bool(self.get_revision_sha(dest, rev_options.rev)[0])
|
||||
return not is_tag_or_branch
|
||||
|
||||
def get_git_version(self) -> Tuple[int, ...]:
|
||||
def get_git_version(self) -> tuple[int, ...]:
|
||||
version = self.run_command(
|
||||
["version"],
|
||||
command_desc="git version",
|
||||
['version'],
|
||||
command_desc='git version',
|
||||
show_stdout=False,
|
||||
stdout_only=True,
|
||||
)
|
||||
|
|
@ -104,7 +109,7 @@ class Git(VersionControl):
|
|||
return tuple(int(c) for c in match.groups())
|
||||
|
||||
@classmethod
|
||||
def get_current_branch(cls, location: str) -> Optional[str]:
|
||||
def get_current_branch(cls, location: str) -> str | None:
|
||||
"""
|
||||
Return the current branch, or None if HEAD isn't at a branch
|
||||
(e.g. detached HEAD).
|
||||
|
|
@ -113,7 +118,7 @@ class Git(VersionControl):
|
|||
# HEAD rather than a symbolic ref. In addition, the -q causes the
|
||||
# command to exit with status code 1 instead of 128 in this case
|
||||
# and to suppress the message to stderr.
|
||||
args = ["symbolic-ref", "-q", "HEAD"]
|
||||
args = ['symbolic-ref', '-q', 'HEAD']
|
||||
output = cls.run_command(
|
||||
args,
|
||||
extra_ok_returncodes=(1,),
|
||||
|
|
@ -123,13 +128,13 @@ class Git(VersionControl):
|
|||
)
|
||||
ref = output.strip()
|
||||
|
||||
if ref.startswith("refs/heads/"):
|
||||
return ref[len("refs/heads/") :]
|
||||
if ref.startswith('refs/heads/'):
|
||||
return ref[len('refs/heads/'):]
|
||||
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def get_revision_sha(cls, dest: str, rev: str) -> Tuple[Optional[str], bool]:
|
||||
def get_revision_sha(cls, dest: str, rev: str) -> tuple[str | None, bool]:
|
||||
"""
|
||||
Return (sha_or_none, is_branch), where sha_or_none is a commit hash
|
||||
if the revision names a remote branch or tag, otherwise None.
|
||||
|
|
@ -140,31 +145,31 @@ class Git(VersionControl):
|
|||
"""
|
||||
# Pass rev to pre-filter the list.
|
||||
output = cls.run_command(
|
||||
["show-ref", rev],
|
||||
['show-ref', rev],
|
||||
cwd=dest,
|
||||
show_stdout=False,
|
||||
stdout_only=True,
|
||||
on_returncode="ignore",
|
||||
on_returncode='ignore',
|
||||
)
|
||||
refs = {}
|
||||
# NOTE: We do not use splitlines here since that would split on other
|
||||
# unicode separators, which can be maliciously used to install a
|
||||
# different revision.
|
||||
for line in output.strip().split("\n"):
|
||||
line = line.rstrip("\r")
|
||||
for line in output.strip().split('\n'):
|
||||
line = line.rstrip('\r')
|
||||
if not line:
|
||||
continue
|
||||
try:
|
||||
ref_sha, ref_name = line.split(" ", maxsplit=2)
|
||||
ref_sha, ref_name = line.split(' ', maxsplit=2)
|
||||
except ValueError:
|
||||
# Include the offending line to simplify troubleshooting if
|
||||
# this error ever occurs.
|
||||
raise ValueError(f"unexpected show-ref line: {line!r}")
|
||||
raise ValueError(f'unexpected show-ref line: {line!r}')
|
||||
|
||||
refs[ref_name] = ref_sha
|
||||
|
||||
branch_ref = f"refs/remotes/origin/{rev}"
|
||||
tag_ref = f"refs/tags/{rev}"
|
||||
branch_ref = f'refs/remotes/origin/{rev}'
|
||||
tag_ref = f'refs/tags/{rev}'
|
||||
|
||||
sha = refs.get(branch_ref)
|
||||
if sha is not None:
|
||||
|
|
@ -183,7 +188,7 @@ class Git(VersionControl):
|
|||
assumed to be always available locally (which is a normal outcome of
|
||||
``git clone`` and ``git fetch --tags``).
|
||||
"""
|
||||
if rev.startswith("refs/"):
|
||||
if rev.startswith('refs/'):
|
||||
# Always fetch remote refs.
|
||||
return True
|
||||
|
||||
|
|
@ -199,7 +204,7 @@ class Git(VersionControl):
|
|||
|
||||
@classmethod
|
||||
def resolve_revision(
|
||||
cls, dest: str, url: HiddenText, rev_options: RevOptions
|
||||
cls, dest: str, url: HiddenText, rev_options: RevOptions,
|
||||
) -> RevOptions:
|
||||
"""
|
||||
Resolve a revision to a new RevOptions object with the SHA1 of the
|
||||
|
|
@ -234,17 +239,17 @@ class Git(VersionControl):
|
|||
|
||||
# fetch the requested revision
|
||||
cls.run_command(
|
||||
make_command("fetch", "-q", url, rev_options.to_args()),
|
||||
make_command('fetch', '-q', url, rev_options.to_args()),
|
||||
cwd=dest,
|
||||
)
|
||||
# Change the revision to the SHA of the ref we fetched
|
||||
sha = cls.get_revision(dest, rev="FETCH_HEAD")
|
||||
sha = cls.get_revision(dest, rev='FETCH_HEAD')
|
||||
rev_options = rev_options.make_new(sha)
|
||||
|
||||
return rev_options
|
||||
|
||||
@classmethod
|
||||
def is_commit_id_equal(cls, dest: str, name: Optional[str]) -> bool:
|
||||
def is_commit_id_equal(cls, dest: str, name: str | None) -> bool:
|
||||
"""
|
||||
Return whether the current commit hash equals the given name.
|
||||
|
||||
|
|
@ -259,56 +264,56 @@ class Git(VersionControl):
|
|||
return cls.get_revision(dest) == name
|
||||
|
||||
def fetch_new(
|
||||
self, dest: str, url: HiddenText, rev_options: RevOptions, verbosity: int
|
||||
self, dest: str, url: HiddenText, rev_options: RevOptions, verbosity: int,
|
||||
) -> None:
|
||||
rev_display = rev_options.to_display()
|
||||
logger.info("Cloning %s%s to %s", url, rev_display, display_path(dest))
|
||||
logger.info('Cloning %s%s to %s', url, rev_display, display_path(dest))
|
||||
if verbosity <= 0:
|
||||
flags: Tuple[str, ...] = ("--quiet",)
|
||||
flags: tuple[str, ...] = ('--quiet',)
|
||||
elif verbosity == 1:
|
||||
flags = ()
|
||||
else:
|
||||
flags = ("--verbose", "--progress")
|
||||
flags = ('--verbose', '--progress')
|
||||
if self.get_git_version() >= (2, 17):
|
||||
# Git added support for partial clone in 2.17
|
||||
# https://git-scm.com/docs/partial-clone
|
||||
# Speeds up cloning by functioning without a complete copy of repository
|
||||
self.run_command(
|
||||
make_command(
|
||||
"clone",
|
||||
"--filter=blob:none",
|
||||
'clone',
|
||||
'--filter=blob:none',
|
||||
*flags,
|
||||
url,
|
||||
dest,
|
||||
)
|
||||
),
|
||||
)
|
||||
else:
|
||||
self.run_command(make_command("clone", *flags, url, dest))
|
||||
self.run_command(make_command('clone', *flags, url, dest))
|
||||
|
||||
if rev_options.rev:
|
||||
# Then a specific revision was requested.
|
||||
rev_options = self.resolve_revision(dest, url, rev_options)
|
||||
branch_name = getattr(rev_options, "branch_name", None)
|
||||
logger.debug("Rev options %s, branch_name %s", rev_options, branch_name)
|
||||
branch_name = getattr(rev_options, 'branch_name', None)
|
||||
logger.debug('Rev options %s, branch_name %s', rev_options, branch_name)
|
||||
if branch_name is None:
|
||||
# Only do a checkout if the current commit id doesn't match
|
||||
# the requested revision.
|
||||
if not self.is_commit_id_equal(dest, rev_options.rev):
|
||||
cmd_args = make_command(
|
||||
"checkout",
|
||||
"-q",
|
||||
'checkout',
|
||||
'-q',
|
||||
rev_options.to_args(),
|
||||
)
|
||||
self.run_command(cmd_args, cwd=dest)
|
||||
elif self.get_current_branch(dest) != branch_name:
|
||||
# Then a specific branch was requested, and that branch
|
||||
# is not yet checked out.
|
||||
track_branch = f"origin/{branch_name}"
|
||||
track_branch = f'origin/{branch_name}'
|
||||
cmd_args = [
|
||||
"checkout",
|
||||
"-b",
|
||||
'checkout',
|
||||
'-b',
|
||||
branch_name,
|
||||
"--track",
|
||||
'--track',
|
||||
track_branch,
|
||||
]
|
||||
self.run_command(cmd_args, cwd=dest)
|
||||
|
|
@ -316,17 +321,17 @@ class Git(VersionControl):
|
|||
sha = self.get_revision(dest)
|
||||
rev_options = rev_options.make_new(sha)
|
||||
|
||||
logger.info("Resolved %s to commit %s", url, rev_options.rev)
|
||||
logger.info('Resolved %s to commit %s', url, rev_options.rev)
|
||||
|
||||
#: repo may contain submodules
|
||||
self.update_submodules(dest)
|
||||
|
||||
def switch(self, dest: str, url: HiddenText, rev_options: RevOptions) -> None:
|
||||
self.run_command(
|
||||
make_command("config", "remote.origin.url", url),
|
||||
make_command('config', 'remote.origin.url', url),
|
||||
cwd=dest,
|
||||
)
|
||||
cmd_args = make_command("checkout", "-q", rev_options.to_args())
|
||||
cmd_args = make_command('checkout', '-q', rev_options.to_args())
|
||||
self.run_command(cmd_args, cwd=dest)
|
||||
|
||||
self.update_submodules(dest)
|
||||
|
|
@ -335,12 +340,12 @@ class Git(VersionControl):
|
|||
# First fetch changes from the default remote
|
||||
if self.get_git_version() >= (1, 9):
|
||||
# fetch tags in addition to everything else
|
||||
self.run_command(["fetch", "-q", "--tags"], cwd=dest)
|
||||
self.run_command(['fetch', '-q', '--tags'], cwd=dest)
|
||||
else:
|
||||
self.run_command(["fetch", "-q"], cwd=dest)
|
||||
self.run_command(['fetch', '-q'], cwd=dest)
|
||||
# Then reset to wanted revision (maybe even origin/master)
|
||||
rev_options = self.resolve_revision(dest, url, rev_options)
|
||||
cmd_args = make_command("reset", "--hard", "-q", rev_options.to_args())
|
||||
cmd_args = make_command('reset', '--hard', '-q', rev_options.to_args())
|
||||
self.run_command(cmd_args, cwd=dest)
|
||||
#: update submodules
|
||||
self.update_submodules(dest)
|
||||
|
|
@ -356,7 +361,7 @@ class Git(VersionControl):
|
|||
# We need to pass 1 for extra_ok_returncodes since the command
|
||||
# exits with return code 1 if there are no matching lines.
|
||||
stdout = cls.run_command(
|
||||
["config", "--get-regexp", r"remote\..*\.url"],
|
||||
['config', '--get-regexp', r'remote\..*\.url'],
|
||||
extra_ok_returncodes=(1,),
|
||||
show_stdout=False,
|
||||
stdout_only=True,
|
||||
|
|
@ -369,10 +374,10 @@ class Git(VersionControl):
|
|||
raise RemoteNotFoundError
|
||||
|
||||
for remote in remotes:
|
||||
if remote.startswith("remote.origin.url "):
|
||||
if remote.startswith('remote.origin.url '):
|
||||
found_remote = remote
|
||||
break
|
||||
url = found_remote.split(" ")[1]
|
||||
url = found_remote.split(' ')[1]
|
||||
return cls._git_remote_to_pip_url(url.strip())
|
||||
|
||||
@staticmethod
|
||||
|
|
@ -392,7 +397,7 @@ class Git(VersionControl):
|
|||
See the corresponding test test_git_remote_url_to_pip() for examples of
|
||||
sample inputs/outputs.
|
||||
"""
|
||||
if re.match(r"\w+://", url):
|
||||
if re.match(r'\w+://', url):
|
||||
# This is already valid. Pass it though as-is.
|
||||
return url
|
||||
if os.path.exists(url):
|
||||
|
|
@ -402,7 +407,7 @@ class Git(VersionControl):
|
|||
scp_match = SCP_REGEX.match(url)
|
||||
if scp_match:
|
||||
# Add an ssh:// prefix and replace the ':' with a '/'.
|
||||
return scp_match.expand(r"ssh://\1\2/\3")
|
||||
return scp_match.expand(r'ssh://\1\2/\3')
|
||||
# Otherwise, bail out.
|
||||
raise RemoteNotValidError(url)
|
||||
|
||||
|
|
@ -413,7 +418,7 @@ class Git(VersionControl):
|
|||
"""
|
||||
try:
|
||||
cls.run_command(
|
||||
["rev-parse", "-q", "--verify", "sha^" + rev],
|
||||
['rev-parse', '-q', '--verify', 'sha^' + rev],
|
||||
cwd=location,
|
||||
log_failed_cmd=False,
|
||||
)
|
||||
|
|
@ -423,11 +428,11 @@ class Git(VersionControl):
|
|||
return True
|
||||
|
||||
@classmethod
|
||||
def get_revision(cls, location: str, rev: Optional[str] = None) -> str:
|
||||
def get_revision(cls, location: str, rev: str | None = None) -> str:
|
||||
if rev is None:
|
||||
rev = "HEAD"
|
||||
rev = 'HEAD'
|
||||
current_rev = cls.run_command(
|
||||
["rev-parse", rev],
|
||||
['rev-parse', rev],
|
||||
show_stdout=False,
|
||||
stdout_only=True,
|
||||
cwd=location,
|
||||
|
|
@ -435,25 +440,25 @@ class Git(VersionControl):
|
|||
return current_rev.strip()
|
||||
|
||||
@classmethod
|
||||
def get_subdirectory(cls, location: str) -> Optional[str]:
|
||||
def get_subdirectory(cls, location: str) -> str | None:
|
||||
"""
|
||||
Return the path to Python project root, relative to the repo root.
|
||||
Return None if the project root is in the repo root.
|
||||
"""
|
||||
# find the repo root
|
||||
git_dir = cls.run_command(
|
||||
["rev-parse", "--git-dir"],
|
||||
['rev-parse', '--git-dir'],
|
||||
show_stdout=False,
|
||||
stdout_only=True,
|
||||
cwd=location,
|
||||
).strip()
|
||||
if not os.path.isabs(git_dir):
|
||||
git_dir = os.path.join(location, git_dir)
|
||||
repo_root = os.path.abspath(os.path.join(git_dir, ".."))
|
||||
repo_root = os.path.abspath(os.path.join(git_dir, '..'))
|
||||
return find_path_to_project_root_from_repo_root(location, repo_root)
|
||||
|
||||
@classmethod
|
||||
def get_url_rev_and_auth(cls, url: str) -> Tuple[str, Optional[str], AuthInfo]:
|
||||
def get_url_rev_and_auth(cls, url: str) -> tuple[str, str | None, AuthInfo]:
|
||||
"""
|
||||
Prefixes stub URLs like 'user@hostname:user/repo.git' with 'ssh://'.
|
||||
That's required because although they use SSH they sometimes don't
|
||||
|
|
@ -463,21 +468,21 @@ class Git(VersionControl):
|
|||
# Works around an apparent Git bug
|
||||
# (see https://article.gmane.org/gmane.comp.version-control.git/146500)
|
||||
scheme, netloc, path, query, fragment = urlsplit(url)
|
||||
if scheme.endswith("file"):
|
||||
initial_slashes = path[: -len(path.lstrip("/"))]
|
||||
if scheme.endswith('file'):
|
||||
initial_slashes = path[: -len(path.lstrip('/'))]
|
||||
newpath = initial_slashes + urllib.request.url2pathname(path).replace(
|
||||
"\\", "/"
|
||||
).lstrip("/")
|
||||
after_plus = scheme.find("+") + 1
|
||||
'\\', '/',
|
||||
).lstrip('/')
|
||||
after_plus = scheme.find('+') + 1
|
||||
url = scheme[:after_plus] + urlunsplit(
|
||||
(scheme[after_plus:], netloc, newpath, query, fragment),
|
||||
)
|
||||
|
||||
if "://" not in url:
|
||||
assert "file:" not in url
|
||||
url = url.replace("git+", "git+ssh://")
|
||||
if '://' not in url:
|
||||
assert 'file:' not in url
|
||||
url = url.replace('git+', 'git+ssh://')
|
||||
url, rev, user_pass = super().get_url_rev_and_auth(url)
|
||||
url = url.replace("ssh://", "")
|
||||
url = url.replace('ssh://', '')
|
||||
else:
|
||||
url, rev, user_pass = super().get_url_rev_and_auth(url)
|
||||
|
||||
|
|
@ -485,37 +490,37 @@ class Git(VersionControl):
|
|||
|
||||
@classmethod
|
||||
def update_submodules(cls, location: str) -> None:
|
||||
if not os.path.exists(os.path.join(location, ".gitmodules")):
|
||||
if not os.path.exists(os.path.join(location, '.gitmodules')):
|
||||
return
|
||||
cls.run_command(
|
||||
["submodule", "update", "--init", "--recursive", "-q"],
|
||||
['submodule', 'update', '--init', '--recursive', '-q'],
|
||||
cwd=location,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_repository_root(cls, location: str) -> Optional[str]:
|
||||
def get_repository_root(cls, location: str) -> str | None:
|
||||
loc = super().get_repository_root(location)
|
||||
if loc:
|
||||
return loc
|
||||
try:
|
||||
r = cls.run_command(
|
||||
["rev-parse", "--show-toplevel"],
|
||||
['rev-parse', '--show-toplevel'],
|
||||
cwd=location,
|
||||
show_stdout=False,
|
||||
stdout_only=True,
|
||||
on_returncode="raise",
|
||||
on_returncode='raise',
|
||||
log_failed_cmd=False,
|
||||
)
|
||||
except BadCommand:
|
||||
logger.debug(
|
||||
"could not determine if %s is under git control "
|
||||
"because git is not available",
|
||||
'could not determine if %s is under git control '
|
||||
'because git is not available',
|
||||
location,
|
||||
)
|
||||
return None
|
||||
except InstallationError:
|
||||
return None
|
||||
return os.path.normpath(r.rstrip("\r\n"))
|
||||
return os.path.normpath(r.rstrip('\r\n'))
|
||||
|
||||
@staticmethod
|
||||
def should_add_vcs_url_prefix(repo_url: str) -> bool:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue