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.

repo_commit_gogit.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. //go:build gogit
  6. // +build gogit
  7. package git
  8. import (
  9. "fmt"
  10. "strings"
  11. "github.com/go-git/go-git/v5/plumbing"
  12. "github.com/go-git/go-git/v5/plumbing/object"
  13. )
  14. // GetRefCommitID returns the last commit ID string of given reference (branch or tag).
  15. func (repo *Repository) GetRefCommitID(name string) (string, error) {
  16. ref, err := repo.gogitRepo.Reference(plumbing.ReferenceName(name), true)
  17. if err != nil {
  18. if err == plumbing.ErrReferenceNotFound {
  19. return "", ErrNotExist{
  20. ID: name,
  21. }
  22. }
  23. return "", err
  24. }
  25. return ref.Hash().String(), nil
  26. }
  27. // ConvertToSHA1 returns a Hash object from a potential ID string
  28. func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
  29. if len(commitID) == 40 {
  30. sha1, err := NewIDFromString(commitID)
  31. if err == nil {
  32. return sha1, nil
  33. }
  34. }
  35. actualCommitID, err := NewCommand("rev-parse", "--verify", commitID).RunInDir(repo.Path)
  36. if err != nil {
  37. if strings.Contains(err.Error(), "unknown revision or path") ||
  38. strings.Contains(err.Error(), "fatal: Needed a single revision") {
  39. return SHA1{}, ErrNotExist{commitID, ""}
  40. }
  41. return SHA1{}, err
  42. }
  43. return NewIDFromString(actualCommitID)
  44. }
  45. // IsCommitExist returns true if given commit exists in current repository.
  46. func (repo *Repository) IsCommitExist(name string) bool {
  47. hash := plumbing.NewHash(name)
  48. _, err := repo.gogitRepo.CommitObject(hash)
  49. return err == nil
  50. }
  51. func convertPGPSignatureForTag(t *object.Tag) *CommitGPGSignature {
  52. if t.PGPSignature == "" {
  53. return nil
  54. }
  55. var w strings.Builder
  56. var err error
  57. if _, err = fmt.Fprintf(&w,
  58. "object %s\ntype %s\ntag %s\ntagger ",
  59. t.Target.String(), t.TargetType.Bytes(), t.Name); err != nil {
  60. return nil
  61. }
  62. if err = t.Tagger.Encode(&w); err != nil {
  63. return nil
  64. }
  65. if _, err = fmt.Fprintf(&w, "\n\n"); err != nil {
  66. return nil
  67. }
  68. if _, err = fmt.Fprintf(&w, t.Message); err != nil {
  69. return nil
  70. }
  71. return &CommitGPGSignature{
  72. Signature: t.PGPSignature,
  73. Payload: strings.TrimSpace(w.String()) + "\n",
  74. }
  75. }
  76. func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
  77. var tagObject *object.Tag
  78. gogitCommit, err := repo.gogitRepo.CommitObject(id)
  79. if err == plumbing.ErrObjectNotFound {
  80. tagObject, err = repo.gogitRepo.TagObject(id)
  81. if err == plumbing.ErrObjectNotFound {
  82. return nil, ErrNotExist{
  83. ID: id.String(),
  84. }
  85. }
  86. if err == nil {
  87. gogitCommit, err = repo.gogitRepo.CommitObject(tagObject.Target)
  88. }
  89. // if we get a plumbing.ErrObjectNotFound here then the repository is broken and it should be 500
  90. }
  91. if err != nil {
  92. return nil, err
  93. }
  94. commit := convertCommit(gogitCommit)
  95. commit.repo = repo
  96. if tagObject != nil {
  97. commit.CommitMessage = strings.TrimSpace(tagObject.Message)
  98. commit.Author = &tagObject.Tagger
  99. commit.Signature = convertPGPSignatureForTag(tagObject)
  100. }
  101. tree, err := gogitCommit.Tree()
  102. if err != nil {
  103. return nil, err
  104. }
  105. commit.Tree.ID = tree.Hash
  106. commit.Tree.gogitTree = tree
  107. return commit, nil
  108. }