A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
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.

110 lines
2.5 KiB

Use native git variants by default with go-git variants as build tag (#13673) * Move last commit cache back into modules/git Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove go-git from the interface for last commit cache Signed-off-by: Andrew Thornton <art27@cantab.net> * move cacheref to last_commit_cache Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove go-git from routers/private/hook Signed-off-by: Andrew Thornton <art27@cantab.net> * Move FindLFSFiles to pipeline Signed-off-by: Andrew Thornton <art27@cantab.net> * Make no-go-git variants Signed-off-by: Andrew Thornton <art27@cantab.net> * Submodule RefID Signed-off-by: Andrew Thornton <art27@cantab.net> * fix issue with GetCommitsInfo Signed-off-by: Andrew Thornton <art27@cantab.net> * fix GetLastCommitForPaths Signed-off-by: Andrew Thornton <art27@cantab.net> * Improve efficiency Signed-off-by: Andrew Thornton <art27@cantab.net> * More efficiency Signed-off-by: Andrew Thornton <art27@cantab.net> * even faster Signed-off-by: Andrew Thornton <art27@cantab.net> * Reduce duplication * As per @lunny Signed-off-by: Andrew Thornton <art27@cantab.net> * attempt to fix drone Signed-off-by: Andrew Thornton <art27@cantab.net> * fix test-tags Signed-off-by: Andrew Thornton <art27@cantab.net> * default to use no-go-git variants and add gogit build tag Signed-off-by: Andrew Thornton <art27@cantab.net> * placate lint Signed-off-by: Andrew Thornton <art27@cantab.net> * as per @6543 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
2 years ago
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. // +build gogit
  6. package git
  7. import (
  8. "fmt"
  9. "strings"
  10. "github.com/go-git/go-git/v5/plumbing"
  11. "github.com/go-git/go-git/v5/plumbing/object"
  12. )
  13. // GetRefCommitID returns the last commit ID string of given reference (branch or tag).
  14. func (repo *Repository) GetRefCommitID(name string) (string, error) {
  15. ref, err := repo.gogitRepo.Reference(plumbing.ReferenceName(name), true)
  16. if err != nil {
  17. if err == plumbing.ErrReferenceNotFound {
  18. return "", ErrNotExist{
  19. ID: name,
  20. }
  21. }
  22. return "", err
  23. }
  24. return ref.Hash().String(), nil
  25. }
  26. // IsCommitExist returns true if given commit exists in current repository.
  27. func (repo *Repository) IsCommitExist(name string) bool {
  28. hash := plumbing.NewHash(name)
  29. _, err := repo.gogitRepo.CommitObject(hash)
  30. return err == nil
  31. }
  32. func convertPGPSignatureForTag(t *object.Tag) *CommitGPGSignature {
  33. if t.PGPSignature == "" {
  34. return nil
  35. }
  36. var w strings.Builder
  37. var err error
  38. if _, err = fmt.Fprintf(&w,
  39. "object %s\ntype %s\ntag %s\ntagger ",
  40. t.Target.String(), t.TargetType.Bytes(), t.Name); err != nil {
  41. return nil
  42. }
  43. if err = t.Tagger.Encode(&w); err != nil {
  44. return nil
  45. }
  46. if _, err = fmt.Fprintf(&w, "\n\n"); err != nil {
  47. return nil
  48. }
  49. if _, err = fmt.Fprintf(&w, t.Message); err != nil {
  50. return nil
  51. }
  52. return &CommitGPGSignature{
  53. Signature: t.PGPSignature,
  54. Payload: strings.TrimSpace(w.String()) + "\n",
  55. }
  56. }
  57. func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
  58. var tagObject *object.Tag
  59. gogitCommit, err := repo.gogitRepo.CommitObject(id)
  60. if err == plumbing.ErrObjectNotFound {
  61. tagObject, err = repo.gogitRepo.TagObject(id)
  62. if err == plumbing.ErrObjectNotFound {
  63. return nil, ErrNotExist{
  64. ID: id.String(),
  65. }
  66. }
  67. if err == nil {
  68. gogitCommit, err = repo.gogitRepo.CommitObject(tagObject.Target)
  69. }
  70. // if we get a plumbing.ErrObjectNotFound here then the repository is broken and it should be 500
  71. }
  72. if err != nil {
  73. return nil, err
  74. }
  75. commit := convertCommit(gogitCommit)
  76. commit.repo = repo
  77. if tagObject != nil {
  78. commit.CommitMessage = strings.TrimSpace(tagObject.Message)
  79. commit.Author = &tagObject.Tagger
  80. commit.Signature = convertPGPSignatureForTag(tagObject)
  81. }
  82. tree, err := gogitCommit.Tree()
  83. if err != nil {
  84. return nil, err
  85. }
  86. commit.Tree.ID = tree.Hash
  87. commit.Tree.gogitTree = tree
  88. return commit, nil
  89. }