Skip to content
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

fix: make git ref resolution less greedy for HEAD #7028

Merged
merged 1 commit into from
Mar 2, 2024

Conversation

abn
Copy link
Member

@abn abn commented Nov 14, 2022

This change ensures that when ref HEAD is specified, it is not incorrectly resolved to refs/head/HEAD. HEAD is not treated as a branch, and correctly resolved from the ref spec.

Resolves: #7024
Closes: #6874

neersighted
neersighted previously approved these changes Nov 14, 2022
Copy link
Member

@neersighted neersighted left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit better than the first attempt (and has tests), but I am a bit wary of it still. Do you have any knowledge of where the duplicate clone (with extras) comes from/how we can prevent it?

@dimbleby
Copy link
Contributor

We clone the package twice in spite of the cache on

@functools.lru_cache(maxsize=None)
def _get_package_from_git(
url: str,
branch: str | None = None,
tag: str | None = None,
rev: str | None = None,
subdirectory: str | None = None,
source_root: Path | None = None,
) -> Package:
because the first time round rev is None and the second time it is "HEAD".

From that point of view the alternative fix that I tried in #6874 (comment) does better, but perhaps it's the wrong direction or has other disadvantages.

@neersighted
Copy link
Member

Right, that makes more sense. What do we think about introducing a new datatype here to avoid the overloaded strings and to encapsulate the HEAD == None logic? The tests look good; we just need to realize that None and HEAD are the same thing (and to avoid the entropy of a string conversion from HEAD causing the cache to miss and clone a second time).

@abn
Copy link
Member Author

abn commented Nov 14, 2022

@neersighted I kind of feel that adding a new datatype here might be overkill at present, but happy to be told otherwise. Alternatively, we could do something like this.

cache-fix.patch

diff --git a/src/poetry/puzzle/provider.py b/src/poetry/puzzle/provider.py
index 4d2e2d30..97abfb8c 100644
--- a/src/poetry/puzzle/provider.py
+++ b/src/poetry/puzzle/provider.py
@@ -86,7 +86,7 @@ class Indicator(ProgressIndicator):  # type: ignore[misc]
 
 
 @functools.lru_cache(maxsize=None)
-def _get_package_from_git(
+def _get_package_from_git_cached(
     url: str,
     branch: str | None = None,
     tag: str | None = None,
@@ -118,6 +118,24 @@ def _get_package_from_git(
     return package
 
 
+def _get_package_from_git(
+    url: str,
+    branch: str | None = None,
+    tag: str | None = None,
+    rev: str | None = None,
+    subdirectory: str | None = None,
+    source_root: Path | None = None,
+) -> Package:
+    return _get_package_from_git_cached(
+            url,
+            branch if branch != "HEAD" else None,
+            tag,
+            rev if rev != "HEAD" else None,
+            subdirectory,
+            source_root,
+        )
+
+
 class Provider:
     UNSAFE_PACKAGES: set[str] = set()
 

Either way, I can raise another PR to deal with the double clone issue. Let's solve the functionality issue by merging this?

Edit: Also there already is

class GitRefSpec:

@dimbleby
Copy link
Contributor

dimbleby commented Jan 2, 2023

cf python-poetry/poetry-plugin-export#172 in which the HEAD reference is again escaping where it oughtn't.

I wonder whether HEAD simply isn't a good reference to use and this fix is just papering over that (and requiring more of the same elsewhere).

Perhaps the alternative at #6874 (comment) is better after all - though I seem to recall there was more unit test noise to work through in that case.

@Kuba314
Copy link

Kuba314 commented Mar 24, 2023

Is there any plan to merge / fix this?

@abn
Copy link
Member Author

abn commented Feb 23, 2024

@Secrus I have rebased this and fixed the mypy issue.

@abn abn requested a review from neersighted February 23, 2024 22:02
@Secrus Secrus removed their assignment Feb 24, 2024
@abn abn force-pushed the dulwich/fix-head branch from 7e35159 to ae0bd34 Compare February 25, 2024 13:41
@abn abn requested review from a team and removed request for neersighted February 25, 2024 13:41
Copy link
Member

@Secrus Secrus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

tests/integration/test_utils_vcs_git.py Show resolved Hide resolved
This change ensures that when ref `HEAD` is specified, it is not incorrectly
resolved to `refs/head/HEAD`. `HEAD` is not treated as a branch, and correctly
resolved from the ref spec.

Resolves: python-poetry#7024
Closes: python-poetry#6874
@abn abn force-pushed the dulwich/fix-head branch from ae0bd34 to f26d726 Compare March 2, 2024 13:21
@abn abn enabled auto-merge (rebase) March 2, 2024 13:22
@abn abn merged commit 0753fb0 into python-poetry:main Mar 2, 2024
20 checks passed
@abn abn deleted the dulwich/fix-head branch March 2, 2024 13:28
Copy link

github-actions bot commented Apr 2, 2024

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

VCS dependency with extras requires explicit branch (due to double clone)
5 participants