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

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