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_nogogit.go 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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"
  10. "strings"
  11. "code.gitea.io/gitea/modules/log"
  12. )
  13. // GetNote retrieves the git-notes data for a given commit.
  14. // FIXME: Add LastCommitCache support
  15. func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) error {
  16. log.Trace("Searching for git note corresponding to the commit %q in the repository %q", commitID, repo.Path)
  17. notes, err := repo.GetCommit(NotesRef)
  18. if err != nil {
  19. log.Error("Unable to get commit from ref %q. Error: %v", NotesRef, err)
  20. return err
  21. }
  22. path := ""
  23. tree := &notes.Tree
  24. log.Trace("Found tree with ID %q while searching for git note corresponding to the commit %q", tree.ID, commitID)
  25. var entry *TreeEntry
  26. originalCommitID := commitID
  27. for len(commitID) > 2 {
  28. entry, err = tree.GetTreeEntryByPath(commitID)
  29. if err == nil {
  30. path += commitID
  31. break
  32. }
  33. if IsErrNotExist(err) {
  34. tree, err = tree.SubTree(commitID[0:2])
  35. path += commitID[0:2] + "/"
  36. commitID = commitID[2:]
  37. }
  38. if err != nil {
  39. log.Error("Unable to find git note corresponding to the commit %q. Error: %v", originalCommitID, err)
  40. return err
  41. }
  42. }
  43. blob := entry.Blob()
  44. dataRc, err := blob.DataAsync()
  45. if err != nil {
  46. log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
  47. return err
  48. }
  49. closed := false
  50. defer func() {
  51. if !closed {
  52. _ = dataRc.Close()
  53. }
  54. }()
  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. _ = dataRc.Close()
  61. closed = true
  62. note.Message = d
  63. treePath := ""
  64. if idx := strings.LastIndex(path, "/"); idx > -1 {
  65. treePath = path[:idx]
  66. path = path[idx+1:]
  67. }
  68. lastCommits, err := GetLastCommitForPaths(ctx, nil, notes, treePath, []string{path})
  69. if err != nil {
  70. log.Error("Unable to get the commit for the path %q. Error: %v", treePath, err)
  71. return err
  72. }
  73. note.Commit = lastCommits[path]
  74. return nil
  75. }