commit.go 6.5 KB

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