You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

commit.go 6.8 kB

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