Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

commit.go 7.7KB

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