commit.go 6.5 KB

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