本文整理汇总了Python中pygit2.Tree方法的典型用法代码示例。如果您正苦于以下问题:Python pygit2.Tree方法的具体用法?Python pygit2.Tree怎么用?Python pygit2.Tree使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygit2
的用法示例。
在下文中一共展示了pygit2.Tree方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: commit_file
# 需要导入模块: import pygit2 [as 别名]
# 或者: from pygit2 import Tree [as 别名]
def commit_file(
self,
name: str,
*,
branch: str,
contents: str,
message: str,
mode: int = pygit2.GIT_FILEMODE_BLOB,
) -> pygit2.Tree:
assert "/" not in name, "creating subtrees is not supported"
ref_name = "refs/heads/" + branch
ref = self.ensure_ref(ref_name)
last_commit = self._repo.get(ref.target)
parents = [last_commit.oid]
old_tree = last_commit.tree
tree_builder = self._repo.TreeBuilder(old_tree)
blob_oid = self._repo.create_blob(contents.encode())
tree_builder.insert(name, blob_oid, mode)
new_tree = tree_builder.write()
author = self.user
commiter = self.user
self._repo.create_commit(ref_name, author, commiter, message, new_tree, parents)
示例2: _path_to_object
# 需要导入模块: import pygit2 [as 别名]
# 或者: from pygit2 import Tree [as 别名]
def _path_to_object(self, path, ref):
comm, ref = self.repo.resolve_refish(ref or self.ref)
parts = path.split("/")
tree = comm.tree
for part in parts:
if part and isinstance(tree, pygit2.Tree):
tree = tree[part]
return tree
示例3: ls
# 需要导入模块: import pygit2 [as 别名]
# 或者: from pygit2 import Tree [as 别名]
def ls(self, path, detail=True, ref=None, **kwargs):
path = self._strip_protocol(path)
tree = self._path_to_object(path, ref)
if isinstance(tree, pygit2.Tree):
out = []
for obj in tree:
if isinstance(obj, pygit2.Tree):
out.append(
{
"type": "directory",
"name": "/".join([path, obj.name]).lstrip("/"),
"hex": obj.hex,
"mode": "%o" % obj.filemode,
"size": 0,
}
)
else:
out.append(
{
"type": "file",
"name": "/".join([path, obj.name]).lstrip("/"),
"hex": obj.hex,
"mode": "%o" % obj.filemode,
"size": obj.size,
}
)
else:
obj = tree
out = [
{
"type": "file",
"name": obj.name,
"hex": obj.hex,
"mode": "%o" % obj.filemode,
"size": obj.size,
}
]
if detail:
return out
return [o["name"] for o in out]
示例4: __init__
# 需要导入模块: import pygit2 [as 别名]
# 或者: from pygit2 import Tree [as 别名]
def __init__(self, repo, treeish='HEAD'):
super().__init__()
self.repo = repo
if isinstance(treeish, pygit2.Index):
self._oid = treeish.write_tree(self.repo)
self._tree = repo[self._oid]
else:
self._rev = repo.revparse_single(treeish)
self._tree = self._rev.peel(pygit2.Tree)
示例5: tree_lookup
# 需要导入模块: import pygit2 [as 别名]
# 或者: from pygit2 import Tree [as 别名]
def tree_lookup(self, target_path, commit):
"""Navigate to the tree or blob object pointed to by the given target
path for the given commit. This is necessary because each git
tree only contains entries for the directory it refers to, not
recursively for all subdirectories.
"""
segments = target_path.split("/")
tree_or_blob = commit.tree
path = ''
while segments:
dirent = segments.pop(0)
if isinstance(tree_or_blob, pygit2.Tree):
if dirent in tree_or_blob:
tree_or_blob = self.repo[tree_or_blob[dirent].oid]
# self.logger.debug(" %s in %s" % (dirent, path))
if path:
path += '/'
path += dirent
else:
# This is probably because we were called on a
# commit whose parent added a new directory.
self.logger.debug(" %s not in %s in %s" %
(dirent, path, commit.hex[:8]))
return None
else:
self.logger.debug(" %s not a tree in %s" %
(tree_or_blob, commit.hex[:8]))
return None
return tree_or_blob
示例6: __get_file_in_tree
# 需要导入模块: import pygit2 [as 别名]
# 或者: from pygit2 import Tree [as 别名]
def __get_file_in_tree(repo_obj, tree, filepath, bail_on_tree=False):
""" Retrieve the entry corresponding to the provided filename in a
given tree.
"""
filename = filepath[0]
if isinstance(tree, pygit2.Blob):
return
for entry in tree:
fname = entry.name
if six.PY2:
fname = entry.name.decode("utf-8")
if fname == filename:
if len(filepath) == 1:
blob = repo_obj.get(entry.id)
# If we can't get the content (for example: an empty folder)
if blob is None:
return
# If we get a tree instead of a blob, let's escape
if isinstance(blob, pygit2.Tree) and bail_on_tree:
return blob
content = blob.data
# If it's a (sane) symlink, we try a single-level dereference
if (
entry.filemode == pygit2.GIT_FILEMODE_LINK
and os.path.normpath(content) == content
and not os.path.isabs(content)
):
try:
dereferenced = tree[content]
except KeyError:
pass
else:
if dereferenced.filemode == pygit2.GIT_FILEMODE_BLOB:
blob = repo_obj[dereferenced.oid]
return blob
else:
try:
nextitem = repo_obj[entry.oid]
except KeyError:
# We could not find the blob/entry in the git repo
# so we bail
return
# If we can't get the content (for example: an empty folder)
if nextitem is None:
return
return __get_file_in_tree(
repo_obj, nextitem, filepath[1:], bail_on_tree=bail_on_tree
)
示例7: view_raw_file
# 需要导入模块: import pygit2 [as 别名]
# 或者: from pygit2 import Tree [as 别名]
def view_raw_file(
repo, identifier, filename=None, username=None, namespace=None
):
""" Displays the raw content of a file of a commit for the specified repo.
"""
repo_obj = flask.g.repo_obj
if repo_obj.is_empty:
flask.abort(404, description="Empty repo cannot have a file")
if identifier in repo_obj.listall_branches():
branch = repo_obj.lookup_branch(identifier)
commit = branch.peel(pygit2.Commit)
else:
try:
commit = repo_obj.get(identifier)
except ValueError:
if "master" not in repo_obj.listall_branches():
flask.abort(404, description="Branch not found")
# If it's not a commit id then it's part of the filename
commit = repo_obj[repo_obj.head.target]
if not commit:
flask.abort(404, description="Commit %s not found" % (identifier))
if isinstance(commit, pygit2.Tag):
commit = commit.peel(pygit2.Commit)
if filename:
if isinstance(commit, pygit2.Blob):
content = commit
else:
content = __get_file_in_tree(
repo_obj, commit.tree, filename.split("/"), bail_on_tree=True
)
if not content or isinstance(content, pygit2.Tree):
flask.abort(404, description="File not found")
data = repo_obj[content.oid].data
else:
if commit.parents:
# We need to take this not so nice road to ensure that the
# identifier retrieved from the URL is actually valid
try:
parent = repo_obj.revparse_single("%s^" % identifier)
diff = repo_obj.diff(parent, commit)
except (KeyError, ValueError):
flask.abort(404, description="Identifier not found")
else:
# First commit in the repo
diff = commit.tree.diff_to_tree(swap=True)
data = diff.patch
if not data:
flask.abort(404, description="No content found")
return (data, 200, pagure.lib.mimetype.get_type_headers(filename, data))