commit.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/gogits/git-module"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/pkg/context"
  11. "github.com/gogits/gogs/pkg/setting"
  12. "github.com/gogits/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.Data["PageIsDiff"] = true
  106. c.Data["RequireHighlightJS"] = true
  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. if git.IsErrNotExist(err) {
  113. c.Handle(404, "Repo.GitRepo.GetCommit", err)
  114. } else {
  115. c.Handle(500, "Repo.GitRepo.GetCommit", err)
  116. }
  117. return
  118. }
  119. diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName),
  120. commitID, setting.Git.MaxGitDiffLines,
  121. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  122. if err != nil {
  123. c.NotFoundOrServerError("GetDiffCommit", git.IsErrNotExist, err)
  124. return
  125. }
  126. parents := make([]string, commit.ParentCount())
  127. for i := 0; i < commit.ParentCount(); i++ {
  128. sha, err := commit.ParentID(i)
  129. parents[i] = sha.String()
  130. if err != nil {
  131. c.Handle(404, "repo.Diff", err)
  132. return
  133. }
  134. }
  135. setEditorconfigIfExists(c)
  136. if c.Written() {
  137. return
  138. }
  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["Title"] = commit.Summary() + " · " + tool.ShortSHA1(commitID)
  145. c.Data["Commit"] = commit
  146. c.Data["Author"] = models.ValidateCommitWithEmail(commit)
  147. c.Data["Diff"] = diff
  148. c.Data["Parents"] = parents
  149. c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  150. c.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", commitID)
  151. if commit.ParentCount() > 0 {
  152. c.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", parents[0])
  153. }
  154. c.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", commitID)
  155. c.HTML(200, DIFF)
  156. }
  157. func RawDiff(c *context.Context) {
  158. if err := git.GetRawDiff(
  159. models.RepoPath(c.Repo.Owner.Name, c.Repo.Repository.Name),
  160. c.Params(":sha"),
  161. git.RawDiffType(c.Params(":ext")),
  162. c.Resp,
  163. ); err != nil {
  164. c.NotFoundOrServerError("GetRawDiff", git.IsErrNotExist, err)
  165. return
  166. }
  167. }
  168. func CompareDiff(c *context.Context) {
  169. c.Data["IsDiffCompare"] = true
  170. userName := c.Repo.Owner.Name
  171. repoName := c.Repo.Repository.Name
  172. beforeCommitID := c.Params(":before")
  173. afterCommitID := c.Params(":after")
  174. commit, err := c.Repo.GitRepo.GetCommit(afterCommitID)
  175. if err != nil {
  176. c.Handle(404, "GetCommit", err)
  177. return
  178. }
  179. diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitID,
  180. afterCommitID, setting.Git.MaxGitDiffLines,
  181. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  182. if err != nil {
  183. c.Handle(404, "GetDiffRange", err)
  184. return
  185. }
  186. commits, err := commit.CommitsBeforeUntil(beforeCommitID)
  187. if err != nil {
  188. c.Handle(500, "CommitsBeforeUntil", err)
  189. return
  190. }
  191. commits = models.ValidateCommitsWithEmails(commits)
  192. c.Data["IsSplitStyle"] = c.Query("style") == "split"
  193. c.Data["CommitRepoLink"] = c.Repo.RepoLink
  194. c.Data["Commits"] = commits
  195. c.Data["CommitsCount"] = commits.Len()
  196. c.Data["BeforeCommitID"] = beforeCommitID
  197. c.Data["AfterCommitID"] = afterCommitID
  198. c.Data["Username"] = userName
  199. c.Data["Reponame"] = repoName
  200. c.Data["IsImageFile"] = commit.IsImageFile
  201. c.Data["Title"] = "Comparing " + tool.ShortSHA1(beforeCommitID) + "..." + tool.ShortSHA1(afterCommitID) + " · " + userName + "/" + repoName
  202. c.Data["Commit"] = commit
  203. c.Data["Diff"] = diff
  204. c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  205. c.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", afterCommitID)
  206. c.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", beforeCommitID)
  207. c.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", afterCommitID)
  208. c.HTML(200, DIFF)
  209. }