Lindenii Project Forge
Commit info | |
---|---|
ID | 24d66ad693eee6df6df37be8b64d594221a6221e |
Author | Runxi Yu<me@runxiyu.org> |
Author date | Wed, 12 Feb 2025 01:42:38 +0800 |
Committer | Runxi Yu<me@runxiyu.org> |
Committer date | Wed, 12 Feb 2025 01:42:38 +0800 |
Actions | Get patch |
repo_commit: Fix format_patch handling of root commits
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)
_, patch, err := get_patch_from_commit(commit)
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)
return "", misc.Wrap_one_error(err_getting_patch_of_commit, 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 ( "errors" "io" "path/filepath" "strings" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" "go.lindenii.runxiyu.org/lindenii-common/misc" ) var err_unsafe_path = errors.New("Unsafe path")
var err_getting_commit_tree = errors.New("Error getting commit tree") var err_getting_patch_of_commit = errors.New("Error getting patch of commit") var err_getting_parent_commit_object = errors.New("Error getting parent commit object")
func open_git_repo(group_name, repo_name string) (*git.Repository, error) { group_name, group_name_ok := misc.Sanitize_path(group_name) if !group_name_ok { return nil, err_unsafe_path } repo_name, repo_name_ok := misc.Sanitize_path(repo_name) if !repo_name_ok { return nil, err_unsafe_path } return git.PlainOpen(filepath.Join(config.Git.Root, group_name, repo_name+".git")) } func build_display_git_tree(tree *object.Tree) []display_git_tree_entry_t { display_git_tree := make([]display_git_tree_entry_t, 0) for _, entry := range tree.Entries { display_git_tree_entry := display_git_tree_entry_t{} os_mode, err := entry.Mode.ToOSFileMode() if err != nil { display_git_tree_entry.Mode = "x---" } else { display_git_tree_entry.Mode = os_mode.String()[:4] } display_git_tree_entry.Is_file = entry.Mode.IsFile() display_git_tree_entry.Size, err = tree.Size(entry.Name) if err != nil { display_git_tree_entry.Size = 0 } display_git_tree_entry.Name = strings.TrimPrefix(entry.Name, "/") display_git_tree = append(display_git_tree, display_git_tree_entry) } return display_git_tree } var err_get_recent_commits = errors.New("Error getting recent commits:") func get_recent_commits(repo *git.Repository, head_hash plumbing.Hash, number_of_commits int) (recent_commits []*object.Commit, err error) { commit_iter, err := repo.Log(&git.LogOptions{From: head_hash}) if err != nil { err = misc.Wrap_one_error(err_get_recent_commits, err) return nil, err } recent_commits = make([]*object.Commit, 0) defer commit_iter.Close() if number_of_commits < 0 { for { this_recent_commit, err := commit_iter.Next() if errors.Is(err, io.EOF) { return recent_commits, nil } else if err != nil { err = misc.Wrap_one_error(err_get_recent_commits, err) return nil, err } recent_commits = append(recent_commits, this_recent_commit) } } else { for range number_of_commits { this_recent_commit, err := commit_iter.Next() if errors.Is(err, io.EOF) { return recent_commits, nil } else if err != nil { err = misc.Wrap_one_error(err_get_recent_commits, err) return nil, err } recent_commits = append(recent_commits, this_recent_commit) } } return recent_commits, err }
func get_patch_from_commit(commit_object *object.Commit) (parent_commit_hash plumbing.Hash, patch *object.Patch, ret_err error) { parent_commit_object, err := commit_object.Parent(0) if errors.Is(err, object.ErrParentNotFound) { commit_tree, err := commit_object.Tree() if err != nil { ret_err = misc.Wrap_one_error(err_getting_commit_tree, err) return } patch, err = (&object.Tree{}).Patch(commit_tree) if err != nil { ret_err = misc.Wrap_one_error(err_getting_patch_of_commit, err) return } } else if err != nil { ret_err = misc.Wrap_one_error(err_getting_parent_commit_object, err) return } else { parent_commit_hash = parent_commit_object.Hash patch, err = parent_commit_object.Patch(commit_object) if err != nil { ret_err = misc.Wrap_one_error(err_getting_patch_of_commit, err) return } } return }
package main import (
"errors"
"net/http" "strings" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/filemode" "github.com/go-git/go-git/v5/plumbing/format/diff"
"github.com/go-git/go-git/v5/plumbing/object"
"go.lindenii.runxiyu.org/lindenii-common/misc" ) 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) return } data["commit_object"] = commit_object data["commit_id"] = commit_id_string
var patch *object.Patch parent_commit_object, err := commit_object.Parent(0) if errors.Is(err, object.ErrParentNotFound) { commit_tree, err := commit_object.Tree() if err != nil { _, _ = w.Write([]byte("Error getting commit tree (for comparing against an empty tree): " + err.Error())) return } patch, err = (&object.Tree{}).Patch(commit_tree) if err != nil { _, _ = w.Write([]byte("Error getting patch of commit: " + err.Error())) return } } else if err != nil { _, _ = w.Write([]byte("Error getting parent commit object: " + err.Error())) return } else { data["parent_commit_hash"] = parent_commit_object.Hash.String() patch, err = parent_commit_object.Patch(commit_object) if err != nil { _, _ = w.Write([]byte("Error getting patch of commit: " + err.Error())) return } }
parent_commit_hash, patch, err := get_patch_from_commit(commit_object) data["parent_commit_hash"] = parent_commit_hash.String()
data["patch"] = patch // TODO: Remove unnecessary context // TODO: Prepend "+"/"-"/" " instead of solely distinguishing based on color usable_file_patches := make([]usable_file_patch, 0) for _, file_patch := range patch.FilePatches() { from, to := file_patch.Files() if from == nil { from = fake_diff_file_null } if to == nil { to = fake_diff_file_null } 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 } } type fake_diff_file struct { hash plumbing.Hash mode filemode.FileMode path string } func (f fake_diff_file) Hash() plumbing.Hash { return f.hash } func (f fake_diff_file) Mode() filemode.FileMode { return f.mode } func (f fake_diff_file) Path() string { return f.path } var fake_diff_file_null = fake_diff_file{ hash: plumbing.NewHash("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"), mode: misc.First_or_panic(filemode.New("100644")), path: "", }