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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. log.Error("Unable to find git note corresponding to the commit %q. Error: %v", originalCommitID, err)
  42. return err
  43. }
  44. }
  45. blob := entry.Blob()
  46. dataRc, err := blob.DataAsync()
  47. if err != nil {
  48. log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
  49. return err
  50. }
  51. closed := false
  52. defer func() {
  53. if !closed {
  54. _ = dataRc.Close()
  55. }
  56. }()
  57. d, err := io.ReadAll(dataRc)
  58. if err != nil {
  59. log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
  60. return err
  61. }
  62. _ = dataRc.Close()
  63. closed = true
  64. note.Message = d
  65. treePath := ""
  66. if idx := strings.LastIndex(path, "/"); idx > -1 {
  67. treePath = path[:idx]
  68. path = path[idx+1:]
  69. }
  70. lastCommits, err := GetLastCommitForPaths(ctx, nil, notes, treePath, []string{path})
  71. if err != nil {
  72. log.Error("Unable to get the commit for the path %q. Error: %v", treePath, err)
  73. return err
  74. }
  75. note.Commit = lastCommits[path]
  76. return nil
  77. }