Skip to content

Commit d7f7502

Browse files
committed
fix: Remotes: handle _remotes via GitRemote
1 parent 87432c5 commit d7f7502

File tree

2 files changed

+88
-31
lines changed

2 files changed

+88
-31
lines changed

libvcs/git.py

+32-20
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class RemoteDict(TypedDict):
130130
push_url: str
131131

132132

133+
GitRepoRemoteDict = Dict[str, GitRemote]
133134
FullRemoteDict = Dict[str, RemoteDict]
134135
RemotesArgs = Union[None, FullRemoteDict, Dict[str, str]]
135136

@@ -192,26 +193,27 @@ def __init__(
192193
if "tls_verify" not in kwargs:
193194
self.tls_verify = False
194195

195-
self._remotes: Union[FullRemoteDict, None]
196+
self._remotes: GitRepoRemoteDict
196197

197198
if remotes is None:
198-
self._remotes: FullRemoteDict = {
199-
"origin": {"fetch_url": url, "push_url": url}
199+
self._remotes: GitRepoRemoteDict = {
200+
"origin": GitRemote(name="origin", fetch_url=url, push_url=url)
200201
}
201202
elif isinstance(remotes, dict):
202-
self._remotes: FullRemoteDict = remotes
203+
self._remotes = {}
203204
for remote_name, url in remotes.items():
204205
if isinstance(url, str):
205-
remotes[remote_name] = {
206-
"fetch_url": url,
207-
"push_url": url,
208-
}
206+
self._remotes[remote_name] = GitRemote(
207+
name=remote_name,
208+
fetch_url=url,
209+
push_url=url,
210+
)
209211

210212
BaseRepo.__init__(self, url, repo_dir, *args, **kwargs)
211213
self.url = (
212-
self._remotes.get("origin")["fetch_url"]
213-
if self._remotes.get("origin")
214-
else next(iter(self._remotes.items()))[1]["fetch_url"]
214+
self._remotes.get("origin").fetch_url
215+
if "origin" in self._remotes
216+
else next(iter(self._remotes.items()))[1].fetch_url
215217
)
216218

217219
@classmethod
@@ -231,32 +233,42 @@ def get_revision(self):
231233
def set_remotes(self, overwrite: bool = False):
232234
remotes = self._remotes
233235
if isinstance(remotes, dict):
234-
for remote_name, url in remotes.items():
236+
for remote_name, git_remote in remotes.items():
237+
235238
existing_remote = self.remote(remote_name)
236-
if isinstance(url, dict) and "fetch_url" in url:
239+
if isinstance(git_remote, GitRemote):
237240
if (
238241
not existing_remote
239-
or existing_remote.fetch_url != url["fetch_url"]
242+
or existing_remote.fetch_url != git_remote.fetch_url
240243
):
241244
self.set_remote(
242-
name=remote_name, url=url["fetch_url"], overwrite=overwrite
245+
name=remote_name,
246+
url=git_remote.fetch_url,
247+
overwrite=overwrite,
243248
)
244249
# refresh if we're setting it, so push can be checked
245250
existing_remote = self.remote(remote_name)
246-
if "push_url" in url:
251+
if git_remote.push_url:
247252
if (
248253
not existing_remote
249-
or existing_remote.push_url != url["push_url"]
254+
or existing_remote.push_url != git_remote.push_url
250255
):
251256
self.set_remote(
252257
name=remote_name,
253-
url=url["push_url"],
258+
url=git_remote.push_url,
254259
push=True,
255260
overwrite=overwrite,
256261
)
257262
else:
258-
if not existing_remote or existing_remote.fetch_url != url:
259-
self.set_remote(name=remote_name, url=url, overwrite=overwrite)
263+
if (
264+
not existing_remote
265+
or existing_remote.fetch_url != git_remote.fetch_url
266+
):
267+
self.set_remote(
268+
name=remote_name,
269+
url=git_remote.fetch_url,
270+
overwrite=overwrite,
271+
)
260272

261273
def obtain(self, *args, **kwargs):
262274
"""Retrieve the repository, clone if doesn't exist."""

tests/test_git.py

+56-11
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,25 @@ def test_remotes(
313313
},
314314
},
315315
lambda git_remote, **kwargs: {
316-
"second_remote": f"file://{git_remote}",
316+
"second_remote": GitRemote(
317+
**{
318+
"name": "second_remote",
319+
"fetch_url": f"file://{git_remote}",
320+
"push_url": f"file://{git_remote}",
321+
}
322+
)
317323
},
318324
lambda git_remote, **kwargs: {
319-
"origin": f"file://{git_remote}",
320-
"second_remote": f"file://{git_remote}",
325+
"origin": GitRemote(
326+
name="origin",
327+
push_url=f"file://{git_remote}",
328+
fetch_url=f"file://{git_remote}",
329+
),
330+
"second_remote": GitRemote(
331+
name="second_remote",
332+
push_url=f"file://{git_remote}",
333+
fetch_url=f"file://{git_remote}",
334+
),
321335
},
322336
],
323337
[
@@ -327,13 +341,48 @@ def test_remotes(
327341
"repo_dir": repos_path / repo_name,
328342
"remotes": {
329343
"origin": f"file://{git_remote}",
344+
# accepts short-hand form since it's inputted in the constructor
345+
"second_remote": f"file://{git_remote}",
330346
},
331347
},
348+
lambda git_remote, **kwargs: {},
332349
lambda git_remote, **kwargs: {
333-
"origin": "https://github.com/vcs-python/libvcs",
350+
"origin": GitRemote(
351+
name="origin",
352+
push_url=f"file://{git_remote}",
353+
fetch_url=f"file://{git_remote}",
354+
),
355+
"second_remote": GitRemote(
356+
name="second_remote",
357+
push_url=f"file://{git_remote}",
358+
fetch_url=f"file://{git_remote}",
359+
),
360+
},
361+
],
362+
[
363+
GitRepo,
364+
lambda git_remote, repos_path, repo_name, **kwargs: {
365+
"url": f"file://{git_remote}",
366+
"repo_dir": repos_path / repo_name,
367+
"remotes": {
368+
"origin": f"file://{git_remote}",
369+
},
370+
},
371+
lambda git_remote, **kwargs: {
372+
"origin": GitRemote(
373+
**{
374+
"name": "second_remote",
375+
"fetch_url": "https://github.com/vcs-python/libvcs",
376+
"push_url": "https://github.com/vcs-python/libvcs",
377+
}
378+
)
334379
},
335380
lambda git_remote, **kwargs: {
336-
"origin": "https://github.com/vcs-python/libvcs",
381+
"origin": GitRemote(
382+
name="origin",
383+
push_url="https://github.com/vcs-python/libvcs",
384+
fetch_url="https://github.com/vcs-python/libvcs",
385+
),
337386
},
338387
],
339388
],
@@ -353,17 +402,13 @@ def test_remotes_update_repo(
353402
git_repo: GitRepo = constructor(**lazy_constructor_options(**locals()))
354403
git_repo.obtain()
355404

356-
git_repo._remotes = lazy_remote_dict(**locals())
405+
git_repo._remotes |= lazy_remote_dict(**locals())
357406
git_repo.update_repo(set_remotes=True)
358407

359408
expected = lazy_remote_expected(**locals())
360409
assert len(expected.keys()) > 0
361410
for expected_remote_name, expected_remote_url in expected.items():
362-
assert (
363-
expected_remote_name,
364-
expected_remote_url,
365-
expected_remote_url,
366-
) == git_repo.remote(expected_remote_name)
411+
assert expected_remote_url == git_repo.remote(expected_remote_name)
367412

368413

369414
def test_git_get_url_and_rev_from_pip_url():

0 commit comments

Comments
 (0)