commits.go 3.7 KB

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