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 12 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
Adds side-by-side diff for images (#6784) * Adds side-by-side diff for images Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Explain blank imports Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use complete word for width and height labels on image compare Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Update index.css from master Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Moves ImageInfo to git commit file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Assign ImageInfo function for template and sets correct target for BeforeSourcePath Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adds missing comment Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Return error if ImageInfo failed Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Avoid template panic when ImageInfo failed for some reason Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show file size on image diff Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes unused helper function Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Reverts copyright year change Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Close file reader Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Update commit.go Sets correct data key * Moves reader.Close() up a few lines * Updates index.css * Updates CSS file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Transfers adjustments for image compare to compare.go file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adjusts variable name Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Apply lesshint recommendations Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Do not show old image on image compare if it is not in index of base commit Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Change file size text Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com>
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
Adds side-by-side diff for images (#6784) * Adds side-by-side diff for images Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Explain blank imports Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use complete word for width and height labels on image compare Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Update index.css from master Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Moves ImageInfo to git commit file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Assign ImageInfo function for template and sets correct target for BeforeSourcePath Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adds missing comment Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Return error if ImageInfo failed Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Avoid template panic when ImageInfo failed for some reason Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show file size on image diff Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes unused helper function Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Reverts copyright year change Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Close file reader Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Update commit.go Sets correct data key * Moves reader.Close() up a few lines * Updates index.css * Updates CSS file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Transfers adjustments for image compare to compare.go file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adjusts variable name Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Apply lesshint recommendations Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Do not show old image on image compare if it is not in index of base commit Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Change file size text Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com>
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
Adds side-by-side diff for images (#6784) * Adds side-by-side diff for images Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Explain blank imports Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use complete word for width and height labels on image compare Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Update index.css from master Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Moves ImageInfo to git commit file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Assign ImageInfo function for template and sets correct target for BeforeSourcePath Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adds missing comment Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Return error if ImageInfo failed Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Avoid template panic when ImageInfo failed for some reason Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show file size on image diff Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes unused helper function Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Reverts copyright year change Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Close file reader Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Update commit.go Sets correct data key * Moves reader.Close() up a few lines * Updates index.css * Updates CSS file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Transfers adjustments for image compare to compare.go file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adjusts variable name Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Apply lesshint recommendations Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Do not show old image on image compare if it is not in index of base commit Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Change file size text Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com>
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
Adds side-by-side diff for images (#6784) * Adds side-by-side diff for images Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Explain blank imports Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Use complete word for width and height labels on image compare Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Update index.css from master Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Moves ImageInfo to git commit file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Assign ImageInfo function for template and sets correct target for BeforeSourcePath Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adds missing comment Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Return error if ImageInfo failed Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Avoid template panic when ImageInfo failed for some reason Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show file size on image diff Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes unused helper function Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Reverts copyright year change Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Close file reader Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Update commit.go Sets correct data key * Moves reader.Close() up a few lines * Updates index.css * Updates CSS file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Transfers adjustments for image compare to compare.go file Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adjusts variable name Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Apply lesshint recommendations Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Do not show old image on image compare if it is not in index of base commit Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Change file size text Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com>
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. baseGitRepo := ctx.Repo.GitRepo
  222. baseCommitID := baseBranch
  223. if ctx.Data["BaseIsCommit"] == false {
  224. if ctx.Data["BaseIsTag"] == true {
  225. baseCommitID, err = baseGitRepo.GetTagCommitID(baseBranch)
  226. } else {
  227. baseCommitID, err = baseGitRepo.GetBranchCommitID(baseBranch)
  228. }
  229. if err != nil {
  230. ctx.ServerError("GetRefCommitID", err)
  231. return false
  232. }
  233. }
  234. baseCommit, err := baseGitRepo.GetCommit(baseCommitID)
  235. if err != nil {
  236. ctx.ServerError("GetCommit", err)
  237. return false
  238. }
  239. compareInfo.Commits = models.ValidateCommitsWithEmails(compareInfo.Commits)
  240. compareInfo.Commits = models.ParseCommitsWithSignature(compareInfo.Commits)
  241. compareInfo.Commits = models.ParseCommitsWithStatus(compareInfo.Commits, headRepo)
  242. ctx.Data["Commits"] = compareInfo.Commits
  243. ctx.Data["CommitCount"] = compareInfo.Commits.Len()
  244. if ctx.Data["CommitCount"] == 0 {
  245. ctx.Data["PageIsComparePull"] = false
  246. }
  247. if compareInfo.Commits.Len() == 1 {
  248. c := compareInfo.Commits.Front().Value.(models.SignCommitWithStatuses)
  249. title = strings.TrimSpace(c.UserCommit.Summary())
  250. body := strings.Split(strings.TrimSpace(c.UserCommit.Message()), "\n")
  251. if len(body) > 1 {
  252. ctx.Data["content"] = strings.Join(body[1:], "\n")
  253. }
  254. } else {
  255. title = headBranch
  256. }
  257. ctx.Data["title"] = title
  258. ctx.Data["Username"] = headUser.Name
  259. ctx.Data["Reponame"] = headRepo.Name
  260. ctx.Data["IsImageFile"] = headCommit.IsImageFile
  261. ctx.Data["ImageInfo"] = func(name string) *git.ImageMetaData {
  262. result, err := headCommit.ImageInfo(name)
  263. if err != nil {
  264. log.Error("ImageInfo failed: %v", err)
  265. return nil
  266. }
  267. return result
  268. }
  269. ctx.Data["FileExistsInBaseCommit"] = func(filename string) bool {
  270. result, err := baseCommit.HasFile(filename)
  271. if err != nil {
  272. log.Error(
  273. "Error while checking if file \"%s\" exists in base commit \"%s\" (repo: %s): %v",
  274. filename,
  275. baseCommit,
  276. baseGitRepo.Path,
  277. err)
  278. return false
  279. }
  280. return result
  281. }
  282. ctx.Data["ImageInfoBase"] = func(name string) *git.ImageMetaData {
  283. result, err := baseCommit.ImageInfo(name)
  284. if err != nil {
  285. log.Error("ImageInfo failed: %v", err)
  286. return nil
  287. }
  288. return result
  289. }
  290. headTarget := path.Join(headUser.Name, repo.Name)
  291. baseTarget := path.Join(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  292. ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", "commit", headCommitID)
  293. ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", "commit", headCommitID)
  294. ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(baseTarget, "src", "commit", baseCommitID)
  295. ctx.Data["BeforeRawPath"] = setting.AppSubURL + "/" + path.Join(baseTarget, "raw", "commit", baseCommitID)
  296. return false
  297. }
  298. // CompareDiff show different from one commit to another commit
  299. func CompareDiff(ctx *context.Context) {
  300. headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
  301. if ctx.Written() {
  302. return
  303. }
  304. nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch)
  305. if ctx.Written() {
  306. return
  307. }
  308. if ctx.Data["PageIsComparePull"] == true {
  309. headBranches, err := headGitRepo.GetBranches()
  310. if err != nil {
  311. ctx.ServerError("GetBranches", err)
  312. return
  313. }
  314. ctx.Data["HeadBranches"] = headBranches
  315. pr, err := models.GetUnmergedPullRequest(headRepo.ID, ctx.Repo.Repository.ID, headBranch, baseBranch)
  316. if err != nil {
  317. if !models.IsErrPullRequestNotExist(err) {
  318. ctx.ServerError("GetUnmergedPullRequest", err)
  319. return
  320. }
  321. } else {
  322. ctx.Data["HasPullRequest"] = true
  323. ctx.Data["PullRequest"] = pr
  324. ctx.HTML(200, tplCompareDiff)
  325. return
  326. }
  327. if !nothingToCompare {
  328. // Setup information for new form.
  329. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  330. if ctx.Written() {
  331. return
  332. }
  333. }
  334. }
  335. beforeCommitID := ctx.Data["BeforeCommitID"].(string)
  336. afterCommitID := ctx.Data["AfterCommitID"].(string)
  337. ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitID) + "..." + base.ShortSha(afterCommitID)
  338. ctx.Data["IsRepoToolbarCommits"] = true
  339. ctx.Data["IsDiffCompare"] = true
  340. ctx.Data["RequireHighlightJS"] = true
  341. ctx.Data["RequireTribute"] = true
  342. ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
  343. setTemplateIfExists(ctx, pullRequestTemplateKey, pullRequestTemplateCandidates)
  344. renderAttachmentSettings(ctx)
  345. ctx.HTML(200, tplCompare)
  346. }