aboutsummaryrefslogtreecommitdiff
path: root/forged/internal/oldgit
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-04-06 09:26:46 +0800
committerRunxi Yu <me@runxiyu.org>2025-04-06 09:27:53 +0800
commitda1d8f4e7c332c7109427915e6459b10209cedce (patch)
tree280b921be3b51f93d82d916b4eaa89387b7102cc /forged/internal/oldgit
parentgit2c, git2d: Rename cmd1 and cmd2 descriptively (diff)
downloadforge-0.1.32.tar.gz
forge-0.1.32.tar.zst
forge-0.1.32.zip
Move the Go stuff to ./forged/v0.1.32
Diffstat (limited to 'forged/internal/oldgit')
-rw-r--r--forged/internal/oldgit/fmtpatch.go56
-rw-r--r--forged/internal/oldgit/oldgit.go5
-rw-r--r--forged/internal/oldgit/patch.go42
3 files changed, 103 insertions, 0 deletions
diff --git a/forged/internal/oldgit/fmtpatch.go b/forged/internal/oldgit/fmtpatch.go
new file mode 100644
index 0000000..79be5d8
--- /dev/null
+++ b/forged/internal/oldgit/fmtpatch.go
@@ -0,0 +1,56 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
+
+package oldgit
+
+import (
+ "bytes"
+ "fmt"
+ "strings"
+ "time"
+
+ "github.com/go-git/go-git/v5/plumbing/object"
+)
+
+// FmtCommitPatch formats a commit object as if it was returned by
+// git-format-patch.
+func FmtCommitPatch(commit *object.Commit) (final string, err error) {
+ var patch *object.Patch
+ var buf bytes.Buffer
+ var author object.Signature
+ var date string
+ var commitTitle, commitDetails string
+
+ if _, patch, err = CommitToPatch(commit); err != nil {
+ return "", err
+ }
+
+ author = commit.Author
+ date = author.When.Format(time.RFC1123Z)
+
+ commitTitle, commitDetails, _ = strings.Cut(commit.Message, "\n")
+
+ // This date is hardcoded in Git.
+ 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", commitTitle)
+
+ if commitDetails != "" {
+ commitDetails1, commitDetails2, _ := strings.Cut(commitDetails, "\n")
+ if strings.TrimSpace(commitDetails1) == "" {
+ commitDetails = commitDetails2
+ }
+ buf.WriteString(commitDetails)
+ 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
+}
diff --git a/forged/internal/oldgit/oldgit.go b/forged/internal/oldgit/oldgit.go
new file mode 100644
index 0000000..4c99d6a
--- /dev/null
+++ b/forged/internal/oldgit/oldgit.go
@@ -0,0 +1,5 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
+
+// Package oldgit provides deprecated functions that depend on go-git.
+package oldgit
diff --git a/forged/internal/oldgit/patch.go b/forged/internal/oldgit/patch.go
new file mode 100644
index 0000000..30bf8e8
--- /dev/null
+++ b/forged/internal/oldgit/patch.go
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
+
+package oldgit
+
+import (
+ "errors"
+
+ "github.com/go-git/go-git/v5/plumbing"
+ "github.com/go-git/go-git/v5/plumbing/object"
+)
+
+// CommitToPatch creates an [object.Patch] from the first parent of a given
+// [object.Commit].
+//
+// TODO: This function should be deprecated as it only diffs with the first
+// parent and does not correctly handle merge commits.
+func CommitToPatch(commit *object.Commit) (parentCommitHash plumbing.Hash, patch *object.Patch, err error) {
+ var parentCommit *object.Commit
+ var commitTree *object.Tree
+
+ parentCommit, err = commit.Parent(0)
+ switch {
+ case errors.Is(err, object.ErrParentNotFound):
+ if commitTree, err = commit.Tree(); err != nil {
+ return
+ }
+ if patch, err = NullTree.Patch(commitTree); err != nil {
+ return
+ }
+ case err != nil:
+ return
+ default:
+ parentCommitHash = parentCommit.Hash
+ if patch, err = parentCommit.Patch(commit); err != nil {
+ return
+ }
+ }
+ return
+}
+
+var NullTree object.Tree //nolint:gochecknoglobals