commit.go 6.4 KB

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