commit.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package repo
  7. import (
  8. "container/list"
  9. "gitote/gitote/models"
  10. "gitote/gitote/pkg/context"
  11. "gitote/gitote/pkg/setting"
  12. "gitote/gitote/pkg/tool"
  13. "path"
  14. "gitlab.com/gitote/git-module"
  15. )
  16. const (
  17. // CommitsTPL page template
  18. CommitsTPL = "repo/commits"
  19. // DiffTPL page template
  20. DiffTPL = "repo/diff/page"
  21. )
  22. func RefCommits(c *context.Context) {
  23. c.Data["PageIsViewFiles"] = true
  24. switch {
  25. case len(c.Repo.TreePath) == 0:
  26. Commits(c)
  27. case c.Repo.TreePath == "search":
  28. SearchCommits(c)
  29. default:
  30. FileHistory(c)
  31. }
  32. }
  33. func RenderIssueLinks(oldCommits *list.List, repoLink string) *list.List {
  34. newCommits := list.New()
  35. for e := oldCommits.Front(); e != nil; e = e.Next() {
  36. c := e.Value.(*git.Commit)
  37. newCommits.PushBack(c)
  38. }
  39. return newCommits
  40. }
  41. // renderCommits shows commits page
  42. func renderCommits(c *context.Context, filename string) {
  43. c.Data["Title"] = c.Tr("repo.commits.commit_history") + " · " + c.Repo.Repository.FullName()
  44. c.Data["PageIsCommits"] = true
  45. c.Data["FileName"] = filename
  46. page := c.QueryInt("page")
  47. if page < 1 {
  48. page = 1
  49. }
  50. pageSize := c.QueryInt("pageSize")
  51. if pageSize < 1 {
  52. pageSize = git.DefaultCommitsPageSize
  53. }
  54. // Both 'git log branchName' and 'git log commitID' work.
  55. var err error
  56. var commits *list.List
  57. if len(filename) == 0 {
  58. commits, err = c.Repo.Commit.CommitsByRangeSize(page, pageSize)
  59. } else {
  60. commits, err = c.Repo.GitRepo.CommitsByFileAndRangeSize(c.Repo.BranchName, filename, page, pageSize)
  61. }
  62. if err != nil {
  63. c.Handle(500, "CommitsByRangeSize/CommitsByFileAndRangeSize", err)
  64. return
  65. }
  66. commits = RenderIssueLinks(commits, c.Repo.RepoLink)
  67. commits = models.ValidateCommitsWithEmails(commits)
  68. c.Data["Commits"] = commits
  69. if page > 1 {
  70. c.Data["HasPrevious"] = true
  71. c.Data["PreviousPage"] = page - 1
  72. }
  73. if commits.Len() == pageSize {
  74. c.Data["HasNext"] = true
  75. c.Data["NextPage"] = page + 1
  76. }
  77. c.Data["PageSize"] = pageSize
  78. c.Data["Username"] = c.Repo.Owner.Name
  79. c.Data["Reponame"] = c.Repo.Repository.Name
  80. c.HTML(200, CommitsTPL)
  81. }
  82. func Commits(c *context.Context) {
  83. renderCommits(c, "")
  84. }
  85. // SearchCommits shows search commits page
  86. func SearchCommits(c *context.Context) {
  87. c.Data["PageIsCommits"] = true
  88. keyword := c.Query("q")
  89. if len(keyword) == 0 {
  90. c.Redirect(c.Repo.RepoLink + "/commits/" + c.Repo.BranchName)
  91. return
  92. }
  93. commits, err := c.Repo.Commit.SearchCommits(keyword)
  94. if err != nil {
  95. c.Handle(500, "SearchCommits", err)
  96. return
  97. }
  98. commits = RenderIssueLinks(commits, c.Repo.RepoLink)
  99. commits = models.ValidateCommitsWithEmails(commits)
  100. c.Data["Commits"] = commits
  101. c.Data["Keyword"] = keyword
  102. c.Data["Username"] = c.Repo.Owner.Name
  103. c.Data["Reponame"] = c.Repo.Repository.Name
  104. c.Data["Branch"] = c.Repo.BranchName
  105. c.HTML(200, CommitsTPL)
  106. }
  107. func FileHistory(c *context.Context) {
  108. renderCommits(c, c.Repo.TreePath)
  109. }
  110. // Diff shows diff page
  111. func Diff(c *context.Context) {
  112. c.RequireHighlightJS()
  113. c.RequireHighlightJS()
  114. userName := c.Repo.Owner.Name
  115. repoName := c.Repo.Repository.Name
  116. commitID := c.Params(":sha")
  117. commit, err := c.Repo.GitRepo.GetCommit(commitID)
  118. if err != nil {
  119. c.NotFoundOrServerError("get commit by ID", git.IsErrNotExist, err)
  120. }
  121. diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName),
  122. commitID, setting.Git.MaxGitDiffLines,
  123. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  124. if err != nil {
  125. c.NotFoundOrServerError("get diff commit", git.IsErrNotExist, err)
  126. return
  127. }
  128. parents := make([]string, commit.ParentCount())
  129. for i := 0; i < commit.ParentCount(); i++ {
  130. sha, err := commit.ParentID(i)
  131. parents[i] = sha.String()
  132. if err != nil {
  133. c.NotFound()
  134. return
  135. }
  136. }
  137. setEditorconfigIfExists(c)
  138. if c.Written() {
  139. return
  140. }
  141. c.Title(commit.Summary() + " · " + tool.ShortSHA1(commitID))
  142. c.Data["CommitID"] = commitID
  143. c.Data["IsSplitStyle"] = c.Query("style") == "split"
  144. c.Data["Username"] = userName
  145. c.Data["Reponame"] = repoName
  146. c.Data["IsImageFile"] = commit.IsImageFile
  147. c.Data["Commit"] = commit
  148. c.Data["Author"] = models.ValidateCommitWithEmail(commit)
  149. c.Data["Diff"] = diff
  150. c.Data["Parents"] = parents
  151. c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  152. c.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", commitID)
  153. if commit.ParentCount() > 0 {
  154. c.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", parents[0])
  155. }
  156. c.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", commitID)
  157. c.Success(DiffTPL)
  158. }
  159. func RawDiff(c *context.Context) {
  160. if err := git.GetRawDiff(
  161. models.RepoPath(c.Repo.Owner.Name, c.Repo.Repository.Name),
  162. c.Params(":sha"),
  163. git.RawDiffType(c.Params(":ext")),
  164. c.Resp,
  165. ); err != nil {
  166. c.NotFoundOrServerError("GetRawDiff", git.IsErrNotExist, err)
  167. return
  168. }
  169. }
  170. func CompareDiff(c *context.Context) {
  171. c.Data["IsDiffCompare"] = true
  172. userName := c.Repo.Owner.Name
  173. repoName := c.Repo.Repository.Name
  174. beforeCommitID := c.Params(":before")
  175. afterCommitID := c.Params(":after")
  176. commit, err := c.Repo.GitRepo.GetCommit(afterCommitID)
  177. if err != nil {
  178. c.Handle(404, "GetCommit", err)
  179. return
  180. }
  181. diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitID,
  182. afterCommitID, setting.Git.MaxGitDiffLines,
  183. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  184. if err != nil {
  185. c.Handle(404, "GetDiffRange", err)
  186. return
  187. }
  188. commits, err := commit.CommitsBeforeUntil(beforeCommitID)
  189. if err != nil {
  190. c.Handle(500, "CommitsBeforeUntil", err)
  191. return
  192. }
  193. commits = models.ValidateCommitsWithEmails(commits)
  194. c.Data["IsSplitStyle"] = c.Query("style") == "split"
  195. c.Data["CommitRepoLink"] = c.Repo.RepoLink
  196. c.Data["Commits"] = commits
  197. c.Data["CommitsCount"] = commits.Len()
  198. c.Data["BeforeCommitID"] = beforeCommitID
  199. c.Data["AfterCommitID"] = afterCommitID
  200. c.Data["Username"] = userName
  201. c.Data["Reponame"] = repoName
  202. c.Data["IsImageFile"] = commit.IsImageFile
  203. c.Data["Title"] = "Comparing " + tool.ShortSHA1(beforeCommitID) + "..." + tool.ShortSHA1(afterCommitID) + " · " + userName + "/" + repoName
  204. c.Data["Commit"] = commit
  205. c.Data["Diff"] = diff
  206. c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  207. c.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", afterCommitID)
  208. c.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", beforeCommitID)
  209. c.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", afterCommitID)
  210. c.HTML(200, DiffTPL)
  211. }