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 8.2KB

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