commit.go 6.4 KB

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