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.go 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. )
  8. // NotesRef is the git ref where Gitea will look for git-notes data.
  9. // The value ("refs/notes/commits") is the default ref used by git-notes.
  10. const NotesRef = "refs/notes/commits"
  11. // Note stores information about a note created using git-notes.
  12. type Note struct {
  13. Message []byte
  14. Commit *Commit
  15. }
  16. // GetNote retrieves the git-notes data for a given commit.
  17. func GetNote(repo *Repository, commitID string, note *Note) error {
  18. notes, err := repo.GetCommit(NotesRef)
  19. if err != nil {
  20. return err
  21. }
  22. entry, err := notes.GetTreeEntryByPath(commitID)
  23. if err != nil {
  24. return err
  25. }
  26. blob := entry.Blob()
  27. dataRc, err := blob.DataAsync()
  28. if err != nil {
  29. return err
  30. }
  31. defer dataRc.Close()
  32. d, err := ioutil.ReadAll(dataRc)
  33. if err != nil {
  34. return err
  35. }
  36. note.Message = d
  37. commit, err := repo.gogitRepo.CommitObject(notes.ID)
  38. if err != nil {
  39. return err
  40. }
  41. commitNodeIndex, commitGraphFile := repo.CommitNodeIndex()
  42. if commitGraphFile != nil {
  43. defer commitGraphFile.Close()
  44. }
  45. commitNode, err := commitNodeIndex.Get(commit.Hash)
  46. if err != nil {
  47. return nil
  48. }
  49. lastCommits, err := getLastCommitForPaths(commitNode, "", []string{commitID})
  50. if err != nil {
  51. return err
  52. }
  53. note.Commit = convertCommit(lastCommits[commitID])
  54. return nil
  55. }