Skip to content

Commit

Permalink
Fix ImportError when git is missing (#1359)
Browse files Browse the repository at this point in the history
  • Loading branch information
aniezurawski committed Jun 15, 2023
1 parent 9854ad4 commit 0cfeb86
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,8 @@
## neptune 1.3.1

### Fixes
- Fix ImportError when git is missing ([#1359](https://github.com/neptune-ai/neptune-client/pull/1359))

## neptune 1.3.0

### Features
Expand Down
67 changes: 40 additions & 27 deletions src/neptune/internal/utils/git.py
Expand Up @@ -30,13 +30,6 @@
Union,
)

import git
from git.exc import (
GitCommandError,
InvalidGitRepositoryError,
NoSuchPathError,
)

from neptune.attributes.constants import (
DIFF_HEAD_INDEX_PATH,
UPSTREAM_INDEX_DIFF,
Expand All @@ -48,6 +41,8 @@
)

if TYPE_CHECKING:
import git

from neptune import Run

_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -76,7 +71,7 @@ def get_git_repo(repo_path):
warnings.warn("GitPython could not be initialized")


def get_repo_from_git_ref(git_ref: Union[GitRef, GitRefDisabled]) -> Optional[git.Repo]:
def get_repo_from_git_ref(git_ref: Union[GitRef, GitRefDisabled]) -> Optional["git.Repo"]:
if git_ref == GitRef.DISABLED:
return None

Expand All @@ -85,8 +80,16 @@ def get_repo_from_git_ref(git_ref: Union[GitRef, GitRefDisabled]) -> Optional[gi
return None

try:
return get_git_repo(repo_path=initial_repo_path)
except (NoSuchPathError, InvalidGitRepositoryError):
from git.exc import (
InvalidGitRepositoryError,
NoSuchPathError,
)

try:
return get_git_repo(repo_path=initial_repo_path)
except (NoSuchPathError, InvalidGitRepositoryError):
return None
except ImportError:
return None


Expand Down Expand Up @@ -126,14 +129,19 @@ class UncommittedChanges:
upstream_sha: Optional[str]


def get_diff(repo: git.Repo, commit_ref: str) -> Optional[str]:
def get_diff(repo: "git.Repo", commit_ref: str) -> Optional[str]:
try:
return repo.git.diff(commit_ref)
except GitCommandError:
return
from git.exc import GitCommandError

try:
return repo.git.diff(commit_ref)
except GitCommandError:
return
except ImportError:
return None


def get_relevant_upstream_commit(repo: git.Repo) -> Optional[git.Commit]:
def get_relevant_upstream_commit(repo: "git.Repo") -> Optional["git.Commit"]:
try:
tracking_branch = repo.active_branch.tracking_branch()
except (TypeError, ValueError):
Expand All @@ -145,31 +153,36 @@ def get_relevant_upstream_commit(repo: git.Repo) -> Optional[git.Commit]:
return search_for_most_recent_ancestor(repo)


def search_for_most_recent_ancestor(repo: git.Repo) -> Optional[git.Commit]:
most_recent_ancestor: Optional[git.Commit] = None
def search_for_most_recent_ancestor(repo: "git.Repo") -> Optional["git.Commit"]:
most_recent_ancestor: Optional["git.Commit"] = None

try:
for branch in repo.heads:
tracking_branch = branch.tracking_branch()
if tracking_branch:
for ancestor in repo.merge_base(repo.head, tracking_branch.commit):
if not most_recent_ancestor or repo.is_ancestor(most_recent_ancestor, ancestor):
most_recent_ancestor = ancestor
except GitCommandError:
pass
from git.exc import GitCommandError

try:
for branch in repo.heads:
tracking_branch = branch.tracking_branch()
if tracking_branch:
for ancestor in repo.merge_base(repo.head, tracking_branch.commit):
if not most_recent_ancestor or repo.is_ancestor(most_recent_ancestor, ancestor):
most_recent_ancestor = ancestor
except GitCommandError:
pass
except ImportError:
return None

return most_recent_ancestor


def get_upstream_index_sha(repo: git.Repo) -> Optional[str]:
def get_upstream_index_sha(repo: "git.Repo") -> Optional[str]:
upstream_commit = get_relevant_upstream_commit(repo)

if upstream_commit and upstream_commit != repo.head.commit:

return upstream_commit.hexsha


def get_uncommitted_changes(repo: Optional[git.Repo]) -> Optional[UncommittedChanges]:
def get_uncommitted_changes(repo: Optional["git.Repo"]) -> Optional[UncommittedChanges]:
if not repo.is_dirty():
return

Expand Down

0 comments on commit 0cfeb86

Please sign in to comment.