aboutsummaryrefslogtreecommitdiff
path: root/git_format_patch.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 /git_format_patch.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 'git_format_patch.go')
-rw-r--r--git_format_patch.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/git_format_patch.go b/git_format_patch.go
new file mode 100644
index 0000000..c899ed5
--- /dev/null
+++ b/git_format_patch.go
@@ -0,0 +1,59 @@
+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
+}
+