aboutsummaryrefslogtreecommitdiff
path: root/handle_repo_commit.go
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-02-11 22:17:37 +0800
committerRunxi Yu <me@runxiyu.org>2025-02-11 22:17:37 +0800
commit84e752e2dd9b1aa84652e01588148c4b81e02d5a (patch)
treeb7669cb92d1e2a9f319aecfacc3de14ce6931f9c /handle_repo_commit.go
parentstyle.css: Better colors in dark mode, and add padding (diff)
downloadforge-84e752e2dd9b1aa84652e01588148c4b81e02d5a.tar.gz
forge-84e752e2dd9b1aa84652e01588148c4b81e02d5a.tar.zst
forge-84e752e2dd9b1aa84652e01588148c4b81e02d5a.zip
repo_commit: Add patch view
Diffstat (limited to 'handle_repo_commit.go')
-rw-r--r--handle_repo_commit.go18
1 files changed, 16 insertions, 2 deletions
diff --git a/handle_repo_commit.go b/handle_repo_commit.go
index 4660ff3..9e10a9f 100644
--- a/handle_repo_commit.go
+++ b/handle_repo_commit.go
@@ -2,6 +2,7 @@ package main
import (
"net/http"
+ "strings"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/format/diff"
@@ -17,19 +18,32 @@ func handle_repo_commit(w http.ResponseWriter, r *http.Request) {
data := make(map[string]any)
// TODO: Sanitize path values
group_name, repo_name, commit_id_string := r.PathValue("group_name"), r.PathValue("repo_name"), r.PathValue("commit_id")
- data["group_name"], data["repo_name"], data["commit_id"] = group_name, repo_name, commit_id_string
+ data["group_name"], data["repo_name"] = group_name, repo_name
repo, err := open_git_repo(group_name, repo_name)
if err != nil {
_, _ = w.Write([]byte("Error opening repo: " + err.Error()))
return
}
- commit_id := plumbing.NewHash(commit_id_string)
+ commit_id_string_real := strings.TrimSuffix(commit_id_string, ".patch")
+ commit_id := plumbing.NewHash(commit_id_string_real)
commit_object, err := repo.CommitObject(commit_id)
if err != nil {
_, _ = w.Write([]byte("Error getting commit object: " + err.Error()))
return
}
+
+ if commit_id_string_real != commit_id_string {
+ patch, err := format_patch_from_commit(commit_object)
+ if err != nil {
+ _, _ = w.Write([]byte("Error formatting patch: " + err.Error()))
+ return
+ }
+ _, _ = w.Write([]byte(patch))
+ return
+ }
+
data["commit_object"] = commit_object
+ data["commit_id"] = commit_object.Hash.String()
parent_commit_object, err := commit_object.Parent(0)
if err != nil {