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.

80 lines
1.8 KiB

  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. package git
  5. import (
  6. "io/ioutil"
  7. "github.com/go-git/go-git/v5/plumbing/object"
  8. )
  9. // NotesRef is the git ref where Gitea will look for git-notes data.
  10. // The value ("refs/notes/commits") is the default ref used by git-notes.
  11. const NotesRef = "refs/notes/commits"
  12. // Note stores information about a note created using git-notes.
  13. type Note struct {
  14. Message []byte
  15. Commit *Commit
  16. }
  17. // GetNote retrieves the git-notes data for a given commit.
  18. func GetNote(repo *Repository, commitID string, note *Note) error {
  19. notes, err := repo.GetCommit(NotesRef)
  20. if err != nil {
  21. return err
  22. }
  23. remainingCommitID := commitID
  24. path := ""
  25. currentTree := notes.Tree.gogitTree
  26. var file *object.File
  27. for len(remainingCommitID) > 2 {
  28. file, err = currentTree.File(remainingCommitID)
  29. if err == nil {
  30. path += remainingCommitID
  31. break
  32. }
  33. if err == object.ErrFileNotFound {
  34. currentTree, err = currentTree.Tree(remainingCommitID[0:2])
  35. path += remainingCommitID[0:2] + "/"
  36. remainingCommitID = remainingCommitID[2:]
  37. }
  38. if err != nil {
  39. return err
  40. }
  41. }
  42. blob := file.Blob
  43. dataRc, err := blob.Reader()
  44. if err != nil {
  45. return err
  46. }
  47. defer dataRc.Close()
  48. d, err := ioutil.ReadAll(dataRc)
  49. if err != nil {
  50. return err
  51. }
  52. note.Message = d
  53. commitNodeIndex, commitGraphFile := repo.CommitNodeIndex()
  54. if commitGraphFile != nil {
  55. defer commitGraphFile.Close()
  56. }
  57. commitNode, err := commitNodeIndex.Get(notes.ID)
  58. if err != nil {
  59. return err
  60. }
  61. lastCommits, err := getLastCommitForPaths(commitNode, "", []string{path})
  62. if err != nil {
  63. return err
  64. }
  65. note.Commit = convertCommit(lastCommits[path])
  66. return nil
  67. }