commit.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. dataRc, err := blob.Data()
  72. if err != nil {
  73. return false
  74. }
  75. buf := make([]byte, 1024)
  76. n, _ := dataRc.Read(buf)
  77. if n > 0 {
  78. buf = buf[:n]
  79. }
  80. dataRc.Close()
  81. _, isImage := base.IsImageFile(buf)
  82. return isImage
  83. }
  84. parents := make([]string, commit.ParentCount())
  85. for i := 0; i < commit.ParentCount(); i++ {
  86. sha, err := commit.ParentId(i)
  87. parents[i] = sha.String()
  88. if err != nil {
  89. ctx.Handle(404, "repo.Diff", err)
  90. return
  91. }
  92. }
  93. ctx.Data["Username"] = userName
  94. ctx.Data["Reponame"] = repoName
  95. ctx.Data["IsImageFile"] = isImageFile
  96. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitId)
  97. ctx.Data["Commit"] = commit
  98. ctx.Data["Diff"] = diff
  99. ctx.Data["Parents"] = parents
  100. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  101. ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId)
  102. ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", commitId)
  103. ctx.HTML(200, "repo/diff")
  104. }
  105. func SearchCommits(ctx *middleware.Context, params martini.Params) {
  106. ctx.Data["IsSearchPage"] = true
  107. ctx.Data["IsRepoToolbarCommits"] = true
  108. keyword := ctx.Query("q")
  109. if len(keyword) == 0 {
  110. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName)
  111. return
  112. }
  113. userName := params["username"]
  114. repoName := params["reponame"]
  115. brs, err := ctx.Repo.GitRepo.GetBranches()
  116. if err != nil {
  117. ctx.Handle(500, "repo.SearchCommits(GetBranches)", err)
  118. return
  119. } else if len(brs) == 0 {
  120. ctx.Handle(404, "repo.SearchCommits(GetBranches)", nil)
  121. return
  122. }
  123. commits, err := ctx.Repo.Commit.SearchCommits(keyword)
  124. if err != nil {
  125. ctx.Handle(500, "repo.SearchCommits(SearchCommits)", err)
  126. return
  127. }
  128. ctx.Data["Keyword"] = keyword
  129. ctx.Data["Username"] = userName
  130. ctx.Data["Reponame"] = repoName
  131. ctx.Data["CommitCount"] = commits.Len()
  132. ctx.Data["Commits"] = commits
  133. ctx.HTML(200, "repo/commits")
  134. }
  135. func FileHistory(ctx *middleware.Context, params martini.Params) {
  136. ctx.Data["IsRepoToolbarCommits"] = true
  137. fileName := params["_1"]
  138. if len(fileName) == 0 {
  139. Commits(ctx, params)
  140. return
  141. }
  142. userName := ctx.Repo.Owner.Name
  143. repoName := ctx.Repo.Repository.Name
  144. branchName := params["branchname"]
  145. brs, err := ctx.Repo.GitRepo.GetBranches()
  146. if err != nil {
  147. ctx.Handle(500, "repo.FileHistory", err)
  148. return
  149. } else if len(brs) == 0 {
  150. ctx.Handle(404, "repo.FileHistory", nil)
  151. return
  152. }
  153. commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
  154. if err != nil {
  155. ctx.Handle(500, "repo.FileHistory(GetCommitsCount)", err)
  156. return
  157. }
  158. if commitsCount == 0 {
  159. ctx.Handle(404, "repo.FileHistory", nil)
  160. return
  161. }
  162. // Calculate and validate page number.
  163. page, _ := base.StrTo(ctx.Query("p")).Int()
  164. if page < 1 {
  165. page = 1
  166. }
  167. lastPage := page - 1
  168. if lastPage < 0 {
  169. lastPage = 0
  170. }
  171. nextPage := page + 1
  172. if nextPage*50 > commitsCount {
  173. nextPage = 0
  174. }
  175. ctx.Data["Commits"], err = ctx.Repo.GitRepo.CommitsByFileAndRange(
  176. branchName, fileName, page)
  177. if err != nil {
  178. ctx.Handle(500, "repo.FileHistory(CommitsByRange)", err)
  179. return
  180. }
  181. ctx.Data["Username"] = userName
  182. ctx.Data["Reponame"] = repoName
  183. ctx.Data["FileName"] = fileName
  184. ctx.Data["CommitCount"] = commitsCount
  185. ctx.Data["LastPageNum"] = lastPage
  186. ctx.Data["NextPageNum"] = nextPage
  187. ctx.HTML(200, "repo/commits")
  188. }