本站源代码
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

291 lines
7.8KB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "path"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/charset"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/git"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/services/gitdiff"
  17. )
  18. const (
  19. tplCommits base.TplName = "repo/commits"
  20. tplGraph base.TplName = "repo/graph"
  21. tplCommitPage base.TplName = "repo/commit_page"
  22. )
  23. // RefCommits render commits page
  24. func RefCommits(ctx *context.Context) {
  25. switch {
  26. case len(ctx.Repo.TreePath) == 0:
  27. Commits(ctx)
  28. case ctx.Repo.TreePath == "search":
  29. SearchCommits(ctx)
  30. default:
  31. FileHistory(ctx)
  32. }
  33. }
  34. // Commits render branch's commits
  35. func Commits(ctx *context.Context) {
  36. ctx.Data["PageIsCommits"] = true
  37. if ctx.Repo.Commit == nil {
  38. ctx.NotFound("Commit not found", nil)
  39. return
  40. }
  41. ctx.Data["PageIsViewCode"] = true
  42. commitsCount, err := ctx.Repo.GetCommitsCount()
  43. if err != nil {
  44. ctx.ServerError("GetCommitsCount", err)
  45. return
  46. }
  47. page := ctx.QueryInt("page")
  48. if page <= 1 {
  49. page = 1
  50. }
  51. // Both `git log branchName` and `git log commitId` work.
  52. commits, err := ctx.Repo.Commit.CommitsByRange(page)
  53. if err != nil {
  54. ctx.ServerError("CommitsByRange", err)
  55. return
  56. }
  57. commits = models.ValidateCommitsWithEmails(commits)
  58. commits = models.ParseCommitsWithSignature(commits)
  59. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  60. ctx.Data["Commits"] = commits
  61. ctx.Data["Username"] = ctx.Repo.Owner.Name
  62. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  63. ctx.Data["CommitCount"] = commitsCount
  64. ctx.Data["Branch"] = ctx.Repo.BranchName
  65. pager := context.NewPagination(int(commitsCount), git.CommitsRangeSize, page, 5)
  66. pager.SetDefaultParams(ctx)
  67. ctx.Data["Page"] = pager
  68. ctx.HTML(200, tplCommits)
  69. }
  70. // Graph render commit graph - show commits from all branches.
  71. func Graph(ctx *context.Context) {
  72. ctx.Data["PageIsCommits"] = true
  73. ctx.Data["PageIsViewCode"] = true
  74. commitsCount, err := ctx.Repo.GetCommitsCount()
  75. if err != nil {
  76. ctx.ServerError("GetCommitsCount", err)
  77. return
  78. }
  79. page := ctx.QueryInt("page")
  80. graph, err := models.GetCommitGraph(ctx.Repo.GitRepo, page)
  81. if err != nil {
  82. ctx.ServerError("GetCommitGraph", err)
  83. return
  84. }
  85. ctx.Data["Graph"] = graph
  86. ctx.Data["Username"] = ctx.Repo.Owner.Name
  87. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  88. ctx.Data["CommitCount"] = commitsCount
  89. ctx.Data["Branch"] = ctx.Repo.BranchName
  90. ctx.Data["RequireGitGraph"] = true
  91. ctx.Data["Page"] = context.NewPagination(int(commitsCount), setting.UI.GraphMaxCommitNum, page, 5)
  92. ctx.HTML(200, tplGraph)
  93. }
  94. // SearchCommits render commits filtered by keyword
  95. func SearchCommits(ctx *context.Context) {
  96. ctx.Data["PageIsCommits"] = true
  97. ctx.Data["PageIsViewCode"] = true
  98. query := strings.Trim(ctx.Query("q"), " ")
  99. if len(query) == 0 {
  100. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchNameSubURL())
  101. return
  102. }
  103. all := ctx.QueryBool("all")
  104. opts := git.NewSearchCommitsOptions(query, all)
  105. commits, err := ctx.Repo.Commit.SearchCommits(opts)
  106. if err != nil {
  107. ctx.ServerError("SearchCommits", err)
  108. return
  109. }
  110. commits = models.ValidateCommitsWithEmails(commits)
  111. commits = models.ParseCommitsWithSignature(commits)
  112. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  113. ctx.Data["Commits"] = commits
  114. ctx.Data["Keyword"] = query
  115. if all {
  116. ctx.Data["All"] = "checked"
  117. }
  118. ctx.Data["Username"] = ctx.Repo.Owner.Name
  119. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  120. ctx.Data["CommitCount"] = commits.Len()
  121. ctx.Data["Branch"] = ctx.Repo.BranchName
  122. ctx.HTML(200, tplCommits)
  123. }
  124. // FileHistory show a file's reversions
  125. func FileHistory(ctx *context.Context) {
  126. ctx.Data["IsRepoToolbarCommits"] = true
  127. fileName := ctx.Repo.TreePath
  128. if len(fileName) == 0 {
  129. Commits(ctx)
  130. return
  131. }
  132. branchName := ctx.Repo.BranchName
  133. commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
  134. if err != nil {
  135. ctx.ServerError("FileCommitsCount", err)
  136. return
  137. } else if commitsCount == 0 {
  138. ctx.NotFound("FileCommitsCount", nil)
  139. return
  140. }
  141. page := ctx.QueryInt("page")
  142. if page <= 1 {
  143. page = 1
  144. }
  145. commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(branchName, fileName, page)
  146. if err != nil {
  147. ctx.ServerError("CommitsByFileAndRange", err)
  148. return
  149. }
  150. commits = models.ValidateCommitsWithEmails(commits)
  151. commits = models.ParseCommitsWithSignature(commits)
  152. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  153. ctx.Data["Commits"] = commits
  154. ctx.Data["Username"] = ctx.Repo.Owner.Name
  155. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  156. ctx.Data["FileName"] = fileName
  157. ctx.Data["CommitCount"] = commitsCount
  158. ctx.Data["Branch"] = branchName
  159. pager := context.NewPagination(int(commitsCount), git.CommitsRangeSize, page, 5)
  160. pager.SetDefaultParams(ctx)
  161. ctx.Data["Page"] = pager
  162. ctx.HTML(200, tplCommits)
  163. }
  164. // Diff show different from current commit to previous commit
  165. func Diff(ctx *context.Context) {
  166. ctx.Data["PageIsDiff"] = true
  167. ctx.Data["RequireHighlightJS"] = true
  168. userName := ctx.Repo.Owner.Name
  169. repoName := ctx.Repo.Repository.Name
  170. commitID := ctx.Params(":sha")
  171. commit, err := ctx.Repo.GitRepo.GetCommit(commitID)
  172. if err != nil {
  173. if git.IsErrNotExist(err) {
  174. ctx.NotFound("Repo.GitRepo.GetCommit", err)
  175. } else {
  176. ctx.ServerError("Repo.GitRepo.GetCommit", err)
  177. }
  178. return
  179. }
  180. if len(commitID) != 40 {
  181. commitID = commit.ID.String()
  182. }
  183. statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository, commitID, 0)
  184. if err != nil {
  185. log.Error("GetLatestCommitStatus: %v", err)
  186. }
  187. ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses)
  188. diff, err := gitdiff.GetDiffCommit(models.RepoPath(userName, repoName),
  189. commitID, setting.Git.MaxGitDiffLines,
  190. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  191. if err != nil {
  192. ctx.NotFound("GetDiffCommit", err)
  193. return
  194. }
  195. parents := make([]string, commit.ParentCount())
  196. for i := 0; i < commit.ParentCount(); i++ {
  197. sha, err := commit.ParentID(i)
  198. parents[i] = sha.String()
  199. if err != nil {
  200. ctx.NotFound("repo.Diff", err)
  201. return
  202. }
  203. }
  204. ctx.Data["CommitID"] = commitID
  205. ctx.Data["Username"] = userName
  206. ctx.Data["Reponame"] = repoName
  207. var parentCommit *git.Commit
  208. if commit.ParentCount() > 0 {
  209. parentCommit, err = ctx.Repo.GitRepo.GetCommit(parents[0])
  210. if err != nil {
  211. ctx.NotFound("GetParentCommit", err)
  212. return
  213. }
  214. }
  215. setImageCompareContext(ctx, parentCommit, commit)
  216. headTarget := path.Join(userName, repoName)
  217. setPathsCompareContext(ctx, parentCommit, commit, headTarget)
  218. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
  219. ctx.Data["Commit"] = commit
  220. ctx.Data["Verification"] = models.ParseCommitWithSignature(commit)
  221. ctx.Data["Author"] = models.ValidateCommitWithEmail(commit)
  222. ctx.Data["Diff"] = diff
  223. ctx.Data["Parents"] = parents
  224. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  225. note := &git.Note{}
  226. err = git.GetNote(ctx.Repo.GitRepo, commitID, note)
  227. if err == nil {
  228. ctx.Data["Note"] = string(charset.ToUTF8WithFallback(note.Message))
  229. ctx.Data["NoteCommit"] = note.Commit
  230. ctx.Data["NoteAuthor"] = models.ValidateCommitWithEmail(note.Commit)
  231. }
  232. ctx.Data["BranchName"], err = commit.GetBranchName()
  233. if err != nil {
  234. ctx.ServerError("commit.GetBranchName", err)
  235. }
  236. ctx.HTML(200, tplCommitPage)
  237. }
  238. // RawDiff dumps diff results of repository in given commit ID to io.Writer
  239. func RawDiff(ctx *context.Context) {
  240. if err := gitdiff.GetRawDiff(
  241. models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name),
  242. ctx.Params(":sha"),
  243. gitdiff.RawDiffType(ctx.Params(":ext")),
  244. ctx.Resp,
  245. ); err != nil {
  246. ctx.ServerError("GetRawDiff", err)
  247. return
  248. }
  249. }
上海开阖软件有限公司 沪ICP备12045867号-1