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.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // +build gogit
  6. package git
  7. import (
  8. "context"
  9. "io/ioutil"
  10. "code.gitea.io/gitea/modules/log"
  11. "github.com/go-git/go-git/v5/plumbing/object"
  12. )
  13. // GetNote retrieves the git-notes data for a given commit.
  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. log.Error("Unable to get commit from ref %q. Error: %v", NotesRef, err)
  19. return err
  20. }
  21. remainingCommitID := commitID
  22. path := ""
  23. currentTree := notes.Tree.gogitTree
  24. log.Trace("Found tree with ID %q while searching for git note corresponding to the commit %q", currentTree.Entries[0].Name, commitID)
  25. var file *object.File
  26. for len(remainingCommitID) > 2 {
  27. file, err = currentTree.File(remainingCommitID)
  28. if err == nil {
  29. path += remainingCommitID
  30. break
  31. }
  32. if err == object.ErrFileNotFound {
  33. currentTree, err = currentTree.Tree(remainingCommitID[0:2])
  34. path += remainingCommitID[0:2] + "/"
  35. remainingCommitID = remainingCommitID[2:]
  36. }
  37. if err != nil {
  38. if err == object.ErrDirectoryNotFound {
  39. return ErrNotExist{ID: remainingCommitID, RelPath: path}
  40. }
  41. log.Error("Unable to find git note corresponding to the commit %q. Error: %v", commitID, err)
  42. return err
  43. }
  44. }
  45. blob := file.Blob
  46. dataRc, err := blob.Reader()
  47. if err != nil {
  48. log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
  49. return err
  50. }
  51. defer dataRc.Close()
  52. d, err := ioutil.ReadAll(dataRc)
  53. if err != nil {
  54. log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
  55. return err
  56. }
  57. note.Message = d
  58. commitNodeIndex, commitGraphFile := repo.CommitNodeIndex()
  59. if commitGraphFile != nil {
  60. defer commitGraphFile.Close()
  61. }
  62. commitNode, err := commitNodeIndex.Get(notes.ID)
  63. if err != nil {
  64. return err
  65. }
  66. lastCommits, err := GetLastCommitForPaths(ctx, commitNode, "", []string{path})
  67. if err != nil {
  68. log.Error("Unable to get the commit for the path %q. Error: %v", path, err)
  69. return err
  70. }
  71. note.Commit = convertCommit(lastCommits[path])
  72. return nil
  73. }