commit.go 6.9 KB

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