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

commit.go 4.0 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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["DiffNotAvailable"] = diff.NumFiles() == 0
  89. ctx.Data["IsRepoToolbarCommits"] = true
  90. ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId)
  91. ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", commitId)
  92. ctx.HTML(200, "repo/diff")
  93. }
  94. func SearchCommits(ctx *middleware.Context, params martini.Params) {
  95. keyword := ctx.Query("q")
  96. if len(keyword) == 0 {
  97. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName)
  98. return
  99. }
  100. userName := params["username"]
  101. repoName := params["reponame"]
  102. branchName := params["branchname"]
  103. brs, err := models.GetBranches(userName, repoName)
  104. if err != nil {
  105. ctx.Handle(500, "repo.SearchCommits(GetBranches)", err)
  106. return
  107. } else if len(brs) == 0 {
  108. ctx.Handle(404, "repo.SearchCommits(GetBranches)", nil)
  109. return
  110. }
  111. var commits *list.List
  112. if !models.IsBranchExist(userName, repoName, branchName) {
  113. ctx.Handle(404, "repo.SearchCommits(IsBranchExist)", err)
  114. return
  115. } else if commits, err = models.SearchCommits(models.RepoPath(userName, repoName), branchName, keyword); err != nil {
  116. ctx.Handle(500, "repo.SearchCommits(SearchCommits)", err)
  117. return
  118. }
  119. ctx.Data["Keyword"] = keyword
  120. ctx.Data["Username"] = userName
  121. ctx.Data["Reponame"] = repoName
  122. ctx.Data["CommitCount"] = commits.Len()
  123. ctx.Data["Commits"] = commits
  124. ctx.Data["IsSearchPage"] = true
  125. ctx.Data["IsRepoToolbarCommits"] = true
  126. ctx.HTML(200, "repo/commits")
  127. }