commit.go 6.7 KB

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