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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "path"
  7. "strings"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. "github.com/Unknwon/paginater"
  15. )
  16. const (
  17. tplCommits base.TplName = "repo/commits"
  18. tplGraph base.TplName = "repo/graph"
  19. tplDiff base.TplName = "repo/diff/page"
  20. )
  21. // RefCommits render commits page
  22. func RefCommits(ctx *context.Context) {
  23. switch {
  24. case len(ctx.Repo.TreePath) == 0:
  25. Commits(ctx)
  26. case ctx.Repo.TreePath == "search":
  27. SearchCommits(ctx)
  28. default:
  29. FileHistory(ctx)
  30. }
  31. }
  32. // Commits render branch's commits
  33. func Commits(ctx *context.Context) {
  34. ctx.Data["PageIsCommits"] = true
  35. if ctx.Repo.Commit == nil {
  36. ctx.NotFound("Commit not found", nil)
  37. return
  38. }
  39. ctx.Data["PageIsViewCode"] = true
  40. commitsCount, err := ctx.Repo.GetCommitsCount()
  41. if err != nil {
  42. ctx.ServerError("GetCommitsCount", err)
  43. return
  44. }
  45. page := ctx.QueryInt("page")
  46. if page <= 1 {
  47. page = 1
  48. }
  49. ctx.Data["Page"] = paginater.New(int(commitsCount), git.CommitsRangeSize, page, 5)
  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. ctx.HTML(200, tplCommits)
  65. }
  66. // Graph render commit graph - show commits from all branches.
  67. func Graph(ctx *context.Context) {
  68. ctx.Data["PageIsCommits"] = true
  69. ctx.Data["PageIsViewCode"] = true
  70. commitsCount, err := ctx.Repo.GetCommitsCount()
  71. if err != nil {
  72. ctx.ServerError("GetCommitsCount", err)
  73. return
  74. }
  75. graph, err := models.GetCommitGraph(ctx.Repo.GitRepo)
  76. if err != nil {
  77. ctx.ServerError("GetCommitGraph", err)
  78. return
  79. }
  80. ctx.Data["Graph"] = graph
  81. ctx.Data["Username"] = ctx.Repo.Owner.Name
  82. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  83. ctx.Data["CommitCount"] = commitsCount
  84. ctx.Data["Branch"] = ctx.Repo.BranchName
  85. ctx.Data["RequireGitGraph"] = true
  86. ctx.HTML(200, tplGraph)
  87. }
  88. // SearchCommits render commits filtered by keyword
  89. func SearchCommits(ctx *context.Context) {
  90. ctx.Data["PageIsCommits"] = true
  91. ctx.Data["PageIsViewCode"] = true
  92. query := strings.Trim(ctx.Query("q"), " ")
  93. if len(query) == 0 {
  94. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchNameSubURL())
  95. return
  96. }
  97. all := ctx.QueryBool("all")
  98. opts := git.NewSearchCommitsOptions(query, all)
  99. commits, err := ctx.Repo.Commit.SearchCommits(opts)
  100. if err != nil {
  101. ctx.ServerError("SearchCommits", err)
  102. return
  103. }
  104. commits = models.ValidateCommitsWithEmails(commits)
  105. commits = models.ParseCommitsWithSignature(commits)
  106. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  107. ctx.Data["Commits"] = commits
  108. ctx.Data["Keyword"] = query
  109. if all {
  110. ctx.Data["All"] = "checked"
  111. }
  112. ctx.Data["Username"] = ctx.Repo.Owner.Name
  113. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  114. ctx.Data["CommitCount"] = commits.Len()
  115. ctx.Data["Branch"] = ctx.Repo.BranchName
  116. ctx.HTML(200, tplCommits)
  117. }
  118. // FileHistory show a file's reversions
  119. func FileHistory(ctx *context.Context) {
  120. ctx.Data["IsRepoToolbarCommits"] = true
  121. fileName := ctx.Repo.TreePath
  122. if len(fileName) == 0 {
  123. Commits(ctx)
  124. return
  125. }
  126. branchName := ctx.Repo.BranchName
  127. commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
  128. if err != nil {
  129. ctx.ServerError("FileCommitsCount", err)
  130. return
  131. } else if commitsCount == 0 {
  132. ctx.NotFound("FileCommitsCount", nil)
  133. return
  134. }
  135. page := ctx.QueryInt("page")
  136. if page <= 1 {
  137. page = 1
  138. }
  139. ctx.Data["Page"] = paginater.New(int(commitsCount), git.CommitsRangeSize, page, 5)
  140. commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(branchName, fileName, page)
  141. if err != nil {
  142. ctx.ServerError("CommitsByFileAndRange", err)
  143. return
  144. }
  145. commits = models.ValidateCommitsWithEmails(commits)
  146. commits = models.ParseCommitsWithSignature(commits)
  147. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  148. ctx.Data["Commits"] = commits
  149. ctx.Data["Username"] = ctx.Repo.Owner.Name
  150. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  151. ctx.Data["FileName"] = fileName
  152. ctx.Data["CommitCount"] = commitsCount
  153. ctx.Data["Branch"] = branchName
  154. ctx.HTML(200, tplCommits)
  155. }
  156. // Diff show different from current commit to previous commit
  157. func Diff(ctx *context.Context) {
  158. ctx.Data["PageIsDiff"] = true
  159. ctx.Data["RequireHighlightJS"] = true
  160. userName := ctx.Repo.Owner.Name
  161. repoName := ctx.Repo.Repository.Name
  162. commitID := ctx.Params(":sha")
  163. commit, err := ctx.Repo.GitRepo.GetCommit(commitID)
  164. if err != nil {
  165. if git.IsErrNotExist(err) {
  166. ctx.NotFound("Repo.GitRepo.GetCommit", err)
  167. } else {
  168. ctx.ServerError("Repo.GitRepo.GetCommit", err)
  169. }
  170. return
  171. }
  172. if len(commitID) != 40 {
  173. commitID = commit.ID.String()
  174. }
  175. statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository, commitID, 0)
  176. if err != nil {
  177. log.Error("GetLatestCommitStatus: %v", err)
  178. }
  179. ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses)
  180. diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName),
  181. commitID, setting.Git.MaxGitDiffLines,
  182. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  183. if err != nil {
  184. ctx.NotFound("GetDiffCommit", err)
  185. return
  186. }
  187. parents := make([]string, commit.ParentCount())
  188. for i := 0; i < commit.ParentCount(); i++ {
  189. sha, err := commit.ParentID(i)
  190. parents[i] = sha.String()
  191. if err != nil {
  192. ctx.NotFound("repo.Diff", err)
  193. return
  194. }
  195. }
  196. ctx.Data["CommitID"] = commitID
  197. ctx.Data["Username"] = userName
  198. ctx.Data["Reponame"] = repoName
  199. ctx.Data["IsImageFile"] = commit.IsImageFile
  200. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
  201. ctx.Data["Commit"] = commit
  202. ctx.Data["Verification"] = models.ParseCommitWithSignature(commit)
  203. ctx.Data["Author"] = models.ValidateCommitWithEmail(commit)
  204. ctx.Data["Diff"] = diff
  205. ctx.Data["Parents"] = parents
  206. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  207. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", commitID)
  208. if commit.ParentCount() > 0 {
  209. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", parents[0])
  210. }
  211. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", "commit", commitID)
  212. ctx.Data["BranchName"], err = commit.GetBranchName()
  213. ctx.HTML(200, tplDiff)
  214. }
  215. // RawDiff dumps diff results of repository in given commit ID to io.Writer
  216. func RawDiff(ctx *context.Context) {
  217. if err := models.GetRawDiff(
  218. models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name),
  219. ctx.Params(":sha"),
  220. models.RawDiffType(ctx.Params(":ext")),
  221. ctx.Resp,
  222. ); err != nil {
  223. ctx.ServerError("GetRawDiff", err)
  224. return
  225. }
  226. }
  227. // CompareDiff show different from one commit to another commit
  228. func CompareDiff(ctx *context.Context) {
  229. ctx.Data["IsRepoToolbarCommits"] = true
  230. ctx.Data["IsDiffCompare"] = true
  231. userName := ctx.Repo.Owner.Name
  232. repoName := ctx.Repo.Repository.Name
  233. beforeCommitID := ctx.Params(":before")
  234. afterCommitID := ctx.Params(":after")
  235. commit, err := ctx.Repo.GitRepo.GetCommit(afterCommitID)
  236. if err != nil {
  237. ctx.NotFound("GetCommit", err)
  238. return
  239. }
  240. diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitID,
  241. afterCommitID, setting.Git.MaxGitDiffLines,
  242. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  243. if err != nil {
  244. ctx.NotFound("GetDiffRange", err)
  245. return
  246. }
  247. commits, err := commit.CommitsBeforeUntil(beforeCommitID)
  248. if err != nil {
  249. ctx.ServerError("CommitsBeforeUntil", err)
  250. return
  251. }
  252. commits = models.ValidateCommitsWithEmails(commits)
  253. commits = models.ParseCommitsWithSignature(commits)
  254. commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
  255. ctx.Data["CommitRepoLink"] = ctx.Repo.RepoLink
  256. ctx.Data["Commits"] = commits
  257. ctx.Data["CommitCount"] = commits.Len()
  258. ctx.Data["BeforeCommitID"] = beforeCommitID
  259. ctx.Data["AfterCommitID"] = afterCommitID
  260. ctx.Data["Username"] = userName
  261. ctx.Data["Reponame"] = repoName
  262. ctx.Data["IsImageFile"] = commit.IsImageFile
  263. ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitID) + "..." + base.ShortSha(afterCommitID) + " · " + userName + "/" + repoName
  264. ctx.Data["Commit"] = commit
  265. ctx.Data["Diff"] = diff
  266. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  267. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", afterCommitID)
  268. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", beforeCommitID)
  269. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", "commit", afterCommitID)
  270. ctx.Data["RequireHighlightJS"] = true
  271. ctx.HTML(200, tplDiff)
  272. }