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

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