Skip to content

Commit

Permalink
fix swansonk14#99: allow no git remote
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-ma committed Jul 11, 2024
1 parent b569790 commit cfdaabf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/tap/tap.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def configure(self) -> None:
pass

@staticmethod
def get_reproducibility_info(repo_path: Optional[PathLike] = None) -> Dict[str, str]:
def get_reproducibility_info(repo_path: Optional[PathLike] = None) -> Dict[str, Optional[str]]:
"""Gets a dictionary of reproducibility information.
Reproducibility information always includes:
Expand All @@ -380,7 +380,8 @@ def get_reproducibility_info(repo_path: Optional[PathLike] = None) -> Dict[str,
If git is installed, reproducibility information also includes:
- git_root: The root of the git repo where the command is run.
- git_url: The url of the current hash of the git repo where the command is run.
Ex. https://github.com/swansonk14/rationale-alignment/tree/<hash>
Ex. https://github.com/swansonk14/rationale-alignment/tree/<hash>.
If it is a local repo, the url is None.
- git_has_uncommitted_changes: Whether the current git repo has uncommitted changes.
:param repo_path: Path to the git repo to examine for reproducibility info.
Expand Down
31 changes: 26 additions & 5 deletions src/tap/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,40 @@ def get_git_root(self) -> str:
"""
return check_output(["git", "rev-parse", "--show-toplevel"], cwd=self.repo_path)

def get_git_url(self, commit_hash: bool = True) -> str:
def get_git_version(self) -> tuple:
"""Gets the version of git.
:return: The version of git, as a tuple of strings.
Example:
>>> get_git_version()
(2, 17, 1) # for git version 2.17.1
"""
raw = check_output(["git", "--version"])
number_start_index = next(i for i, c in enumerate(raw) if c.isdigit())
return tuple(int(num) for num in raw[number_start_index:].split("."))

def get_git_url(self, commit_hash: bool = True) -> Optional[str]:
"""Gets the https url of the git repo where the command is run.
:param commit_hash: If True, the url links to the latest local git commit hash.
If False, the url links to the general git url.
:return: The https url of the current git repo.
"""
# Get git url (either https or ssh)
input_remote = (
["git", "remote", "get-url", "origin"]
if self.get_git_version() >= (2, 0)
else ["git", "config", "--get", "remote.origin.url"]
)
try:
url = check_output(["git", "remote", "get-url", "origin"], cwd=self.repo_path)
except subprocess.CalledProcessError:
# For git versions <2.0
url = check_output(["git", "config", "--get", "remote.origin.url"], cwd=self.repo_path)
url = check_output(input_remote, cwd=self.repo_path)
except subprocess.CalledProcessError as e:
if e.returncode == 2:
# https://git-scm.com/docs/git-remote#_exit_status
# 2: The remote does not exist.
return None
raise e

# Remove .git at end
url = url[: -len(".git")]
Expand Down
11 changes: 11 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ def test_get_git_url_ssh_hash_enterprise(self) -> None:
url = f"{true_url}/tree/"
self.assertEqual(self.git_info.get_git_url(commit_hash=True)[: len(url)], url)

def test_get_git_url_no_remote(self) -> None:
subprocess.run(["git", "remote", "remove", "origin"])
self.assertIsNone(self.git_info.get_git_url())

def test_get_git_version(self) -> None:
git_version = self.git_info.get_git_version()
self.assertEqual(len(git_version), 3)
self.assertIsInstance(git_version, tuple)
for v in git_version:
self.assertIsInstance(v, int)

def test_has_uncommitted_changes_false(self) -> None:
self.assertFalse(self.git_info.has_uncommitted_changes())

Expand Down

0 comments on commit cfdaabf

Please sign in to comment.