Cloning with remote callback results in HEAD pointing to user's `init.defaultBranch`
還沒有人認領這個 Issue。
評估
研究方向
從提供的 pygit2.clone_repository 呼叫及其 remote_cb 回呼開始,分別重現有回呼和無回呼時的行為。在回呼執行前檢查 /tmp/pygit2/.git/HEAD,並將其與複製之儲存庫的預設分支進行比較。完成標準是:使用回呼進行複製時,HEAD 指向複製之儲存庫的預設分支,而不是 init.defaultBranch;並且在所有測試複製行為的地方新增 coverage。
由索引模型根據 Issue 內容生成。
描述
Cloning with a remote callback results in HEAD pointing to the user's init.defaultBranch instead of the default branch of the cloned repository. See repro below.
Looks like an upstream bug to me, just confirming with you... thanks :)
I'll also note that HEAD already contains the wrong reference before the callback is executed. (Reproduce this e.g. by placing a breakpoint() in remote_cb below, and inspecting HEAD from the debugger prompt.) Still, the issue does not reproduce without the callback.
Repro
❯ docker run --rm -ti python bash
root@e747d803b7f3:/# pip install pygit2
Collecting pygit2
Downloading pygit2-1.5.0-cp39-cp39-manylinux2014_x86_64.whl (3.1 MB)
|████████████████████████████████| 3.1 MB 2.4 MB/s
Collecting cffi>=1.4.0
Downloading cffi-1.14.5-cp39-cp39-manylinux1_x86_64.whl (406 kB)
|████████████████████████████████| 406 kB 15.7 MB/s
Collecting cached-property
Downloading cached_property-1.5.2-py2.py3-none-any.whl (7.6 kB)
Collecting pycparser
Downloading pycparser-2.20-py2.py3-none-any.whl (112 kB)
|████████████████████████████████| 112 kB 17.0 MB/s
Installing collected packages: pycparser, cffi, cached-property, pygit2
Successfully installed cached-property-1.5.2 cffi-1.14.5 pycparser-2.20 pygit2-1.5.0
root@e747d803b7f3:/# cat >> ~/.gitconfig
[init]
defaultBranch = teapot
root@e747d803b7f3:/# python
Python 3.9.4 (default, Apr 10 2021, 15:31:19)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygit2
>>> def remote_cb(repo, name, url):
... n = name.decode()
... repo.config[f"remote.{n}.mirror"] = True
... return repo.remotes.create(name, url, "+refs/*:refs/*")
...
>>> repo = pygit2.clone_repository("https://github.com/libgit2/pygit2", "/tmp/pygit2", remote=remote_cb)
>>> repo.head
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
_pygit2.GitError: reference 'refs/heads/teapot' not found
>>> print(open("/tmp/pygit2/.git/HEAD").read())
ref: refs/heads/teapot
Workaround
import contextlib
import pygit2
def _fix_repository_head(repository: pygit2.Repository) -> pygit2.Reference:
"""Work around a bug in libgit2 resulting in a bogus HEAD reference.
Cloning with a remote callback results in HEAD pointing to the user's
`init.defaultBranch` instead of the default branch of the cloned repository.
"""
head = repository.references["HEAD"]
with contextlib.suppress(KeyError):
return head.resolve()
for branch in ["main", "master"]:
ref = f"refs/heads/{branch}"
if head.target != ref and ref in repository.references:
head.set_target(ref, message="repair broken HEAD after clone")
return head.resolve()
- 主要語言
- Python
- 星號
- 1.7k
- 分支
- 408
- 平均合併
- 1 天 15 小時
- 30 天內合併 PR
- 7
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
libgit2/pygit2 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 72/100
-
難度 1/5 1 小時以內 新手友好度 88/100
-
Feature Request: Support app-level temporary config overrides using libgit2's backend supprt可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉
難度 4/5 3-5 天 新手友好度 45/100
-
難度 3/5 1-2 天 新手友好度 48/100
-
難度 3/5 1-2 天 新手友好度 55/100
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 68/100
維護者通常 3 天內回覆
-
難度 2/5 1-3 小時 新手友好度 88/100
modelcontextprotocol/python-sdk#3648 ·
維護者通常 1 天內回覆
-
docs good first issue
難度 2/5 1-3 小時 新手友好度 78/100
VenetoStato/giorgio#6 ·
-
難度 2/5 1-3 小時 新手友好度 74/100
維護者通常 1 天內回覆
-
難度 1/5 1 小時以內 新手友好度 70/100
EclipseFdn/open-vsx.org#13831 ·
維護者通常 1 天內回覆