Skip to content

Commit af67584

Browse files
committed
Add mirror support when git pull.
1 parent 8ab8bae commit af67584

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

rubisco/kernel/git.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from pathlib import Path
2323

2424
from git.exc import GitError
25+
from git.remote import Remote
2526
from git.repo import Repo
2627
from git.util import RemoteProgress
2728

@@ -121,12 +122,20 @@ def is_git_repo(path: Path) -> bool:
121122
return True
122123

123124

124-
def git_update(path: Path, branch: str = "main") -> None:
125+
def git_update(
126+
path: Path,
127+
branch: str = "main",
128+
*,
129+
protocol: str = "http",
130+
use_fastest: bool = True,
131+
) -> None:
125132
"""Update a git repository.
126133
127134
Args:
128135
path (Path): Path to the repository.
129136
branch (str, optional): Branch to update. Defaults to "main".
137+
protocol (str, optional): Protocol to use. Defaults to "http".
138+
use_fastest (bool, optional): Use the fastest mirror. Defaults to True.
130139
131140
"""
132141
if not path.exists():
@@ -152,8 +161,26 @@ def git_update(path: Path, branch: str = "main") -> None:
152161
)
153162
try:
154163
repo = Repo(path)
155-
origin = repo.remote("origin")
156-
origin.pull()
164+
rubisco_url = Remote(repo, "rubisco-url")
165+
if rubisco_url:
166+
mirror_url = get_url(
167+
rubisco_url.url,
168+
protocol=protocol,
169+
use_fastest=use_fastest,
170+
)
171+
if not Remote(repo, "mirror").exists():
172+
repo.create_remote("mirror", mirror_url)
173+
else:
174+
repo.remote("mirror").set_url(mirror_url)
175+
mirror = repo.remote("mirror")
176+
mirror.pull(branch)
177+
# Set upstream to origin.
178+
repo.branches[branch].set_tracking_branch(
179+
repo.remotes.origin.refs[branch],
180+
)
181+
else:
182+
origin = repo.remote("origin")
183+
origin.pull()
157184
except GitError as exc:
158185
logger.error("Git operation failed: %s", str(exc))
159186
raise RUError(
@@ -201,7 +228,7 @@ def git_clone( # pylint: disable=R0913, R0917 # noqa: PLR0913
201228
),
202229
)
203230
logger.warning("Repository already exists, Updating...")
204-
git_update(path, branch)
231+
git_update(path, branch, protocol=protocol, use_fastest=use_fastest)
205232
return
206233

207234
old_url = url
@@ -235,6 +262,7 @@ def git_clone( # pylint: disable=R0913, R0917 # noqa: PLR0913
235262
if old_url != url: # Reset the origin URL to official.
236263
origin_url = get_url(old_url, protocol=protocol, use_fastest=False)
237264
repo.remote("origin").set_url(origin_url)
265+
repo.create_remote("rubisco-url", old_url)
238266
repo.create_remote("mirror", url)
239267
# Set the upstream branch.
240268
repo.branches[branch].set_tracking_branch(

0 commit comments

Comments
 (0)