commit.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. const (
  13. COMMITS base.TplName = "repo/commits"
  14. DIFF base.TplName = "repo/diff"
  15. )
  16. func Commits(ctx *middleware.Context, params martini.Params) {
  17. ctx.Data["IsRepoToolbarCommits"] = true
  18. userName := ctx.Repo.Owner.Name
  19. repoName := ctx.Repo.Repository.Name
  20. brs, err := ctx.Repo.GitRepo.GetBranches()
  21. if err != nil {
  22. ctx.Handle(500, "repo.Commits(GetBranches)", err)
  23. return
  24. } else if len(brs) == 0 {
  25. ctx.Handle(404, "repo.Commits(GetBranches)", nil)
  26. return
  27. }
  28. commitsCount, err := ctx.Repo.Commit.CommitsCount()
  29. if err != nil {
  30. ctx.Handle(500, "repo.Commits(GetCommitsCount)", err)
  31. return
  32. }
  33. // Calculate and validate page number.
  34. page, _ := base.StrTo(ctx.Query("p")).Int()
  35. if page < 1 {
  36. page = 1
  37. }
  38. lastPage := page - 1
  39. if lastPage < 0 {
  40. lastPage = 0
  41. }
  42. nextPage := page + 1
  43. if nextPage*50 > commitsCount {
  44. nextPage = 0
  45. }
  46. // Both `git log branchName` and `git log commitId` work.
  47. ctx.Data["Commits"], err = ctx.Repo.Commit.CommitsByRange(page)
  48. if err != nil {
  49. ctx.Handle(500, "repo.Commits(CommitsByRange)", err)
  50. return
  51. }
  52. ctx.Data["Username"] = userName
  53. ctx.Data["Reponame"] = repoName
  54. ctx.Data["CommitCount"] = commitsCount
  55. ctx.Data["LastPageNum"] = lastPage
  56. ctx.Data["NextPageNum"] = nextPage
  57. ctx.HTML(200, COMMITS)
  58. }
  59. func SearchCommits(ctx *middleware.Context, params martini.Params) {
  60. ctx.Data["IsSearchPage"] = true
  61. ctx.Data["IsRepoToolbarCommits"] = true
  62. keyword := ctx.Query("q")
  63. if len(keyword) == 0 {
  64. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName)
  65. return
  66. }
  67. userName := params["username"]
  68. repoName := params["reponame"]
  69. brs, err := ctx.Repo.GitRepo.GetBranches()
  70. if err != nil {
  71. ctx.Handle(500, "repo.SearchCommits(GetBranches)", err)
  72. return
  73. } else if len(brs) == 0 {
  74. ctx.Handle(404, "repo.SearchCommits(GetBranches)", nil)
  75. return
  76. }
  77. commits, err := ctx.Repo.Commit.SearchCommits(keyword)
  78. if err != nil {
  79. ctx.Handle(500, "repo.SearchCommits(SearchCommits)", err)
  80. return
  81. }
  82. ctx.Data["Keyword"] = keyword
  83. ctx.Data["Username"] = userName
  84. ctx.Data["Reponame"] = repoName
  85. ctx.Data["CommitCount"] = commits.Len()
  86. ctx.Data["Commits"] = commits
  87. ctx.HTML(200, COMMITS)
  88. }
  89. func Diff(ctx *middleware.Context, params martini.Params) {
  90. ctx.Data["IsRepoToolbarCommits"] = true
  91. userName := ctx.Repo.Owner.Name
  92. repoName := ctx.Repo.Repository.Name
  93. commitId := ctx.Repo.CommitId
  94. commit := ctx.Repo.Commit
  95. diff, err := models.GetDiff(models.RepoPath(userName, repoName), commitId)
  96. if err != nil {
  97. ctx.Handle(404, "repo.Diff(GetDiff)", err)
  98. return
  99. }
  100. isImageFile := func(name string) bool {
  101. blob, err := ctx.Repo.Commit.GetBlobByPath(name)
  102. if err != nil {
  103. return false
  104. }
  105. dataRc, err := blob.Data()
  106. if err != nil {
  107. return false
  108. }
  109. buf := make([]byte, 1024)
  110. n, _ := dataRc.Read(buf)
  111. if n > 0 {
  112. buf = buf[:n]
  113. }
  114. dataRc.Close()
  115. _, isImage := base.IsImageFile(buf)
  116. return isImage
  117. }
  118. parents := make([]string, commit.ParentCount())
  119. for i := 0; i < commit.ParentCount(); i++ {
  120. sha, err := commit.ParentId(i)
  121. parents[i] = sha.String()
  122. if err != nil {
  123. ctx.Handle(404, "repo.Diff", err)
  124. return
  125. }
  126. }
  127. ctx.Data["Username"] = userName
  128. ctx.Data["Reponame"] = repoName
  129. ctx.Data["IsImageFile"] = isImageFile
  130. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitId)
  131. ctx.Data["Commit"] = commit
  132. ctx.Data["Diff"] = diff
  133. ctx.Data["Parents"] = parents
  134. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  135. ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId)
  136. ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", commitId)
  137. ctx.HTML(200, DIFF)
  138. }
  139. func FileHistory(ctx *middleware.Context, params martini.Params) {
  140. ctx.Data["IsRepoToolbarCommits"] = true
  141. fileName := params["_1"]
  142. if len(fileName) == 0 {
  143. Commits(ctx, params)
  144. return
  145. }
  146. userName := ctx.Repo.Owner.Name
  147. repoName := ctx.Repo.Repository.Name
  148. branchName := params["branchname"]
  149. brs, err := ctx.Repo.GitRepo.GetBranches()
  150. if err != nil {
  151. ctx.Handle(500, "repo.FileHistory", err)
  152. return
  153. } else if len(brs) == 0 {
  154. ctx.Handle(404, "repo.FileHistory", nil)
  155. return
  156. }
  157. commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
  158. if err != nil {
  159. ctx.Handle(500, "repo.FileHistory(GetCommitsCount)", err)
  160. return
  161. } else if commitsCount == 0 {
  162. ctx.Handle(404, "repo.FileHistory", nil)
  163. return
  164. }
  165. // Calculate and validate page number.
  166. page, _ := base.StrTo(ctx.Query("p")).Int()
  167. if page < 1 {
  168. page = 1
  169. }
  170. lastPage := page - 1
  171. if lastPage < 0 {
  172. lastPage = 0
  173. }
  174. nextPage := page + 1
  175. if nextPage*50 > commitsCount {
  176. nextPage = 0
  177. }
  178. ctx.Data["Commits"], err = ctx.Repo.GitRepo.CommitsByFileAndRange(
  179. branchName, fileName, page)
  180. if err != nil {
  181. ctx.Handle(500, "repo.FileHistory(CommitsByRange)", err)
  182. return
  183. }
  184. ctx.Data["Username"] = userName
  185. ctx.Data["Reponame"] = repoName
  186. ctx.Data["FileName"] = fileName
  187. ctx.Data["CommitCount"] = commitsCount
  188. ctx.Data["LastPageNum"] = lastPage
  189. ctx.Data["NextPageNum"] = nextPage
  190. ctx.HTML(200, COMMITS)
  191. }