選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

compare.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // Copyright 2019 The Gitea 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. "strings"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. )
  15. const (
  16. tplCompare base.TplName = "repo/diff/compare"
  17. )
  18. // ParseCompareInfo parse compare info between two commit for preparing comparing references
  19. func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *git.Repository, *git.CompareInfo, string, string) {
  20. baseRepo := ctx.Repo.Repository
  21. // Get compared branches information
  22. // format: <base branch>...[<head repo>:]<head branch>
  23. // base<-head: master...head:feature
  24. // same repo: master...feature
  25. var (
  26. headUser *models.User
  27. headBranch string
  28. isSameRepo bool
  29. infoPath string
  30. err error
  31. )
  32. infoPath = ctx.Params("*")
  33. infos := strings.Split(infoPath, "...")
  34. if len(infos) != 2 {
  35. log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)
  36. ctx.NotFound("CompareAndPullRequest", nil)
  37. return nil, nil, nil, nil, "", ""
  38. }
  39. baseBranch := infos[0]
  40. ctx.Data["BaseBranch"] = baseBranch
  41. // If there is no head repository, it means compare between same repository.
  42. headInfos := strings.Split(infos[1], ":")
  43. if len(headInfos) == 1 {
  44. isSameRepo = true
  45. headUser = ctx.Repo.Owner
  46. headBranch = headInfos[0]
  47. } else if len(headInfos) == 2 {
  48. headUser, err = models.GetUserByName(headInfos[0])
  49. if err != nil {
  50. if models.IsErrUserNotExist(err) {
  51. ctx.NotFound("GetUserByName", nil)
  52. } else {
  53. ctx.ServerError("GetUserByName", err)
  54. }
  55. return nil, nil, nil, nil, "", ""
  56. }
  57. headBranch = headInfos[1]
  58. isSameRepo = headUser.ID == ctx.Repo.Owner.ID
  59. } else {
  60. ctx.NotFound("CompareAndPullRequest", nil)
  61. return nil, nil, nil, nil, "", ""
  62. }
  63. ctx.Data["HeadUser"] = headUser
  64. ctx.Data["HeadBranch"] = headBranch
  65. ctx.Repo.PullRequest.SameRepo = isSameRepo
  66. // Check if base branch is valid.
  67. baseIsCommit := ctx.Repo.GitRepo.IsCommitExist(baseBranch)
  68. baseIsBranch := ctx.Repo.GitRepo.IsBranchExist(baseBranch)
  69. baseIsTag := ctx.Repo.GitRepo.IsTagExist(baseBranch)
  70. if !baseIsCommit && !baseIsBranch && !baseIsTag {
  71. // Check if baseBranch is short sha commit hash
  72. if baseCommit, _ := ctx.Repo.GitRepo.GetCommit(baseBranch); baseCommit != nil {
  73. baseBranch = baseCommit.ID.String()
  74. ctx.Data["BaseBranch"] = baseBranch
  75. baseIsCommit = true
  76. } else {
  77. ctx.NotFound("IsRefExist", nil)
  78. return nil, nil, nil, nil, "", ""
  79. }
  80. }
  81. ctx.Data["BaseIsCommit"] = baseIsCommit
  82. ctx.Data["BaseIsBranch"] = baseIsBranch
  83. ctx.Data["BaseIsTag"] = baseIsTag
  84. // Check if current user has fork of repository or in the same repository.
  85. headRepo, has := models.HasForkedRepo(headUser.ID, baseRepo.ID)
  86. if !has && !isSameRepo {
  87. ctx.Data["PageIsComparePull"] = false
  88. }
  89. var headGitRepo *git.Repository
  90. if isSameRepo {
  91. headRepo = ctx.Repo.Repository
  92. headGitRepo = ctx.Repo.GitRepo
  93. ctx.Data["BaseName"] = headUser.Name
  94. } else {
  95. headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
  96. ctx.Data["BaseName"] = baseRepo.OwnerName
  97. if err != nil {
  98. ctx.ServerError("OpenRepository", err)
  99. return nil, nil, nil, nil, "", ""
  100. }
  101. }
  102. // user should have permission to read baseRepo's codes and pulls, NOT headRepo's
  103. permBase, err := models.GetUserRepoPermission(baseRepo, ctx.User)
  104. if err != nil {
  105. ctx.ServerError("GetUserRepoPermission", err)
  106. return nil, nil, nil, nil, "", ""
  107. }
  108. if !permBase.CanRead(models.UnitTypeCode) {
  109. if log.IsTrace() {
  110. log.Trace("Permission Denied: User: %-v cannot read code in Repo: %-v\nUser in baseRepo has Permissions: %-+v",
  111. ctx.User,
  112. baseRepo,
  113. permBase)
  114. }
  115. ctx.NotFound("ParseCompareInfo", nil)
  116. return nil, nil, nil, nil, "", ""
  117. }
  118. // user should have permission to read headrepo's codes
  119. permHead, err := models.GetUserRepoPermission(headRepo, ctx.User)
  120. if err != nil {
  121. ctx.ServerError("GetUserRepoPermission", err)
  122. return nil, nil, nil, nil, "", ""
  123. }
  124. if !permHead.CanRead(models.UnitTypeCode) {
  125. if log.IsTrace() {
  126. log.Trace("Permission Denied: User: %-v cannot read code in Repo: %-v\nUser in headRepo has Permissions: %-+v",
  127. ctx.User,
  128. headRepo,
  129. permHead)
  130. }
  131. ctx.NotFound("ParseCompareInfo", nil)
  132. return nil, nil, nil, nil, "", ""
  133. }
  134. // Check if head branch is valid.
  135. headIsCommit := ctx.Repo.GitRepo.IsCommitExist(headBranch)
  136. headIsBranch := headGitRepo.IsBranchExist(headBranch)
  137. headIsTag := headGitRepo.IsTagExist(headBranch)
  138. if !headIsCommit && !headIsBranch && !headIsTag {
  139. // Check if headBranch is short sha commit hash
  140. if headCommit, _ := ctx.Repo.GitRepo.GetCommit(headBranch); headCommit != nil {
  141. headBranch = headCommit.ID.String()
  142. ctx.Data["HeadBranch"] = headBranch
  143. headIsCommit = true
  144. } else {
  145. ctx.NotFound("IsRefExist", nil)
  146. return nil, nil, nil, nil, "", ""
  147. }
  148. }
  149. ctx.Data["HeadIsCommit"] = headIsCommit
  150. ctx.Data["HeadIsBranch"] = headIsBranch
  151. ctx.Data["HeadIsTag"] = headIsTag
  152. // Treat as pull request if both references are branches
  153. if ctx.Data["PageIsComparePull"] == nil {
  154. ctx.Data["PageIsComparePull"] = headIsBranch && baseIsBranch
  155. }
  156. if ctx.Data["PageIsComparePull"] == true && !permBase.CanReadIssuesOrPulls(true) {
  157. if log.IsTrace() {
  158. log.Trace("Permission Denied: User: %-v cannot create/read pull requests in Repo: %-v\nUser in baseRepo has Permissions: %-+v",
  159. ctx.User,
  160. baseRepo,
  161. permBase)
  162. }
  163. ctx.NotFound("ParseCompareInfo", nil)
  164. return nil, nil, nil, nil, "", ""
  165. }
  166. compareInfo, err := headGitRepo.GetCompareInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
  167. if err != nil {
  168. ctx.ServerError("GetCompareInfo", err)
  169. return nil, nil, nil, nil, "", ""
  170. }
  171. ctx.Data["BeforeCommitID"] = compareInfo.MergeBase
  172. return headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch
  173. }
  174. // PrepareCompareDiff renders compare diff page
  175. func PrepareCompareDiff(
  176. ctx *context.Context,
  177. headUser *models.User,
  178. headRepo *models.Repository,
  179. headGitRepo *git.Repository,
  180. compareInfo *git.CompareInfo,
  181. baseBranch, headBranch string) bool {
  182. var (
  183. repo = ctx.Repo.Repository
  184. err error
  185. title string
  186. )
  187. // Get diff information.
  188. ctx.Data["CommitRepoLink"] = headRepo.Link()
  189. headCommitID := headBranch
  190. if ctx.Data["HeadIsCommit"] == false {
  191. if ctx.Data["HeadIsTag"] == true {
  192. headCommitID, err = headGitRepo.GetTagCommitID(headBranch)
  193. } else {
  194. headCommitID, err = headGitRepo.GetBranchCommitID(headBranch)
  195. }
  196. if err != nil {
  197. ctx.ServerError("GetRefCommitID", err)
  198. return false
  199. }
  200. }
  201. ctx.Data["AfterCommitID"] = headCommitID
  202. if headCommitID == compareInfo.MergeBase {
  203. ctx.Data["IsNothingToCompare"] = true
  204. return true
  205. }
  206. diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
  207. compareInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
  208. setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  209. if err != nil {
  210. ctx.ServerError("GetDiffRange", err)
  211. return false
  212. }
  213. ctx.Data["Diff"] = diff
  214. ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
  215. headCommit, err := headGitRepo.GetCommit(headCommitID)
  216. if err != nil {
  217. ctx.ServerError("GetCommit", err)
  218. return false
  219. }
  220. compareInfo.Commits = models.ValidateCommitsWithEmails(compareInfo.Commits)
  221. compareInfo.Commits = models.ParseCommitsWithSignature(compareInfo.Commits)
  222. compareInfo.Commits = models.ParseCommitsWithStatus(compareInfo.Commits, headRepo)
  223. ctx.Data["Commits"] = compareInfo.Commits
  224. ctx.Data["CommitCount"] = compareInfo.Commits.Len()
  225. if ctx.Data["CommitCount"] == 0 {
  226. ctx.Data["PageIsComparePull"] = false
  227. }
  228. if compareInfo.Commits.Len() == 1 {
  229. c := compareInfo.Commits.Front().Value.(models.SignCommitWithStatuses)
  230. title = strings.TrimSpace(c.UserCommit.Summary())
  231. body := strings.Split(strings.TrimSpace(c.UserCommit.Message()), "\n")
  232. if len(body) > 1 {
  233. ctx.Data["content"] = strings.Join(body[1:], "\n")
  234. }
  235. } else {
  236. title = headBranch
  237. }
  238. ctx.Data["title"] = title
  239. ctx.Data["Username"] = headUser.Name
  240. ctx.Data["Reponame"] = headRepo.Name
  241. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  242. headTarget := path.Join(headUser.Name, repo.Name)
  243. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", headCommitID)
  244. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", compareInfo.MergeBase)
  245. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", "commit", headCommitID)
  246. return false
  247. }
  248. // CompareDiff show different from one commit to another commit
  249. func CompareDiff(ctx *context.Context) {
  250. headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  251. if ctx.Written() {
  252. return
  253. }
  254. nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch)
  255. if ctx.Written() {
  256. return
  257. }
  258. if ctx.Data["PageIsComparePull"] == true {
  259. headBranches, err := headGitRepo.GetBranches()
  260. if err != nil {
  261. ctx.ServerError("GetBranches", err)
  262. return
  263. }
  264. ctx.Data["HeadBranches"] = headBranches
  265. pr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
  266. if err != nil {
  267. if !models.IsErrPullRequestNotExist(err) {
  268. ctx.ServerError("GetUnmergedPullRequest", err)
  269. return
  270. }
  271. } else {
  272. ctx.Data["HasPullRequest"] = true
  273. ctx.Data["PullRequest"] = pr
  274. ctx.HTML(200, tplCompareDiff)
  275. return
  276. }
  277. if !nothingToCompare {
  278. // Setup information for new form.
  279. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  280. if ctx.Written() {
  281. return
  282. }
  283. }
  284. }
  285. beforeCommitID := ctx.Data["BeforeCommitID"].(string)
  286. afterCommitID := ctx.Data["AfterCommitID"].(string)
  287. ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitID) + "..." + base.ShortSha(afterCommitID)
  288. ctx.Data["IsRepoToolbarCommits"] = true
  289. ctx.Data["IsDiffCompare"] = true
  290. ctx.Data["RequireHighlightJS"] = true
  291. ctx.Data["RequireTribute"] = true
  292. ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
  293. setTemplateIfExists(ctx, pullRequestTemplateKey, pullRequestTemplateCandidates)
  294. renderAttachmentSettings(ctx)
  295. ctx.HTML(200, tplCompare)
  296. }