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.

291 lines
7.0 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. "github.com/gogits/gogs/modules/setting"
  12. )
  13. const (
  14. COMMITS base.TplName = "repo/commits"
  15. DIFF base.TplName = "repo/diff"
  16. )
  17. func Commits(ctx *middleware.Context) {
  18. ctx.Data["IsRepoToolbarCommits"] = true
  19. userName := ctx.Repo.Owner.Name
  20. repoName := ctx.Repo.Repository.Name
  21. brs, err := ctx.Repo.GitRepo.GetBranches()
  22. if err != nil {
  23. ctx.Handle(500, "GetBranches", err)
  24. return
  25. } else if len(brs) == 0 {
  26. ctx.Handle(404, "GetBranches", nil)
  27. return
  28. }
  29. commitsCount, err := ctx.Repo.Commit.CommitsCount()
  30. if err != nil {
  31. ctx.Handle(500, "GetCommitsCount", err)
  32. return
  33. }
  34. // Calculate and validate page number.
  35. page, _ := com.StrTo(ctx.Query("p")).Int()
  36. if page < 1 {
  37. page = 1
  38. }
  39. lastPage := page - 1
  40. if lastPage < 0 {
  41. lastPage = 0
  42. }
  43. nextPage := page + 1
  44. if nextPage*50 > commitsCount {
  45. nextPage = 0
  46. }
  47. // Both `git log branchName` and `git log commitId` work.
  48. commits, err := ctx.Repo.Commit.CommitsByRange(page)
  49. if err != nil {
  50. ctx.Handle(500, "CommitsByRange", err)
  51. return
  52. }
  53. commits = models.ValidateCommitsWithEmails(commits)
  54. ctx.Data["Commits"] = commits
  55. ctx.Data["Username"] = userName
  56. ctx.Data["Reponame"] = repoName
  57. ctx.Data["CommitCount"] = commitsCount
  58. ctx.Data["LastPageNum"] = lastPage
  59. ctx.Data["NextPageNum"] = nextPage
  60. ctx.HTML(200, COMMITS)
  61. }
  62. func SearchCommits(ctx *middleware.Context) {
  63. ctx.Data["IsSearchPage"] = true
  64. ctx.Data["IsRepoToolbarCommits"] = true
  65. keyword := ctx.Query("q")
  66. if len(keyword) == 0 {
  67. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName)
  68. return
  69. }
  70. userName := ctx.Params(":username")
  71. repoName := ctx.Params(":reponame")
  72. brs, err := ctx.Repo.GitRepo.GetBranches()
  73. if err != nil {
  74. ctx.Handle(500, "GetBranches", err)
  75. return
  76. } else if len(brs) == 0 {
  77. ctx.Handle(404, "GetBranches", nil)
  78. return
  79. }
  80. commits, err := ctx.Repo.Commit.SearchCommits(keyword)
  81. if err != nil {
  82. ctx.Handle(500, "SearchCommits", err)
  83. return
  84. }
  85. commits = models.ValidateCommitsWithEmails(commits)
  86. ctx.Data["Keyword"] = keyword
  87. ctx.Data["Username"] = userName
  88. ctx.Data["Reponame"] = repoName
  89. ctx.Data["CommitCount"] = commits.Len()
  90. ctx.Data["Commits"] = commits
  91. ctx.HTML(200, COMMITS)
  92. }
  93. func Diff(ctx *middleware.Context) {
  94. ctx.Data["IsRepoToolbarCommits"] = true
  95. userName := ctx.Repo.Owner.Name
  96. repoName := ctx.Repo.Repository.Name
  97. commitId := ctx.Repo.CommitId
  98. commit := ctx.Repo.Commit
  99. diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName),
  100. commitId, setting.MaxGitDiffLines)
  101. if err != nil {
  102. ctx.Handle(404, "GetDiffCommit", err)
  103. return
  104. }
  105. isImageFile := func(name string) bool {
  106. blob, err := ctx.Repo.Commit.GetBlobByPath(name)
  107. if err != nil {
  108. return false
  109. }
  110. dataRc, err := blob.Data()
  111. if err != nil {
  112. return false
  113. }
  114. buf := make([]byte, 1024)
  115. n, _ := dataRc.Read(buf)
  116. if n > 0 {
  117. buf = buf[:n]
  118. }
  119. _, isImage := base.IsImageFile(buf)
  120. return isImage
  121. }
  122. parents := make([]string, commit.ParentCount())
  123. for i := 0; i < commit.ParentCount(); i++ {
  124. sha, err := commit.ParentId(i)
  125. parents[i] = sha.String()
  126. if err != nil {
  127. ctx.Handle(404, "repo.Diff", err)
  128. return
  129. }
  130. }
  131. ctx.Data["Username"] = userName
  132. ctx.Data["Reponame"] = repoName
  133. ctx.Data["IsImageFile"] = isImageFile
  134. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitId)
  135. ctx.Data["Commit"] = commit
  136. ctx.Data["Diff"] = diff
  137. ctx.Data["Parents"] = parents
  138. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  139. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", commitId)
  140. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "raw", commitId)
  141. ctx.HTML(200, DIFF)
  142. }
  143. func CompareDiff(ctx *middleware.Context) {
  144. ctx.Data["IsRepoToolbarCommits"] = true
  145. ctx.Data["IsDiffCompare"] = true
  146. userName := ctx.Repo.Owner.Name
  147. repoName := ctx.Repo.Repository.Name
  148. beforeCommitId := ctx.Params(":before")
  149. afterCommitId := ctx.Params(":after")
  150. commit, err := ctx.Repo.GitRepo.GetCommit(afterCommitId)
  151. if err != nil {
  152. ctx.Handle(404, "GetCommit", err)
  153. return
  154. }
  155. diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitId,
  156. afterCommitId, setting.MaxGitDiffLines)
  157. if err != nil {
  158. ctx.Handle(404, "GetDiffRange", err)
  159. return
  160. }
  161. isImageFile := func(name string) bool {
  162. blob, err := commit.GetBlobByPath(name)
  163. if err != nil {
  164. return false
  165. }
  166. dataRc, err := blob.Data()
  167. if err != nil {
  168. return false
  169. }
  170. buf := make([]byte, 1024)
  171. n, _ := dataRc.Read(buf)
  172. if n > 0 {
  173. buf = buf[:n]
  174. }
  175. _, isImage := base.IsImageFile(buf)
  176. return isImage
  177. }
  178. commits, err := commit.CommitsBeforeUntil(beforeCommitId)
  179. if err != nil {
  180. ctx.Handle(500, "CommitsBeforeUntil", err)
  181. return
  182. }
  183. ctx.Data["Commits"] = commits
  184. ctx.Data["CommitCount"] = commits.Len()
  185. ctx.Data["BeforeCommitId"] = beforeCommitId
  186. ctx.Data["AfterCommitId"] = afterCommitId
  187. ctx.Data["Username"] = userName
  188. ctx.Data["Reponame"] = repoName
  189. ctx.Data["IsImageFile"] = isImageFile
  190. ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitId) + "..." + base.ShortSha(afterCommitId) + " · " + userName + "/" + repoName
  191. ctx.Data["Commit"] = commit
  192. ctx.Data["Diff"] = diff
  193. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  194. ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", afterCommitId)
  195. ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", afterCommitId)
  196. ctx.HTML(200, DIFF)
  197. }
  198. func FileHistory(ctx *middleware.Context) {
  199. ctx.Data["IsRepoToolbarCommits"] = true
  200. fileName := ctx.Params("*")
  201. if len(fileName) == 0 {
  202. Commits(ctx)
  203. return
  204. }
  205. userName := ctx.Repo.Owner.Name
  206. repoName := ctx.Repo.Repository.Name
  207. branchName := ctx.Params(":branchname")
  208. brs, err := ctx.Repo.GitRepo.GetBranches()
  209. if err != nil {
  210. ctx.Handle(500, "GetBranches", err)
  211. return
  212. } else if len(brs) == 0 {
  213. ctx.Handle(404, "GetBranches", nil)
  214. return
  215. }
  216. commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
  217. if err != nil {
  218. ctx.Handle(500, "repo.FileHistory(GetCommitsCount)", err)
  219. return
  220. } else if commitsCount == 0 {
  221. ctx.Handle(404, "repo.FileHistory", nil)
  222. return
  223. }
  224. // Calculate and validate page number.
  225. page := com.StrTo(ctx.Query("p")).MustInt()
  226. if page < 1 {
  227. page = 1
  228. }
  229. lastPage := page - 1
  230. if lastPage < 0 {
  231. lastPage = 0
  232. }
  233. nextPage := page + 1
  234. if nextPage*50 > commitsCount {
  235. nextPage = 0
  236. }
  237. ctx.Data["Commits"], err = ctx.Repo.GitRepo.CommitsByFileAndRange(
  238. branchName, fileName, page)
  239. if err != nil {
  240. ctx.Handle(500, "repo.FileHistory(CommitsByRange)", err)
  241. return
  242. }
  243. ctx.Data["Username"] = userName
  244. ctx.Data["Reponame"] = repoName
  245. ctx.Data["FileName"] = fileName
  246. ctx.Data["CommitCount"] = commitsCount
  247. ctx.Data["LastPageNum"] = lastPage
  248. ctx.Data["NextPageNum"] = nextPage
  249. ctx.HTML(200, COMMITS)
  250. }