2020-12-17 14:00:47 +00:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-17 14:00:47 +00:00
|
|
|
|
2021-08-24 11:47:09 -05:00
|
|
|
//go:build gogit
|
2020-12-17 14:00:47 +00:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2021-06-07 00:44:58 +01:00
|
|
|
"context"
|
2021-09-22 13:38:34 +08:00
|
|
|
"io"
|
2020-12-17 14:00:47 +00:00
|
|
|
|
2021-08-11 06:31:40 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
|
2023-12-13 21:02:00 +00:00
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
2020-12-17 14:00:47 +00:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetNote retrieves the git-notes data for a given commit.
|
2021-10-08 14:08:22 +01:00
|
|
|
// FIXME: Add LastCommitCache support
|
2021-06-07 00:44:58 +01:00
|
|
|
func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) error {
|
2021-08-11 06:31:40 +05:30
|
|
|
log.Trace("Searching for git note corresponding to the commit %q in the repository %q", commitID, repo.Path)
|
2020-12-17 14:00:47 +00:00
|
|
|
notes, err := repo.GetCommit(NotesRef)
|
|
|
|
if err != nil {
|
2022-02-06 07:11:35 +00:00
|
|
|
if IsErrNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-11 06:31:40 +05:30
|
|
|
log.Error("Unable to get commit from ref %q. Error: %v", NotesRef, err)
|
2020-12-17 14:00:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
remainingCommitID := commitID
|
|
|
|
path := ""
|
|
|
|
currentTree := notes.Tree.gogitTree
|
2021-08-11 06:31:40 +05:30
|
|
|
log.Trace("Found tree with ID %q while searching for git note corresponding to the commit %q", currentTree.Entries[0].Name, commitID)
|
2020-12-17 14:00:47 +00:00
|
|
|
var file *object.File
|
|
|
|
for len(remainingCommitID) > 2 {
|
|
|
|
file, err = currentTree.File(remainingCommitID)
|
|
|
|
if err == nil {
|
|
|
|
path += remainingCommitID
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err == object.ErrFileNotFound {
|
|
|
|
currentTree, err = currentTree.Tree(remainingCommitID[0:2])
|
|
|
|
path += remainingCommitID[0:2] + "/"
|
|
|
|
remainingCommitID = remainingCommitID[2:]
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-08-09 20:54:34 +05:30
|
|
|
if err == object.ErrDirectoryNotFound {
|
|
|
|
return ErrNotExist{ID: remainingCommitID, RelPath: path}
|
|
|
|
}
|
2021-08-11 06:31:40 +05:30
|
|
|
log.Error("Unable to find git note corresponding to the commit %q. Error: %v", commitID, err)
|
2020-12-17 14:00:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
blob := file.Blob
|
|
|
|
dataRc, err := blob.Reader()
|
|
|
|
if err != nil {
|
2021-08-11 06:31:40 +05:30
|
|
|
log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
|
2020-12-17 14:00:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer dataRc.Close()
|
2021-09-22 13:38:34 +08:00
|
|
|
d, err := io.ReadAll(dataRc)
|
2020-12-17 14:00:47 +00:00
|
|
|
if err != nil {
|
2021-08-11 06:31:40 +05:30
|
|
|
log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
|
2020-12-17 14:00:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
note.Message = d
|
|
|
|
|
|
|
|
commitNodeIndex, commitGraphFile := repo.CommitNodeIndex()
|
|
|
|
if commitGraphFile != nil {
|
|
|
|
defer commitGraphFile.Close()
|
|
|
|
}
|
|
|
|
|
2023-12-13 21:02:00 +00:00
|
|
|
commitNode, err := commitNodeIndex.Get(plumbing.Hash(notes.ID.RawValue()))
|
2020-12-17 14:00:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-08 14:08:22 +01:00
|
|
|
lastCommits, err := GetLastCommitForPaths(ctx, nil, commitNode, "", []string{path})
|
2020-12-17 14:00:47 +00:00
|
|
|
if err != nil {
|
2021-08-11 06:31:40 +05:30
|
|
|
log.Error("Unable to get the commit for the path %q. Error: %v", path, err)
|
2020-12-17 14:00:47 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-07-25 16:39:42 +01:00
|
|
|
note.Commit = lastCommits[path]
|
2020-12-17 14:00:47 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|