You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

commit.go 8.5 kB

11 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. "container/list"
  7. "path"
  8. "code.gitea.io/git"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/setting"
  13. "github.com/Unknwon/paginater"
  14. )
  15. const (
  16. tplCommits base.TplName = "repo/commits"
  17. tplGraph base.TplName = "repo/graph"
  18. tplDiff base.TplName = "repo/diff/page"
  19. )
  20. // RefCommits render commits page
  21. func RefCommits(ctx *context.Context) {
  22. switch {
  23. case len(ctx.Repo.TreePath) == 0:
  24. Commits(ctx)
  25. case ctx.Repo.TreePath == "search":
  26. SearchCommits(ctx)
  27. default:
  28. FileHistory(ctx)
  29. }
  30. }
  31. func renderIssueLinks(oldCommits *list.List, repoLink string) *list.List {
  32. newCommits := list.New()
  33. for e := oldCommits.Front(); e != nil; e = e.Next() {
  34. c := e.Value.(*git.Commit)
  35. newCommits.PushBack(c)
  36. }
  37. return newCommits
  38. }
  39. // Commits render branch's commits
  40. func Commits(ctx *context.Context) {
  41. ctx.Data["PageIsCommits"] = true
  42. commitsCount, err := ctx.Repo.Commit.CommitsCount()
  43. if err != nil {
  44. ctx.Handle(500, "GetCommitsCount", err)
  45. return
  46. }
  47. page := ctx.QueryInt("page")
  48. if page <= 1 {
  49. page = 1
  50. }
  51. ctx.Data["Page"] = paginater.New(int(commitsCount), git.CommitsRangeSize, page, 5)
  52. // Both `git log branchName` and `git log commitId` work.
  53. commits, err := ctx.Repo.Commit.CommitsByRange(page)
  54. if err != nil {
  55. ctx.Handle(500, "CommitsByRange", err)
  56. return
  57. }
  58. commits = renderIssueLinks(commits, ctx.Repo.RepoLink)
  59. commits = models.ValidateCommitsWithEmails(commits)
  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. ctx.HTML(200, tplCommits)
  66. }
  67. // Graph render commit graph - show commits from all branches.
  68. func Graph(ctx *context.Context) {
  69. ctx.Data["PageIsCommits"] = true
  70. commitsCount, err := ctx.Repo.Commit.CommitsCount()
  71. if err != nil {
  72. ctx.Handle(500, "GetCommitsCount", err)
  73. return
  74. }
  75. graph, err := models.GetCommitGraph(ctx.Repo.GitRepo)
  76. if err != nil {
  77. ctx.Handle(500, "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. keyword := ctx.Query("q")
  92. if len(keyword) == 0 {
  93. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName)
  94. return
  95. }
  96. commits, err := ctx.Repo.Commit.SearchCommits(keyword)
  97. if err != nil {
  98. ctx.Handle(500, "SearchCommits", err)
  99. return
  100. }
  101. commits = renderIssueLinks(commits, ctx.Repo.RepoLink)
  102. commits = models.ValidateCommitsWithEmails(commits)
  103. ctx.Data["Commits"] = commits
  104. ctx.Data["Keyword"] = keyword
  105. ctx.Data["Username"] = ctx.Repo.Owner.Name
  106. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  107. ctx.Data["CommitCount"] = commits.Len()
  108. ctx.Data["Branch"] = ctx.Repo.BranchName
  109. ctx.HTML(200, tplCommits)
  110. }
  111. // FileHistory show a file's reversions
  112. func FileHistory(ctx *context.Context) {
  113. ctx.Data["IsRepoToolbarCommits"] = true
  114. fileName := ctx.Repo.TreePath
  115. if len(fileName) == 0 {
  116. Commits(ctx)
  117. return
  118. }
  119. branchName := ctx.Repo.BranchName
  120. commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
  121. if err != nil {
  122. ctx.Handle(500, "FileCommitsCount", err)
  123. return
  124. } else if commitsCount == 0 {
  125. ctx.Handle(404, "FileCommitsCount", nil)
  126. return
  127. }
  128. page := ctx.QueryInt("page")
  129. if page <= 1 {
  130. page = 1
  131. }
  132. ctx.Data["Page"] = paginater.New(int(commitsCount), git.CommitsRangeSize, page, 5)
  133. commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(branchName, fileName, page)
  134. if err != nil {
  135. ctx.Handle(500, "CommitsByFileAndRange", err)
  136. return
  137. }
  138. commits = renderIssueLinks(commits, ctx.Repo.RepoLink)
  139. commits = models.ValidateCommitsWithEmails(commits)
  140. ctx.Data["Commits"] = commits
  141. ctx.Data["Username"] = ctx.Repo.Owner.Name
  142. ctx.Data["Reponame"] = ctx.Repo.Repository.Name
  143. ctx.Data["FileName"] = fileName
  144. ctx.Data["CommitCount"] = commitsCount
  145. ctx.Data["Branch"] = branchName
  146. ctx.HTML(200, tplCommits)
  147. }
  148. // Diff show different from current commit to previous commit
  149. func Diff(ctx *context.Context) {
  150. ctx.Data["PageIsDiff"] = true
  151. ctx.Data["RequireHighlightJS"] = true
  152. userName := ctx.Repo.Owner.Name
  153. repoName := ctx.Repo.Repository.Name
  154. commitID := ctx.Params(":sha")
  155. commit, err := ctx.Repo.GitRepo.GetCommit(commitID)
  156. if err != nil {
  157. if git.IsErrNotExist(err) {
  158. ctx.Handle(404, "Repo.GitRepo.GetCommit", err)
  159. } else {
  160. ctx.Handle(500, "Repo.GitRepo.GetCommit", err)
  161. }
  162. return
  163. }
  164. if len(commitID) != 40 {
  165. commitID = commit.ID.String()
  166. }
  167. diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName),
  168. commitID, setting.Git.MaxGitDiffLines,
  169. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  170. if err != nil {
  171. ctx.Handle(404, "GetDiffCommit", err)
  172. return
  173. }
  174. parents := make([]string, commit.ParentCount())
  175. for i := 0; i < commit.ParentCount(); i++ {
  176. sha, err := commit.ParentID(i)
  177. parents[i] = sha.String()
  178. if err != nil {
  179. ctx.Handle(404, "repo.Diff", err)
  180. return
  181. }
  182. }
  183. ctx.Data["CommitID"] = commitID
  184. ctx.Data["Username"] = userName
  185. ctx.Data["Reponame"] = repoName
  186. ctx.Data["IsImageFile"] = commit.IsImageFile
  187. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID)
  188. ctx.Data["Commit"] = commit
  189. ctx.Data["Author"] = models.ValidateCommitWithEmail(commit)
  190. ctx.Data["Diff"] = diff
  191. ctx.Data["Parents"] = parents
  192. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  193. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", commitID)
  194. if commit.ParentCount() > 0 {
  195. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", parents[0])
  196. }
  197. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", commitID)
  198. ctx.HTML(200, tplDiff)
  199. }
  200. // RawDiff dumps diff results of repository in given commit ID to io.Writer
  201. func RawDiff(ctx *context.Context) {
  202. if err := models.GetRawDiff(
  203. models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name),
  204. ctx.Params(":sha"),
  205. models.RawDiffType(ctx.Params(":ext")),
  206. ctx.Resp,
  207. ); err != nil {
  208. ctx.Handle(500, "GetRawDiff", err)
  209. return
  210. }
  211. }
  212. // CompareDiff show different from one commit to another commit
  213. func CompareDiff(ctx *context.Context) {
  214. ctx.Data["IsRepoToolbarCommits"] = true
  215. ctx.Data["IsDiffCompare"] = true
  216. userName := ctx.Repo.Owner.Name
  217. repoName := ctx.Repo.Repository.Name
  218. beforeCommitID := ctx.Params(":before")
  219. afterCommitID := ctx.Params(":after")
  220. commit, err := ctx.Repo.GitRepo.GetCommit(afterCommitID)
  221. if err != nil {
  222. ctx.Handle(404, "GetCommit", err)
  223. return
  224. }
  225. diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitID,
  226. afterCommitID, setting.Git.MaxGitDiffLines,
  227. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  228. if err != nil {
  229. ctx.Handle(404, "GetDiffRange", err)
  230. return
  231. }
  232. commits, err := commit.CommitsBeforeUntil(beforeCommitID)
  233. if err != nil {
  234. ctx.Handle(500, "CommitsBeforeUntil", err)
  235. return
  236. }
  237. commits = models.ValidateCommitsWithEmails(commits)
  238. ctx.Data["CommitRepoLink"] = ctx.Repo.RepoLink
  239. ctx.Data["Commits"] = commits
  240. ctx.Data["CommitCount"] = commits.Len()
  241. ctx.Data["BeforeCommitID"] = beforeCommitID
  242. ctx.Data["AfterCommitID"] = afterCommitID
  243. ctx.Data["Username"] = userName
  244. ctx.Data["Reponame"] = repoName
  245. ctx.Data["IsImageFile"] = commit.IsImageFile
  246. ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitID) + "..." + base.ShortSha(afterCommitID) + " · " + userName + "/" + repoName
  247. ctx.Data["Commit"] = commit
  248. ctx.Data["Diff"] = diff
  249. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  250. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", afterCommitID)
  251. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", beforeCommitID)
  252. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", afterCommitID)
  253. ctx.HTML(200, tplDiff)
  254. }