chore: simplify GetNote (#9985)

Return the Note object (avoid C-style functions).

Motivation to refactor this function is to avoid the function that uses last commit cache for git-notes, because it is not needed at the scale of git-notes. In the worst case it can be considered to make a patch to git to get the message and commitID, because git seems to have efficient code to do this (for getting messages, but does not expose the commit id).

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9985
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
Reviewed-by: Michael Kriese <michael.kriese@gmx.de>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
Gusted
2025-11-06 13:23:35 +01:00
committed by Gusted
parent 54b3066e45
commit f9a6460cec
5 changed files with 29 additions and 41 deletions
+12 -22
View File
@@ -1,4 +1,5 @@
// Copyright 2019 The Gitea Authors. All rights reserved.
// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package git
@@ -7,7 +8,6 @@ import (
"context"
"io"
"os"
"strings"
"forgejo.org/modules/log"
)
@@ -23,16 +23,14 @@ type Note struct {
}
// GetNote retrieves the git-notes data for a given commit.
// FIXME: Add LastCommitCache support
func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) error {
func GetNote(ctx context.Context, repo *Repository, commitID string) (*Note, error) {
log.Trace("Searching for git note corresponding to the commit %q in the repository %q", commitID, repo.Path)
notes, err := repo.GetCommit(NotesRef)
if err != nil {
if IsErrNotExist(err) {
return err
if !IsErrNotExist(err) {
log.Error("Unable to get commit from ref %q. Error: %v", NotesRef, err)
}
log.Error("Unable to get commit from ref %q. Error: %v", NotesRef, err)
return err
return nil, err
}
path := ""
@@ -58,7 +56,7 @@ func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note)
if !IsErrNotExist(err) {
log.Error("Unable to find git note corresponding to the commit %q. Error: %v", originalCommitID, err)
}
return err
return nil, err
}
}
@@ -66,7 +64,7 @@ func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note)
dataRc, err := blob.DataAsync()
if err != nil {
log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
return err
return nil, err
}
closed := false
defer func() {
@@ -77,26 +75,18 @@ func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note)
d, err := io.ReadAll(dataRc)
if err != nil {
log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
return err
return nil, err
}
_ = dataRc.Close()
closed = true
note.Message = d
treePath := ""
if idx := strings.LastIndex(path, "/"); idx > -1 {
treePath = path[:idx]
path = path[idx+1:]
}
lastCommits, err := GetLastCommitForPaths(ctx, notes, treePath, []string{path})
lastCommit, err := repo.getCommitByPathWithID(notes.ID, path)
if err != nil {
log.Error("Unable to get the commit for the path %q. Error: %v", treePath, err)
return err
log.Error("Unable to get the commit for the path %q. Error: %v", path, err)
return nil, err
}
note.Commit = lastCommits[path]
return nil
return &Note{Message: d, Commit: lastCommit}, nil
}
func SetNote(ctx context.Context, repo *Repository, commitID, notes, doerName, doerEmail string) error {