Skip to content

Hopefully fixes Orderedict error #1301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix more missing types in Symbolic.py, cos GuthubActions pytest stuck
  • Loading branch information
Yobmod committed Jul 28, 2021
commit 390efbf521d62d9cb188c7688288878ef1b1b45d
8 changes: 5 additions & 3 deletions git/objects/submodule/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ def update(self, recursive: bool = False, init: bool = True, to_latest_revision:
progress.update(op, i, len_rmts, prefix + "Done fetching remote of submodule %r" % self.name)
# END fetch new data
except InvalidGitRepositoryError:
mrepo = None
if not init:
return self
# END early abort if init is not allowed
Expand Down Expand Up @@ -603,7 +604,7 @@ def update(self, recursive: bool = False, init: bool = True, to_latest_revision:

# make sure HEAD is not detached
mrepo.head.set_reference(local_branch, logmsg="submodule: attaching head to %s" % local_branch)
mrepo.head.ref.set_tracking_branch(remote_branch)
mrepo.head.reference.set_tracking_branch(remote_branch)
except (IndexError, InvalidGitRepositoryError):
log.warning("Failed to checkout tracking branch %s", self.branch_path)
# END handle tracking branch
Expand All @@ -629,13 +630,14 @@ def update(self, recursive: bool = False, init: bool = True, to_latest_revision:
if mrepo is not None and to_latest_revision:
msg_base = "Cannot update to latest revision in repository at %r as " % mrepo.working_dir
if not is_detached:
rref = mrepo.head.ref.tracking_branch()
rref = mrepo.head.reference.tracking_branch()
if rref is not None:
rcommit = rref.commit
binsha = rcommit.binsha
hexsha = rcommit.hexsha
else:
log.error("%s a tracking branch was not set for local branch '%s'", msg_base, mrepo.head.ref)
log.error("%s a tracking branch was not set for local branch '%s'",
msg_base, mrepo.head.reference)
# END handle remote ref
else:
log.error("%s there was no local tracking branch", msg_base)
Expand Down
41 changes: 25 additions & 16 deletions git/refs/symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

if TYPE_CHECKING:
from git.repo import Repo
from git.refs import Reference, Head, HEAD, TagReference, RemoteReference
from git.refs import Reference, Head, TagReference, RemoteReference

T_References = TypeVar('T_References', bound='SymbolicReference')

Expand Down Expand Up @@ -60,6 +60,7 @@ class SymbolicReference(object):
def __init__(self, repo: 'Repo', path: PathLike, check_path: bool = False) -> None:
self.repo = repo
self.path = str(path)
self.ref = self._get_reference()

def __str__(self) -> str:
return self.path
Expand Down Expand Up @@ -279,7 +280,7 @@ def set_object(self, object, logmsg=None): # @ReservedAssignment
object = property(_get_object, set_object, doc="Return the object our ref currently refers to")

def _get_reference(self
) -> Union['HEAD', 'Head', 'RemoteReference', 'TagReference', 'Reference', 'SymbolicReference']:
) -> Union['Head', 'RemoteReference', 'TagReference', 'Reference']:
""":return: Reference Object we point to
:raise TypeError: If this symbolic reference is detached, hence it doesn't point
to a reference, but to a commit"""
Expand All @@ -288,7 +289,8 @@ def _get_reference(self
raise TypeError("%s is a detached symbolic reference as it points to %r" % (self, sha))
return self.from_path(self.repo, target_ref_path)

def set_reference(self, ref, logmsg=None):
def set_reference(self, ref: Union[str, Commit_ish, 'SymbolicReference'], logmsg: Union[str, None] = None
) -> None:
"""Set ourselves to the given ref. It will stay a symbol if the ref is a Reference.
Otherwise an Object, given as Object instance or refspec, is assumed and if valid,
will be set which effectively detaches the refererence if it was a purely
Expand Down Expand Up @@ -355,12 +357,16 @@ def set_reference(self, ref, logmsg=None):
if logmsg is not None:
self.log_append(oldbinsha, logmsg)

return self
return None

# aliased reference
reference: Union[Commit_ish, 'Head', 'Reference'] = property( # type: ignore
_get_reference, set_reference, doc="Returns the Reference we point to")
ref = reference
@ property
def reference(self) -> Union['Head', 'RemoteReference', 'TagReference', 'Reference']:
return self._get_reference()

@ reference.setter
def reference(self, ref: Union[str, Commit_ish, 'SymbolicReference'], logmsg: Union[str, None] = None
) -> None:
return self.set_reference(ref=ref, logmsg=logmsg)

def is_valid(self) -> bool:
"""
Expand All @@ -374,7 +380,7 @@ def is_valid(self) -> bool:
else:
return True

@property
@ property
def is_detached(self):
"""
:return:
Expand Down Expand Up @@ -424,7 +430,7 @@ def log_entry(self, index):
In that case, it will be faster than the ``log()`` method"""
return RefLog.entry_at(RefLog.path(self), index)

@classmethod
@ classmethod
def to_full_path(cls, path: Union[PathLike, 'SymbolicReference']) -> str:
"""
:return: string with a full repository-relative path which can be used to initialize
Expand All @@ -439,7 +445,7 @@ def to_full_path(cls, path: Union[PathLike, 'SymbolicReference']) -> str:
full_ref_path = '%s/%s' % (cls._common_path_default, path)
return full_ref_path

@classmethod
@ classmethod
def delete(cls, repo: 'Repo', path: PathLike) -> None:
"""Delete the reference at the given path

Expand Down Expand Up @@ -497,8 +503,10 @@ def delete(cls, repo: 'Repo', path: PathLike) -> None:
os.remove(reflog_path)
# END remove reflog

@classmethod
def _create(cls, repo, path, resolve, reference, force, logmsg=None):
@ classmethod
def _create(cls: Type[T_References], repo: 'Repo', path: PathLike, resolve: bool,
reference: Union[str, 'SymbolicReference'],
force: bool, logmsg: Union[str, None] = None) -> T_References:
"""internal method used to create a new symbolic reference.
If resolve is False, the reference will be taken as is, creating
a proper symbolic reference. Otherwise it will be resolved to the
Expand Down Expand Up @@ -531,8 +539,9 @@ def _create(cls, repo, path, resolve, reference, force, logmsg=None):
return ref

@classmethod
def create(cls, repo: 'Repo', path: PathLike, reference: Union[Commit_ish, str] = 'SymbolicReference',
logmsg: Union[str, None] = None, force: bool = False, **kwargs: Any):
def create(cls: Type[T_References], repo: 'Repo', path: PathLike,
reference: Union[Commit_ish, str, 'SymbolicReference'] = 'SymbolicReference',
logmsg: Union[str, None] = None, force: bool = False, **kwargs: Any) -> T_References:
"""Create a new symbolic reference, hence a reference pointing , to another reference.

:param repo:
Expand Down Expand Up @@ -669,7 +678,7 @@ def iter_items(cls, repo: 'Repo', common_path: Union[PathLike, None] = None, *ar
return (r for r in cls._iter_items(repo, common_path) if r.__class__ == SymbolicReference or not r.is_detached)

@classmethod
def from_path(cls, repo, path):
def from_path(cls, repo: 'Repo', path: PathLike) -> Union['Head', 'RemoteReference', 'TagReference', 'Reference']:
"""
:param path: full .git-directory-relative path name to the Reference to instantiate
:note: use to_full_path() if you only have a partial path of a known Reference Type
Expand Down
6 changes: 4 additions & 2 deletions git/refs/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

# typing ------------------------------------------------------------------

from typing import Any, Union, TYPE_CHECKING
from typing import Any, Type, Union, TYPE_CHECKING
from git.types import Commit_ish, PathLike

if TYPE_CHECKING:
from git.repo import Repo
from git.objects import Commit
from git.objects import TagObject
from git.refs import SymbolicReference


# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -68,7 +69,8 @@ def object(self) -> Commit_ish: # type: ignore[override]
return Reference._get_object(self)

@classmethod
def create(cls, repo: 'Repo', path: PathLike, reference: Union[Commit_ish, str] = 'HEAD',
def create(cls: Type['TagReference'], repo: 'Repo', path: PathLike,
reference: Union[Commit_ish, str, 'SymbolicReference'] = 'HEAD',
logmsg: Union[str, None] = None,
force: bool = False, **kwargs: Any) -> 'TagReference':
"""Create a new tag reference.
Expand Down
3 changes: 2 additions & 1 deletion git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,9 +788,10 @@ def ignored(self, *paths: PathLike) -> List[PathLike]:
return proc.replace("\\\\", "\\").replace('"', "").split("\n")

@property
def active_branch(self) -> 'HEAD':
def active_branch(self) -> Head:
"""The name of the currently active branch.
:return: Head to the active branch"""
# reveal_type(self.head.reference) # => Reference
return self.head.reference

def blame_incremental(self, rev: TBD, file: TBD, **kwargs: Any) -> Optional[Iterator['BlameEntry']]:
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ filterwarnings = 'ignore::DeprecationWarning'
# filterwarnings ignore::WarningType # ignores those warnings

[tool.mypy]
# disallow_untyped_defs = true
# disallow_untyped_defs = True
no_implicit_optional = true
warn_redundant_casts = true
# warn_unused_ignores = true
# warn_unreachable = true
# warn_unused_ignores = True
# warn_unreachable = True
show_error_codes = true

# TODO: remove when 'gitdb' is fully annotated
Expand Down
9 changes: 9 additions & 0 deletions t.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from git import Repo


def get_active_branch(gitobj: Repo) -> str:
return gitobj.active_branch.name


gitobj = Repo(".")
print(get_active_branch(gitobj))