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

259 lines
7.4 KiB

10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  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/go-gitea/git"
  10. "github.com/go-gitea/gitea/models"
  11. "github.com/go-gitea/gitea/modules/base"
  12. "github.com/go-gitea/gitea/modules/context"
  13. "github.com/go-gitea/gitea/modules/setting"
  14. )
  15. const (
  16. COMMITS base.TplName = "repo/commits"
  17. DIFF base.TplName = "repo/diff/page"
  18. )
  19. func RefCommits(ctx *context.Context) {
  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 Commits(ctx *context.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 *context.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 *context.Context) {
  87. ctx.Data["IsRepoToolbarCommits"] = true
  88. fileName := ctx.Repo.TreePath
  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 *context.Context) {
  123. ctx.Data["PageIsDiff"] = true
  124. ctx.Data["RequireHighlightJS"] = true
  125. userName := ctx.Repo.Owner.Name
  126. repoName := ctx.Repo.Repository.Name
  127. commitID := ctx.Params(":sha")
  128. commit, err := ctx.Repo.GitRepo.GetCommit(commitID)
  129. if err != nil {
  130. if git.IsErrNotExist(err) {
  131. ctx.Handle(404, "Repo.GitRepo.GetCommit", err)
  132. } else {
  133. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  134. }
  135. return
  136. }
  137. if len(commitID) != 40 {
  138. commitID = commit.ID.String()
  139. }
  140. diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName),
  141. commitID, setting.Git.MaxGitDiffLines,
  142. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  143. if err != nil {
  144. ctx.Handle(404, "GetDiffCommit", err)
  145. return
  146. }
  147. parents := make([]string, commit.ParentCount())
  148. for i := 0; i < commit.ParentCount(); i++ {
  149. sha, err := commit.ParentID(i)
  150. parents[i] = sha.String()
  151. if err != nil {
  152. ctx.Handle(404, "repo.Diff", err)
  153. return
  154. }
  155. }
  156. ctx.Data["CommitID"] = commitID
  157. ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"
  158. ctx.Data["Username"] = userName
  159. ctx.Data["Reponame"] = repoName
  160. ctx.Data["IsImageFile"] = commit.IsImageFile
  161. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
  162. ctx.Data["Commit"] = commit
  163. ctx.Data["Author"] = models.ValidateCommitWithEmail(commit)
  164. ctx.Data["Diff"] = diff
  165. ctx.Data["Parents"] = parents
  166. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  167. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", commitID)
  168. if commit.ParentCount() > 0 {
  169. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", parents[0])
  170. }
  171. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "raw", commitID)
  172. ctx.HTML(200, DIFF)
  173. }
  174. func RawDiff(ctx *context.Context) {
  175. if err := models.GetRawDiff(
  176. models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name),
  177. ctx.Params(":sha"),
  178. models.RawDiffType(ctx.Params(":ext")),
  179. ctx.Resp,
  180. ); err != nil {
  181. ctx.Handle(500, "GetRawDiff", err)
  182. return
  183. }
  184. }
  185. func CompareDiff(ctx *context.Context) {
  186. ctx.Data["IsRepoToolbarCommits"] = true
  187. ctx.Data["IsDiffCompare"] = true
  188. userName := ctx.Repo.Owner.Name
  189. repoName := ctx.Repo.Repository.Name
  190. beforeCommitID := ctx.Params(":before")
  191. afterCommitID := ctx.Params(":after")
  192. commit, err := ctx.Repo.GitRepo.GetCommit(afterCommitID)
  193. if err != nil {
  194. ctx.Handle(404, "GetCommit", err)
  195. return
  196. }
  197. diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitID,
  198. afterCommitID, setting.Git.MaxGitDiffLines,
  199. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  200. if err != nil {
  201. ctx.Handle(404, "GetDiffRange", err)
  202. return
  203. }
  204. commits, err := commit.CommitsBeforeUntil(beforeCommitID)
  205. if err != nil {
  206. ctx.Handle(500, "CommitsBeforeUntil", err)
  207. return
  208. }
  209. commits = models.ValidateCommitsWithEmails(commits)
  210. ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"
  211. ctx.Data["CommitRepoLink"] = ctx.Repo.RepoLink
  212. ctx.Data["Commits"] = commits
  213. ctx.Data["CommitCount"] = commits.Len()
  214. ctx.Data["BeforeCommitID"] = beforeCommitID
  215. ctx.Data["AfterCommitID"] = afterCommitID
  216. ctx.Data["Username"] = userName
  217. ctx.Data["Reponame"] = repoName
  218. ctx.Data["IsImageFile"] = commit.IsImageFile
  219. ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitID) + "..." + base.ShortSha(afterCommitID) + " · " + userName + "/" + repoName
  220. ctx.Data["Commit"] = commit
  221. ctx.Data["Diff"] = diff
  222. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  223. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", afterCommitID)
  224. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", beforeCommitID)
  225. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "raw", afterCommitID)
  226. ctx.HTML(200, DIFF)
  227. }