You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

commit.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2014 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. "path"
  7. "github.com/go-martini/martini"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/base"
  10. "github.com/gogits/gogs/modules/middleware"
  11. )
  12. func Commits(ctx *middleware.Context, params martini.Params) {
  13. userName := ctx.Repo.Owner.Name
  14. repoName := ctx.Repo.Repository.Name
  15. brs, err := ctx.Repo.GitRepo.GetBranches()
  16. if err != nil {
  17. ctx.Handle(500, "repo.Commits", err)
  18. return
  19. } else if len(brs) == 0 {
  20. ctx.Handle(404, "repo.Commits", nil)
  21. return
  22. }
  23. commitsCount, err := ctx.Repo.Commit.CommitsCount()
  24. if err != nil {
  25. ctx.Handle(500, "repo.Commits(GetCommitsCount)", err)
  26. return
  27. }
  28. // Calculate and validate page number.
  29. page, _ := base.StrTo(ctx.Query("p")).Int()
  30. if page < 1 {
  31. page = 1
  32. }
  33. lastPage := page - 1
  34. if lastPage < 0 {
  35. lastPage = 0
  36. }
  37. nextPage := page + 1
  38. if nextPage*50 > commitsCount {
  39. nextPage = 0
  40. }
  41. //both `git log branchName` and `git log commitId` work
  42. commits, err := ctx.Repo.Commit.CommitsByRange(page)
  43. if err != nil {
  44. ctx.Handle(500, "repo.Commits(get commits)", err)
  45. return
  46. }
  47. ctx.Data["Username"] = userName
  48. ctx.Data["Reponame"] = repoName
  49. ctx.Data["CommitCount"] = commitsCount
  50. ctx.Data["Commits"] = commits
  51. ctx.Data["LastPageNum"] = lastPage
  52. ctx.Data["NextPageNum"] = nextPage
  53. ctx.Data["IsRepoToolbarCommits"] = true
  54. ctx.HTML(200, "repo/commits")
  55. }
  56. func Diff(ctx *middleware.Context, params martini.Params) {
  57. userName := ctx.Repo.Owner.Name
  58. repoName := ctx.Repo.Repository.Name
  59. commitId := ctx.Repo.CommitId
  60. commit := ctx.Repo.Commit
  61. diff, err := models.GetDiff(models.RepoPath(userName, repoName), commitId)
  62. if err != nil {
  63. ctx.Handle(404, "repo.Diff", err)
  64. return
  65. }
  66. isImageFile := func(name string) bool {
  67. blob, err := ctx.Repo.Commit.GetBlobByPath(name)
  68. if err != nil {
  69. return false
  70. }
  71. data, err := blob.Data()
  72. if err != nil {
  73. return false
  74. }
  75. _, isImage := base.IsImageFile(data)
  76. return isImage
  77. }
  78. ctx.Data["IsImageFile"] = isImageFile
  79. ctx.Data["Title"] = commit.Message() + " · " + base.ShortSha(commitId)
  80. ctx.Data["Commit"] = commit
  81. ctx.Data["Diff"] = diff
  82. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  83. ctx.Data["IsRepoToolbarCommits"] = true
  84. ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId)
  85. ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", commitId)
  86. ctx.HTML(200, "repo/diff")
  87. }
  88. func SearchCommits(ctx *middleware.Context, params martini.Params) {
  89. keyword := ctx.Query("q")
  90. if len(keyword) == 0 {
  91. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName)
  92. return
  93. }
  94. userName := params["username"]
  95. repoName := params["reponame"]
  96. brs, err := ctx.Repo.GitRepo.GetBranches()
  97. if err != nil {
  98. ctx.Handle(500, "repo.SearchCommits(GetBranches)", err)
  99. return
  100. } else if len(brs) == 0 {
  101. ctx.Handle(404, "repo.SearchCommits(GetBranches)", nil)
  102. return
  103. }
  104. commits, err := ctx.Repo.Commit.SearchCommits(keyword)
  105. if err != nil {
  106. ctx.Handle(500, "repo.SearchCommits(SearchCommits)", err)
  107. return
  108. }
  109. ctx.Data["Keyword"] = keyword
  110. ctx.Data["Username"] = userName
  111. ctx.Data["Reponame"] = repoName
  112. ctx.Data["CommitCount"] = commits.Len()
  113. ctx.Data["Commits"] = commits
  114. ctx.Data["IsSearchPage"] = true
  115. ctx.Data["IsRepoToolbarCommits"] = true
  116. ctx.HTML(200, "repo/commits")
  117. }