commits.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "time"
  9. "gitlab.com/gitote/git-module"
  10. api "gitlab.com/gitote/go-gitote-client"
  11. "gitote/gitote/models"
  12. "gitote/gitote/models/errors"
  13. "gitote/gitote/pkg/context"
  14. "gitote/gitote/pkg/setting"
  15. )
  16. func GetSingleCommit(c *context.APIContext) {
  17. gitRepo, err := git.OpenRepository(c.Repo.Repository.RepoPath())
  18. if err != nil {
  19. c.ServerError("OpenRepository", err)
  20. return
  21. }
  22. commit, err := gitRepo.GetCommit(c.Params(":sha"))
  23. if err != nil {
  24. c.NotFoundOrServerError("GetCommit", git.IsErrNotExist, err)
  25. return
  26. }
  27. // Retrieve author and committer information
  28. var apiAuthor, apiCommitter *api.User
  29. author, err := models.GetUserByEmail(commit.Author.Email)
  30. if err != nil && !errors.IsUserNotExist(err) {
  31. c.ServerError("Get user by author email", err)
  32. return
  33. } else if err == nil {
  34. apiAuthor = author.APIFormat()
  35. }
  36. // Save one query if the author is also the committer
  37. if commit.Committer.Email == commit.Author.Email {
  38. apiCommitter = apiAuthor
  39. } else {
  40. committer, err := models.GetUserByEmail(commit.Committer.Email)
  41. if err != nil && !errors.IsUserNotExist(err) {
  42. c.ServerError("Get user by committer email", err)
  43. return
  44. } else if err == nil {
  45. apiCommitter = committer.APIFormat()
  46. }
  47. }
  48. // Retrieve parent(s) of the commit
  49. apiParents := make([]*api.CommitMeta, commit.ParentCount())
  50. for i := 0; i < commit.ParentCount(); i++ {
  51. sha, _ := commit.ParentID(i)
  52. apiParents[i] = &api.CommitMeta{
  53. URL: c.BaseURL + "/repos/" + c.Repo.Repository.FullName() + "/commits/" + sha.String(),
  54. SHA: sha.String(),
  55. }
  56. }
  57. c.JSONSuccess(&api.Commit{
  58. CommitMeta: &api.CommitMeta{
  59. URL: setting.AppURL + c.Link[1:],
  60. SHA: commit.ID.String(),
  61. },
  62. HTMLURL: c.Repo.Repository.HTMLURL() + "/commits/" + commit.ID.String(),
  63. RepoCommit: &api.RepoCommit{
  64. URL: setting.AppURL + c.Link[1:],
  65. Author: &api.CommitUser{
  66. Name: commit.Author.Name,
  67. Email: commit.Author.Email,
  68. Date: commit.Author.When.Format(time.RFC3339),
  69. },
  70. Committer: &api.CommitUser{
  71. Name: commit.Committer.Name,
  72. Email: commit.Committer.Email,
  73. Date: commit.Committer.When.Format(time.RFC3339),
  74. },
  75. Message: commit.Summary(),
  76. Tree: &api.CommitMeta{
  77. URL: c.BaseURL + "/repos/" + c.Repo.Repository.FullName() + "/tree/" + commit.ID.String(),
  78. SHA: commit.ID.String(),
  79. },
  80. },
  81. Author: apiAuthor,
  82. Committer: apiCommitter,
  83. Parents: apiParents,
  84. })
  85. }