本文整理汇总了Python中pygit2.Repository.get方法的典型用法代码示例。如果您正苦于以下问题:Python Repository.get方法的具体用法?Python Repository.get怎么用?Python Repository.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygit2.Repository
的用法示例。
在下文中一共展示了Repository.get方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: GitStorage
# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import get [as 别名]
class GitStorage(BaseStorage):
_backend = None
def __init__(self, context, repo_path=None):
self.context = context
rp = IStorageInfo(context).path
try:
self.repo = Repository(discover_repository(rp))
except KeyError:
# discover_repository may have failed.
raise PathNotFoundError('repository does not exist at path')
self.checkout() # defaults to HEAD.
@property
def empty_root(self):
return {'': '_empty_root'}
def _get_empty_root(self):
return self.empty_root
def _get_obj(self, path, cls=None):
if path == '' and self._commit is None:
# special case
return self._get_empty_root()
if self._commit is None:
raise PathNotFoundError('repository is empty')
root = self._commit.tree
try:
breadcrumbs = []
fragments = list(reversed(path.split('/')))
node = root
oid = None
while fragments:
fragment = fragments.pop()
if not fragment == '':
# no empty string entries, also skips over '//' and
# leaves the final node (if directory) as the tree.
oid = node[fragment].oid
node = self.repo.get(oid)
breadcrumbs.append(fragment)
if node is None:
# strange. Looks like it's either submodules only
# have entry nodes or pygit2 doesn't fully support
# this. Try to manually resolve the .gitmodules
# file.
if cls is None:
# Only return this if a specific type was not
# expected.
submods = parse_gitmodules(self.repo.get(
root[GIT_MODULE_FILE].oid).data)
submod = submods.get('/'.join(breadcrumbs))
if submod:
fragments.reverse()
return {
'': '_subrepo',
'location': submod,
'path': '/'.join(fragments),
'rev': oid.hex,
}
if node and (cls is None or isinstance(node, cls)):
return node
except KeyError:
# can't find what is needed in repo, raised by pygit2
raise PathNotFoundError('path not found')
# not what we were looking for.
if cls == Tree:
raise PathNotDirError('path not dir')
elif cls == Blob:
raise PathNotFileError('path not file')
raise PathNotFoundError('path not found')
@property
def _commit(self):
return self.__commit
@property
def rev(self):
if self.__commit:
return self.__commit.hex
return None
@property
def shortrev(self):
# TODO this is an interim solution.
if self.rev:
return self.rev[:12]
def basename(self, name):
return name.split('/')[-1]
def checkout(self, rev=None):
# None maps to the default revision.
if rev is None:
#.........这里部分代码省略.........
示例2: GitRepo
# 需要导入模块: from pygit2 import Repository [as 别名]
# 或者: from pygit2.Repository import get [as 别名]
class GitRepo(object):
''' git repo class '''
def __init__(self, path):
try:
self.__repo = Repository(path)
except Exception as e:
self.__repo = None
print(e)
def get_info(self):
if not self.__repo:
return None
signature = self.__repo.default_signature
result = {
'path': self.__repo.path,
'workdir': self.__repo.workdir,
'bare': self.__repo.is_bare,
'empty': self.__repo.is_empty,
'name': signature.name,
'email': signature.email,
'time': signature.time,
'offset': signature.offset,
}
return result
def get_all_references(self):
return self.__repo.listall_references()
def get_reference(self, name):
if not self.__repo:
return None
ref = None
try:
ref = self.__repo.lookup_reference(name)
except Exception as e:
print(e)
return ref
def get_all_branches(self, branch_type=None):
if not self.__repo:
return None
if branch_type:
return self.__repo.listall_branches(branch_type)
r = self.__repo.listall_branches(GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE)
return r
def get_branch(self, name, branch_type=GIT_BRANCH_LOCAL):
if not self.__repo:
return None
return self.__repo.lookup_branch(name, branch_type)
def check_branch(self, name, branch_type=None):
if not branch_type:
if '/' in name:
branch_type = GIT_BRANCH_REMOTE
else:
branch_type = GIT_BRANCH_LOCAL
try:
result = self.get_branch(name, branch_type)
return result
except Exception as e:
print(e)
return False
def get_current_commit(self):
if not self.__repo:
return None
commit = self.__repo.revparse_single('HEAD')
return self.get_commit(commit)
def get_commit_by_branch(self, branch):
if not self.__repo:
return None
query = 'refs/'
if hasattr(branch, 'remote_name'):
query += 'remotes/'
else:
query += 'heads/'
query += branch.branch_name
try:
ref = self.get_reference(query)
commit = ref.target
return self.get_commit(commit)
except Exception as e:
print(e)
return None
def get_commit_by_tag(self, tag):
if self.__repo is None:
return None
if tag:
commit = tag.get_object()
return self.get_commit(commit)
return None
def get_commit(self, oid_or_commit):
''' return a commit w/ json '''
if not self.__repo or not oid_or_commit:
#.........这里部分代码省略.........