Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.13
3.1.14
4 changes: 2 additions & 2 deletions git/refs/symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def _iter_packed_refs(cls, repo):
"""Returns an iterator yielding pairs of sha1/path pairs (as bytes) for the corresponding refs.
:note: The packed refs file will be kept open as long as we iterate"""
try:
with open(cls._get_packed_refs_path(repo), 'rt') as fp:
with open(cls._get_packed_refs_path(repo), 'rt', encoding='UTF-8') as fp:
for line in fp:
line = line.strip()
if not line:
Expand Down Expand Up @@ -513,7 +513,7 @@ def _create(cls, repo, path, resolve, reference, force, logmsg=None):
return ref

@classmethod
def create(cls, repo, path, reference='HEAD', force=False, logmsg=None):
def create(cls, repo, path, reference='HEAD', force=False, logmsg=None, **kwargs):
"""Create a new symbolic reference, hence a reference pointing to another reference.

:param repo:
Expand Down
38 changes: 25 additions & 13 deletions git/repo/fun.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Package with general repository related functions"""
from git.refs.tag import Tag
import os
import stat
from string import digits
Expand All @@ -15,18 +16,28 @@
import os.path as osp
from git.cmd import Git

# Typing ----------------------------------------------------------------------

from typing import AnyStr, Union, Optional, cast, TYPE_CHECKING
from git.types import PathLike
if TYPE_CHECKING:
from .base import Repo
from git.db import GitCmdObjectDB
from git.objects import Commit, TagObject, Blob, Tree

# ----------------------------------------------------------------------------

__all__ = ('rev_parse', 'is_git_dir', 'touch', 'find_submodule_git_dir', 'name_to_object', 'short_to_long', 'deref_tag',
'to_commit', 'find_worktree_git_dir')


def touch(filename):
def touch(filename: str) -> str:
with open(filename, "ab"):
pass
return filename


def is_git_dir(d):
def is_git_dir(d: PathLike) -> bool:
""" This is taken from the git setup.c:is_git_directory
function.

Expand All @@ -48,7 +59,7 @@ def is_git_dir(d):
return False


def find_worktree_git_dir(dotgit):
def find_worktree_git_dir(dotgit: PathLike) -> Optional[str]:
"""Search for a gitdir for this worktree."""
try:
statbuf = os.stat(dotgit)
Expand All @@ -67,15 +78,15 @@ def find_worktree_git_dir(dotgit):
return None


def find_submodule_git_dir(d):
def find_submodule_git_dir(d: PathLike) -> Optional[PathLike]:
"""Search for a submodule repo."""
if is_git_dir(d):
return d

try:
with open(d) as fp:
content = fp.read().rstrip()
except (IOError, OSError):
except IOError:
# it's probably not a file
pass
else:
Expand All @@ -92,7 +103,7 @@ def find_submodule_git_dir(d):
return None


def short_to_long(odb, hexsha):
def short_to_long(odb: 'GitCmdObjectDB', hexsha: AnyStr) -> Optional[bytes]:
""":return: long hexadecimal sha1 from the given less-than-40 byte hexsha
or None if no candidate could be found.
:param hexsha: hexsha with less than 40 byte"""
Expand All @@ -103,14 +114,15 @@ def short_to_long(odb, hexsha):
# END exception handling


def name_to_object(repo, name, return_ref=False):
def name_to_object(repo: 'Repo', name: str, return_ref: bool = False
) -> Union[SymbolicReference, 'Commit', 'TagObject', 'Blob', 'Tree']:
"""
:return: object specified by the given name, hexshas ( short and long )
as well as references are supported
:param return_ref: if name specifies a reference, we will return the reference
instead of the object. Otherwise it will raise BadObject or BadName
"""
hexsha = None
hexsha = None # type: Union[None, str, bytes]

# is it a hexsha ? Try the most common ones, which is 7 to 40
if repo.re_hexsha_shortened.match(name):
Expand Down Expand Up @@ -150,7 +162,7 @@ def name_to_object(repo, name, return_ref=False):
return Object.new_from_sha(repo, hex_to_bin(hexsha))


def deref_tag(tag):
def deref_tag(tag: Tag) -> 'TagObject':
"""Recursively dereference a tag and return the resulting object"""
while True:
try:
Expand All @@ -161,7 +173,7 @@ def deref_tag(tag):
return tag


def to_commit(obj):
def to_commit(obj: Object) -> Union['Commit', 'TagObject']:
"""Convert the given object to a commit if possible and return it"""
if obj.type == 'tag':
obj = deref_tag(obj)
Expand All @@ -172,7 +184,7 @@ def to_commit(obj):
return obj


def rev_parse(repo, rev):
def rev_parse(repo: 'Repo', rev: str) -> Union['Commit', 'Tag', 'Tree', 'Blob']:
"""
:return: Object at the given revision, either Commit, Tag, Tree or Blob
:param rev: git-rev-parse compatible revision specification as string, please see
Expand All @@ -188,7 +200,7 @@ def rev_parse(repo, rev):
raise NotImplementedError("commit by message search ( regex )")
# END handle search

obj = None
obj = cast(Object, None) # not ideal. Should use guards
ref = None
output_type = "commit"
start = 0
Expand Down Expand Up @@ -238,7 +250,7 @@ def rev_parse(repo, rev):
pass # error raised later
# END exception handling
elif output_type in ('', 'blob'):
if obj.type == 'tag':
if obj and obj.type == 'tag':
obj = deref_tag(obj)
else:
# cannot do anything for non-tags
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
gitdb>=4.0.1,<5
typing-extensions>=3.7.4.0
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ flake8
tox
virtualenv
nose
typing-extensions>=3.7.4.0
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ commands = {posargs}
# E266 = too many leading '#' for block comment
# E731 = do not assign a lambda expression, use a def
# W293 = Blank line contains whitespace
ignore = E265,W293,E266,E731
# W504 = Line break after operator
ignore = E265,W293,E266,E731, W504
max-line-length = 120
exclude = .tox,.venv,build,dist,doc,git/ext/