From 414b7ff73aed8fa544ab2c99db454ead58e5b04d Mon Sep 17 00:00:00 2001 From: Luka Hietala Date: Tue, 9 Dec 2025 18:19:57 +0200 Subject: [PATCH] improve get refs --- app.py | 16 ++++++++-------- git/ref.py | 24 ------------------------ git/reference.py | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 32 deletions(-) delete mode 100644 git/ref.py create mode 100644 git/reference.py diff --git a/app.py b/app.py index 5ecebc9..3b97a32 100644 --- a/app.py +++ b/app.py @@ -5,7 +5,7 @@ from dotenv import load_dotenv 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 @@ -51,7 +51,7 @@ def repo_detail(repo_name): 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: @@ -70,7 +70,7 @@ def repo_commits(repo_name): 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 @@ -108,7 +108,7 @@ def commit_patch(repo_name, commit_id): 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("//tree", defaults={'path': ''}) @@ -123,7 +123,7 @@ def repo_tree_path(repo_name, 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) @@ -138,7 +138,7 @@ def repo_blob_path(repo_name, 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}") 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) @@ -153,7 +153,7 @@ def repo_blame_path(repo_name, 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}") # if ajax (for loading) if request.headers.get('X-Requested-With') == 'XMLHttpRequest': @@ -167,7 +167,7 @@ def repo_blame_path(repo_name, path): 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 @@ -1,24 +0,0 @@ -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 @@ -0,0 +1,17 @@ +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 -- 2.47.3