You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
6.8 KiB

10 years ago
  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.GetDiffCommit(models.RepoPath(userName, repoName), commitId)
  96. if err != nil {
  97. ctx.Handle(404, "GetDiffCommit", 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 CompareDiff(ctx *middleware.Context) {
  139. ctx.Data["IsRepoToolbarCommits"] = true
  140. ctx.Data["IsDiffCompare"] = true
  141. userName := ctx.Repo.Owner.Name
  142. repoName := ctx.Repo.Repository.Name
  143. beforeCommitId := ctx.Params(":before")
  144. afterCommitId := ctx.Params(":after")
  145. commit, err := ctx.Repo.GitRepo.GetCommit(afterCommitId)
  146. if err != nil {
  147. ctx.Handle(404, "GetCommit", err)
  148. return
  149. }
  150. diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitId, afterCommitId)
  151. if err != nil {
  152. ctx.Handle(404, "GetDiffRange", err)
  153. return
  154. }
  155. isImageFile := func(name string) bool {
  156. blob, err := commit.GetBlobByPath(name)
  157. if err != nil {
  158. return false
  159. }
  160. dataRc, err := blob.Data()
  161. if err != nil {
  162. return false
  163. }
  164. buf := make([]byte, 1024)
  165. n, _ := dataRc.Read(buf)
  166. if n > 0 {
  167. buf = buf[:n]
  168. }
  169. _, isImage := base.IsImageFile(buf)
  170. return isImage
  171. }
  172. commits, err := commit.CommitsBeforeUntil(beforeCommitId)
  173. if err != nil {
  174. ctx.Handle(500, "CommitsBeforeUntil", err)
  175. return
  176. }
  177. ctx.Data["Commits"] = commits
  178. ctx.Data["CommitCount"] = commits.Len()
  179. ctx.Data["BeforeCommitId"] = beforeCommitId
  180. ctx.Data["AfterCommitId"] = afterCommitId
  181. ctx.Data["Username"] = userName
  182. ctx.Data["Reponame"] = repoName
  183. ctx.Data["IsImageFile"] = isImageFile
  184. ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitId) + "..." + base.ShortSha(afterCommitId) + " · " + userName + "/" + repoName
  185. ctx.Data["Commit"] = commit
  186. ctx.Data["Diff"] = diff
  187. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  188. ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", afterCommitId)
  189. ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", afterCommitId)
  190. ctx.HTML(200, DIFF)
  191. }
  192. func FileHistory(ctx *middleware.Context) {
  193. ctx.Data["IsRepoToolbarCommits"] = true
  194. fileName := ctx.Params("*")
  195. if len(fileName) == 0 {
  196. Commits(ctx)
  197. return
  198. }
  199. userName := ctx.Repo.Owner.Name
  200. repoName := ctx.Repo.Repository.Name
  201. branchName := ctx.Params(":branchname")
  202. brs, err := ctx.Repo.GitRepo.GetBranches()
  203. if err != nil {
  204. ctx.Handle(500, "GetBranches", err)
  205. return
  206. } else if len(brs) == 0 {
  207. ctx.Handle(404, "GetBranches", nil)
  208. return
  209. }
  210. commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
  211. if err != nil {
  212. ctx.Handle(500, "repo.FileHistory(GetCommitsCount)", err)
  213. return
  214. } else if commitsCount == 0 {
  215. ctx.Handle(404, "repo.FileHistory", nil)
  216. return
  217. }
  218. // Calculate and validate page number.
  219. page := com.StrTo(ctx.Query("p")).MustInt()
  220. if page < 1 {
  221. page = 1
  222. }
  223. lastPage := page - 1
  224. if lastPage < 0 {
  225. lastPage = 0
  226. }
  227. nextPage := page + 1
  228. if nextPage*50 > commitsCount {
  229. nextPage = 0
  230. }
  231. ctx.Data["Commits"], err = ctx.Repo.GitRepo.CommitsByFileAndRange(
  232. branchName, fileName, page)
  233. if err != nil {
  234. ctx.Handle(500, "repo.FileHistory(CommitsByRange)", err)
  235. return
  236. }
  237. ctx.Data["Username"] = userName
  238. ctx.Data["Reponame"] = repoName
  239. ctx.Data["FileName"] = fileName
  240. ctx.Data["CommitCount"] = commitsCount
  241. ctx.Data["LastPageNum"] = lastPage
  242. ctx.Data["NextPageNum"] = nextPage
  243. ctx.HTML(200, COMMITS)
  244. }