commit.go 7.4 KB

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