commit.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. // COMMITS page template
  18. COMMITS = "repo/commits"
  19. // DIFF page template
  20. DIFF = "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. func renderCommits(c *context.Context, filename string) {
  42. c.Data["Title"] = c.Tr("repo.commits.commit_history") + " · " + c.Repo.Repository.FullName()
  43. c.Data["PageIsCommits"] = true
  44. c.Data["FileName"] = filename
  45. page := c.QueryInt("page")
  46. if page < 1 {
  47. page = 1
  48. }
  49. pageSize := c.QueryInt("pageSize")
  50. if pageSize < 1 {
  51. pageSize = git.DefaultCommitsPageSize
  52. }
  53. // Both 'git log branchName' and 'git log commitID' work.
  54. var err error
  55. var commits *list.List
  56. if len(filename) == 0 {
  57. commits, err = c.Repo.Commit.CommitsByRangeSize(page, pageSize)
  58. } else {
  59. commits, err = c.Repo.GitRepo.CommitsByFileAndRangeSize(c.Repo.BranchName, filename, page, pageSize)
  60. }
  61. if err != nil {
  62. c.Handle(500, "CommitsByRangeSize/CommitsByFileAndRangeSize", err)
  63. return
  64. }
  65. commits = RenderIssueLinks(commits, c.Repo.RepoLink)
  66. commits = models.ValidateCommitsWithEmails(commits)
  67. c.Data["Commits"] = commits
  68. if page > 1 {
  69. c.Data["HasPrevious"] = true
  70. c.Data["PreviousPage"] = page - 1
  71. }
  72. if commits.Len() == pageSize {
  73. c.Data["HasNext"] = true
  74. c.Data["NextPage"] = page + 1
  75. }
  76. c.Data["PageSize"] = pageSize
  77. c.Data["Username"] = c.Repo.Owner.Name
  78. c.Data["Reponame"] = c.Repo.Repository.Name
  79. c.HTML(200, COMMITS)
  80. }
  81. func Commits(c *context.Context) {
  82. renderCommits(c, "")
  83. }
  84. func SearchCommits(c *context.Context) {
  85. c.Data["PageIsCommits"] = true
  86. keyword := c.Query("q")
  87. if len(keyword) == 0 {
  88. c.Redirect(c.Repo.RepoLink + "/commits/" + c.Repo.BranchName)
  89. return
  90. }
  91. commits, err := c.Repo.Commit.SearchCommits(keyword)
  92. if err != nil {
  93. c.Handle(500, "SearchCommits", err)
  94. return
  95. }
  96. commits = RenderIssueLinks(commits, c.Repo.RepoLink)
  97. commits = models.ValidateCommitsWithEmails(commits)
  98. c.Data["Commits"] = commits
  99. c.Data["Keyword"] = keyword
  100. c.Data["Username"] = c.Repo.Owner.Name
  101. c.Data["Reponame"] = c.Repo.Repository.Name
  102. c.Data["Branch"] = c.Repo.BranchName
  103. c.HTML(200, COMMITS)
  104. }
  105. func FileHistory(c *context.Context) {
  106. renderCommits(c, c.Repo.TreePath)
  107. }
  108. func Diff(c *context.Context) {
  109. c.RequireHighlightJS()
  110. c.RequireHighlightJS()
  111. userName := c.Repo.Owner.Name
  112. repoName := c.Repo.Repository.Name
  113. commitID := c.Params(":sha")
  114. commit, err := c.Repo.GitRepo.GetCommit(commitID)
  115. if err != nil {
  116. c.NotFoundOrServerError("get commit by ID", git.IsErrNotExist, err)
  117. }
  118. diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName),
  119. commitID, setting.Git.MaxGitDiffLines,
  120. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  121. if err != nil {
  122. c.NotFoundOrServerError("get diff commit", git.IsErrNotExist, err)
  123. return
  124. }
  125. parents := make([]string, commit.ParentCount())
  126. for i := 0; i < commit.ParentCount(); i++ {
  127. sha, err := commit.ParentID(i)
  128. parents[i] = sha.String()
  129. if err != nil {
  130. c.NotFound()
  131. return
  132. }
  133. }
  134. setEditorconfigIfExists(c)
  135. if c.Written() {
  136. return
  137. }
  138. c.Title(commit.Summary() + " · " + tool.ShortSHA1(commitID))
  139. c.Data["CommitID"] = commitID
  140. c.Data["IsSplitStyle"] = c.Query("style") == "split"
  141. c.Data["Username"] = userName
  142. c.Data["Reponame"] = repoName
  143. c.Data["IsImageFile"] = commit.IsImageFile
  144. c.Data["Commit"] = commit
  145. c.Data["Author"] = models.ValidateCommitWithEmail(commit)
  146. c.Data["Diff"] = diff
  147. c.Data["Parents"] = parents
  148. c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  149. c.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", commitID)
  150. if commit.ParentCount() > 0 {
  151. c.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", parents[0])
  152. }
  153. c.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", commitID)
  154. c.Success(DIFF)
  155. }
  156. func RawDiff(c *context.Context) {
  157. if err := git.GetRawDiff(
  158. models.RepoPath(c.Repo.Owner.Name, c.Repo.Repository.Name),
  159. c.Params(":sha"),
  160. git.RawDiffType(c.Params(":ext")),
  161. c.Resp,
  162. ); err != nil {
  163. c.NotFoundOrServerError("GetRawDiff", git.IsErrNotExist, err)
  164. return
  165. }
  166. }
  167. func CompareDiff(c *context.Context) {
  168. c.Data["IsDiffCompare"] = true
  169. userName := c.Repo.Owner.Name
  170. repoName := c.Repo.Repository.Name
  171. beforeCommitID := c.Params(":before")
  172. afterCommitID := c.Params(":after")
  173. commit, err := c.Repo.GitRepo.GetCommit(afterCommitID)
  174. if err != nil {
  175. c.Handle(404, "GetCommit", err)
  176. return
  177. }
  178. diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitID,
  179. afterCommitID, setting.Git.MaxGitDiffLines,
  180. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  181. if err != nil {
  182. c.Handle(404, "GetDiffRange", err)
  183. return
  184. }
  185. commits, err := commit.CommitsBeforeUntil(beforeCommitID)
  186. if err != nil {
  187. c.Handle(500, "CommitsBeforeUntil", err)
  188. return
  189. }
  190. commits = models.ValidateCommitsWithEmails(commits)
  191. c.Data["IsSplitStyle"] = c.Query("style") == "split"
  192. c.Data["CommitRepoLink"] = c.Repo.RepoLink
  193. c.Data["Commits"] = commits
  194. c.Data["CommitsCount"] = commits.Len()
  195. c.Data["BeforeCommitID"] = beforeCommitID
  196. c.Data["AfterCommitID"] = afterCommitID
  197. c.Data["Username"] = userName
  198. c.Data["Reponame"] = repoName
  199. c.Data["IsImageFile"] = commit.IsImageFile
  200. c.Data["Title"] = "Comparing " + tool.ShortSHA1(beforeCommitID) + "..." + tool.ShortSHA1(afterCommitID) + " · " + userName + "/" + repoName
  201. c.Data["Commit"] = commit
  202. c.Data["Diff"] = diff
  203. c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  204. c.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", afterCommitID)
  205. c.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", beforeCommitID)
  206. c.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", afterCommitID)
  207. c.HTML(200, DIFF)
  208. }