commit.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/Unknwon/com"
  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) {
  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, "GetBranches", err)
  23. return
  24. } else if len(brs) == 0 {
  25. ctx.Handle(404, "GetBranches", nil)
  26. return
  27. }
  28. commitsCount, err := ctx.Repo.Commit.CommitsCount()
  29. if err != nil {
  30. ctx.Handle(500, "GetCommitsCount", err)
  31. return
  32. }
  33. // Calculate and validate page number.
  34. page, _ := com.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, "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) {
  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 := ctx.Params(":username")
  68. repoName := ctx.Params(":reponame")
  69. brs, err := ctx.Repo.GitRepo.GetBranches()
  70. if err != nil {
  71. ctx.Handle(500, "GetBranches", err)
  72. return
  73. } else if len(brs) == 0 {
  74. ctx.Handle(404, "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) {
  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, "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. _, isImage := base.IsImageFile(buf)
  115. return isImage
  116. }
  117. parents := make([]string, commit.ParentCount())
  118. for i := 0; i < commit.ParentCount(); i++ {
  119. sha, err := commit.ParentId(i)
  120. parents[i] = sha.String()
  121. if err != nil {
  122. ctx.Handle(404, "repo.Diff", err)
  123. return
  124. }
  125. }
  126. ctx.Data["Username"] = userName
  127. ctx.Data["Reponame"] = repoName
  128. ctx.Data["IsImageFile"] = isImageFile
  129. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitId)
  130. ctx.Data["Commit"] = commit
  131. ctx.Data["Diff"] = diff
  132. ctx.Data["Parents"] = parents
  133. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  134. ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId)
  135. ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", commitId)
  136. ctx.HTML(200, DIFF)
  137. }
  138. func FileHistory(ctx *middleware.Context) {
  139. ctx.Data["IsRepoToolbarCommits"] = true
  140. fileName := ctx.Params("*")
  141. if len(fileName) == 0 {
  142. Commits(ctx)
  143. return
  144. }
  145. userName := ctx.Repo.Owner.Name
  146. repoName := ctx.Repo.Repository.Name
  147. branchName := ctx.Params(":branchname")
  148. brs, err := ctx.Repo.GitRepo.GetBranches()
  149. if err != nil {
  150. ctx.Handle(500, "GetBranches", err)
  151. return
  152. } else if len(brs) == 0 {
  153. ctx.Handle(404, "GetBranches", nil)
  154. return
  155. }
  156. commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
  157. if err != nil {
  158. ctx.Handle(500, "repo.FileHistory(GetCommitsCount)", err)
  159. return
  160. } else if commitsCount == 0 {
  161. ctx.Handle(404, "repo.FileHistory", nil)
  162. return
  163. }
  164. // Calculate and validate page number.
  165. page := com.StrTo(ctx.Query("p")).MustInt()
  166. if page < 1 {
  167. page = 1
  168. }
  169. lastPage := page - 1
  170. if lastPage < 0 {
  171. lastPage = 0
  172. }
  173. nextPage := page + 1
  174. if nextPage*50 > commitsCount {
  175. nextPage = 0
  176. }
  177. ctx.Data["Commits"], err = ctx.Repo.GitRepo.CommitsByFileAndRange(
  178. branchName, fileName, page)
  179. if err != nil {
  180. ctx.Handle(500, "repo.FileHistory(CommitsByRange)", err)
  181. return
  182. }
  183. ctx.Data["Username"] = userName
  184. ctx.Data["Reponame"] = repoName
  185. ctx.Data["FileName"] = fileName
  186. ctx.Data["CommitCount"] = commitsCount
  187. ctx.Data["LastPageNum"] = lastPage
  188. ctx.Data["NextPageNum"] = nextPage
  189. ctx.HTML(200, COMMITS)
  190. }