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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. // Both `git log branchName` and `git log commitId` work.
  53. commits, err := ctx.Repo.Commit.CommitsByRange(page)
  54. if err != nil {
  55. ctx.ServerError("CommitsByRange", err)
  56. return
  57. }
  58. commits = models.ValidateCommitsWithEmails(commits)
  59. commits = models.ParseCommitsWithSignature(commits)
  60. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  61. ctx.Data["Commits"] = commits
  62. ctx.Data["Username"] = ctx.Repo.Owner.Name
  63. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  64. ctx.Data["CommitCount"] = commitsCount
  65. ctx.Data["Branch"] = ctx.Repo.BranchName
  66. pager := context.NewPagination(int(commitsCount), git.CommitsRangeSize, page, 5)
  67. pager.SetDefaultParams(ctx)
  68. ctx.Data["Page"] = pager
  69. ctx.HTML(200, tplCommits)
  70. }
  71. // Graph render commit graph - show commits from all branches.
  72. func Graph(ctx *context.Context) {
  73. ctx.Data["PageIsCommits"] = true
  74. ctx.Data["PageIsViewCode"] = true
  75. commitsCount, err := ctx.Repo.GetCommitsCount()
  76. if err != nil {
  77. ctx.ServerError("GetCommitsCount", err)
  78. return
  79. }
  80. allCommitsCount, err := ctx.Repo.GitRepo.GetAllCommitsCount()
  81. if err != nil {
  82. ctx.ServerError("GetAllCommitsCount", err)
  83. return
  84. }
  85. page := ctx.QueryInt("page")
  86. graph, err := gitgraph.GetCommitGraph(ctx.Repo.GitRepo, page)
  87. if err != nil {
  88. ctx.ServerError("GetCommitGraph", err)
  89. return
  90. }
  91. ctx.Data["Graph"] = graph
  92. ctx.Data["Username"] = ctx.Repo.Owner.Name
  93. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  94. ctx.Data["CommitCount"] = commitsCount
  95. ctx.Data["Branch"] = ctx.Repo.BranchName
  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["AfterCommitID"] = commitID
  211. ctx.Data["Username"] = userName
  212. ctx.Data["Reponame"] = repoName
  213. var parentCommit *git.Commit
  214. if commit.ParentCount() > 0 {
  215. parentCommit, err = ctx.Repo.GitRepo.GetCommit(parents[0])
  216. if err != nil {
  217. ctx.NotFound("GetParentCommit", err)
  218. return
  219. }
  220. }
  221. setImageCompareContext(ctx, parentCommit, commit)
  222. headTarget := path.Join(userName, repoName)
  223. setPathsCompareContext(ctx, parentCommit, commit, headTarget)
  224. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
  225. ctx.Data["Commit"] = commit
  226. ctx.Data["Verification"] = models.ParseCommitWithSignature(commit)
  227. ctx.Data["Author"] = models.ValidateCommitWithEmail(commit)
  228. ctx.Data["Diff"] = diff
  229. ctx.Data["Parents"] = parents
  230. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  231. note := &git.Note{}
  232. err = git.GetNote(ctx.Repo.GitRepo, commitID, note)
  233. if err == nil {
  234. ctx.Data["Note"] = string(charset.ToUTF8WithFallback(note.Message))
  235. ctx.Data["NoteCommit"] = note.Commit
  236. ctx.Data["NoteAuthor"] = models.ValidateCommitWithEmail(note.Commit)
  237. }
  238. ctx.Data["BranchName"], err = commit.GetBranchName()
  239. if err != nil {
  240. ctx.ServerError("commit.GetBranchName", err)
  241. }
  242. ctx.HTML(200, tplCommitPage)
  243. }
  244. // RawDiff dumps diff results of repository in given commit ID to io.Writer
  245. func RawDiff(ctx *context.Context) {
  246. if err := gitdiff.GetRawDiff(
  247. models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name),
  248. ctx.Params(":sha"),
  249. gitdiff.RawDiffType(ctx.Params(":ext")),
  250. ctx.Resp,
  251. ); err != nil {
  252. ctx.ServerError("GetRawDiff", err)
  253. return
  254. }
  255. }