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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. 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. // SetReference sets the commit ID string of given reference (e.g. branch or tag).
  27. func (repo *Repository) SetReference(name, commitID string) error {
  28. return repo.gogitRepo.Storer.SetReference(plumbing.NewReferenceFromStrings(name, commitID))
  29. }
  30. // RemoveReference removes the given reference (e.g. branch or tag).
  31. func (repo *Repository) RemoveReference(name string) error {
  32. return repo.gogitRepo.Storer.RemoveReference(plumbing.ReferenceName(name))
  33. }
  34. // ConvertToSHA1 returns a Hash object from a potential ID string
  35. func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
  36. if len(commitID) == 40 {
  37. sha1, err := NewIDFromString(commitID)
  38. if err == nil {
  39. return sha1, nil
  40. }
  41. }
  42. actualCommitID, _, err := NewCommand(repo.Ctx, "rev-parse", "--verify", commitID).RunStdString(&RunOpts{Dir: repo.Path})
  43. if err != nil {
  44. if strings.Contains(err.Error(), "unknown revision or path") ||
  45. strings.Contains(err.Error(), "fatal: Needed a single revision") {
  46. return SHA1{}, ErrNotExist{commitID, ""}
  47. }
  48. return SHA1{}, err
  49. }
  50. return NewIDFromString(actualCommitID)
  51. }
  52. // IsCommitExist returns true if given commit exists in current repository.
  53. func (repo *Repository) IsCommitExist(name string) bool {
  54. hash := plumbing.NewHash(name)
  55. _, err := repo.gogitRepo.CommitObject(hash)
  56. return err == nil
  57. }
  58. func convertPGPSignatureForTag(t *object.Tag) *CommitGPGSignature {
  59. if t.PGPSignature == "" {
  60. return nil
  61. }
  62. var w strings.Builder
  63. var err error
  64. if _, err = fmt.Fprintf(&w,
  65. "object %s\ntype %s\ntag %s\ntagger ",
  66. t.Target.String(), t.TargetType.Bytes(), t.Name); err != nil {
  67. return nil
  68. }
  69. if err = t.Tagger.Encode(&w); err != nil {
  70. return nil
  71. }
  72. if _, err = fmt.Fprintf(&w, "\n\n"); err != nil {
  73. return nil
  74. }
  75. if _, err = fmt.Fprintf(&w, t.Message); err != nil {
  76. return nil
  77. }
  78. return &CommitGPGSignature{
  79. Signature: t.PGPSignature,
  80. Payload: strings.TrimSpace(w.String()) + "\n",
  81. }
  82. }
  83. func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
  84. var tagObject *object.Tag
  85. gogitCommit, err := repo.gogitRepo.CommitObject(id)
  86. if err == plumbing.ErrObjectNotFound {
  87. tagObject, err = repo.gogitRepo.TagObject(id)
  88. if err == plumbing.ErrObjectNotFound {
  89. return nil, ErrNotExist{
  90. ID: id.String(),
  91. }
  92. }
  93. if err == nil {
  94. gogitCommit, err = repo.gogitRepo.CommitObject(tagObject.Target)
  95. }
  96. // if we get a plumbing.ErrObjectNotFound here then the repository is broken and it should be 500
  97. }
  98. if err != nil {
  99. return nil, err
  100. }
  101. commit := convertCommit(gogitCommit)
  102. commit.repo = repo
  103. if tagObject != nil {
  104. commit.CommitMessage = strings.TrimSpace(tagObject.Message)
  105. commit.Author = &tagObject.Tagger
  106. commit.Signature = convertPGPSignatureForTag(tagObject)
  107. }
  108. tree, err := gogitCommit.Tree()
  109. if err != nil {
  110. return nil, err
  111. }
  112. commit.Tree.ID = tree.Hash
  113. commit.Tree.gogitTree = tree
  114. return commit, nil
  115. }