commit.go 6.9 KB

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