Python pygit2.Tree() Examples
The following are 7
code examples of pygit2.Tree().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pygit2
, or try the search function
.
Example #1
Source File: git_server.py From tsrc with BSD 3-Clause "New" or "Revised" License | 6 votes |
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)
Example #2
Source File: git.py From filesystem_spec with BSD 3-Clause "New" or "Revised" License | 5 votes |
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
Example #3
Source File: git.py From filesystem_spec with BSD 3-Clause "New" or "Revised" License | 5 votes |
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]
Example #4
Source File: repo.py From git-annex-adapter with GNU General Public License v3.0 | 5 votes |
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)
Example #5
Source File: detector.py From git-deps with GNU General Public License v2.0 | 5 votes |
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
Example #6
Source File: utils.py From pagure with GNU General Public License v2.0 | 4 votes |
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 )
Example #7
Source File: repo.py From pagure with GNU General Public License v2.0 | 4 votes |
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))