You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

compare.go 11 kB

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