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 5.2 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. ctx.Data["IsRepoToolbarCommits"] = true
  14. userName := ctx.Repo.Owner.Name
  15. repoName := ctx.Repo.Repository.Name
  16. brs, err := ctx.Repo.GitRepo.GetBranches()
  17. if err != nil {
  18. ctx.Handle(500, "repo.Commits", err)
  19. return
  20. } else if len(brs) == 0 {
  21. ctx.Handle(404, "repo.Commits", nil)
  22. return
  23. }
  24. commitsCount, err := ctx.Repo.Commit.CommitsCount()
  25. if err != nil {
  26. ctx.Handle(500, "repo.Commits(GetCommitsCount)", err)
  27. return
  28. }
  29. // Calculate and validate page number.
  30. page, _ := base.StrTo(ctx.Query("p")).Int()
  31. if page < 1 {
  32. page = 1
  33. }
  34. lastPage := page - 1
  35. if lastPage < 0 {
  36. lastPage = 0
  37. }
  38. nextPage := page + 1
  39. if nextPage*50 > commitsCount {
  40. nextPage = 0
  41. }
  42. // Both `git log branchName` and `git log commitId` work.
  43. ctx.Data["Commits"], err = ctx.Repo.Commit.CommitsByRange(page)
  44. if err != nil {
  45. ctx.Handle(500, "repo.Commits(CommitsByRange)", err)
  46. return
  47. }
  48. ctx.Data["Username"] = userName
  49. ctx.Data["Reponame"] = repoName
  50. ctx.Data["CommitCount"] = commitsCount
  51. ctx.Data["LastPageNum"] = lastPage
  52. ctx.Data["NextPageNum"] = nextPage
  53. ctx.HTML(200, "repo/commits")
  54. }
  55. func Diff(ctx *middleware.Context, params martini.Params) {
  56. ctx.Data["IsRepoToolbarCommits"] = true
  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["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId)
  96. ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", commitId)
  97. ctx.HTML(200, "repo/diff")
  98. }
  99. func SearchCommits(ctx *middleware.Context, params martini.Params) {
  100. ctx.Data["IsSearchPage"] = true
  101. ctx.Data["IsRepoToolbarCommits"] = true
  102. keyword := ctx.Query("q")
  103. if len(keyword) == 0 {
  104. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName)
  105. return
  106. }
  107. userName := params["username"]
  108. repoName := params["reponame"]
  109. brs, err := ctx.Repo.GitRepo.GetBranches()
  110. if err != nil {
  111. ctx.Handle(500, "repo.SearchCommits(GetBranches)", err)
  112. return
  113. } else if len(brs) == 0 {
  114. ctx.Handle(404, "repo.SearchCommits(GetBranches)", nil)
  115. return
  116. }
  117. commits, err := ctx.Repo.Commit.SearchCommits(keyword)
  118. if err != nil {
  119. ctx.Handle(500, "repo.SearchCommits(SearchCommits)", err)
  120. return
  121. }
  122. ctx.Data["Keyword"] = keyword
  123. ctx.Data["Username"] = userName
  124. ctx.Data["Reponame"] = repoName
  125. ctx.Data["CommitCount"] = commits.Len()
  126. ctx.Data["Commits"] = commits
  127. ctx.HTML(200, "repo/commits")
  128. }
  129. func FileHistory(ctx *middleware.Context, params martini.Params) {
  130. ctx.Data["IsRepoToolbarCommits"] = true
  131. fileName := params["_1"]
  132. if len(fileName) == 0 {
  133. Commits(ctx, params)
  134. return
  135. }
  136. userName := ctx.Repo.Owner.Name
  137. repoName := ctx.Repo.Repository.Name
  138. branchName := params["branchname"]
  139. brs, err := ctx.Repo.GitRepo.GetBranches()
  140. if err != nil {
  141. ctx.Handle(500, "repo.FileHistory", err)
  142. return
  143. } else if len(brs) == 0 {
  144. ctx.Handle(404, "repo.FileHistory", nil)
  145. return
  146. }
  147. commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
  148. if err != nil {
  149. ctx.Handle(500, "repo.FileHistory(GetCommitsCount)", err)
  150. return
  151. }
  152. if commitsCount == 0 {
  153. ctx.Handle(404, "repo.FileHistory", nil)
  154. return
  155. }
  156. // Calculate and validate page number.
  157. page, _ := base.StrTo(ctx.Query("p")).Int()
  158. if page < 1 {
  159. page = 1
  160. }
  161. lastPage := page - 1
  162. if lastPage < 0 {
  163. lastPage = 0
  164. }
  165. nextPage := page + 1
  166. if nextPage*50 > commitsCount {
  167. nextPage = 0
  168. }
  169. ctx.Data["Commits"], err = ctx.Repo.GitRepo.CommitsByFileAndRange(
  170. branchName, fileName, page)
  171. if err != nil {
  172. ctx.Handle(500, "repo.FileHistory(CommitsByRange)", err)
  173. return
  174. }
  175. ctx.Data["Username"] = userName
  176. ctx.Data["Reponame"] = repoName
  177. ctx.Data["FileName"] = fileName
  178. ctx.Data["CommitCount"] = commitsCount
  179. ctx.Data["LastPageNum"] = lastPage
  180. ctx.Data["NextPageNum"] = nextPage
  181. ctx.HTML(200, "repo/commits")
  182. }