From b5e7fec184889ccc2c5c83b495774f2581cf93b1 Mon Sep 17 00:00:00 2001 From: Luka Hietala Date: Sat, 22 Nov 2025 20:20:25 +0200 Subject: [PATCH] allow to view raw patch --- app.py | 15 +++++++++++++++ templates/commit.html | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index c6504f0..6b0b144 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,5 @@ import os +import subprocess from flask import Flask, render_template, request, abort from datetime import datetime from dotenv import load_dotenv @@ -142,6 +143,20 @@ def commit_detail(repo_name, commit_id): commit = get_commit(f"{repo_path}/{repo_name}", commit_id) return render_template("commit.html", repo_name=repo_name, commit=commit) +@app.route("//patch/") +def commit_patch(repo_name, commit_id): + if not validate_repo_name(repo_name): + abort(404) + if not validate_ref_as_commit(f"{repo_path}/{repo_name}", commit_id): + abort(400, "Invalid commit id") + # Use git format-patch to generate the patch + result = subprocess.run(['git', '--git-dir', f"{repo_path}/{repo_name}", 'format-patch', '-1', '--stdout', commit_id], + capture_output=True, text=True, encoding='utf-8') + # git leaves encoded headers like =?UTF-8?Q?...?=, but that should be fine for patch view for now, TODO + if result.returncode != 0: + abort(500, f"Failed to generate patch: {result.stderr}") + return result.stdout, 200, {'Content-Type': 'text/plain; charset=utf-8'} + @app.route("//refs") def repo_refs(repo_name): if not validate_repo_name(repo_name): diff --git a/templates/commit.html b/templates/commit.html index 48627bf..2dd5001 100644 --- a/templates/commit.html +++ b/templates/commit.html @@ -31,7 +31,7 @@ {% for file in commit.changed_files %} - {{ file.file }} + {{ file.file }} +{{ file.additions }} -{{ file.deletions }} {{ file.status }} @@ -40,7 +40,7 @@ {% if commit.parent_id %} -

View Full Diff

+

View Full Diff | Patch

{% endif %} {% else %}

No changes compared to parent

-- 2.47.3