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.

60 lines
1.3 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. "gopkg.in/src-d/go-git.v4/plumbing"
  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. entry, err := notes.GetTreeEntryByPath(commitID)
  24. if err != nil {
  25. return err
  26. }
  27. blob := entry.Blob()
  28. dataRc, err := blob.DataAsync()
  29. if err != nil {
  30. return err
  31. }
  32. defer dataRc.Close()
  33. d, err := ioutil.ReadAll(dataRc)
  34. if err != nil {
  35. return err
  36. }
  37. note.Message = d
  38. commit, err := repo.gogitRepo.CommitObject(plumbing.Hash(notes.ID))
  39. if err != nil {
  40. return err
  41. }
  42. lastCommits, err := getLastCommitForPaths(commit, "", []string{commitID})
  43. if err != nil {
  44. return err
  45. }
  46. note.Commit = convertCommit(lastCommits[commitID])
  47. return nil
  48. }