code.gitea.io/gitea@v1.19.3/modules/git/notes_gogit.go (about) 1 // Copyright 2019 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 //go:build gogit 5 6 package git 7 8 import ( 9 "context" 10 "io" 11 12 "code.gitea.io/gitea/modules/log" 13 14 "github.com/go-git/go-git/v5/plumbing/object" 15 ) 16 17 // GetNote retrieves the git-notes data for a given commit. 18 // FIXME: Add LastCommitCache support 19 func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) error { 20 log.Trace("Searching for git note corresponding to the commit %q in the repository %q", commitID, repo.Path) 21 notes, err := repo.GetCommit(NotesRef) 22 if err != nil { 23 if IsErrNotExist(err) { 24 return err 25 } 26 log.Error("Unable to get commit from ref %q. Error: %v", NotesRef, err) 27 return err 28 } 29 30 remainingCommitID := commitID 31 path := "" 32 currentTree := notes.Tree.gogitTree 33 log.Trace("Found tree with ID %q while searching for git note corresponding to the commit %q", currentTree.Entries[0].Name, commitID) 34 var file *object.File 35 for len(remainingCommitID) > 2 { 36 file, err = currentTree.File(remainingCommitID) 37 if err == nil { 38 path += remainingCommitID 39 break 40 } 41 if err == object.ErrFileNotFound { 42 currentTree, err = currentTree.Tree(remainingCommitID[0:2]) 43 path += remainingCommitID[0:2] + "/" 44 remainingCommitID = remainingCommitID[2:] 45 } 46 if err != nil { 47 if err == object.ErrDirectoryNotFound { 48 return ErrNotExist{ID: remainingCommitID, RelPath: path} 49 } 50 log.Error("Unable to find git note corresponding to the commit %q. Error: %v", commitID, err) 51 return err 52 } 53 } 54 55 blob := file.Blob 56 dataRc, err := blob.Reader() 57 if err != nil { 58 log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err) 59 return err 60 } 61 62 defer dataRc.Close() 63 d, err := io.ReadAll(dataRc) 64 if err != nil { 65 log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err) 66 return err 67 } 68 note.Message = d 69 70 commitNodeIndex, commitGraphFile := repo.CommitNodeIndex() 71 if commitGraphFile != nil { 72 defer commitGraphFile.Close() 73 } 74 75 commitNode, err := commitNodeIndex.Get(notes.ID) 76 if err != nil { 77 return err 78 } 79 80 lastCommits, err := GetLastCommitForPaths(ctx, nil, commitNode, "", []string{path}) 81 if err != nil { 82 log.Error("Unable to get the commit for the path %q. Error: %v", path, err) 83 return err 84 } 85 note.Commit = lastCommits[path] 86 87 return nil 88 }