1
0

commit.go 7.2 KB

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