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.

278 lines
7.4 KiB

10 years ago
10 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
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  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/Unknwon/com"
  9. "github.com/Unknwon/paginater"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/git"
  13. "github.com/gogits/gogs/modules/middleware"
  14. "github.com/gogits/gogs/modules/setting"
  15. )
  16. const (
  17. COMMITS base.TplName = "repo/commits"
  18. DIFF base.TplName = "repo/diff"
  19. )
  20. func RefCommits(ctx *middleware.Context) {
  21. switch {
  22. case len(ctx.Repo.TreeName) == 0:
  23. Commits(ctx)
  24. case ctx.Repo.TreeName == "search":
  25. SearchCommits(ctx)
  26. default:
  27. FileHistory(ctx)
  28. }
  29. }
  30. func RenderIssueLinks(oldCommits *list.List, repoLink string) *list.List {
  31. newCommits := list.New()
  32. for e := oldCommits.Front(); e != nil; e = e.Next() {
  33. c := e.Value.(*git.Commit)
  34. c.CommitMessage = c.CommitMessage
  35. newCommits.PushBack(c)
  36. }
  37. return newCommits
  38. }
  39. func Commits(ctx *middleware.Context) {
  40. ctx.Data["PageIsCommits"] = true
  41. userName := ctx.Repo.Owner.Name
  42. repoName := ctx.Repo.Repository.Name
  43. brs, err := ctx.Repo.GitRepo.GetBranches()
  44. if err != nil {
  45. ctx.Handle(500, "GetBranches", err)
  46. return
  47. } else if len(brs) == 0 {
  48. ctx.Handle(404, "GetBranches", nil)
  49. return
  50. }
  51. commitsCount, err := ctx.Repo.Commit.CommitsCount()
  52. if err != nil {
  53. ctx.Handle(500, "GetCommitsCount", err)
  54. return
  55. }
  56. page := ctx.QueryInt("page")
  57. if page <= 1 {
  58. page = 1
  59. }
  60. ctx.Data["Page"] = paginater.New(commitsCount, git.CommitsRangeSize, page, 5)
  61. // Both `git log branchName` and `git log commitId` work.
  62. commits, err := ctx.Repo.Commit.CommitsByRange(page)
  63. if err != nil {
  64. ctx.Handle(500, "CommitsByRange", err)
  65. return
  66. }
  67. commits = RenderIssueLinks(commits, ctx.Repo.RepoLink)
  68. commits = models.ValidateCommitsWithEmails(commits)
  69. ctx.Data["Commits"] = commits
  70. ctx.Data["Username"] = userName
  71. ctx.Data["Reponame"] = repoName
  72. ctx.Data["CommitCount"] = commitsCount
  73. ctx.HTML(200, COMMITS)
  74. }
  75. func SearchCommits(ctx *middleware.Context) {
  76. ctx.Data["PageIsCommits"] = true
  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 := ctx.Params(":username")
  83. repoName := ctx.Params(":reponame")
  84. brs, err := ctx.Repo.GitRepo.GetBranches()
  85. if err != nil {
  86. ctx.Handle(500, "GetBranches", err)
  87. return
  88. } else if len(brs) == 0 {
  89. ctx.Handle(404, "GetBranches", nil)
  90. return
  91. }
  92. commits, err := ctx.Repo.Commit.SearchCommits(keyword)
  93. if err != nil {
  94. ctx.Handle(500, "SearchCommits", err)
  95. return
  96. }
  97. commits = RenderIssueLinks(commits, ctx.Repo.RepoLink)
  98. commits = models.ValidateCommitsWithEmails(commits)
  99. ctx.Data["Keyword"] = keyword
  100. ctx.Data["Username"] = userName
  101. ctx.Data["Reponame"] = repoName
  102. ctx.Data["CommitCount"] = commits.Len()
  103. ctx.Data["Commits"] = commits
  104. ctx.HTML(200, COMMITS)
  105. }
  106. func FileHistory(ctx *middleware.Context) {
  107. ctx.Data["IsRepoToolbarCommits"] = true
  108. fileName := ctx.Repo.TreeName
  109. if len(fileName) == 0 {
  110. Commits(ctx)
  111. return
  112. }
  113. userName := ctx.Repo.Owner.Name
  114. repoName := ctx.Repo.Repository.Name
  115. branchName := ctx.Repo.BranchName
  116. brs, err := ctx.Repo.GitRepo.GetBranches()
  117. if err != nil {
  118. ctx.Handle(500, "GetBranches", err)
  119. return
  120. } else if len(brs) == 0 {
  121. ctx.Handle(404, "GetBranches", nil)
  122. return
  123. }
  124. commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
  125. if err != nil {
  126. ctx.Handle(500, "repo.FileHistory(GetCommitsCount)", err)
  127. return
  128. } else if commitsCount == 0 {
  129. ctx.Handle(404, "repo.FileHistory", nil)
  130. return
  131. }
  132. // Calculate and validate page number.
  133. page := com.StrTo(ctx.Query("p")).MustInt()
  134. if page < 1 {
  135. page = 1
  136. }
  137. lastPage := page - 1
  138. if lastPage < 0 {
  139. lastPage = 0
  140. }
  141. nextPage := page + 1
  142. if nextPage*50 > commitsCount {
  143. nextPage = 0
  144. }
  145. commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(
  146. branchName, fileName, page)
  147. if err != nil {
  148. ctx.Handle(500, "repo.FileHistory(CommitsByRange)", err)
  149. return
  150. }
  151. commits = RenderIssueLinks(commits, ctx.Repo.RepoLink)
  152. commits = models.ValidateCommitsWithEmails(commits)
  153. ctx.Data["Commits"] = commits
  154. ctx.Data["Username"] = userName
  155. ctx.Data["Reponame"] = repoName
  156. ctx.Data["FileName"] = fileName
  157. ctx.Data["CommitCount"] = commitsCount
  158. ctx.Data["LastPageNum"] = lastPage
  159. ctx.Data["NextPageNum"] = nextPage
  160. ctx.HTML(200, COMMITS)
  161. }
  162. func Diff(ctx *middleware.Context) {
  163. ctx.Data["PageIsDiff"] = true
  164. userName := ctx.Repo.Owner.Name
  165. repoName := ctx.Repo.Repository.Name
  166. commitID := ctx.Repo.CommitID
  167. commit := ctx.Repo.Commit
  168. commit.CommitMessage = commit.CommitMessage
  169. diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName),
  170. commitID, setting.Git.MaxGitDiffLines)
  171. if err != nil {
  172. ctx.Handle(404, "GetDiffCommit", err)
  173. return
  174. }
  175. parents := make([]string, commit.ParentCount())
  176. for i := 0; i < commit.ParentCount(); i++ {
  177. sha, err := commit.ParentId(i)
  178. parents[i] = sha.String()
  179. if err != nil {
  180. ctx.Handle(404, "repo.Diff", err)
  181. return
  182. }
  183. }
  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, DIFF)
  199. }
  200. func CompareDiff(ctx *middleware.Context) {
  201. ctx.Data["IsRepoToolbarCommits"] = true
  202. ctx.Data["IsDiffCompare"] = true
  203. userName := ctx.Repo.Owner.Name
  204. repoName := ctx.Repo.Repository.Name
  205. beforeCommitID := ctx.Params(":before")
  206. afterCommitID := ctx.Params(":after")
  207. commit, err := ctx.Repo.GitRepo.GetCommit(afterCommitID)
  208. if err != nil {
  209. ctx.Handle(404, "GetCommit", err)
  210. return
  211. }
  212. diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitID,
  213. afterCommitID, setting.Git.MaxGitDiffLines)
  214. if err != nil {
  215. ctx.Handle(404, "GetDiffRange", err)
  216. return
  217. }
  218. commits, err := commit.CommitsBeforeUntil(beforeCommitID)
  219. if err != nil {
  220. ctx.Handle(500, "CommitsBeforeUntil", err)
  221. return
  222. }
  223. commits = models.ValidateCommitsWithEmails(commits)
  224. ctx.Data["CommitRepoLink"] = ctx.Repo.RepoLink
  225. ctx.Data["Commits"] = commits
  226. ctx.Data["CommitCount"] = commits.Len()
  227. ctx.Data["BeforeCommitID"] = beforeCommitID
  228. ctx.Data["AfterCommitID"] = afterCommitID
  229. ctx.Data["Username"] = userName
  230. ctx.Data["Reponame"] = repoName
  231. ctx.Data["IsImageFile"] = commit.IsImageFile
  232. ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitID) + "..." + base.ShortSha(afterCommitID) + " · " + userName + "/" + repoName
  233. ctx.Data["Commit"] = commit
  234. ctx.Data["Diff"] = diff
  235. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  236. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", afterCommitID)
  237. ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", beforeCommitID)
  238. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "raw", afterCommitID)
  239. ctx.HTML(200, DIFF)
  240. }