Skip to content

Commit cfdaabf

Browse files
committed
fix #99: allow no git remote
1 parent b569790 commit cfdaabf

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

src/tap/tap.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def configure(self) -> None:
370370
pass
371371

372372
@staticmethod
373-
def get_reproducibility_info(repo_path: Optional[PathLike] = None) -> Dict[str, str]:
373+
def get_reproducibility_info(repo_path: Optional[PathLike] = None) -> Dict[str, Optional[str]]:
374374
"""Gets a dictionary of reproducibility information.
375375
376376
Reproducibility information always includes:
@@ -380,7 +380,8 @@ def get_reproducibility_info(repo_path: Optional[PathLike] = None) -> Dict[str,
380380
If git is installed, reproducibility information also includes:
381381
- git_root: The root of the git repo where the command is run.
382382
- git_url: The url of the current hash of the git repo where the command is run.
383-
Ex. https://github.com/swansonk14/rationale-alignment/tree/<hash>
383+
Ex. https://github.com/swansonk14/rationale-alignment/tree/<hash>.
384+
If it is a local repo, the url is None.
384385
- git_has_uncommitted_changes: Whether the current git repo has uncommitted changes.
385386
386387
:param repo_path: Path to the git repo to examine for reproducibility info.

src/tap/utils.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,40 @@ def get_git_root(self) -> str:
7070
"""
7171
return check_output(["git", "rev-parse", "--show-toplevel"], cwd=self.repo_path)
7272

73-
def get_git_url(self, commit_hash: bool = True) -> str:
73+
def get_git_version(self) -> tuple:
74+
"""Gets the version of git.
75+
76+
:return: The version of git, as a tuple of strings.
77+
78+
Example:
79+
>>> get_git_version()
80+
(2, 17, 1) # for git version 2.17.1
81+
"""
82+
raw = check_output(["git", "--version"])
83+
number_start_index = next(i for i, c in enumerate(raw) if c.isdigit())
84+
return tuple(int(num) for num in raw[number_start_index:].split("."))
85+
86+
def get_git_url(self, commit_hash: bool = True) -> Optional[str]:
7487
"""Gets the https url of the git repo where the command is run.
7588
7689
:param commit_hash: If True, the url links to the latest local git commit hash.
7790
If False, the url links to the general git url.
7891
:return: The https url of the current git repo.
7992
"""
8093
# Get git url (either https or ssh)
94+
input_remote = (
95+
["git", "remote", "get-url", "origin"]
96+
if self.get_git_version() >= (2, 0)
97+
else ["git", "config", "--get", "remote.origin.url"]
98+
)
8199
try:
82-
url = check_output(["git", "remote", "get-url", "origin"], cwd=self.repo_path)
83-
except subprocess.CalledProcessError:
84-
# For git versions <2.0
85-
url = check_output(["git", "config", "--get", "remote.origin.url"], cwd=self.repo_path)
100+
url = check_output(input_remote, cwd=self.repo_path)
101+
except subprocess.CalledProcessError as e:
102+
if e.returncode == 2:
103+
# https://git-scm.com/docs/git-remote#_exit_status
104+
# 2: The remote does not exist.
105+
return None
106+
raise e
86107

87108
# Remove .git at end
88109
url = url[: -len(".git")]

tests/test_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ def test_get_git_url_ssh_hash_enterprise(self) -> None:
108108
url = f"{true_url}/tree/"
109109
self.assertEqual(self.git_info.get_git_url(commit_hash=True)[: len(url)], url)
110110

111+
def test_get_git_url_no_remote(self) -> None:
112+
subprocess.run(["git", "remote", "remove", "origin"])
113+
self.assertIsNone(self.git_info.get_git_url())
114+
115+
def test_get_git_version(self) -> None:
116+
git_version = self.git_info.get_git_version()
117+
self.assertEqual(len(git_version), 3)
118+
self.assertIsInstance(git_version, tuple)
119+
for v in git_version:
120+
self.assertIsInstance(v, int)
121+
111122
def test_has_uncommitted_changes_false(self) -> None:
112123
self.assertFalse(self.git_info.has_uncommitted_changes())
113124

0 commit comments

Comments
 (0)