2020-12-17 07:00:47 -07:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// +build !gogit
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2021-06-06 16:44:58 -07:00
|
|
|
"context"
|
2020-12-17 07:00:47 -07:00
|
|
|
"io/ioutil"
|
2021-04-21 11:00:27 -07:00
|
|
|
"strings"
|
2020-12-17 07:00:47 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetNote retrieves the git-notes data for a given commit.
|
2021-06-06 16:44:58 -07:00
|
|
|
func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) error {
|
2020-12-17 07:00:47 -07:00
|
|
|
notes, err := repo.GetCommit(NotesRef)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := ""
|
|
|
|
|
|
|
|
tree := ¬es.Tree
|
|
|
|
|
|
|
|
var entry *TreeEntry
|
|
|
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dataRc, err := entry.Blob().DataAsync()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-09 18:27:03 -07:00
|
|
|
closed := false
|
|
|
|
defer func() {
|
|
|
|
if !closed {
|
|
|
|
_ = dataRc.Close()
|
|
|
|
}
|
|
|
|
}()
|
2020-12-17 07:00:47 -07:00
|
|
|
d, err := ioutil.ReadAll(dataRc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-09 18:27:03 -07:00
|
|
|
_ = dataRc.Close()
|
|
|
|
closed = true
|
2020-12-17 07:00:47 -07:00
|
|
|
note.Message = d
|
|
|
|
|
2021-04-21 11:00:27 -07:00
|
|
|
treePath := ""
|
|
|
|
if idx := strings.LastIndex(path, "/"); idx > -1 {
|
|
|
|
treePath = path[:idx]
|
|
|
|
path = path[idx+1:]
|
|
|
|
}
|
|
|
|
|
2021-06-06 16:44:58 -07:00
|
|
|
lastCommits, err := GetLastCommitForPaths(ctx, notes, treePath, []string{path})
|
2020-12-17 07:00:47 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
note.Commit = lastCommits[0]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|