Skip to content

Commit

Permalink
Fix ImportError when git is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
aniezurawski committed Jun 15, 2023
1 parent 9854ad4 commit daecdd5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 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
53 changes: 33 additions & 20 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 @@ -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 @@ -128,9 +131,14 @@ class UncommittedChanges:

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]:
Expand All @@ -149,14 +157,19 @@ 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

Expand Down

0 comments on commit daecdd5

Please sign in to comment.