commit.go 3.8 KB

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