From 669835ce1aac39f8275b225e596f97640dab298f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Tue, 25 Aug 2020 01:07:53 +0200 Subject: [PATCH] Handle per-path logs Ref: ~sircmpwn/git.sr.ht#156 --- gitsrht/blueprints/repo.py | 12 +++--------- gitsrht/git.py | 26 ++++++++++++++++++++++++-- gitsrht/templates/blob.html | 6 ++++++ gitsrht/templates/tree.html | 6 ++++++ 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/gitsrht/blueprints/repo.py b/gitsrht/blueprints/repo.py index 4c5be40..9008902 100644 --- a/gitsrht/blueprints/repo.py +++ b/gitsrht/blueprints/repo.py @@ -10,7 +10,7 @@ from flask import Blueprint, render_template, abort, current_app, send_file, req from flask import Response, url_for, session, redirect from gitsrht.editorconfig import EditorConfig from gitsrht.git import Repository as GitRepository, commit_time, annotate_tree -from gitsrht.git import diffstat, get_log +from gitsrht.git import diffstat, get_log, diff_for_commit from gitsrht.rss import generate_feed from gitsrht.types import Artifact from io import BytesIO @@ -399,7 +399,7 @@ def log(owner, repo, ref, path): except ValueError: abort(404) - commits = get_log(git_repo, commit) + commits = get_log(git_repo, commit, path) return render_template("log.html", view="log", owner=owner, repo=repo, ref=ref, path=path, @@ -434,13 +434,7 @@ def commit(owner, repo, ref): commit, ref, _ = lookup_ref(git_repo, ref, None) if not isinstance(commit, pygit2.Commit): abort(404) - try: - parent = git_repo.revparse_single(ref + "^") - diff = git_repo.diff(parent, ref) - except KeyError: - parent = None - diff = commit.tree.diff_to_tree(swap=True) - diff.find_similar(pygit2.GIT_DIFF_FIND_RENAMES) + parent, diff = diff_for_commit(git_repo, commit) refs = collect_refs(git_repo) return render_template("commit.html", view="log", owner=owner, repo=repo, ref=ref, refs=refs, diff --git a/gitsrht/git.py b/gitsrht/git.py index 0d10918..894fdd1 100644 --- a/gitsrht/git.py +++ b/gitsrht/git.py @@ -25,10 +25,32 @@ def commit_time(commit): def _get_ref(repo, ref): return repo._get(ref) -def get_log(git_repo, commit, commits_per_page=20, until=None): +def diff_for_commit(git_repo, commit): + try: + parent = git_repo.revparse_single(commit.id.hex + "^") + diff = git_repo.diff(parent, commit) + except KeyError: + parent = None + diff = commit.tree.diff_to_tree(swap=True) + diff.find_similar(pygit2.GIT_DIFF_FIND_RENAMES) + return parent, diff + +def get_log(git_repo, commit, path="", commits_per_page=20, until=None): commits = list() for commit in git_repo.walk(commit.id, pygit2.GIT_SORT_TOPOLOGICAL): - commits.append(commit) + if path: + _, diff = diff_for_commit(git_repo, commit) + for patch in diff: + exact = False + if patch.delta.new_file.path == path: + path = patch.delta.old_file.path + exact = True + if exact or patch.delta.new_file.path.startswith(path + "/"): + commits.append(commit) + break + else: + commits.append(commit) + if until is not None and commit == until: break elif len(commits) >= commits_per_page + 1: diff --git a/gitsrht/templates/blob.html b/gitsrht/templates/blob.html index 440cee8..846df31 100644 --- a/gitsrht/templates/blob.html +++ b/gitsrht/templates/blob.html @@ -30,6 +30,12 @@ pre, body { binary=True).replace("Byte", "byte")}} + + Log + + {{ utils.breadcrumb(ref, repo, path, path_join) }} + + Log + +