2020-12-17 22:00:47 +08:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-17 22:00:47 +08:00
|
|
|
|
2021-08-25 00:47:09 +08:00
|
|
|
//go:build !gogit
|
2020-12-17 22:00:47 +08:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2021-06-07 07:44:58 +08:00
|
|
|
"context"
|
2021-09-22 13:38:34 +08:00
|
|
|
"io"
|
2021-04-22 02:00:27 +08:00
|
|
|
"strings"
|
2021-08-11 09:01:40 +08:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-12-17 22:00:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetNote retrieves the git-notes data for a given commit.
|
2021-10-08 21:08:22 +08:00
|
|
|
// FIXME: Add LastCommitCache support
|
2021-06-07 07:44:58 +08:00
|
|
|
func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) error {
|
2021-08-11 09:01:40 +08:00
|
|
|
log.Trace("Searching for git note corresponding to the commit %q in the repository %q", commitID, repo.Path)
|
2020-12-17 22:00:47 +08:00
|
|
|
notes, err := repo.GetCommit(NotesRef)
|
|
|
|
if err != nil {
|
2022-02-06 15:11:35 +08:00
|
|
|
if IsErrNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-11 09:01:40 +08:00
|
|
|
log.Error("Unable to get commit from ref %q. Error: %v", NotesRef, err)
|
2020-12-17 22:00:47 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := ""
|
|
|
|
|
|
|
|
tree := ¬es.Tree
|
2021-08-11 09:01:40 +08:00
|
|
|
log.Trace("Found tree with ID %q while searching for git note corresponding to the commit %q", tree.ID, commitID)
|
2020-12-17 22:00:47 +08:00
|
|
|
|
|
|
|
var entry *TreeEntry
|
2021-08-11 09:01:40 +08:00
|
|
|
originalCommitID := commitID
|
2020-12-17 22:00:47 +08:00
|
|
|
for len(commitID) > 2 {
|
|
|
|
entry, err = tree.GetTreeEntryByPath(commitID)
|
|
|
|
if err == nil {
|
|
|
|
path += commitID
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if IsErrNotExist(err) {
|
|
|
|
tree, err = tree.SubTree(commitID[0:2])
|
|
|
|
path += commitID[0:2] + "/"
|
|
|
|
commitID = commitID[2:]
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-06-07 16:39:50 +08:00
|
|
|
// Err may have been updated by the SubTree we need to recheck if it's again an ErrNotExist
|
|
|
|
if !IsErrNotExist(err) {
|
|
|
|
log.Error("Unable to find git note corresponding to the commit %q. Error: %v", originalCommitID, err)
|
|
|
|
}
|
2020-12-17 22:00:47 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 09:01:40 +08:00
|
|
|
blob := entry.Blob()
|
|
|
|
dataRc, err := blob.DataAsync()
|
2020-12-17 22:00:47 +08:00
|
|
|
if err != nil {
|
2021-08-11 09:01:40 +08:00
|
|
|
log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
|
2020-12-17 22:00:47 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-05-10 09:27:03 +08:00
|
|
|
closed := false
|
|
|
|
defer func() {
|
|
|
|
if !closed {
|
|
|
|
_ = dataRc.Close()
|
|
|
|
}
|
|
|
|
}()
|
2021-09-22 13:38:34 +08:00
|
|
|
d, err := io.ReadAll(dataRc)
|
2020-12-17 22:00:47 +08:00
|
|
|
if err != nil {
|
2021-08-11 09:01:40 +08:00
|
|
|
log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
|
2020-12-17 22:00:47 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-05-10 09:27:03 +08:00
|
|
|
_ = dataRc.Close()
|
|
|
|
closed = true
|
2020-12-17 22:00:47 +08:00
|
|
|
note.Message = d
|
|
|
|
|
2021-04-22 02:00:27 +08:00
|
|
|
treePath := ""
|
|
|
|
if idx := strings.LastIndex(path, "/"); idx > -1 {
|
|
|
|
treePath = path[:idx]
|
|
|
|
path = path[idx+1:]
|
|
|
|
}
|
|
|
|
|
2022-07-25 23:39:42 +08:00
|
|
|
lastCommits, err := GetLastCommitForPaths(ctx, notes, treePath, []string{path})
|
2020-12-17 22:00:47 +08:00
|
|
|
if err != nil {
|
2021-08-11 09:01:40 +08:00
|
|
|
log.Error("Unable to get the commit for the path %q. Error: %v", treePath, err)
|
2020-12-17 22:00:47 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-06-21 06:00:46 +08:00
|
|
|
note.Commit = lastCommits[path]
|
2020-12-17 22:00:47 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|