Skip to content

Commit

Permalink
Fix caching for repos without remotes
Browse files Browse the repository at this point in the history
  • Loading branch information
lakesare committed Jun 27, 2021
1 parent 5a9312d commit 9d0e221
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
22 changes: 15 additions & 7 deletions sciunit/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,22 +211,30 @@ def get_version(self, cached: bool = True) -> str:

version = property(get_version)

def get_remote(self, remote: str = "origin") -> Remote:
def get_remote(self, remoteName: str = "origin") -> Remote:
"""Get a git remote object for this instance.
Args:
remote (str, optional): The remote Git repo. Defaults to 'origin'.
remoteName (str, optional): The remote Git repo. Defaults to 'origin'.
Returns:
Remote: The git remote object for this instance.
"""
repo = self.get_repo()
if repo is not None:
remotes = {r.name: r for r in repo.remotes}
r = repo.remotes[0] if remote not in remotes else remotes[remote]

if repo is None:
return None

if len(repo.remotes) == 0:
return None

matching = [r for r in repo.remotes if r.name == remoteName]
if (len(matching) > 0):
# => Remote with remoteName
return matching[0]
else:
r = None
return r
# => just any first Remote
return repo.remotes[0]

def get_remote_url(self, remote: str = "origin", cached: bool = True) -> str:
"""Get a git remote URL for this instance.
Expand Down
6 changes: 4 additions & 2 deletions sciunit/unit_test/base_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ def test_Versioned(self):
from sciunit.base import Versioned

ver = Versioned()
self.assertEqual("origin", str(ver.get_remote("I am not a remote")))
self.assertEqual("origin", str(ver.get_remote()))
# We check our sciunit .git repo here (and we do have a remote named "origin"!)
self.assertEqual("origin", ver.get_remote("I am not a remote").name)
self.assertEqual("origin", ver.get_remote().name)

self.assertIsInstance(ver.get_repo(), Repo)
self.assertIsInstance(ver.get_remote_url("I am not a remote"), str)

Expand Down

0 comments on commit 9d0e221

Please sign in to comment.