From 1db988a330727805332887225f712af1edf299e7 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 2 Jan 2020 11:51:35 -0500 Subject: [PATCH] Fix compatibility issues with pygit2 1.x pygit2 1.x+ repurposed the tree entry type field to an integer, and moved the old behavior to type_str. This patch adds compatibility with both pygit2 versions 0.x and 1.x. --- gitsrht/blueprints/api.py | 10 +++++++--- gitsrht/blueprints/repo.py | 8 ++++++-- gitsrht/editorconfig.py | 8 ++++++-- gitsrht/git.py | 3 ++- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/gitsrht/blueprints/api.py b/gitsrht/blueprints/api.py index ec88288..f403da3 100644 --- a/gitsrht/blueprints/api.py +++ b/gitsrht/blueprints/api.py @@ -48,7 +48,7 @@ def tree_to_dict(t): { "name": e.name, "id": str(e.id), - "type": e.type, + "type": (e.type_str if hasattr(e, "type_str") else e.type), "mode": e.filemode, } for e in t ] @@ -144,7 +144,9 @@ def repo_tree_GET(username, reponame, ref, path): if not tree or part not in tree: abort(404) entry = tree[part] - if entry.type == "blob": + etype = (entry.type_str + if hasattr(entry, "type_str") else entry.type) + if etype == "blob": abort(404) tree = git_repo.get(entry.id) if not tree: @@ -209,7 +211,9 @@ def repo_blob_GET(username, reponame, ref, path): if part not in tree: abort(404) entry = tree[part] - if entry.type == "blob": + etype = (entry.type_str + if hasattr(entry, "type_str") else entry.type) + if etype == "blob": tree = annotate_tree(git_repo, tree, commit) commit = next(e.commit for e in tree if e.name == entry.name) blob = git_repo.get(entry.id) diff --git a/gitsrht/blueprints/repo.py b/gitsrht/blueprints/repo.py index 94f0479..382b171 100644 --- a/gitsrht/blueprints/repo.py +++ b/gitsrht/blueprints/repo.py @@ -175,7 +175,9 @@ def tree(owner, repo, ref, path): if not tree or part not in tree: abort(404) entry = tree[part] - if entry.type == "blob": + etype = (entry.type_str + if hasattr(entry, "type_str") else entry.type) + if etype == "blob": tree = annotate_tree(git_repo, tree, commit) commit = next( (e.commit for e in tree if e.name == entry.name), None) @@ -217,7 +219,9 @@ def raw_blob(owner, repo, ref, path): if part not in tree: abort(404) entry = tree[part] - if entry.type == "blob": + etype = (entry.type_str + if hasattr(entry, "type_str") else entry.type) + if etype == "blob": tree = annotate_tree(git_repo, tree, commit) commit = next(e.commit for e in tree if e.name == entry.name) blob = git_repo.get(entry.id) diff --git a/gitsrht/editorconfig.py b/gitsrht/editorconfig.py index 0be8049..272033f 100644 --- a/gitsrht/editorconfig.py +++ b/gitsrht/editorconfig.py @@ -20,7 +20,9 @@ class EditorConfig: if not directory in tree: return None entry = tree[directory] - if entry.type != 'tree': + etype = (entry.type_str + if hasattr(entry, "type_str") else entry.type) + if etype != 'tree': return None tree = self.repo.get(entry.id) trees += [tree] @@ -29,7 +31,9 @@ class EditorConfig: if ".editorconfig" not in tree: continue entry = tree[".editorconfig"] - if entry.type != "blob": + etype = (entry.type_str + if hasattr(entry, "type_str") else entry.type) + if etype != "blob": continue blob = self.repo.get(entry.id) try: diff --git a/gitsrht/git.py b/gitsrht/git.py index b99f1c6..4831508 100644 --- a/gitsrht/git.py +++ b/gitsrht/git.py @@ -71,7 +71,8 @@ class AnnotatedTreeEntry: if entry: self.id = entry.id.hex self.name = entry.name - self.type = entry.type + self.type = (entry.type_str + if hasattr(entry, "type_str") else entry.type) self.filemode = entry.filemode def fetch_blob(self): -- 2.38.4