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.

commit.go 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. allCommitsCount, err := ctx.Repo.GitRepo.GetAllCommitsCount()
  80. if err != nil {
  81. ctx.ServerError("GetAllCommitsCount", err)
  82. return
  83. }
  84. page := ctx.QueryInt("page")
  85. graph, err := models.GetCommitGraph(ctx.Repo.GitRepo, page)
  86. if err != nil {
  87. ctx.ServerError("GetCommitGraph", err)
  88. return
  89. }
  90. ctx.Data["Graph"] = graph
  91. ctx.Data["Username"] = ctx.Repo.Owner.Name
  92. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  93. ctx.Data["CommitCount"] = commitsCount
  94. ctx.Data["Branch"] = ctx.Repo.BranchName
  95. ctx.Data["RequireGitGraph"] = true
  96. ctx.Data["Page"] = context.NewPagination(int(allCommitsCount), setting.UI.GraphMaxCommitNum, page, 5)
  97. ctx.HTML(200, tplGraph)
  98. }
  99. // SearchCommits render commits filtered by keyword
  100. func SearchCommits(ctx *context.Context) {
  101. ctx.Data["PageIsCommits"] = true
  102. ctx.Data["PageIsViewCode"] = true
  103. query := strings.Trim(ctx.Query("q"), " ")
  104. if len(query) == 0 {
  105. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchNameSubURL())
  106. return
  107. }
  108. all := ctx.QueryBool("all")
  109. opts := git.NewSearchCommitsOptions(query, all)
  110. commits, err := ctx.Repo.Commit.SearchCommits(opts)
  111. if err != nil {
  112. ctx.ServerError("SearchCommits", err)
  113. return
  114. }
  115. commits = models.ValidateCommitsWithEmails(commits)
  116. commits = models.ParseCommitsWithSignature(commits)
  117. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  118. ctx.Data["Commits"] = commits
  119. ctx.Data["Keyword"] = query
  120. if all {
  121. ctx.Data["All"] = "checked"
  122. }
  123. ctx.Data["Username"] = ctx.Repo.Owner.Name
  124. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  125. ctx.Data["CommitCount"] = commits.Len()
  126. ctx.Data["Branch"] = ctx.Repo.BranchName
  127. ctx.HTML(200, tplCommits)
  128. }
  129. // FileHistory show a file's reversions
  130. func FileHistory(ctx *context.Context) {
  131. ctx.Data["IsRepoToolbarCommits"] = true
  132. fileName := ctx.Repo.TreePath
  133. if len(fileName) == 0 {
  134. Commits(ctx)
  135. return
  136. }
  137. branchName := ctx.Repo.BranchName
  138. commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
  139. if err != nil {
  140. ctx.ServerError("FileCommitsCount", err)
  141. return
  142. } else if commitsCount == 0 {
  143. ctx.NotFound("FileCommitsCount", nil)
  144. return
  145. }
  146. page := ctx.QueryInt("page")
  147. if page <= 1 {
  148. page = 1
  149. }
  150. commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(branchName, fileName, page)
  151. if err != nil {
  152. ctx.ServerError("CommitsByFileAndRange", err)
  153. return
  154. }
  155. commits = models.ValidateCommitsWithEmails(commits)
  156. commits = models.ParseCommitsWithSignature(commits)
  157. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  158. ctx.Data["Commits"] = commits
  159. ctx.Data["Username"] = ctx.Repo.Owner.Name
  160. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  161. ctx.Data["FileName"] = fileName
  162. ctx.Data["CommitCount"] = commitsCount
  163. ctx.Data["Branch"] = branchName
  164. pager := context.NewPagination(int(commitsCount), git.CommitsRangeSize, page, 5)
  165. pager.SetDefaultParams(ctx)
  166. ctx.Data["Page"] = pager
  167. ctx.HTML(200, tplCommits)
  168. }
  169. // Diff show different from current commit to previous commit
  170. func Diff(ctx *context.Context) {
  171. ctx.Data["PageIsDiff"] = true
  172. ctx.Data["RequireHighlightJS"] = true
  173. userName := ctx.Repo.Owner.Name
  174. repoName := ctx.Repo.Repository.Name
  175. commitID := ctx.Params(":sha")
  176. commit, err := ctx.Repo.GitRepo.GetCommit(commitID)
  177. if err != nil {
  178. if git.IsErrNotExist(err) {
  179. ctx.NotFound("Repo.GitRepo.GetCommit", err)
  180. } else {
  181. ctx.ServerError("Repo.GitRepo.GetCommit", err)
  182. }
  183. return
  184. }
  185. if len(commitID) != 40 {
  186. commitID = commit.ID.String()
  187. }
  188. statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository, commitID, 0)
  189. if err != nil {
  190. log.Error("GetLatestCommitStatus: %v", err)
  191. }
  192. ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses)
  193. diff, err := gitdiff.GetDiffCommit(models.RepoPath(userName, repoName),
  194. commitID, setting.Git.MaxGitDiffLines,
  195. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  196. if err != nil {
  197. ctx.NotFound("GetDiffCommit", err)
  198. return
  199. }
  200. parents := make([]string, commit.ParentCount())
  201. for i := 0; i < commit.ParentCount(); i++ {
  202. sha, err := commit.ParentID(i)
  203. parents[i] = sha.String()
  204. if err != nil {
  205. ctx.NotFound("repo.Diff", err)
  206. return
  207. }
  208. }
  209. ctx.Data["CommitID"] = commitID
  210. ctx.Data["Username"] = userName
  211. ctx.Data["Reponame"] = repoName
  212. var parentCommit *git.Commit
  213. if commit.ParentCount() > 0 {
  214. parentCommit, err = ctx.Repo.GitRepo.GetCommit(parents[0])
  215. if err != nil {
  216. ctx.NotFound("GetParentCommit", err)
  217. return
  218. }
  219. }
  220. setImageCompareContext(ctx, parentCommit, commit)
  221. headTarget := path.Join(userName, repoName)
  222. setPathsCompareContext(ctx, parentCommit, commit, headTarget)
  223. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
  224. ctx.Data["Commit"] = commit
  225. ctx.Data["Verification"] = models.ParseCommitWithSignature(commit)
  226. ctx.Data["Author"] = models.ValidateCommitWithEmail(commit)
  227. ctx.Data["Diff"] = diff
  228. ctx.Data["Parents"] = parents
  229. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  230. note := &git.Note{}
  231. err = git.GetNote(ctx.Repo.GitRepo, commitID, note)
  232. if err == nil {
  233. ctx.Data["Note"] = string(charset.ToUTF8WithFallback(note.Message))
  234. ctx.Data["NoteCommit"] = note.Commit
  235. ctx.Data["NoteAuthor"] = models.ValidateCommitWithEmail(note.Commit)
  236. }
  237. ctx.Data["BranchName"], err = commit.GetBranchName()
  238. if err != nil {
  239. ctx.ServerError("commit.GetBranchName", err)
  240. }
  241. ctx.HTML(200, tplCommitPage)
  242. }
  243. // RawDiff dumps diff results of repository in given commit ID to io.Writer
  244. func RawDiff(ctx *context.Context) {
  245. if err := gitdiff.GetRawDiff(
  246. models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name),
  247. ctx.Params(":sha"),
  248. gitdiff.RawDiffType(ctx.Params(":ext")),
  249. ctx.Resp,
  250. ); err != nil {
  251. ctx.ServerError("GetRawDiff", err)
  252. return
  253. }
  254. }