commit.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. //both `git log branchName` and `git log commitId` work
  45. commits, err := models.GetCommitsByRange(repoPath, branchName, page)
  46. ctx.Data["Username"] = userName
  47. ctx.Data["Reponame"] = repoName
  48. ctx.Data["CommitCount"] = commitsCount
  49. ctx.Data["Commits"] = commits
  50. ctx.Data["LastPageNum"] = lastPage
  51. ctx.Data["NextPageNum"] = nextPage
  52. ctx.Data["IsRepoToolbarCommits"] = true
  53. ctx.HTML(200, "repo/commits")
  54. }
  55. func Diff(ctx *middleware.Context, params martini.Params) {
  56. userName := ctx.Repo.Owner.Name
  57. repoName := ctx.Repo.Repository.Name
  58. branchName := ctx.Repo.BranchName
  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. repoFile, err := models.GetTargetFile(userName, repoName,
  68. branchName, commitId, name)
  69. if err != nil {
  70. return false
  71. }
  72. blob, err := repoFile.LookupBlob()
  73. if err != nil {
  74. return false
  75. }
  76. data := blob.Contents()
  77. _, isImage := base.IsImageFile(data)
  78. return isImage
  79. }
  80. ctx.Data["IsImageFile"] = isImageFile
  81. ctx.Data["Title"] = commit.Message() + " · " + base.ShortSha(commitId)
  82. ctx.Data["Commit"] = commit
  83. ctx.Data["Diff"] = diff
  84. ctx.Data["IsRepoToolbarCommits"] = true
  85. ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId)
  86. ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", commitId)
  87. ctx.HTML(200, "repo/diff")
  88. }
  89. func SearchCommits(ctx *middleware.Context, params martini.Params) {
  90. keyword := ctx.Query("q")
  91. if len(keyword) == 0 {
  92. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName)
  93. return
  94. }
  95. userName := params["username"]
  96. repoName := params["reponame"]
  97. branchName := params["branchname"]
  98. brs, err := models.GetBranches(userName, repoName)
  99. if err != nil {
  100. ctx.Handle(500, "repo.SearchCommits(GetBranches)", err)
  101. return
  102. } else if len(brs) == 0 {
  103. ctx.Handle(404, "repo.SearchCommits(GetBranches)", nil)
  104. return
  105. }
  106. var commits *list.List
  107. if !models.IsBranchExist(userName, repoName, branchName) {
  108. ctx.Handle(404, "repo.SearchCommits(IsBranchExist)", err)
  109. return
  110. } else if commits, err = models.SearchCommits(models.RepoPath(userName, repoName), branchName, keyword); err != nil {
  111. ctx.Handle(500, "repo.SearchCommits(SearchCommits)", err)
  112. return
  113. }
  114. ctx.Data["Keyword"] = keyword
  115. ctx.Data["Username"] = userName
  116. ctx.Data["Reponame"] = repoName
  117. ctx.Data["CommitCount"] = commits.Len()
  118. ctx.Data["Commits"] = commits
  119. ctx.Data["IsSearchPage"] = true
  120. ctx.Data["IsRepoToolbarCommits"] = true
  121. ctx.HTML(200, "repo/commits")
  122. }