Skip to content

Commit

Permalink
Try finding commit using short SHA if it is not on HEAD (#9748)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhainin authored Nov 18, 2024
1 parent 31d18de commit d09f238
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 112 deletions.
99 changes: 47 additions & 52 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ poetry-core = { git = "https://github.com/python-poetry/poetry-core.git", branch
build = "^1.2.1"
cachecontrol = { version = "^0.14.0", extras = ["filecache"] }
cleo = "^2.2.1"
dulwich = "^0.22.1"
dulwich = "^0.22.6"
fastjsonschema = "^2.18.0"
importlib-metadata = { version = ">=4.4", python = "<3.10" }
installer = "^0.7.0"
Expand Down Expand Up @@ -189,6 +189,7 @@ addopts = [ "-n", "logical", "-ra", "--strict-config", "--strict-markers" ]
testpaths = ["tests"]
markers = [
"network: mark tests that require internet access",
"skip_git_mock: mark tests that should not auto-apply git_mock"
]
log_cli_level = "INFO"
xfail_strict = true
Expand Down
23 changes: 10 additions & 13 deletions src/poetry/vcs/git/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ class GitRefSpec:
tag: str | None = None
ref: bytes = dataclasses.field(default_factory=lambda: b"HEAD")

def resolve(self, remote_refs: FetchPackResult) -> None:
def resolve(self, remote_refs: FetchPackResult, repo: Repo) -> None:
"""
Resolve the ref using the provided remote refs.
"""
self._normalise(remote_refs=remote_refs)
self._normalise(remote_refs=remote_refs, repo=repo)
self._set_head(remote_refs=remote_refs)

def _normalise(self, remote_refs: FetchPackResult) -> None:
def _normalise(self, remote_refs: FetchPackResult, repo: Repo) -> None:
"""
Internal helper method to determine if given revision is
1. a branch or tag; if so, set corresponding properties.
Expand Down Expand Up @@ -98,7 +98,12 @@ def _normalise(self, remote_refs: FetchPackResult) -> None:
for sha in remote_refs.refs.values():
if sha.startswith(short_sha):
self.revision = sha.decode("utf-8")
break
return

# no heads with such SHA, let's check all objects
for sha in repo.object_store.iter_prefix(short_sha):
self.revision = sha.decode("utf-8")
return

def _set_head(self, remote_refs: FetchPackResult) -> None:
"""
Expand Down Expand Up @@ -269,7 +274,7 @@ def _clone(cls, url: str, refspec: GitRefSpec, target: Path) -> Repo:
)

try:
refspec.resolve(remote_refs=remote_refs)
refspec.resolve(remote_refs=remote_refs, repo=local)
except KeyError: # branch / ref does not exist
raise PoetryConsoleError(
f"Failed to clone {url} at '{refspec.key}', verify ref exists on"
Expand Down Expand Up @@ -313,14 +318,6 @@ def _clone(cls, url: str, refspec: GitRefSpec, target: Path) -> Repo:
if isinstance(e, AssertionError) and "Invalid object name" not in str(e):
raise

logger.debug(
"\nRequested ref (<c2>%s</c2>) was not fetched to local copy and"
" cannot be used. The following error was"
" raised:\n\n\t<warning>%s</>",
refspec.key,
e,
)

raise PoetryConsoleError(
f"Failed to clone {url} at '{refspec.key}', verify ref exists on"
" remote."
Expand Down
6 changes: 5 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from keyring.credentials import SimpleCredential
from keyring.errors import KeyringError
from keyring.errors import KeyringLocked
from pytest import FixtureRequest

from poetry.config.config import Config as BaseConfig
from poetry.config.dict_config_source import DictConfigSource
Expand Down Expand Up @@ -323,7 +324,10 @@ def isolate_environ() -> Iterator[None]:


@pytest.fixture(autouse=True)
def git_mock(mocker: MockerFixture) -> None:
def git_mock(mocker: MockerFixture, request: FixtureRequest) -> None:
if request.node.get_closest_marker("skip_git_mock"):
return

# Patch git module to not actually clone projects
mocker.patch("poetry.vcs.git.Git.clone", new=mock_clone)
p = mocker.patch("poetry.vcs.git.Git.get_revision")
Expand Down
Loading

0 comments on commit d09f238

Please sign in to comment.