commit.go 6.2 KB

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