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