Skip to content

Commit

Permalink
addressing pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rpmcginty committed May 15, 2024
1 parent ee3994e commit 09d791b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 54 deletions.
75 changes: 27 additions & 48 deletions src/aibs_informatics_cdk_lib/common/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ def is_local_repo(repo_path: Union[str, Path]) -> bool:
bool: True if the path is a local Git repository, False otherwise.
"""
repo_path = Path(repo_path)
return (repo_path / ".git").is_dir()
try:
subprocess.check_output(["git", "-C", repo_path, "rev-parse", "--is-inside-work-tree"])
return True
except subprocess.CalledProcessError:
return False


def get_commit_hash(repo_url_or_path: Union[str, Path]) -> str:
Expand Down Expand Up @@ -127,64 +131,39 @@ def get_repo_name(repo_url_or_path: Union[str, Path]) -> str:
str: The name of the repository.
"""
if isinstance(repo_url_or_path, str) and is_repo_url(repo_url_or_path):
return get_repo_name_from_url(repo_url_or_path)
return GitUrl(repo_url_or_path).repo_name
elif is_local_repo(repo_url_or_path):
repo_path = Path(repo_url_or_path)
return get_repo_name_from_local(repo_path)
else:
raise ValueError("The input must be a string or a Path object.")


def get_repo_name_from_url(repo_url: str) -> str:
"""
Extracts the repository name from a Git repository URL.
Args:
repo_url (str): The URL of the Git repository.
Returns:
str: The name of the repository.
"""

# Extract the repository name
return GitUrl(repo_url).repo_name


def get_repo_name_from_local(repo_path: Union[str, Path]) -> str:
"""
Extracts the repository name from a local Git repository clone.
try:
# Get the remote URL of the 'origin' remote (commonly used name for the default remote)
remote_url = (
subprocess.check_output(
["git", "config", "--get", "remote.origin.url"], cwd=repo_path
)
.decode("utf-8")
.strip()
)

Args:
repo_path (Path): The file system path to the local Git repository.
# Strip trailing slashes or .git if present
remote_url = remote_url.rstrip("/").rstrip(".git")

Returns:
str: The name of the repository, or None if it cannot be determined.
"""
try:
# Get the remote URL of the 'origin' remote (commonly used name for the default remote)
remote_url = (
subprocess.check_output(["git", "config", "--get", "remote.origin.url"], cwd=repo_path)
.decode("utf-8")
.strip()
)

# Strip trailing slashes or .git if present
remote_url = remote_url.rstrip("/").rstrip(".git")
# Extract the repository name
repo_name = os.path.basename(remote_url)

# Extract the repository name
repo_name = os.path.basename(remote_url)
return repo_name
except subprocess.CalledProcessError as e:
logger.error(f"An error occurred: {e}")
raise e

return repo_name
except subprocess.CalledProcessError as e:
logger.error(f"An error occurred: {e}")
raise e
else:
raise ValueError("The input must be a string or a Path object.")


def construct_repo_path(repo_url: str, target_dir: Optional[Union[str, Path]] = None) -> Path:
target_dir = Path(target_dir) if target_dir else Path(tempfile.gettempdir())

repo_name = get_repo_name_from_url(repo_url)
repo_commit_hash = get_commit_hash_from_url(repo_url)
repo_name = get_repo_name(repo_url)
repo_commit_hash = get_commit_hash(repo_url)

target_base_name = f"{repo_name}_{repo_commit_hash}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ def AIBS_INFORMATICS_AWS_LAMBDA(self) -> CodeAsset:
*PYTHON_GLOB_EXCLUDES,
"**/cdk.out/",
"**/scripts/**",
"gcs-docker-tools/**",
"gcs-cli",
],
# ignore_mode=cdk.IgnoreMode.GIT,
bundling=cdk.BundlingOptions(
image=bundling_image,
working_directory=f"/asset-input",
Expand Down
6 changes: 3 additions & 3 deletions test/aibs_informatics_cdk_lib/common/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ def test__is_repo_url(self):
assert not is_repo_url(self.tmp_path().as_posix())

def test__is_local_repo(self):
assert not is_local_repo(self.GIT_URL_HTTPS)
p = self.tmp_path()
(p / ".git").mkdir()
p = clone_repo(self.GIT_URL_HTTPS, self.tmp_path())
assert is_local_repo(p.as_posix())
assert not is_local_repo(p.parent.as_posix())
assert not is_local_repo(self.GIT_URL_HTTPS)

def test__get_repo_name__works_for_url(self):
repo_name = get_repo_name(self.GIT_URL_HTTPS)
Expand Down

0 comments on commit 09d791b

Please sign in to comment.