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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. //go:build !gogit
  4. package git
  5. import (
  6. "context"
  7. "io"
  8. "strings"
  9. "code.gitea.io/gitea/modules/log"
  10. )
  11. // GetNote retrieves the git-notes data for a given commit.
  12. // FIXME: Add LastCommitCache support
  13. func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) error {
  14. log.Trace("Searching for git note corresponding to the commit %q in the repository %q", commitID, repo.Path)
  15. notes, err := repo.GetCommit(NotesRef)
  16. if err != nil {
  17. if IsErrNotExist(err) {
  18. return err
  19. }
  20. log.Error("Unable to get commit from ref %q. Error: %v", NotesRef, err)
  21. return err
  22. }
  23. path := ""
  24. tree := &notes.Tree
  25. log.Trace("Found tree with ID %q while searching for git note corresponding to the commit %q", tree.ID, commitID)
  26. var entry *TreeEntry
  27. originalCommitID := commitID
  28. for len(commitID) > 2 {
  29. entry, err = tree.GetTreeEntryByPath(commitID)
  30. if err == nil {
  31. path += commitID
  32. break
  33. }
  34. if IsErrNotExist(err) {
  35. tree, err = tree.SubTree(commitID[0:2])
  36. path += commitID[0:2] + "/"
  37. commitID = commitID[2:]
  38. }
  39. if err != nil {
  40. // Err may have been updated by the SubTree we need to recheck if it's again an ErrNotExist
  41. if !IsErrNotExist(err) {
  42. log.Error("Unable to find git note corresponding to the commit %q. Error: %v", originalCommitID, err)
  43. }
  44. return err
  45. }
  46. }
  47. blob := entry.Blob()
  48. dataRc, err := blob.DataAsync()
  49. if err != nil {
  50. log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
  51. return err
  52. }
  53. closed := false
  54. defer func() {
  55. if !closed {
  56. _ = dataRc.Close()
  57. }
  58. }()
  59. d, err := io.ReadAll(dataRc)
  60. if err != nil {
  61. log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
  62. return err
  63. }
  64. _ = dataRc.Close()
  65. closed = true
  66. note.Message = d
  67. treePath := ""
  68. if idx := strings.LastIndex(path, "/"); idx > -1 {
  69. treePath = path[:idx]
  70. path = path[idx+1:]
  71. }
  72. lastCommits, err := GetLastCommitForPaths(ctx, notes, treePath, []string{path})
  73. if err != nil {
  74. log.Error("Unable to get the commit for the path %q. Error: %v", treePath, err)
  75. return err
  76. }
  77. note.Commit = lastCommits[path]
  78. return nil
  79. }