diff options
author | Runxi Yu <me@runxiyu.org> | 2025-04-06 01:30:02 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-04-06 01:30:02 +0800 |
commit | 7b7e20e60c1c6b858ae0c4eb78d414912263642f (patch) | |
tree | 988bc0a39e142dcfd52af98ec65e11578474002c /internal/oldgit | |
parent | irc: Factor the IRC stuff into its own package (diff) | |
download | forge-7b7e20e60c1c6b858ae0c4eb78d414912263642f.tar.gz forge-7b7e20e60c1c6b858ae0c4eb78d414912263642f.tar.zst forge-7b7e20e60c1c6b858ae0c4eb78d414912263642f.zip |
oldgit: Separate some go-git stuff into here
Diffstat (limited to '')
-rw-r--r-- | internal/oldgit/fmtpatch.go (renamed from git_format_patch.go) | 8 | ||||
-rw-r--r-- | internal/oldgit/patch.go | 39 |
2 files changed, 43 insertions, 4 deletions
diff --git a/git_format_patch.go b/internal/oldgit/fmtpatch.go index 5628ce1..79be5d8 100644 --- a/git_format_patch.go +++ b/internal/oldgit/fmtpatch.go @@ -1,7 +1,7 @@ // SPDX-License-Identifier: AGPL-3.0-only // SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> -package forge +package oldgit import ( "bytes" @@ -12,16 +12,16 @@ import ( "github.com/go-git/go-git/v5/plumbing/object" ) -// fmtCommitPatch formats a commit object as if it was returned by +// FmtCommitPatch formats a commit object as if it was returned by // git-format-patch. -func fmtCommitPatch(commit *object.Commit) (final string, err error) { +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 { + if _, patch, err = CommitToPatch(commit); err != nil { return "", err } 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 |