commits.go 3.8 KB

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