commit.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/git"
  12. "github.com/gogits/gogs/modules/middleware"
  13. "github.com/gogits/gogs/modules/setting"
  14. )
  15. const (
  16. COMMITS base.TplName = "repo/commits"
  17. DIFF base.TplName = "repo/diff"
  18. )
  19. func RefCommits(ctx *middleware.Context) {
  20. switch {
  21. case len(ctx.Repo.TreeName) == 0:
  22. Commits(ctx)
  23. case ctx.Repo.TreeName == "search":
  24. SearchCommits(ctx)
  25. default:
  26. FileHistory(ctx)
  27. }
  28. }
  29. func Commits(ctx *middleware.Context) {
  30. ctx.Data["IsRepoToolbarCommits"] = true
  31. userName := ctx.Repo.Owner.Name
  32. repoName := ctx.Repo.Repository.Name
  33. brs, err := ctx.Repo.GitRepo.GetBranches()
  34. if err != nil {
  35. ctx.Handle(500, "GetBranches", err)
  36. return
  37. } else if len(brs) == 0 {
  38. ctx.Handle(404, "GetBranches", nil)
  39. return
  40. }
  41. commitsCount, err := ctx.Repo.Commit.CommitsCount()
  42. if err != nil {
  43. ctx.Handle(500, "GetCommitsCount", err)
  44. return
  45. }
  46. // Calculate and validate page number.
  47. page, _ := com.StrTo(ctx.Query("p")).Int()
  48. if page < 1 {
  49. page = 1
  50. }
  51. lastPage := page - 1
  52. if lastPage < 0 {
  53. lastPage = 0
  54. }
  55. nextPage := page + 1
  56. if page*50 > commitsCount {
  57. nextPage = 0
  58. }
  59. // Both `git log branchName` and `git log commitId` work.
  60. commits, err := ctx.Repo.Commit.CommitsByRange(page)
  61. if err != nil {
  62. ctx.Handle(500, "CommitsByRange", err)
  63. return
  64. }
  65. commits = RenderIssueLinks(commits, ctx.Repo.RepoLink)
  66. commits = models.ValidateCommitsWithEmails(commits)
  67. ctx.Data["Commits"] = commits
  68. ctx.Data["Username"] = userName
  69. ctx.Data["Reponame"] = repoName
  70. ctx.Data["CommitCount"] = commitsCount
  71. ctx.Data["LastPageNum"] = lastPage
  72. ctx.Data["NextPageNum"] = nextPage
  73. ctx.HTML(200, COMMITS)
  74. }
  75. func RenderIssueLinks(oldCommits *list.List, repoLink string) *list.List {
  76. newCommits := list.New()
  77. for e := oldCommits.Front(); e != nil; e = e.Next() {
  78. c := e.Value.(*git.Commit)
  79. c.CommitMessage = string(base.RenderissueIndexPattern([]byte(c.CommitMessage), repoLink))
  80. newCommits.PushBack(c)
  81. }
  82. return newCommits
  83. }
  84. func SearchCommits(ctx *middleware.Context) {
  85. ctx.Data["IsSearchPage"] = true
  86. ctx.Data["IsRepoToolbarCommits"] = true
  87. keyword := ctx.Query("q")
  88. if len(keyword) == 0 {
  89. ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName)
  90. return
  91. }
  92. userName := ctx.Params(":username")
  93. repoName := ctx.Params(":reponame")
  94. brs, err := ctx.Repo.GitRepo.GetBranches()
  95. if err != nil {
  96. ctx.Handle(500, "GetBranches", err)
  97. return
  98. } else if len(brs) == 0 {
  99. ctx.Handle(404, "GetBranches", nil)
  100. return
  101. }
  102. commits, err := ctx.Repo.Commit.SearchCommits(keyword)
  103. if err != nil {
  104. ctx.Handle(500, "SearchCommits", err)
  105. return
  106. }
  107. commits = RenderIssueLinks(commits, ctx.Repo.RepoLink)
  108. commits = models.ValidateCommitsWithEmails(commits)
  109. ctx.Data["Keyword"] = keyword
  110. ctx.Data["Username"] = userName
  111. ctx.Data["Reponame"] = repoName
  112. ctx.Data["CommitCount"] = commits.Len()
  113. ctx.Data["Commits"] = commits
  114. ctx.HTML(200, COMMITS)
  115. }
  116. func FileHistory(ctx *middleware.Context) {
  117. ctx.Data["IsRepoToolbarCommits"] = true
  118. fileName := ctx.Repo.TreeName
  119. if len(fileName) == 0 {
  120. Commits(ctx)
  121. return
  122. }
  123. userName := ctx.Repo.Owner.Name
  124. repoName := ctx.Repo.Repository.Name
  125. branchName := ctx.Repo.BranchName
  126. brs, err := ctx.Repo.GitRepo.GetBranches()
  127. if err != nil {
  128. ctx.Handle(500, "GetBranches", err)
  129. return
  130. } else if len(brs) == 0 {
  131. ctx.Handle(404, "GetBranches", nil)
  132. return
  133. }
  134. commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
  135. if err != nil {
  136. ctx.Handle(500, "repo.FileHistory(GetCommitsCount)", err)
  137. return
  138. } else if commitsCount == 0 {
  139. ctx.Handle(404, "repo.FileHistory", nil)
  140. return
  141. }
  142. // Calculate and validate page number.
  143. page := com.StrTo(ctx.Query("p")).MustInt()
  144. if page < 1 {
  145. page = 1
  146. }
  147. lastPage := page - 1
  148. if lastPage < 0 {
  149. lastPage = 0
  150. }
  151. nextPage := page + 1
  152. if nextPage*50 > commitsCount {
  153. nextPage = 0
  154. }
  155. commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(
  156. branchName, fileName, page)
  157. if err != nil {
  158. ctx.Handle(500, "repo.FileHistory(CommitsByRange)", err)
  159. return
  160. }
  161. commits = RenderIssueLinks(commits, ctx.Repo.RepoLink)
  162. commits = models.ValidateCommitsWithEmails(commits)
  163. ctx.Data["Commits"] = commits
  164. ctx.Data["Username"] = userName
  165. ctx.Data["Reponame"] = repoName
  166. ctx.Data["FileName"] = fileName
  167. ctx.Data["CommitCount"] = commitsCount
  168. ctx.Data["LastPageNum"] = lastPage
  169. ctx.Data["NextPageNum"] = nextPage
  170. ctx.HTML(200, COMMITS)
  171. }
  172. func Diff(ctx *middleware.Context) {
  173. ctx.Data["IsRepoToolbarCommits"] = true
  174. userName := ctx.Repo.Owner.Name
  175. repoName := ctx.Repo.Repository.Name
  176. commitId := ctx.Repo.CommitId
  177. commit := ctx.Repo.Commit
  178. commit.CommitMessage = string(base.RenderissueIndexPattern([]byte(commit.CommitMessage), ctx.Repo.RepoLink))
  179. diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName),
  180. commitId, setting.MaxGitDiffLines)
  181. if err != nil {
  182. ctx.Handle(404, "GetDiffCommit", err)
  183. return
  184. }
  185. isImageFile := func(name string) bool {
  186. blob, err := ctx.Repo.Commit.GetBlobByPath(name)
  187. if err != nil {
  188. return false
  189. }
  190. dataRc, err := blob.Data()
  191. if err != nil {
  192. return false
  193. }
  194. buf := make([]byte, 1024)
  195. n, _ := dataRc.Read(buf)
  196. if n > 0 {
  197. buf = buf[:n]
  198. }
  199. _, isImage := base.IsImageFile(buf)
  200. return isImage
  201. }
  202. parents := make([]string, commit.ParentCount())
  203. for i := 0; i < commit.ParentCount(); i++ {
  204. sha, err := commit.ParentId(i)
  205. parents[i] = sha.String()
  206. if err != nil {
  207. ctx.Handle(404, "repo.Diff", err)
  208. return
  209. }
  210. }
  211. ctx.Data["Username"] = userName
  212. ctx.Data["Reponame"] = repoName
  213. ctx.Data["IsImageFile"] = isImageFile
  214. ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitId)
  215. ctx.Data["Commit"] = commit
  216. ctx.Data["Author"] = models.ValidateCommitWithEmail(commit)
  217. ctx.Data["Diff"] = diff
  218. ctx.Data["Parents"] = parents
  219. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  220. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", commitId)
  221. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "raw", commitId)
  222. ctx.HTML(200, DIFF)
  223. }
  224. func CompareDiff(ctx *middleware.Context) {
  225. ctx.Data["IsRepoToolbarCommits"] = true
  226. ctx.Data["IsDiffCompare"] = true
  227. userName := ctx.Repo.Owner.Name
  228. repoName := ctx.Repo.Repository.Name
  229. beforeCommitId := ctx.Params(":before")
  230. afterCommitId := ctx.Params(":after")
  231. commit, err := ctx.Repo.GitRepo.GetCommit(afterCommitId)
  232. if err != nil {
  233. ctx.Handle(404, "GetCommit", err)
  234. return
  235. }
  236. diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitId,
  237. afterCommitId, setting.MaxGitDiffLines)
  238. if err != nil {
  239. ctx.Handle(404, "GetDiffRange", err)
  240. return
  241. }
  242. isImageFile := func(name string) bool {
  243. blob, err := commit.GetBlobByPath(name)
  244. if err != nil {
  245. return false
  246. }
  247. dataRc, err := blob.Data()
  248. if err != nil {
  249. return false
  250. }
  251. buf := make([]byte, 1024)
  252. n, _ := dataRc.Read(buf)
  253. if n > 0 {
  254. buf = buf[:n]
  255. }
  256. _, isImage := base.IsImageFile(buf)
  257. return isImage
  258. }
  259. commits, err := commit.CommitsBeforeUntil(beforeCommitId)
  260. if err != nil {
  261. ctx.Handle(500, "CommitsBeforeUntil", err)
  262. return
  263. }
  264. commits = models.ValidateCommitsWithEmails(commits)
  265. ctx.Data["Commits"] = commits
  266. ctx.Data["CommitCount"] = commits.Len()
  267. ctx.Data["BeforeCommitId"] = beforeCommitId
  268. ctx.Data["AfterCommitId"] = afterCommitId
  269. ctx.Data["Username"] = userName
  270. ctx.Data["Reponame"] = repoName
  271. ctx.Data["IsImageFile"] = isImageFile
  272. ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitId) + "..." + base.ShortSha(afterCommitId) + " · " + userName + "/" + repoName
  273. ctx.Data["Commit"] = commit
  274. ctx.Data["Diff"] = diff
  275. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  276. ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", afterCommitId)
  277. ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "raw", afterCommitId)
  278. ctx.HTML(200, DIFF)
  279. }