Hi… I am well aware that this diff view is very suboptimal. It will be fixed when the refactored server comes along!
*.go: Reformat
package main
import (
"bytes"
"errors"
"fmt"
"strings"
"time"
"github.com/go-git/go-git/v5/plumbing/object"
"go.lindenii.runxiyu.org/lindenii-common/misc"
)
var err_get_patch = errors.New("Failed to get patch from commit")
func format_patch_from_commit(commit *object.Commit) (string, error) {
parent, err := commit.Parent(0)
if err != nil {
return "", err
}
var patch *object.Patch
patch, err = parent.Patch(commit)
if err != nil {
return "", misc.Wrap_one_error(err_get_patch, err)
}
var buf bytes.Buffer
author := commit.Author
date := author.When.Format(time.RFC1123Z)
commit_msg_title, commit_msg_details, _ := strings.Cut(commit.Message, "\n")
fmt.Fprintf(&buf, "From %s Mon Sep 17 00:00:00 2001\n", commit.Hash)
fmt.Fprintf(&buf, "From: %s <%s>\n", author.Name, author.Email)
fmt.Fprintf(&buf, "Date: %s\n", date)
fmt.Fprintf(&buf, "Subject: [PATCH] %s\n\n", commit_msg_title)
if commit_msg_details != "" {
fmt.Println("fdsafsad")
commit_msg_details_first_line, commit_msg_details_rest, _ := strings.Cut(commit_msg_details, "\n")
if strings.TrimSpace(commit_msg_details_first_line) == "" {
commit_msg_details = commit_msg_details_rest
}
buf.WriteString(commit_msg_details)
buf.WriteString("\n")
}
buf.WriteString("---\n")
fmt.Fprint(&buf, patch.Stats().String())
fmt.Fprintln(&buf)
buf.WriteString(patch.String())
fmt.Fprintf(&buf, "\n-- \n2.48.1\n")
return buf.String(), nil
}
package main
import (
"net/http"
"strings"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/format/diff"
)
type usable_file_patch struct {
From diff.File
To diff.File
Chunks []diff.Chunk
}
func handle_repo_commit(w http.ResponseWriter, r *http.Request) {
data := make(map[string]any)
group_name, repo_name, commit_id_specified_string := r.PathValue("group_name"), r.PathValue("repo_name"), r.PathValue("commit_id")
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_specified_string_without_suffix := strings.TrimSuffix(commit_id_specified_string, ".patch")
commit_id := plumbing.NewHash(commit_id_specified_string_without_suffix)
commit_object, err := repo.CommitObject(commit_id)
if err != nil {
_, _ = w.Write([]byte("Error getting commit object: " + err.Error()))
return
}
if commit_id_specified_string_without_suffix != commit_id_specified_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
}
commit_id_string := commit_object.Hash.String()
if commit_id_string != commit_id_specified_string {
http.Redirect(w, r, commit_id_string, http.StatusSeeOther)
http.Redirect(w, r, commit_id_string, http.StatusSeeOther)
return
}
data["commit_object"] = commit_object
data["commit_id"] = commit_id_string
parent_commit_object, err := commit_object.Parent(0)
if err != nil {
_, _ = w.Write([]byte("Error getting parent commit object: " + err.Error()))
return
}
data["parent_commit_object"] = parent_commit_object
patch, err := parent_commit_object.Patch(commit_object)
if err != nil {
_, _ = w.Write([]byte("Error getting patch of commit: " + err.Error()))
return
}
data["patch"] = patch
// TODO: Remove unnecessary context
usable_file_patches := make([]usable_file_patch, 0)
for _, file_patch := range patch.FilePatches() {
from, to := file_patch.Files()
usable_file_patch := usable_file_patch{
Chunks: file_patch.Chunks(),
From: from,
To: to,
}
usable_file_patches = append(usable_file_patches, usable_file_patch)
}
data["file_patches"] = usable_file_patches
err = templates.ExecuteTemplate(w, "repo_commit", data)
if err != nil {
_, _ = w.Write([]byte("Error rendering template: " + err.Error()))
return
}
}
package main import ( "errors" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "go.lindenii.runxiyu.org/lindenii-common/misc" ) var (
err_getting_tag_reference = errors.New("Error getting tag reference")
err_getting_tag_reference = errors.New("Error getting tag reference")
err_getting_branch_reference = errors.New("Error getting branch reference")
err_getting_head = errors.New("Error getting HEAD")
err_getting_head = errors.New("Error getting HEAD")
)
func get_ref_hash_from_type_and_name(repo *git.Repository, ref_type, ref_name string) (ref_hash plumbing.Hash, ret_err error) {
switch ref_type {
case "head":
head, err := repo.Head()
if err != nil {
ret_err = misc.Wrap_one_error(err_getting_head, err)
return
}
ref_hash = head.Hash()
case "commit":
ref_hash = plumbing.NewHash(ref_name)
case "branch":
ref, err := repo.Reference(plumbing.NewBranchReferenceName(ref_name), true)
if err != nil {
ret_err = misc.Wrap_one_error(err_getting_branch_reference, err)
return
}
ref_hash = ref.Hash()
case "tag":
ref, err := repo.Reference(plumbing.NewTagReferenceName(ref_name), true)
if err != nil {
ret_err = misc.Wrap_one_error(err_getting_tag_reference, err)
return
}
ref_hash = ref.Hash()
default:
panic("Invalid ref type " + ref_type)
}
return
}
package main
import (
"errors"
"net/http"
"net/url"
)
var (
err_duplicate_ref_spec = errors.New("Duplicate ref spec")
err_no_ref_spec = errors.New("No ref spec")
err_no_ref_spec = errors.New("No ref spec")
)
func get_param_ref_and_type(r *http.Request) (ref_type, ref string, err error) {
qr := r.URL.RawQuery
q, err := url.ParseQuery(qr)
if err != nil {
return
}
done := false
for _, _ref_type := range []string{"commit", "branch", "tag"} {
_ref, ok := q[_ref_type]
if ok {
if done {
err = err_duplicate_ref_spec
return
} else {
done = true
if len(_ref) != 1 {
err = err_duplicate_ref_spec
return
}
ref = _ref[0]
ref_type = _ref_type
}
}
}
if !done {
err = err_no_ref_spec
}
return
}