commit.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/gogs/git-module"
  8. "gogs.io/gogs/internal/conf"
  9. "gogs.io/gogs/internal/context"
  10. "gogs.io/gogs/internal/db"
  11. "gogs.io/gogs/internal/gitutil"
  12. "gogs.io/gogs/internal/tool"
  13. )
  14. const (
  15. COMMITS = "repo/commits"
  16. DIFF = "repo/diff/page"
  17. )
  18. func RefCommits(c *context.Context) {
  19. c.Data["PageIsViewFiles"] = true
  20. switch {
  21. case len(c.Repo.TreePath) == 0:
  22. Commits(c)
  23. case c.Repo.TreePath == "search":
  24. SearchCommits(c)
  25. default:
  26. FileHistory(c)
  27. }
  28. }
  29. // TODO(unknwon)
  30. func RenderIssueLinks(oldCommits []*git.Commit, repoLink string) []*git.Commit {
  31. return oldCommits
  32. }
  33. func renderCommits(c *context.Context, filename string) {
  34. c.Data["Title"] = c.Tr("repo.commits.commit_history") + " · " + c.Repo.Repository.FullName()
  35. c.Data["PageIsCommits"] = true
  36. c.Data["FileName"] = filename
  37. page := c.QueryInt("page")
  38. if page < 1 {
  39. page = 1
  40. }
  41. pageSize := c.QueryInt("pageSize")
  42. if pageSize < 1 {
  43. pageSize = conf.UI.User.CommitsPagingNum
  44. }
  45. commits, err := c.Repo.Commit.CommitsByPage(page, pageSize, git.CommitsByPageOptions{Path: filename})
  46. if err != nil {
  47. c.ServerError("paging commits", err)
  48. return
  49. }
  50. commits = RenderIssueLinks(commits, c.Repo.RepoLink)
  51. c.Data["Commits"] = db.ValidateCommitsWithEmails(commits)
  52. if page > 1 {
  53. c.Data["HasPrevious"] = true
  54. c.Data["PreviousPage"] = page - 1
  55. }
  56. if len(commits) == pageSize {
  57. c.Data["HasNext"] = true
  58. c.Data["NextPage"] = page + 1
  59. }
  60. c.Data["PageSize"] = pageSize
  61. c.Data["Username"] = c.Repo.Owner.Name
  62. c.Data["Reponame"] = c.Repo.Repository.Name
  63. c.HTML(200, COMMITS)
  64. }
  65. func Commits(c *context.Context) {
  66. renderCommits(c, "")
  67. }
  68. func SearchCommits(c *context.Context) {
  69. c.Data["PageIsCommits"] = true
  70. keyword := c.Query("q")
  71. if len(keyword) == 0 {
  72. c.Redirect(c.Repo.RepoLink + "/commits/" + c.Repo.BranchName)
  73. return
  74. }
  75. commits, err := c.Repo.Commit.SearchCommits(keyword)
  76. if err != nil {
  77. c.ServerError("SearchCommits", err)
  78. return
  79. }
  80. commits = RenderIssueLinks(commits, c.Repo.RepoLink)
  81. c.Data["Commits"] = db.ValidateCommitsWithEmails(commits)
  82. c.Data["Keyword"] = keyword
  83. c.Data["Username"] = c.Repo.Owner.Name
  84. c.Data["Reponame"] = c.Repo.Repository.Name
  85. c.Data["Branch"] = c.Repo.BranchName
  86. c.HTML(200, COMMITS)
  87. }
  88. func FileHistory(c *context.Context) {
  89. renderCommits(c, c.Repo.TreePath)
  90. }
  91. func Diff(c *context.Context) {
  92. c.PageIs("Diff")
  93. c.RequireHighlightJS()
  94. userName := c.Repo.Owner.Name
  95. repoName := c.Repo.Repository.Name
  96. commitID := c.Params(":sha")
  97. commit, err := c.Repo.GitRepo.CatFileCommit(commitID)
  98. if err != nil {
  99. // TODO: Move checker to gitutil package
  100. c.NotFoundOrServerError("get commit by ID", gitutil.IsErrRevisionNotExist, err)
  101. return
  102. }
  103. diff, err := gitutil.RepoDiff(c.Repo.GitRepo,
  104. commitID, conf.Git.MaxDiffFiles, conf.Git.MaxDiffLines, conf.Git.MaxDiffLineChars,
  105. )
  106. if err != nil {
  107. c.NotFoundOrServerError("get diff", gitutil.IsErrRevisionNotExist, err)
  108. return
  109. }
  110. parents := make([]string, commit.ParentsCount())
  111. for i := 0; i < commit.ParentsCount(); i++ {
  112. sha, err := commit.ParentID(i)
  113. parents[i] = sha.String()
  114. if err != nil {
  115. c.NotFound()
  116. return
  117. }
  118. }
  119. setEditorconfigIfExists(c)
  120. if c.Written() {
  121. return
  122. }
  123. c.Title(commit.Summary() + " · " + tool.ShortSHA1(commitID))
  124. c.Data["CommitID"] = commitID
  125. c.Data["IsSplitStyle"] = c.Query("style") == "split"
  126. c.Data["Username"] = userName
  127. c.Data["Reponame"] = repoName
  128. c.Data["IsImageFile"] = commit.IsImageFile
  129. c.Data["Commit"] = commit
  130. c.Data["Author"] = db.ValidateCommitWithEmail(commit)
  131. c.Data["Diff"] = diff
  132. c.Data["Parents"] = parents
  133. c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  134. c.Data["SourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", commitID)
  135. if commit.ParentsCount() > 0 {
  136. c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", parents[0])
  137. }
  138. c.Data["RawPath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "raw", commitID)
  139. c.Success(DIFF)
  140. }
  141. func RawDiff(c *context.Context) {
  142. if err := c.Repo.GitRepo.RawDiff(
  143. c.Params(":sha"),
  144. git.RawDiffFormat(c.Params(":ext")),
  145. c.Resp,
  146. ); err != nil {
  147. c.NotFoundOrServerError("get raw diff", gitutil.IsErrRevisionNotExist, err)
  148. return
  149. }
  150. }
  151. func CompareDiff(c *context.Context) {
  152. c.Data["IsDiffCompare"] = true
  153. userName := c.Repo.Owner.Name
  154. repoName := c.Repo.Repository.Name
  155. beforeCommitID := c.Params(":before")
  156. afterCommitID := c.Params(":after")
  157. commit, err := c.Repo.GitRepo.CatFileCommit(afterCommitID)
  158. if err != nil {
  159. c.Handle(404, "GetCommit", err)
  160. return
  161. }
  162. diff, err := gitutil.RepoDiff(c.Repo.GitRepo,
  163. afterCommitID, conf.Git.MaxDiffFiles, conf.Git.MaxDiffLines, conf.Git.MaxDiffLineChars,
  164. git.DiffOptions{Base: beforeCommitID},
  165. )
  166. if err != nil {
  167. c.ServerError("get diff", err)
  168. return
  169. }
  170. commits, err := commit.CommitsAfter(beforeCommitID)
  171. if err != nil {
  172. c.ServerError("get commits after", err)
  173. return
  174. }
  175. c.Data["IsSplitStyle"] = c.Query("style") == "split"
  176. c.Data["CommitRepoLink"] = c.Repo.RepoLink
  177. c.Data["Commits"] = db.ValidateCommitsWithEmails(commits)
  178. c.Data["CommitsCount"] = len(commits)
  179. c.Data["BeforeCommitID"] = beforeCommitID
  180. c.Data["AfterCommitID"] = afterCommitID
  181. c.Data["Username"] = userName
  182. c.Data["Reponame"] = repoName
  183. c.Data["IsImageFile"] = commit.IsImageFile
  184. c.Data["Title"] = "Comparing " + tool.ShortSHA1(beforeCommitID) + "..." + tool.ShortSHA1(afterCommitID) + " · " + userName + "/" + repoName
  185. c.Data["Commit"] = commit
  186. c.Data["Diff"] = diff
  187. c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  188. c.Data["SourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", afterCommitID)
  189. c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", beforeCommitID)
  190. c.Data["RawPath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "raw", afterCommitID)
  191. c.HTML(200, DIFF)
  192. }