commit.go 7.3 KB

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