commit.go 4.0 KB

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