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 3.8 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. parents := make([]string, commit.ParentCount())
  79. for i := 0; i < commit.ParentCount(); i++ {
  80. sha, err := commit.ParentId(i)
  81. parents[i] = sha.String()
  82. if err != nil {
  83. ctx.Handle(404, "repo.Diff", err)
  84. return
  85. }
  86. }
  87. ctx.Data["Username"] = userName
  88. ctx.Data["Reponame"] = repoName
  89. ctx.Data["IsImageFile"] = isImageFile
  90. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitId)
  91. ctx.Data["Commit"] = commit
  92. ctx.Data["Diff"] = diff
  93. ctx.Data["Parents"] = parents
  94. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  95. ctx.Data["IsRepoToolbarCommits"] = true
  96. ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId)
  97. ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", commitId)
  98. ctx.HTML(200, "repo/diff")
  99. }
  100. func SearchCommits(ctx *middleware.Context, params martini.Params) {
  101. keyword := ctx.Query("q")
  102. if len(keyword) == 0 {
  103. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName)
  104. return
  105. }
  106. userName := params["username"]
  107. repoName := params["reponame"]
  108. brs, err := ctx.Repo.GitRepo.GetBranches()
  109. if err != nil {
  110. ctx.Handle(500, "repo.SearchCommits(GetBranches)", err)
  111. return
  112. } else if len(brs) == 0 {
  113. ctx.Handle(404, "repo.SearchCommits(GetBranches)", nil)
  114. return
  115. }
  116. commits, err := ctx.Repo.Commit.SearchCommits(keyword)
  117. if err != nil {
  118. ctx.Handle(500, "repo.SearchCommits(SearchCommits)", err)
  119. return
  120. }
  121. ctx.Data["Keyword"] = keyword
  122. ctx.Data["Username"] = userName
  123. ctx.Data["Reponame"] = repoName
  124. ctx.Data["CommitCount"] = commits.Len()
  125. ctx.Data["Commits"] = commits
  126. ctx.Data["IsSearchPage"] = true
  127. ctx.Data["IsRepoToolbarCommits"] = true
  128. ctx.HTML(200, "repo/commits")
  129. }