commits.go 2.6 KB

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