commit.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/Unknwon/paginater"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/git"
  12. "github.com/gogits/gogs/modules/middleware"
  13. "github.com/gogits/gogs/modules/setting"
  14. )
  15. import "github.com/davecheney/profile"
  16. const (
  17. COMMITS base.TplName = "repo/commits"
  18. DIFF base.TplName = "repo/diff"
  19. )
  20. func RefCommits(ctx *middleware.Context) {
  21. switch {
  22. case len(ctx.Repo.TreeName) == 0:
  23. Commits(ctx)
  24. case ctx.Repo.TreeName == "search":
  25. SearchCommits(ctx)
  26. default:
  27. FileHistory(ctx)
  28. }
  29. }
  30. func RenderIssueLinks(oldCommits *list.List, repoLink string) *list.List {
  31. newCommits := list.New()
  32. for e := oldCommits.Front(); e != nil; e = e.Next() {
  33. c := e.Value.(*git.Commit)
  34. newCommits.PushBack(c)
  35. }
  36. return newCommits
  37. }
  38. func Commits(ctx *middleware.Context) {
  39. defer profile.Start(profile.CPUProfile).Stop()
  40. ctx.Data["PageIsCommits"] = true
  41. commitsCount, err := ctx.Repo.Commit.CommitsCount()
  42. if err != nil {
  43. ctx.Handle(500, "GetCommitsCount", err)
  44. return
  45. }
  46. page := ctx.QueryInt("page")
  47. if page <= 1 {
  48. page = 1
  49. }
  50. ctx.Data["Page"] = paginater.New(commitsCount, git.CommitsRangeSize, page, 5)
  51. // Both `git log branchName` and `git log commitId` work.
  52. commits, err := ctx.Repo.Commit.CommitsByRange(page)
  53. if err != nil {
  54. ctx.Handle(500, "CommitsByRange", err)
  55. return
  56. }
  57. commits = RenderIssueLinks(commits, ctx.Repo.RepoLink)
  58. commits = models.ValidateCommitsWithEmails(commits)
  59. ctx.Data["Commits"] = commits
  60. ctx.Data["Username"] = ctx.Repo.Owner.Name
  61. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  62. ctx.Data["CommitCount"] = commitsCount
  63. ctx.Data["Branch"] = ctx.Repo.BranchName
  64. ctx.HTML(200, COMMITS)
  65. }
  66. func SearchCommits(ctx *middleware.Context) {
  67. ctx.Data["PageIsCommits"] = true
  68. keyword := ctx.Query("q")
  69. if len(keyword) == 0 {
  70. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName)
  71. return
  72. }
  73. commits, err := ctx.Repo.Commit.SearchCommits(keyword)
  74. if err != nil {
  75. ctx.Handle(500, "SearchCommits", err)
  76. return
  77. }
  78. commits = RenderIssueLinks(commits, ctx.Repo.RepoLink)
  79. commits = models.ValidateCommitsWithEmails(commits)
  80. ctx.Data["Commits"] = commits
  81. ctx.Data["Keyword"] = keyword
  82. ctx.Data["Username"] = ctx.Repo.Owner.Name
  83. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  84. ctx.Data["CommitCount"] = commits.Len()
  85. ctx.Data["Branch"] = ctx.Repo.BranchName
  86. ctx.HTML(200, COMMITS)
  87. }
  88. func FileHistory(ctx *middleware.Context) {
  89. ctx.Data["IsRepoToolbarCommits"] = true
  90. fileName := ctx.Repo.TreeName
  91. if len(fileName) == 0 {
  92. Commits(ctx)
  93. return
  94. }
  95. branchName := ctx.Repo.BranchName
  96. commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
  97. if err != nil {
  98. ctx.Handle(500, "FileCommitsCount", err)
  99. return
  100. } else if commitsCount == 0 {
  101. ctx.Handle(404, "FileCommitsCount", nil)
  102. return
  103. }
  104. page := ctx.QueryInt("page")
  105. if page <= 1 {
  106. page = 1
  107. }
  108. ctx.Data["Page"] = paginater.New(commitsCount, git.CommitsRangeSize, page, 5)
  109. commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(branchName, fileName, page)
  110. if err != nil {
  111. ctx.Handle(500, "CommitsByFileAndRange", err)
  112. return
  113. }
  114. commits = RenderIssueLinks(commits, ctx.Repo.RepoLink)
  115. commits = models.ValidateCommitsWithEmails(commits)
  116. ctx.Data["Commits"] = commits
  117. ctx.Data["Username"] = ctx.Repo.Owner.Name
  118. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  119. ctx.Data["FileName"] = fileName
  120. ctx.Data["CommitCount"] = commitsCount
  121. ctx.Data["Branch"] = branchName
  122. ctx.HTML(200, COMMITS)
  123. }
  124. func Diff(ctx *middleware.Context) {
  125. ctx.Data["PageIsDiff"] = true
  126. userName := ctx.Repo.Owner.Name
  127. repoName := ctx.Repo.Repository.Name
  128. commitID := ctx.Repo.CommitID
  129. commit := ctx.Repo.Commit
  130. diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName),
  131. commitID, setting.Git.MaxGitDiffLines)
  132. if err != nil {
  133. ctx.Handle(404, "GetDiffCommit", err)
  134. return
  135. }
  136. parents := make([]string, commit.ParentCount())
  137. for i := 0; i < commit.ParentCount(); i++ {
  138. sha, err := commit.ParentId(i)
  139. parents[i] = sha.String()
  140. if err != nil {
  141. ctx.Handle(404, "repo.Diff", err)
  142. return
  143. }
  144. }
  145. ctx.Data["Username"] = userName
  146. ctx.Data["Reponame"] = repoName
  147. ctx.Data["IsImageFile"] = commit.IsImageFile
  148. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
  149. ctx.Data["Commit"] = commit
  150. ctx.Data["Author"] = models.ValidateCommitWithEmail(commit)
  151. ctx.Data["Diff"] = diff
  152. ctx.Data["Parents"] = parents
  153. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  154. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", commitID)
  155. if commit.ParentCount() > 0 {
  156. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", parents[0])
  157. }
  158. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "raw", commitID)
  159. ctx.HTML(200, DIFF)
  160. }
  161. func CompareDiff(ctx *middleware.Context) {
  162. ctx.Data["IsRepoToolbarCommits"] = true
  163. ctx.Data["IsDiffCompare"] = true
  164. userName := ctx.Repo.Owner.Name
  165. repoName := ctx.Repo.Repository.Name
  166. beforeCommitID := ctx.Params(":before")
  167. afterCommitID := ctx.Params(":after")
  168. commit, err := ctx.Repo.GitRepo.GetCommit(afterCommitID)
  169. if err != nil {
  170. ctx.Handle(404, "GetCommit", err)
  171. return
  172. }
  173. diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitID,
  174. afterCommitID, setting.Git.MaxGitDiffLines)
  175. if err != nil {
  176. ctx.Handle(404, "GetDiffRange", err)
  177. return
  178. }
  179. commits, err := commit.CommitsBeforeUntil(beforeCommitID)
  180. if err != nil {
  181. ctx.Handle(500, "CommitsBeforeUntil", err)
  182. return
  183. }
  184. commits = models.ValidateCommitsWithEmails(commits)
  185. ctx.Data["CommitRepoLink"] = ctx.Repo.RepoLink
  186. ctx.Data["Commits"] = commits
  187. ctx.Data["CommitCount"] = commits.Len()
  188. ctx.Data["BeforeCommitID"] = beforeCommitID
  189. ctx.Data["AfterCommitID"] = afterCommitID
  190. ctx.Data["Username"] = userName
  191. ctx.Data["Reponame"] = repoName
  192. ctx.Data["IsImageFile"] = commit.IsImageFile
  193. ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitID) + "..." + base.ShortSha(afterCommitID) + " · " + userName + "/" + repoName
  194. ctx.Data["Commit"] = commit
  195. ctx.Data["Diff"] = diff
  196. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  197. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", afterCommitID)
  198. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", beforeCommitID)
  199. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "raw", afterCommitID)
  200. ctx.HTML(200, DIFF)
  201. }