commit.go 6.7 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/modules/base"
  11. "github.com/gogits/gogs/modules/context"
  12. "github.com/gogits/gogs/modules/setting"
  13. )
  14. const (
  15. COMMITS base.TplName = "repo/commits"
  16. DIFF base.TplName = "repo/diff/page"
  17. )
  18. func RefCommits(ctx *context.Context) {
  19. ctx.Data["PageIsViewFiles"] = true
  20. switch {
  21. case len(ctx.Repo.TreePath) == 0:
  22. Commits(ctx)
  23. case ctx.Repo.TreePath == "search":
  24. SearchCommits(ctx)
  25. default:
  26. FileHistory(ctx)
  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(ctx *context.Context, filename string) {
  38. ctx.Data["Title"] = ctx.Tr("repo.commits.commit_history") + " · " + ctx.Repo.Repository.FullName()
  39. ctx.Data["PageIsCommits"] = true
  40. page := ctx.QueryInt("page")
  41. if page < 1 {
  42. page = 1
  43. }
  44. pageSize := ctx.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 = ctx.Repo.Commit.CommitsByRangeSize(page, pageSize)
  53. } else {
  54. commits, err = ctx.Repo.GitRepo.CommitsByFileAndRangeSize(ctx.Repo.BranchName, filename, page, pageSize)
  55. }
  56. if err != nil {
  57. ctx.Handle(500, "CommitsByRangeSize/CommitsByFileAndRangeSize", err)
  58. return
  59. }
  60. commits = RenderIssueLinks(commits, ctx.Repo.RepoLink)
  61. commits = models.ValidateCommitsWithEmails(commits)
  62. ctx.Data["Commits"] = commits
  63. if page > 1 {
  64. ctx.Data["HasPrevious"] = true
  65. ctx.Data["PreviousPage"] = page - 1
  66. }
  67. if commits.Len() == pageSize {
  68. ctx.Data["HasNext"] = true
  69. ctx.Data["NextPage"] = page + 1
  70. }
  71. ctx.Data["PageSize"] = pageSize
  72. ctx.Data["Username"] = ctx.Repo.Owner.Name
  73. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  74. ctx.HTML(200, COMMITS)
  75. }
  76. func Commits(ctx *context.Context) {
  77. renderCommits(ctx, "")
  78. }
  79. func SearchCommits(ctx *context.Context) {
  80. ctx.Data["PageIsCommits"] = true
  81. keyword := ctx.Query("q")
  82. if len(keyword) == 0 {
  83. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName)
  84. return
  85. }
  86. commits, err := ctx.Repo.Commit.SearchCommits(keyword)
  87. if err != nil {
  88. ctx.Handle(500, "SearchCommits", err)
  89. return
  90. }
  91. commits = RenderIssueLinks(commits, ctx.Repo.RepoLink)
  92. commits = models.ValidateCommitsWithEmails(commits)
  93. ctx.Data["Commits"] = commits
  94. ctx.Data["Keyword"] = keyword
  95. ctx.Data["Username"] = ctx.Repo.Owner.Name
  96. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  97. ctx.Data["Branch"] = ctx.Repo.BranchName
  98. ctx.HTML(200, COMMITS)
  99. }
  100. func FileHistory(ctx *context.Context) {
  101. renderCommits(ctx, ctx.Repo.TreePath)
  102. }
  103. func Diff(ctx *context.Context) {
  104. ctx.Data["PageIsDiff"] = true
  105. ctx.Data["RequireHighlightJS"] = true
  106. userName := ctx.Repo.Owner.Name
  107. repoName := ctx.Repo.Repository.Name
  108. commitID := ctx.Params(":sha")
  109. commit, err := ctx.Repo.GitRepo.GetCommit(commitID)
  110. if err != nil {
  111. if git.IsErrNotExist(err) {
  112. ctx.Handle(404, "Repo.GitRepo.GetCommit", err)
  113. } else {
  114. ctx.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. ctx.Handle(404, "GetDiffCommit", 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. ctx.Handle(404, "repo.Diff", err)
  131. return
  132. }
  133. }
  134. setEditorconfigIfExists(ctx)
  135. if ctx.Written() {
  136. return
  137. }
  138. ctx.Data["CommitID"] = commitID
  139. ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"
  140. ctx.Data["Username"] = userName
  141. ctx.Data["Reponame"] = repoName
  142. ctx.Data["IsImageFile"] = commit.IsImageFile
  143. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
  144. ctx.Data["Commit"] = commit
  145. ctx.Data["Author"] = models.ValidateCommitWithEmail(commit)
  146. ctx.Data["Diff"] = diff
  147. ctx.Data["Parents"] = parents
  148. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  149. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", commitID)
  150. if commit.ParentCount() > 0 {
  151. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", parents[0])
  152. }
  153. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "raw", commitID)
  154. ctx.HTML(200, DIFF)
  155. }
  156. func RawDiff(ctx *context.Context) {
  157. if err := models.GetRawDiff(
  158. models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name),
  159. ctx.Params(":sha"),
  160. models.RawDiffType(ctx.Params(":ext")),
  161. ctx.Resp,
  162. ); err != nil {
  163. ctx.Handle(500, "GetRawDiff", err)
  164. return
  165. }
  166. }
  167. func CompareDiff(ctx *context.Context) {
  168. ctx.Data["IsDiffCompare"] = true
  169. userName := ctx.Repo.Owner.Name
  170. repoName := ctx.Repo.Repository.Name
  171. beforeCommitID := ctx.Params(":before")
  172. afterCommitID := ctx.Params(":after")
  173. commit, err := ctx.Repo.GitRepo.GetCommit(afterCommitID)
  174. if err != nil {
  175. ctx.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. ctx.Handle(404, "GetDiffRange", err)
  183. return
  184. }
  185. commits, err := commit.CommitsBeforeUntil(beforeCommitID)
  186. if err != nil {
  187. ctx.Handle(500, "CommitsBeforeUntil", err)
  188. return
  189. }
  190. commits = models.ValidateCommitsWithEmails(commits)
  191. ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"
  192. ctx.Data["CommitRepoLink"] = ctx.Repo.RepoLink
  193. ctx.Data["Commits"] = commits
  194. ctx.Data["CommitsCount"] = commits.Len()
  195. ctx.Data["BeforeCommitID"] = beforeCommitID
  196. ctx.Data["AfterCommitID"] = afterCommitID
  197. ctx.Data["Username"] = userName
  198. ctx.Data["Reponame"] = repoName
  199. ctx.Data["IsImageFile"] = commit.IsImageFile
  200. ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitID) + "..." + base.ShortSha(afterCommitID) + " · " + userName + "/" + repoName
  201. ctx.Data["Commit"] = commit
  202. ctx.Data["Diff"] = diff
  203. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  204. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", afterCommitID)
  205. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", beforeCommitID)
  206. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "raw", afterCommitID)
  207. ctx.HTML(200, DIFF)
  208. }