commit.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "github.com/go-martini/martini"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/middleware"
  12. )
  13. func Commits(ctx *middleware.Context, params martini.Params) {
  14. userName := params["username"]
  15. repoName := params["reponame"]
  16. branchName := params["branchname"]
  17. brs, err := models.GetBranches(userName, repoName)
  18. if err != nil {
  19. ctx.Handle(500, "repo.Commits", err)
  20. return
  21. } else if len(brs) == 0 {
  22. ctx.Handle(404, "repo.Commits", nil)
  23. return
  24. }
  25. var commits *list.List
  26. if models.IsBranchExist(userName, repoName, branchName) {
  27. commits, err = models.GetCommitsByBranch(userName, repoName, branchName)
  28. } else {
  29. commits, err = models.GetCommitsByCommitId(userName, repoName, branchName)
  30. }
  31. if err != nil {
  32. ctx.Handle(404, "repo.Commits", err)
  33. return
  34. }
  35. ctx.Data["Username"] = userName
  36. ctx.Data["Reponame"] = repoName
  37. ctx.Data["CommitCount"] = commits.Len()
  38. ctx.Data["Commits"] = commits
  39. ctx.Data["IsRepoToolbarCommits"] = true
  40. ctx.HTML(200, "repo/commits")
  41. }
  42. func Diff(ctx *middleware.Context, params martini.Params) {
  43. userName := ctx.Repo.Owner.Name
  44. repoName := ctx.Repo.Repository.Name
  45. branchName := ctx.Repo.BranchName
  46. commitId := ctx.Repo.CommitId
  47. commit := ctx.Repo.Commit
  48. diff, err := models.GetDiff(models.RepoPath(userName, repoName), commitId)
  49. if err != nil {
  50. ctx.Handle(404, "repo.Diff", err)
  51. return
  52. }
  53. isImageFile := func(name string) bool {
  54. repoFile, err := models.GetTargetFile(userName, repoName,
  55. branchName, commitId, name)
  56. if err != nil {
  57. return false
  58. }
  59. blob, err := repoFile.LookupBlob()
  60. if err != nil {
  61. return false
  62. }
  63. data := blob.Contents()
  64. _, isImage := base.IsImageFile(data)
  65. return isImage
  66. }
  67. ctx.Data["IsImageFile"] = isImageFile
  68. ctx.Data["Title"] = commit.Message() + " · " + base.ShortSha(commitId)
  69. ctx.Data["Commit"] = commit
  70. ctx.Data["Diff"] = diff
  71. ctx.Data["IsRepoToolbarCommits"] = true
  72. ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId)
  73. ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", commitId)
  74. ctx.HTML(200, "repo/diff")
  75. }
  76. func SearchCommits(ctx *middleware.Context, params martini.Params) {
  77. keyword := ctx.Query("q")
  78. if len(keyword) == 0 {
  79. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName)
  80. return
  81. }
  82. userName := params["username"]
  83. repoName := params["reponame"]
  84. branchName := params["branchname"]
  85. brs, err := models.GetBranches(userName, repoName)
  86. if err != nil {
  87. ctx.Handle(500, "repo.SearchCommits(GetBranches)", err)
  88. return
  89. } else if len(brs) == 0 {
  90. ctx.Handle(404, "repo.SearchCommits(GetBranches)", nil)
  91. return
  92. }
  93. var commits *list.List
  94. if !models.IsBranchExist(userName, repoName, branchName) {
  95. ctx.Handle(404, "repo.SearchCommits(IsBranchExist)", err)
  96. return
  97. } else if commits, err = models.SearchCommits(models.RepoPath(userName, repoName), branchName, keyword); err != nil {
  98. ctx.Handle(500, "repo.SearchCommits(SearchCommits)", err)
  99. return
  100. }
  101. ctx.Data["Keyword"] = keyword
  102. ctx.Data["Username"] = userName
  103. ctx.Data["Reponame"] = repoName
  104. ctx.Data["CommitCount"] = commits.Len()
  105. ctx.Data["Commits"] = commits
  106. ctx.Data["IsRepoToolbarCommits"] = true
  107. ctx.HTML(200, "repo/commits")
  108. }