commits.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2018 The Gogs 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. package repo
  5. import (
  6. "net/http"
  7. "strings"
  8. "time"
  9. "github.com/gogs/git-module"
  10. api "github.com/gogs/go-gogs-client"
  11. "github.com/gogs/gogs/models"
  12. "github.com/gogs/gogs/models/errors"
  13. "github.com/gogs/gogs/pkg/context"
  14. "github.com/gogs/gogs/pkg/setting"
  15. )
  16. func GetSingleCommit(c *context.APIContext) {
  17. if strings.Contains(c.Req.Header.Get("Accept"), api.MediaApplicationSHA) {
  18. c.SetParams("*", c.Params(":sha"))
  19. GetReferenceSHA(c)
  20. return
  21. }
  22. gitRepo, err := git.OpenRepository(c.Repo.Repository.RepoPath())
  23. if err != nil {
  24. c.ServerError("OpenRepository", err)
  25. return
  26. }
  27. commit, err := gitRepo.GetCommit(c.Params(":sha"))
  28. if err != nil {
  29. c.NotFoundOrServerError("GetCommit", git.IsErrNotExist, err)
  30. return
  31. }
  32. // Retrieve author and committer information
  33. var apiAuthor, apiCommitter *api.User
  34. author, err := models.GetUserByEmail(commit.Author.Email)
  35. if err != nil && !errors.IsUserNotExist(err) {
  36. c.ServerError("Get user by author email", err)
  37. return
  38. } else if err == nil {
  39. apiAuthor = author.APIFormat()
  40. }
  41. // Save one query if the author is also the committer
  42. if commit.Committer.Email == commit.Author.Email {
  43. apiCommitter = apiAuthor
  44. } else {
  45. committer, err := models.GetUserByEmail(commit.Committer.Email)
  46. if err != nil && !errors.IsUserNotExist(err) {
  47. c.ServerError("Get user by committer email", err)
  48. return
  49. } else if err == nil {
  50. apiCommitter = committer.APIFormat()
  51. }
  52. }
  53. // Retrieve parent(s) of the commit
  54. apiParents := make([]*api.CommitMeta, commit.ParentCount())
  55. for i := 0; i < commit.ParentCount(); i++ {
  56. sha, _ := commit.ParentID(i)
  57. apiParents[i] = &api.CommitMeta{
  58. URL: c.BaseURL + "/repos/" + c.Repo.Repository.FullName() + "/commits/" + sha.String(),
  59. SHA: sha.String(),
  60. }
  61. }
  62. c.JSONSuccess(&api.Commit{
  63. CommitMeta: &api.CommitMeta{
  64. URL: setting.AppURL + c.Link[1:],
  65. SHA: commit.ID.String(),
  66. },
  67. HTMLURL: c.Repo.Repository.HTMLURL() + "/commits/" + commit.ID.String(),
  68. RepoCommit: &api.RepoCommit{
  69. URL: setting.AppURL + c.Link[1:],
  70. Author: &api.CommitUser{
  71. Name: commit.Author.Name,
  72. Email: commit.Author.Email,
  73. Date: commit.Author.When.Format(time.RFC3339),
  74. },
  75. Committer: &api.CommitUser{
  76. Name: commit.Committer.Name,
  77. Email: commit.Committer.Email,
  78. Date: commit.Committer.When.Format(time.RFC3339),
  79. },
  80. Message: commit.Summary(),
  81. Tree: &api.CommitMeta{
  82. URL: c.BaseURL + "/repos/" + c.Repo.Repository.FullName() + "/tree/" + commit.ID.String(),
  83. SHA: commit.ID.String(),
  84. },
  85. },
  86. Author: apiAuthor,
  87. Committer: apiCommitter,
  88. Parents: apiParents,
  89. })
  90. }
  91. func GetReferenceSHA(c *context.APIContext) {
  92. gitRepo, err := git.OpenRepository(c.Repo.Repository.RepoPath())
  93. if err != nil {
  94. c.ServerError("OpenRepository", err)
  95. return
  96. }
  97. ref := c.Params("*")
  98. refType := 0 // 0-undetermined, 1-branch, 2-tag
  99. if strings.HasPrefix(ref, git.BRANCH_PREFIX) {
  100. ref = strings.TrimPrefix(ref, git.BRANCH_PREFIX)
  101. refType = 1
  102. } else if strings.HasPrefix(ref, git.TAG_PREFIX) {
  103. ref = strings.TrimPrefix(ref, git.TAG_PREFIX)
  104. refType = 2
  105. } else {
  106. if gitRepo.IsBranchExist(ref) {
  107. refType = 1
  108. } else if gitRepo.IsTagExist(ref) {
  109. refType = 2
  110. } else {
  111. c.NotFound()
  112. return
  113. }
  114. }
  115. var sha string
  116. if refType == 1 {
  117. sha, err = gitRepo.GetBranchCommitID(ref)
  118. } else if refType == 2 {
  119. sha, err = gitRepo.GetTagCommitID(ref)
  120. }
  121. if err != nil {
  122. c.NotFoundOrServerError("get reference commit ID", git.IsErrNotExist, err)
  123. return
  124. }
  125. c.PlainText(http.StatusOK, []byte(sha))
  126. }