commit.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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["Commit"] = commit
  129. c.Data["Author"] = db.ValidateCommitWithEmail(commit)
  130. c.Data["Diff"] = diff
  131. c.Data["Parents"] = parents
  132. c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  133. c.Data["SourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", commitID)
  134. if commit.ParentsCount() > 0 {
  135. c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", parents[0])
  136. }
  137. c.Data["RawPath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "raw", commitID)
  138. c.Success(DIFF)
  139. }
  140. func RawDiff(c *context.Context) {
  141. if err := c.Repo.GitRepo.RawDiff(
  142. c.Params(":sha"),
  143. git.RawDiffFormat(c.Params(":ext")),
  144. c.Resp,
  145. ); err != nil {
  146. c.NotFoundOrError(gitutil.NewError(err), "get raw diff")
  147. return
  148. }
  149. }
  150. func CompareDiff(c *context.Context) {
  151. c.Data["IsDiffCompare"] = true
  152. userName := c.Repo.Owner.Name
  153. repoName := c.Repo.Repository.Name
  154. beforeCommitID := c.Params(":before")
  155. afterCommitID := c.Params(":after")
  156. commit, err := c.Repo.GitRepo.CatFileCommit(afterCommitID)
  157. if err != nil {
  158. c.NotFoundOrError(gitutil.NewError(err), "get head commit")
  159. return
  160. }
  161. diff, err := gitutil.RepoDiff(c.Repo.GitRepo,
  162. afterCommitID, conf.Git.MaxDiffFiles, conf.Git.MaxDiffLines, conf.Git.MaxDiffLineChars,
  163. git.DiffOptions{Base: beforeCommitID},
  164. )
  165. if err != nil {
  166. c.NotFoundOrError(gitutil.NewError(err), "get diff")
  167. return
  168. }
  169. commits, err := commit.CommitsAfter(beforeCommitID)
  170. if err != nil {
  171. c.NotFoundOrError(gitutil.NewError(err), "get commits after")
  172. return
  173. }
  174. c.Data["IsSplitStyle"] = c.Query("style") == "split"
  175. c.Data["CommitRepoLink"] = c.Repo.RepoLink
  176. c.Data["Commits"] = db.ValidateCommitsWithEmails(commits)
  177. c.Data["CommitsCount"] = len(commits)
  178. c.Data["BeforeCommitID"] = beforeCommitID
  179. c.Data["AfterCommitID"] = afterCommitID
  180. c.Data["Username"] = userName
  181. c.Data["Reponame"] = repoName
  182. c.Data["IsImageFile"] = commit.IsImageFile
  183. c.Data["Title"] = "Comparing " + tool.ShortSHA1(beforeCommitID) + "..." + tool.ShortSHA1(afterCommitID) + " · " + userName + "/" + repoName
  184. c.Data["Commit"] = commit
  185. c.Data["Diff"] = diff
  186. c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  187. c.Data["SourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", afterCommitID)
  188. c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", beforeCommitID)
  189. c.Data["RawPath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "raw", afterCommitID)
  190. c.Success(DIFF)
  191. }