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_nogogit.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2020 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. // +build !gogit
  6. package git
  7. import (
  8. "bufio"
  9. "errors"
  10. "io"
  11. "io/ioutil"
  12. "strings"
  13. "code.gitea.io/gitea/modules/log"
  14. )
  15. // ResolveReference resolves a name to a reference
  16. func (repo *Repository) ResolveReference(name string) (string, error) {
  17. stdout, err := NewCommand("show-ref", "--hash", name).RunInDir(repo.Path)
  18. if err != nil {
  19. if strings.Contains(err.Error(), "not a valid ref") {
  20. return "", ErrNotExist{name, ""}
  21. }
  22. return "", err
  23. }
  24. stdout = strings.TrimSpace(stdout)
  25. if stdout == "" {
  26. return "", ErrNotExist{name, ""}
  27. }
  28. return stdout, nil
  29. }
  30. // GetRefCommitID returns the last commit ID string of given reference (branch or tag).
  31. func (repo *Repository) GetRefCommitID(name string) (string, error) {
  32. wr, rd, cancel := repo.CatFileBatchCheck()
  33. defer cancel()
  34. _, _ = wr.Write([]byte(name + "\n"))
  35. shaBs, _, _, err := ReadBatchLine(rd)
  36. if IsErrNotExist(err) {
  37. return "", ErrNotExist{name, ""}
  38. }
  39. return string(shaBs), nil
  40. }
  41. // IsCommitExist returns true if given commit exists in current repository.
  42. func (repo *Repository) IsCommitExist(name string) bool {
  43. _, err := NewCommand("cat-file", "-e", name).RunInDir(repo.Path)
  44. return err == nil
  45. }
  46. func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
  47. wr, rd, cancel := repo.CatFileBatch()
  48. defer cancel()
  49. _, _ = wr.Write([]byte(id.String() + "\n"))
  50. return repo.getCommitFromBatchReader(rd, id)
  51. }
  52. func (repo *Repository) getCommitFromBatchReader(rd *bufio.Reader, id SHA1) (*Commit, error) {
  53. _, typ, size, err := ReadBatchLine(rd)
  54. if err != nil {
  55. if errors.Is(err, io.EOF) || IsErrNotExist(err) {
  56. return nil, ErrNotExist{ID: id.String()}
  57. }
  58. return nil, err
  59. }
  60. switch typ {
  61. case "missing":
  62. return nil, ErrNotExist{ID: id.String()}
  63. case "tag":
  64. // then we need to parse the tag
  65. // and load the commit
  66. data, err := ioutil.ReadAll(io.LimitReader(rd, size))
  67. if err != nil {
  68. return nil, err
  69. }
  70. _, err = rd.Discard(1)
  71. if err != nil {
  72. return nil, err
  73. }
  74. tag, err := parseTagData(data)
  75. if err != nil {
  76. return nil, err
  77. }
  78. tag.repo = repo
  79. commit, err := tag.Commit()
  80. if err != nil {
  81. return nil, err
  82. }
  83. commit.CommitMessage = strings.TrimSpace(tag.Message)
  84. commit.Author = tag.Tagger
  85. commit.Signature = tag.Signature
  86. return commit, nil
  87. case "commit":
  88. commit, err := CommitFromReader(repo, id, io.LimitReader(rd, size))
  89. if err != nil {
  90. return nil, err
  91. }
  92. _, err = rd.Discard(1)
  93. if err != nil {
  94. return nil, err
  95. }
  96. return commit, nil
  97. default:
  98. log.Debug("Unknown typ: %s", typ)
  99. _, err = rd.Discard(int(size) + 1)
  100. if err != nil {
  101. return nil, err
  102. }
  103. return nil, ErrNotExist{
  104. ID: id.String(),
  105. }
  106. }
  107. }
  108. // ConvertToSHA1 returns a Hash object from a potential ID string
  109. func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
  110. if len(commitID) == 40 && SHAPattern.MatchString(commitID) {
  111. sha1, err := NewIDFromString(commitID)
  112. if err == nil {
  113. return sha1, nil
  114. }
  115. }
  116. wr, rd, cancel := repo.CatFileBatchCheck()
  117. defer cancel()
  118. _, err := wr.Write([]byte(commitID + "\n"))
  119. if err != nil {
  120. return SHA1{}, err
  121. }
  122. sha, _, _, err := ReadBatchLine(rd)
  123. if err != nil {
  124. if IsErrNotExist(err) {
  125. return SHA1{}, ErrNotExist{commitID, ""}
  126. }
  127. return SHA1{}, err
  128. }
  129. return MustIDFromString(string(sha)), nil
  130. }