Lindenii Project Forge
Login
Commit info
ID84e752e2dd9b1aa84652e01588148c4b81e02d5a
AuthorRunxi Yu<me@runxiyu.org>
Author dateTue, 11 Feb 2025 22:17:37 +0800
CommitterRunxi Yu<me@runxiyu.org>
Committer dateTue, 11 Feb 2025 22:17:37 +0800
Actions
Get patch
repo_commit: Add patch view
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)
	// 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 {
		_, _ = 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 (
	"flag"
	"net"
	"net/http"

	"go.lindenii.runxiyu.org/lindenii-common/clog"
)

func main() {
	config_path := flag.String(
		"config",
		"/etc/lindenii/forge.scfg",
		"path to configuration file",
	)
	flag.Parse()

	err := load_config(*config_path)
	if err != nil {
		clog.Fatal(1, "Loading configuration: "+err.Error())
	}

	err = load_templates()
	if err != nil {
		clog.Fatal(1, "Loading templates: "+err.Error())
	}

	err = serve_static()
	if err != nil {
		clog.Fatal(1, "Serving static: "+err.Error())
	}

	serve_source()

	http.HandleFunc("/{$}", handle_index)
	http.HandleFunc("/g/{group_name}/repos/{$}", handle_group_repos)
	http.HandleFunc("/g/{group_name}/repos/{repo_name}/{$}", handle_repo_index)
	http.HandleFunc("/g/{group_name}/repos/{repo_name}/tree/{ref}/{rest...}", handle_repo_tree)
	http.HandleFunc("/g/{group_name}/repos/{repo_name}/raw/{ref}/{rest...}", handle_repo_raw)
	http.HandleFunc("/g/{group_name}/repos/{repo_name}/log/{ref}/", handle_repo_log)
	http.HandleFunc("/g/{group_name}/repos/{repo_name}/commit/{commit_id}/", handle_repo_commit)
	http.HandleFunc("/g/{group_name}/repos/{repo_name}/commit/{commit_id}", handle_repo_commit)

	listener, err := net.Listen(config.HTTP.Net, config.HTTP.Addr)
	if err != nil {
		clog.Fatal(1, "Listening: "+err.Error())
	}

	err = http.Serve(listener, nil)
	if err != nil {
		clog.Fatal(1, "Serving: "+err.Error())
	}
}
{{- define "repo_commit" -}}
<!DOCTYPE html>
<html lang="en">
	<head>
		{{ template "head_common" . }}
		<title>{{ .group_name }}/repos/{{ .repo_name }} &ndash; Lindenii Forge</title>
	</head>
	<body class="repo-commit">
		<div class="padding-wrapper scroll">
			<table id="commit-info-table">
				<thead>
					<tr class="title-row">
						<th colspan="2">Commit Info</th>
					</tr>
				</thead>
				<tbody>
					<tr>
						<th scope="row">ID</th>
						<td>{{ .commit_id }}</td>
					</tr>
					<tr>
						<th scope="row">Author</th>
						<td>{{ .commit_object.Author.Name }} &lt;<a href="mailto:{{ .commit_object.Author.Email }}">{{ .commit_object.Author.Email }}</a>&gt;</td>
					</tr>
					<tr>
						<th scope="row">Author Date</th>
						<td>{{ .commit_object.Author.When.Format "Mon, 02 Jan 2006 15:04:05 -0700" }}</td>
					</tr>
					<tr>
						<th scope="row">Committer</th>
						<td>{{ .commit_object.Committer.Name }} &lt;<a href="mailto:{{ .commit_object.Committer.Email }}">{{ .commit_object.Committer.Email }}</a>&gt;</td>
					</tr>
					<tr>
						<th scope="row">Committer Date</th>
						<td>{{ .commit_object.Committer.When.Format "Mon, 02 Jan 2006 15:04:05 -0700" }}</td>
					</tr>
					<tr>
						<th scope="row">Message</th>
						<td><pre>{{ .commit_object.Message }}</pre></td>
					</tr>
					<tr>
						<th scope="row">Actions</th>
						<td><pre><a href="{{ .commit_object.Hash }}.patch">Get as Patch</a></pre></td>
					</tr>
				</tbody>
			</table>
		</div>
		<div class="padding-wrapper">
			{{ $parent_commit_object := .parent_commit_object }}
			{{ $commit_object := .commit_object }}
			{{ range .file_patches }}
				<div class="file-patch toggle-on-wrapper">
					<input type="checkbox" id="toggle-{{ .From.Hash }}{{ .To.Hash }}" class="file-toggle toggle-on-toggle">
					<label for="toggle-{{ .From.Hash }}{{ .To.Hash }}" class="file-header toggle-on-header">
						<span>
							--- a/<a href="../../tree/{{ .From.Path }}?commit={{ $parent_commit_object.Hash }}">{{ .From.Path }}</a> {{ .From.Mode }}
							<br />
							+++ b/<a href="../../tree/{{ .To.Path }}?commit={{ $commit_object.Hash }}">{{ .To.Path }}</a> {{ .To.Mode }}
						</span>
					</label>
					<div class="file-content toggle-on-content scroll">
						{{ range .Chunks }}
							{{ if eq .Type 0 }}
								<pre class="chunk chunk-unchanged">{{ .Content }}</pre>
							{{ else if eq .Type 1 }}
								<pre class="chunk chunk-addition">{{ .Content }}</pre>
							{{ else if eq .Type 2 }}
								<pre class="chunk chunk-deletion">{{ .Content }}</pre>
							{{ else }}
								<pre class="chunk chunk-unknown">{{ .Content }}</pre>
							{{ end }}
						{{ end }}
					</div>
				</div>
			{{ end }}
		</div>
		<footer>
			{{ template "footer" . }}
		</footer>
	</body>
</html>
{{- end -}}