commit.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. gocontext "context"
  7. "path"
  8. "time"
  9. "github.com/gogs/git-module"
  10. "gogs.io/gogs/internal/conf"
  11. "gogs.io/gogs/internal/context"
  12. "gogs.io/gogs/internal/db"
  13. "gogs.io/gogs/internal/gitutil"
  14. "gogs.io/gogs/internal/tool"
  15. )
  16. const (
  17. COMMITS = "repo/commits"
  18. DIFF = "repo/diff/page"
  19. )
  20. func RefCommits(c *context.Context) {
  21. c.Data["PageIsViewFiles"] = true
  22. switch {
  23. case c.Repo.TreePath == "":
  24. Commits(c)
  25. case c.Repo.TreePath == "search":
  26. SearchCommits(c)
  27. default:
  28. FileHistory(c)
  29. }
  30. }
  31. // TODO(unknwon)
  32. func RenderIssueLinks(oldCommits []*git.Commit, _ string) []*git.Commit {
  33. return oldCommits
  34. }
  35. func renderCommits(c *context.Context, filename string) {
  36. c.Data["Title"] = c.Tr("repo.commits.commit_history") + " · " + c.Repo.Repository.FullName()
  37. c.Data["PageIsCommits"] = true
  38. c.Data["FileName"] = filename
  39. page := c.QueryInt("page")
  40. if page < 1 {
  41. page = 1
  42. }
  43. pageSize := c.QueryInt("pageSize")
  44. if pageSize < 1 {
  45. pageSize = conf.UI.User.CommitsPagingNum
  46. }
  47. commits, err := c.Repo.Commit.CommitsByPage(page, pageSize, git.CommitsByPageOptions{Path: filename})
  48. if err != nil {
  49. c.Error(err, "paging commits")
  50. return
  51. }
  52. commits = RenderIssueLinks(commits, c.Repo.RepoLink)
  53. c.Data["Commits"] = matchUsersWithCommitEmails(c.Req.Context(), commits)
  54. if page > 1 {
  55. c.Data["HasPrevious"] = true
  56. c.Data["PreviousPage"] = page - 1
  57. }
  58. if len(commits) == pageSize {
  59. c.Data["HasNext"] = true
  60. c.Data["NextPage"] = page + 1
  61. }
  62. c.Data["PageSize"] = pageSize
  63. c.Data["Username"] = c.Repo.Owner.Name
  64. c.Data["Reponame"] = c.Repo.Repository.Name
  65. c.Success(COMMITS)
  66. }
  67. func Commits(c *context.Context) {
  68. renderCommits(c, "")
  69. }
  70. func SearchCommits(c *context.Context) {
  71. c.Data["PageIsCommits"] = true
  72. keyword := c.Query("q")
  73. if keyword == "" {
  74. c.Redirect(c.Repo.RepoLink + "/commits/" + c.Repo.BranchName)
  75. return
  76. }
  77. commits, err := c.Repo.Commit.SearchCommits(keyword)
  78. if err != nil {
  79. c.Error(err, "search commits")
  80. return
  81. }
  82. commits = RenderIssueLinks(commits, c.Repo.RepoLink)
  83. c.Data["Commits"] = matchUsersWithCommitEmails(c.Req.Context(), commits)
  84. c.Data["Keyword"] = keyword
  85. c.Data["Username"] = c.Repo.Owner.Name
  86. c.Data["Reponame"] = c.Repo.Repository.Name
  87. c.Data["Branch"] = c.Repo.BranchName
  88. c.Success(COMMITS)
  89. }
  90. func FileHistory(c *context.Context) {
  91. renderCommits(c, c.Repo.TreePath)
  92. }
  93. // tryGetUserByEmail returns a non-nil value if the email is corresponding to an
  94. // existing user.
  95. func tryGetUserByEmail(ctx gocontext.Context, email string) *db.User {
  96. user, _ := db.Users.GetByEmail(ctx, email)
  97. return user
  98. }
  99. func Diff(c *context.Context) {
  100. c.PageIs("Diff")
  101. c.RequireHighlightJS()
  102. userName := c.Repo.Owner.Name
  103. repoName := c.Repo.Repository.Name
  104. commitID := c.Params(":sha")
  105. commit, err := c.Repo.GitRepo.CatFileCommit(commitID)
  106. if err != nil {
  107. c.NotFoundOrError(gitutil.NewError(err), "get commit by ID")
  108. return
  109. }
  110. diff, err := gitutil.RepoDiff(c.Repo.GitRepo,
  111. commitID, conf.Git.MaxDiffFiles, conf.Git.MaxDiffLines, conf.Git.MaxDiffLineChars,
  112. git.DiffOptions{Timeout: time.Duration(conf.Git.Timeout.Diff) * time.Second},
  113. )
  114. if err != nil {
  115. c.NotFoundOrError(gitutil.NewError(err), "get diff")
  116. return
  117. }
  118. parents := make([]string, commit.ParentsCount())
  119. for i := 0; i < commit.ParentsCount(); i++ {
  120. sha, err := commit.ParentID(i)
  121. if err != nil {
  122. c.NotFound()
  123. return
  124. }
  125. parents[i] = sha.String()
  126. }
  127. setEditorconfigIfExists(c)
  128. if c.Written() {
  129. return
  130. }
  131. c.RawTitle(commit.Summary() + " · " + tool.ShortSHA1(commitID))
  132. c.Data["CommitID"] = commitID
  133. c.Data["IsSplitStyle"] = c.Query("style") == "split"
  134. c.Data["Username"] = userName
  135. c.Data["Reponame"] = repoName
  136. c.Data["IsImageFile"] = commit.IsImageFile
  137. c.Data["IsImageFileByIndex"] = commit.IsImageFileByIndex
  138. c.Data["Commit"] = commit
  139. c.Data["Author"] = tryGetUserByEmail(c.Req.Context(), commit.Author.Email)
  140. c.Data["Diff"] = diff
  141. c.Data["Parents"] = parents
  142. c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  143. c.Data["SourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", commitID)
  144. c.Data["RawPath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "raw", commitID)
  145. if commit.ParentsCount() > 0 {
  146. c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", parents[0])
  147. c.Data["BeforeRawPath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "raw", parents[0])
  148. }
  149. c.Success(DIFF)
  150. }
  151. func RawDiff(c *context.Context) {
  152. if err := c.Repo.GitRepo.RawDiff(
  153. c.Params(":sha"),
  154. git.RawDiffFormat(c.Params(":ext")),
  155. c.Resp,
  156. ); err != nil {
  157. c.NotFoundOrError(gitutil.NewError(err), "get raw diff")
  158. return
  159. }
  160. }
  161. type userCommit struct {
  162. User *db.User
  163. *git.Commit
  164. }
  165. // matchUsersWithCommitEmails matches existing users using commit author emails.
  166. func matchUsersWithCommitEmails(ctx gocontext.Context, oldCommits []*git.Commit) []*userCommit {
  167. emailToUsers := make(map[string]*db.User)
  168. newCommits := make([]*userCommit, len(oldCommits))
  169. for i := range oldCommits {
  170. var u *db.User
  171. if v, ok := emailToUsers[oldCommits[i].Author.Email]; !ok {
  172. u, _ = db.Users.GetByEmail(ctx, oldCommits[i].Author.Email)
  173. emailToUsers[oldCommits[i].Author.Email] = u
  174. } else {
  175. u = v
  176. }
  177. newCommits[i] = &userCommit{
  178. User: u,
  179. Commit: oldCommits[i],
  180. }
  181. }
  182. return newCommits
  183. }
  184. func CompareDiff(c *context.Context) {
  185. c.Data["IsDiffCompare"] = true
  186. userName := c.Repo.Owner.Name
  187. repoName := c.Repo.Repository.Name
  188. beforeCommitID := c.Params(":before")
  189. afterCommitID := c.Params(":after")
  190. commit, err := c.Repo.GitRepo.CatFileCommit(afterCommitID)
  191. if err != nil {
  192. c.NotFoundOrError(gitutil.NewError(err), "get head commit")
  193. return
  194. }
  195. diff, err := gitutil.RepoDiff(c.Repo.GitRepo,
  196. afterCommitID, conf.Git.MaxDiffFiles, conf.Git.MaxDiffLines, conf.Git.MaxDiffLineChars,
  197. git.DiffOptions{Base: beforeCommitID, Timeout: time.Duration(conf.Git.Timeout.Diff) * time.Second},
  198. )
  199. if err != nil {
  200. c.NotFoundOrError(gitutil.NewError(err), "get diff")
  201. return
  202. }
  203. commits, err := commit.CommitsAfter(beforeCommitID)
  204. if err != nil {
  205. c.NotFoundOrError(gitutil.NewError(err), "get commits after")
  206. return
  207. }
  208. c.Data["IsSplitStyle"] = c.Query("style") == "split"
  209. c.Data["CommitRepoLink"] = c.Repo.RepoLink
  210. c.Data["Commits"] = matchUsersWithCommitEmails(c.Req.Context(), commits)
  211. c.Data["CommitsCount"] = len(commits)
  212. c.Data["BeforeCommitID"] = beforeCommitID
  213. c.Data["AfterCommitID"] = afterCommitID
  214. c.Data["Username"] = userName
  215. c.Data["Reponame"] = repoName
  216. c.Data["IsImageFile"] = commit.IsImageFile
  217. c.Data["IsImageFileByIndex"] = commit.IsImageFileByIndex
  218. c.Data["Title"] = "Comparing " + tool.ShortSHA1(beforeCommitID) + "..." + tool.ShortSHA1(afterCommitID) + " · " + userName + "/" + repoName
  219. c.Data["Commit"] = commit
  220. c.Data["Diff"] = diff
  221. c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  222. c.Data["SourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", afterCommitID)
  223. c.Data["RawPath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "raw", afterCommitID)
  224. c.Data["BeforeSourcePath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "src", beforeCommitID)
  225. c.Data["BeforeRawPath"] = conf.Server.Subpath + "/" + path.Join(userName, repoName, "raw", beforeCommitID)
  226. c.Success(DIFF)
  227. }