diff --git a/app.py b/app.py
index 5ecebc9..3b97a32 100644
--- a/app.py
+++ b/app.py
from git.repository import get_bare_repos
from git.commit import get_commits, get_commit
-from git.ref import get_refs
+from git.reference import get_references
from git.tree import get_tree_items
from git.blob import get_blob
from git.misc import get_version, validate_repo_name, validate_ref, validate_ref_as_commit, sanitize_path
if not validate_ref_as_commit(f"{repo_path}/{repo_name}", ref):
abort(400, "Invalid ref")
commits = get_commits(f"{repo_path}/{repo_name}", ref=ref, max_count=10)
- refs = get_refs(f"{repo_path}/{repo_name}")
+ refs = get_references(f"{repo_path}/{repo_name}")
readme = None
for filename in ['README.md', 'README']:
try:
ref = request.args.get('ref', 'HEAD').strip()
if not validate_ref_as_commit(f"{repo_path}/{repo_name}", ref):
abort(400, "Invalid ref")
- refs = get_refs(f"{repo_path}/{repo_name}")
+ refs = get_references(f"{repo_path}/{repo_name}")
page = int(request.args.get('page', 0))
# maybe pages are not the wisest way to do this?
per_page = 50
def repo_refs(repo_name):
if not validate_repo_name(repo_name):
abort(404)
- refs = get_refs(f"{repo_path}/{repo_name}")
+ refs = get_references(f"{repo_path}/{repo_name}")
return render_template("refs.html", repo_name=repo_name, refs=refs)
@app.route("/<repo_name>/tree", defaults={'path': ''})
path = sanitize_path(path)
except ValueError:
abort(400, "Invalid path")
- refs = get_refs(f"{repo_path}/{repo_name}")
+ refs = get_references(f"{repo_path}/{repo_name}")
tree_items = get_tree_items(f"{repo_path}/{repo_name}", ref, path)
return render_template("tree.html", repo_name=repo_name, ref=ref, path=path, tree_items=tree_items, refs=refs)
path = sanitize_path(path)
except ValueError:
abort(400, "Invalid path")
- refs = get_refs(f"{repo_path}/{repo_name}")
+ refs = get_references(f"{repo_path}/{repo_name}")
blob = get_blob(f"{repo_path}/{repo_name}", ref, path)
return render_template("blob.html", repo_name=repo_name, ref=ref, path=path, blob=blob, refs=refs)
path = sanitize_path(path)
except ValueError:
abort(400, "Invalid path")
- refs = get_refs(f"{repo_path}/{repo_name}")
+ refs = get_references(f"{repo_path}/{repo_name}")
# if ajax (for loading)
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
def repo_diff(repo_name):
if not validate_repo_name(repo_name):
abort(404)
- refs = get_refs(f"{repo_path}/{repo_name}")
+ refs = get_references(f"{repo_path}/{repo_name}")
id1 = request.args.get('id1')
id2 = request.args.get('id2')
if id1 and not validate_ref_as_commit(f"{repo_path}/{repo_name}", id1):
diff --git a/git/ref.py b/git/ref.py
deleted file mode 100644
index 0eae983..0000000
--- a/git/ref.py
+++ /dev/null
-import pygit2 as git
-
-# retrieves all refs (branches, tags) for given repo path
-# TODO: handle symbolic refs and situation when HEAD is unborn or pointing to nothing
-def get_refs(path):
- repo = git.Repository(path)
- refs = []
- # add head ref manually
- head_ref = repo.head
- refs.append({
- 'name': "HEAD",
- 'shorthand': "HEAD",
- 'target': str(head_ref.target)
- })
-
- for ref_name in repo.listall_references():
- ref = repo.lookup_reference(ref_name)
- refs.append({
- 'name': ref.name,
- 'shorthand': ref.shorthand,
- # TODO: type, branch or tag
- 'target': str(ref.target)
- })
- return refs
\ No newline at end of file
diff --git a/git/reference.py b/git/reference.py
new file mode 100644
index 0000000..c54d0fd
--- /dev/null
+++ b/git/reference.py
+import pygit2 as git
+
+
+def get_references(path):
+ repo = git.Repository(path)
+ refs = []
+
+ for ref_name in repo.listall_references():
+ try:
+ ref = repo.lookup_reference(ref_name).resolve()
+ except git.GitError:
+ continue
+
+ refs.append(
+ {"name": ref.name, "shorthand": ref.shorthand, "target": str(ref.target)}
+ )
+ return refs