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.

commit.go 7.0KB

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