commit.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.Error(err, "paging commits")
  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.Success(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.Error(err, "search commits")
  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.Success(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. c.NotFoundOrError(gitutil.NewError(err), "get commit by ID")
  100. return
  101. }
  102. diff, err := gitutil.RepoDiff(c.Repo.GitRepo,
  103. commitID, conf.Git.MaxDiffFiles, conf.Git.MaxDiffLines, conf.Git.MaxDiffLineChars,
  104. )
  105. if err != nil {
  106. c.NotFoundOrError(gitutil.NewError(err), "get diff")
  107. return
  108. }
  109. parents := make([]string, commit.ParentsCount())
  110. for i := 0; i < commit.ParentsCount(); i++ {
  111. sha, err := commit.ParentID(i)
  112. if err != nil {
  113. c.NotFound()
  114. return
  115. }
  116. parents[i] = sha.String()
  117. }
  118. setEditorconfigIfExists(c)
  119. if c.Written() {
  120. return
  121. }
  122. c.RawTitle(commit.Summary() + " · " + tool.ShortSHA1(commitID))
  123. c.Data["CommitID"] = commitID
  124. c.Data["IsSplitStyle"] = c.Query("style") == "split"
  125. c.Data["Username"] = userName
  126. c.Data["Reponame"] = repoName
  127. c.Data["IsImageFile"] = commit.IsImageFile
  128. c.Data["IsImageFileByIndex"] = commit.IsImageFileByIndex
  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. c.Data["RawPath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "raw", commitID)
  136. if commit.ParentsCount() > 0 {
  137. c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", parents[0])
  138. c.Data["BeforeRawPath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "raw", parents[0])
  139. }
  140. c.Success(DIFF)
  141. }
  142. func RawDiff(c *context.Context) {
  143. if err := c.Repo.GitRepo.RawDiff(
  144. c.Params(":sha"),
  145. git.RawDiffFormat(c.Params(":ext")),
  146. c.Resp,
  147. ); err != nil {
  148. c.NotFoundOrError(gitutil.NewError(err), "get raw diff")
  149. return
  150. }
  151. }
  152. func CompareDiff(c *context.Context) {
  153. c.Data["IsDiffCompare"] = true
  154. userName := c.Repo.Owner.Name
  155. repoName := c.Repo.Repository.Name
  156. beforeCommitID := c.Params(":before")
  157. afterCommitID := c.Params(":after")
  158. commit, err := c.Repo.GitRepo.CatFileCommit(afterCommitID)
  159. if err != nil {
  160. c.NotFoundOrError(gitutil.NewError(err), "get head commit")
  161. return
  162. }
  163. diff, err := gitutil.RepoDiff(c.Repo.GitRepo,
  164. afterCommitID, conf.Git.MaxDiffFiles, conf.Git.MaxDiffLines, conf.Git.MaxDiffLineChars,
  165. git.DiffOptions{Base: beforeCommitID},
  166. )
  167. if err != nil {
  168. c.NotFoundOrError(gitutil.NewError(err), "get diff")
  169. return
  170. }
  171. commits, err := commit.CommitsAfter(beforeCommitID)
  172. if err != nil {
  173. c.NotFoundOrError(gitutil.NewError(err), "get commits after")
  174. return
  175. }
  176. c.Data["IsSplitStyle"] = c.Query("style") == "split"
  177. c.Data["CommitRepoLink"] = c.Repo.RepoLink
  178. c.Data["Commits"] = db.ValidateCommitsWithEmails(commits)
  179. c.Data["CommitsCount"] = len(commits)
  180. c.Data["BeforeCommitID"] = beforeCommitID
  181. c.Data["AfterCommitID"] = afterCommitID
  182. c.Data["Username"] = userName
  183. c.Data["Reponame"] = repoName
  184. c.Data["IsImageFile"] = commit.IsImageFile
  185. c.Data["IsImageFileByIndex"] = commit.IsImageFileByIndex
  186. c.Data["Title"] = "Comparing " + tool.ShortSHA1(beforeCommitID) + "..." + tool.ShortSHA1(afterCommitID) + " · " + userName + "/" + repoName
  187. c.Data["Commit"] = commit
  188. c.Data["Diff"] = diff
  189. c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  190. c.Data["SourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", afterCommitID)
  191. c.Data["RawPath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "raw", afterCommitID)
  192. c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", beforeCommitID)
  193. c.Data["BeforeRawPath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "raw", beforeCommitID)
  194. c.Success(DIFF)
  195. }