aboutsummaryrefslogtreecommitdiff
path: root/internal/oldgit/patch.go
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-04-06 01:30:02 +0800
committerRunxi Yu <me@runxiyu.org>2025-04-06 01:30:02 +0800
commit7b7e20e60c1c6b858ae0c4eb78d414912263642f (patch)
tree988bc0a39e142dcfd52af98ec65e11578474002c /internal/oldgit/patch.go
parentirc: Factor the IRC stuff into its own package (diff)
downloadforge-7b7e20e60c1c6b858ae0c4eb78d414912263642f.tar.gz
forge-7b7e20e60c1c6b858ae0c4eb78d414912263642f.tar.zst
forge-7b7e20e60c1c6b858ae0c4eb78d414912263642f.zip
oldgit: Separate some go-git stuff into here
Diffstat (limited to 'internal/oldgit/patch.go')
-rw-r--r--internal/oldgit/patch.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/internal/oldgit/patch.go b/internal/oldgit/patch.go
new file mode 100644
index 0000000..329bdfb
--- /dev/null
+++ b/internal/oldgit/patch.go
@@ -0,0 +1,39 @@
+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