-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
repo defer importing git until required
Many use use cases for augpathlib do not involve RepoPath so we defer importing git until the first time a RepoPath is instantiated. I'm sure that there is some tradeoff that will come back to haunt me by doing this, but forcing anyone who wants to use a library to pay the worst case time cost for the maximal use case seems at least as bad.
- Loading branch information
Showing
2 changed files
with
84 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import git | ||
|
||
|
||
class _Repo(git.Repo): # FIXME should we subclass Repo for this or patch ?? | ||
""" monkey patching """ | ||
|
||
def getRef(self, ref_name): | ||
for ref in self.refs: | ||
if ref.name == ref_name: | ||
return ref | ||
|
||
else: | ||
raise ValueError(f'No ref with name: {ref_name}') | ||
|
||
|
||
# monkey patch git.Repo | ||
git.Repo.getRef = _Repo.getRef | ||
|
||
|
||
class _Reference(git.Reference): | ||
""" monkey patching """ | ||
def __enter__(self): | ||
""" Checkout the ref for this head. | ||
`git stash --all` beforehand and restore during __exit__. | ||
If the ref is the same, then the stash step still happens. | ||
If you need to modify the uncommitted state of a repo this | ||
is not the tool you should use. """ | ||
|
||
if not self.is_valid(): | ||
raise exc.InvalidRefError(f'Not a valid ref: {self.name}') | ||
|
||
self.__original_branch = self.repo.active_branch | ||
self.__stash = self.repo.git.stash('--all') # always stash | ||
if self.__stash == 'No local changes to save': | ||
self.__stash = None | ||
|
||
if self == self.__original_branch: | ||
return self | ||
|
||
self.checkout() | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_value, traceback): | ||
_stash = self.repo.git.stash('--all') # always stash on the way out as well | ||
if _stash == 'No local changes to save': | ||
stash = 'stash@{0}' | ||
else: | ||
stash = "stash@{1}" | ||
|
||
if self.__original_branch != self: | ||
self.__original_branch.checkout() | ||
|
||
# TODO check to make sure no other stashes were pushed on top | ||
if self.__stash is not None: | ||
self.repo.git.stash('pop', stash) | ||
|
||
self.__stash = None | ||
|
||
|
||
# monkey patch git.Reference | ||
git.Reference.__enter__ = _Reference.__enter__ | ||
git.Reference.__exit__ = _Reference.__exit__ |