commit.go 7.1 KB

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