You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

notes_gogit.go 2.4KB

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