From 7b7e20e60c1c6b858ae0c4eb78d414912263642f Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 6 Apr 2025 01:30:02 +0800 Subject: oldgit: Separate some go-git stuff into here --- git_format_patch.go | 56 --------------------------------------------- git_misc.go | 32 -------------------------- http_handle_repo_commit.go | 5 ++-- internal/oldgit/fmtpatch.go | 56 +++++++++++++++++++++++++++++++++++++++++++++ internal/oldgit/patch.go | 39 +++++++++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 90 deletions(-) delete mode 100644 git_format_patch.go create mode 100644 internal/oldgit/fmtpatch.go create mode 100644 internal/oldgit/patch.go diff --git a/git_format_patch.go b/git_format_patch.go deleted file mode 100644 index 5628ce1..0000000 --- a/git_format_patch.go +++ /dev/null @@ -1,56 +0,0 @@ -// SPDX-License-Identifier: AGPL-3.0-only -// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu - -package forge - -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/git_misc.go b/git_misc.go index 40247f1..83ee11c 100644 --- a/git_misc.go +++ b/git_misc.go @@ -10,7 +10,6 @@ import ( "iter" "github.com/go-git/go-git/v5" - "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" "github.com/jackc/pgx/v5/pgtype" ) @@ -86,34 +85,3 @@ func commitIterSeqErr(commitIter object.CommitIter) (iter.Seq[*object.Commit], * } }, &err } - -// 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 diff --git a/http_handle_repo_commit.go b/http_handle_repo_commit.go index 196489f..22ee20d 100644 --- a/http_handle_repo_commit.go +++ b/http_handle_repo_commit.go @@ -14,6 +14,7 @@ import ( "github.com/go-git/go-git/v5/plumbing/format/diff" "github.com/go-git/go-git/v5/plumbing/object" "go.lindenii.runxiyu.org/forge/internal/misc" + "go.lindenii.runxiyu.org/forge/internal/oldgit" "go.lindenii.runxiyu.org/forge/internal/web" ) @@ -52,7 +53,7 @@ func (s *Server) httpHandleRepoCommit(writer http.ResponseWriter, request *http. } if commitIDStrSpecNoSuffix != commitIDStrSpec { var patchStr string - if patchStr, err = fmtCommitPatch(commitObj); err != nil { + if patchStr, err = oldgit.FmtCommitPatch(commitObj); err != nil { web.ErrorPage500(s.templates, writer, params, "Error formatting patch: "+err.Error()) return } @@ -69,7 +70,7 @@ func (s *Server) httpHandleRepoCommit(writer http.ResponseWriter, request *http. params["commit_object"] = commitObj params["commit_id"] = commitIDStr - parentCommitHash, patch, err = commitToPatch(commitObj) + parentCommitHash, patch, err = oldgit.CommitToPatch(commitObj) if err != nil { web.ErrorPage500(s.templates, writer, params, "Error getting patch from commit: "+err.Error()) return diff --git a/internal/oldgit/fmtpatch.go b/internal/oldgit/fmtpatch.go new file mode 100644 index 0000000..79be5d8 --- /dev/null +++ b/internal/oldgit/fmtpatch.go @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu + +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/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 -- cgit v1.2.3