commit.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. // "path"
  7. // "github.com/Unknwon/com"
  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. // const (
  14. // COMMITS base.TplName = "repo/commits"
  15. // DIFF base.TplName = "repo/diff"
  16. // )
  17. // func Commits(ctx *middleware.Context, params martini.Params) {
  18. // ctx.Data["IsRepoToolbarCommits"] = true
  19. // userName := ctx.Repo.Owner.Name
  20. // repoName := ctx.Repo.Repository.Name
  21. // brs, err := ctx.Repo.GitRepo.GetBranches()
  22. // if err != nil {
  23. // ctx.Handle(500, "repo.Commits(GetBranches)", err)
  24. // return
  25. // } else if len(brs) == 0 {
  26. // ctx.Handle(404, "repo.Commits(GetBranches)", nil)
  27. // return
  28. // }
  29. // commitsCount, err := ctx.Repo.Commit.CommitsCount()
  30. // if err != nil {
  31. // ctx.Handle(500, "repo.Commits(GetCommitsCount)", err)
  32. // return
  33. // }
  34. // // Calculate and validate page number.
  35. // page, _ := com.StrTo(ctx.Query("p")).Int()
  36. // if page < 1 {
  37. // page = 1
  38. // }
  39. // lastPage := page - 1
  40. // if lastPage < 0 {
  41. // lastPage = 0
  42. // }
  43. // nextPage := page + 1
  44. // if nextPage*50 > commitsCount {
  45. // nextPage = 0
  46. // }
  47. // // Both `git log branchName` and `git log commitId` work.
  48. // // ctx.Data["Commits"], err = ctx.Repo.Commit.CommitsByRange(page)
  49. // // if err != nil {
  50. // // ctx.Handle(500, "repo.Commits(CommitsByRange)", err)
  51. // // return
  52. // // }
  53. // ctx.Data["Username"] = userName
  54. // ctx.Data["Reponame"] = repoName
  55. // ctx.Data["CommitCount"] = commitsCount
  56. // ctx.Data["LastPageNum"] = lastPage
  57. // ctx.Data["NextPageNum"] = nextPage
  58. // ctx.HTML(200, COMMITS)
  59. // }
  60. // func SearchCommits(ctx *middleware.Context, params martini.Params) {
  61. // ctx.Data["IsSearchPage"] = true
  62. // ctx.Data["IsRepoToolbarCommits"] = true
  63. // keyword := ctx.Query("q")
  64. // if len(keyword) == 0 {
  65. // ctx.Redirect(ctx.Repo.RepoLink + "/commits/" + ctx.Repo.BranchName)
  66. // return
  67. // }
  68. // userName := params["username"]
  69. // repoName := params["reponame"]
  70. // brs, err := ctx.Repo.GitRepo.GetBranches()
  71. // if err != nil {
  72. // ctx.Handle(500, "repo.SearchCommits(GetBranches)", err)
  73. // return
  74. // } else if len(brs) == 0 {
  75. // ctx.Handle(404, "repo.SearchCommits(GetBranches)", nil)
  76. // return
  77. // }
  78. // // commits, err := ctx.Repo.Commit.SearchCommits(keyword)
  79. // // if err != nil {
  80. // // ctx.Handle(500, "repo.SearchCommits(SearchCommits)", err)
  81. // // return
  82. // // }
  83. // ctx.Data["Keyword"] = keyword
  84. // ctx.Data["Username"] = userName
  85. // ctx.Data["Reponame"] = repoName
  86. // // ctx.Data["CommitCount"] = commits.Len()
  87. // // ctx.Data["Commits"] = commits
  88. // ctx.HTML(200, COMMITS)
  89. // }
  90. // func Diff(ctx *middleware.Context, params martini.Params) {
  91. // ctx.Data["IsRepoToolbarCommits"] = true
  92. // userName := ctx.Repo.Owner.Name
  93. // repoName := ctx.Repo.Repository.Name
  94. // commitId := ctx.Repo.CommitId
  95. // commit := ctx.Repo.Commit
  96. // diff, err := models.GetDiff(models.RepoPath(userName, repoName), commitId)
  97. // if err != nil {
  98. // ctx.Handle(404, "repo.Diff(GetDiff)", err)
  99. // return
  100. // }
  101. // isImageFile := func(name string) bool {
  102. // // blob, err := ctx.Repo.Commit.GetBlobByPath(name)
  103. // // if err != nil {
  104. // // return false
  105. // // }
  106. // // dataRc, err := blob.Data()
  107. // // if err != nil {
  108. // // return false
  109. // // }
  110. // // buf := make([]byte, 1024)
  111. // // n, _ := dataRc.Read(buf)
  112. // // if n > 0 {
  113. // // buf = buf[:n]
  114. // // }
  115. // // dataRc.Close()
  116. // // _, isImage := base.IsImageFile(buf)
  117. // // return isImage
  118. // return false
  119. // }
  120. // parents := make([]string, commit.ParentCount())
  121. // for i := 0; i < commit.ParentCount(); i++ {
  122. // sha, err := commit.ParentId(i)
  123. // parents[i] = sha.String()
  124. // if err != nil {
  125. // ctx.Handle(404, "repo.Diff", err)
  126. // return
  127. // }
  128. // }
  129. // ctx.Data["Username"] = userName
  130. // ctx.Data["Reponame"] = repoName
  131. // ctx.Data["IsImageFile"] = isImageFile
  132. // ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitId)
  133. // ctx.Data["Commit"] = commit
  134. // ctx.Data["Diff"] = diff
  135. // ctx.Data["Parents"] = parents
  136. // ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  137. // ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId)
  138. // ctx.Data["RawPath"] = "/" + path.Join(userName, repoName, "raw", commitId)
  139. // ctx.HTML(200, DIFF)
  140. // }
  141. // func FileHistory(ctx *middleware.Context, params martini.Params) {
  142. // ctx.Data["IsRepoToolbarCommits"] = true
  143. // fileName := params["_1"]
  144. // if len(fileName) == 0 {
  145. // Commits(ctx, params)
  146. // return
  147. // }
  148. // userName := ctx.Repo.Owner.Name
  149. // repoName := ctx.Repo.Repository.Name
  150. // branchName := params["branchname"]
  151. // brs, err := ctx.Repo.GitRepo.GetBranches()
  152. // if err != nil {
  153. // ctx.Handle(500, "repo.FileHistory", err)
  154. // return
  155. // } else if len(brs) == 0 {
  156. // ctx.Handle(404, "repo.FileHistory", nil)
  157. // return
  158. // }
  159. // // commitsCount, err := ctx.Repo.GitRepo.FileCommitsCount(branchName, fileName)
  160. // // if err != nil {
  161. // // ctx.Handle(500, "repo.FileHistory(GetCommitsCount)", err)
  162. // // return
  163. // // } else if commitsCount == 0 {
  164. // // ctx.Handle(404, "repo.FileHistory", nil)
  165. // // return
  166. // // }
  167. // // Calculate and validate page number.
  168. // // page, _ := base.StrTo(ctx.Query("p")).Int()
  169. // // if page < 1 {
  170. // // page = 1
  171. // // }
  172. // // lastPage := page - 1
  173. // // if lastPage < 0 {
  174. // // lastPage = 0
  175. // // }
  176. // // nextPage := page + 1
  177. // // if nextPage*50 > commitsCount {
  178. // // nextPage = 0
  179. // // }
  180. // // ctx.Data["Commits"], err = ctx.Repo.GitRepo.CommitsByFileAndRange(
  181. // // branchName, fileName, page)
  182. // // if err != nil {
  183. // // ctx.Handle(500, "repo.FileHistory(CommitsByRange)", err)
  184. // // return
  185. // // }
  186. // ctx.Data["Username"] = userName
  187. // ctx.Data["Reponame"] = repoName
  188. // ctx.Data["FileName"] = fileName
  189. // // ctx.Data["CommitCount"] = commitsCount
  190. // // ctx.Data["LastPageNum"] = lastPage
  191. // // ctx.Data["NextPageNum"] = nextPage
  192. // ctx.HTML(200, COMMITS)
  193. // }