2019-06-08 05:29:29 +09:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2019-11-15 11:52:59 +09:00
|
|
|
"bufio"
|
2021-03-30 05:44:28 +09:00
|
|
|
"encoding/csv"
|
|
|
|
"errors"
|
2019-10-05 04:58:54 +09:00
|
|
|
"fmt"
|
2019-11-15 11:52:59 +09:00
|
|
|
"html"
|
2021-04-06 00:30:52 +09:00
|
|
|
"net/http"
|
2019-06-08 05:29:29 +09:00
|
|
|
"path"
|
2021-03-30 05:44:28 +09:00
|
|
|
"path/filepath"
|
2019-06-08 05:29:29 +09:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/base"
|
2021-03-30 05:44:28 +09:00
|
|
|
"code.gitea.io/gitea/modules/charset"
|
2019-06-08 05:29:29 +09:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2021-03-30 05:44:28 +09:00
|
|
|
csv_module "code.gitea.io/gitea/modules/csv"
|
2019-06-08 05:29:29 +09:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-10-05 14:49:33 +09:00
|
|
|
"code.gitea.io/gitea/modules/upload"
|
2021-07-25 11:59:27 +09:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-09-06 11:20:09 +09:00
|
|
|
"code.gitea.io/gitea/services/gitdiff"
|
2019-06-08 05:29:29 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-11-15 11:52:59 +09:00
|
|
|
tplCompare base.TplName = "repo/diff/compare"
|
|
|
|
tplBlobExcerpt base.TplName = "repo/diff/blob_excerpt"
|
2019-06-08 05:29:29 +09:00
|
|
|
)
|
|
|
|
|
2021-03-30 05:44:28 +09:00
|
|
|
// setCompareContext sets context data.
|
|
|
|
func setCompareContext(ctx *context.Context, base *git.Commit, head *git.Commit, headTarget string) {
|
|
|
|
ctx.Data["BaseCommit"] = base
|
|
|
|
ctx.Data["HeadCommit"] = head
|
|
|
|
|
2021-06-05 21:32:19 +09:00
|
|
|
ctx.Data["GetBlobByPathForCommit"] = func(commit *git.Commit, path string) *git.Blob {
|
|
|
|
if commit == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
blob, err := commit.GetBlobByPath(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return blob
|
|
|
|
}
|
|
|
|
|
2021-03-30 05:44:28 +09:00
|
|
|
setPathsCompareContext(ctx, base, head, headTarget)
|
2021-06-05 21:32:19 +09:00
|
|
|
setImageCompareContext(ctx)
|
2021-03-30 05:44:28 +09:00
|
|
|
setCsvCompareContext(ctx)
|
|
|
|
}
|
|
|
|
|
2019-10-05 04:58:54 +09:00
|
|
|
// setPathsCompareContext sets context data for source and raw paths
|
|
|
|
func setPathsCompareContext(ctx *context.Context, base *git.Commit, head *git.Commit, headTarget string) {
|
|
|
|
sourcePath := setting.AppSubURL + "/%s/src/commit/%s"
|
|
|
|
rawPath := setting.AppSubURL + "/%s/raw/commit/%s"
|
|
|
|
|
|
|
|
ctx.Data["SourcePath"] = fmt.Sprintf(sourcePath, headTarget, head.ID)
|
|
|
|
ctx.Data["RawPath"] = fmt.Sprintf(rawPath, headTarget, head.ID)
|
|
|
|
if base != nil {
|
|
|
|
baseTarget := path.Join(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
|
|
|
|
ctx.Data["BeforeSourcePath"] = fmt.Sprintf(sourcePath, baseTarget, base.ID)
|
|
|
|
ctx.Data["BeforeRawPath"] = fmt.Sprintf(rawPath, baseTarget, base.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setImageCompareContext sets context data that is required by image compare template
|
2021-06-05 21:32:19 +09:00
|
|
|
func setImageCompareContext(ctx *context.Context) {
|
|
|
|
ctx.Data["IsBlobAnImage"] = func(blob *git.Blob) bool {
|
|
|
|
if blob == nil {
|
|
|
|
return false
|
2019-10-05 04:58:54 +09:00
|
|
|
}
|
2021-06-05 21:32:19 +09:00
|
|
|
|
|
|
|
st, err := blob.GuessContentType()
|
2019-10-05 04:58:54 +09:00
|
|
|
if err != nil {
|
2021-06-05 21:32:19 +09:00
|
|
|
log.Error("GuessContentType failed: %v", err)
|
|
|
|
return false
|
2019-10-05 04:58:54 +09:00
|
|
|
}
|
2021-06-05 21:32:19 +09:00
|
|
|
return st.IsImage() && (setting.UI.SVG.Enabled || !st.IsSvgImage())
|
2019-10-05 04:58:54 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 05:44:28 +09:00
|
|
|
// setCsvCompareContext sets context data that is required by the CSV compare template
|
|
|
|
func setCsvCompareContext(ctx *context.Context) {
|
|
|
|
ctx.Data["IsCsvFile"] = func(diffFile *gitdiff.DiffFile) bool {
|
|
|
|
extension := strings.ToLower(filepath.Ext(diffFile.Name))
|
|
|
|
return extension == ".csv" || extension == ".tsv"
|
|
|
|
}
|
|
|
|
|
|
|
|
type CsvDiffResult struct {
|
|
|
|
Sections []*gitdiff.TableDiffSection
|
|
|
|
Error string
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["CreateCsvDiff"] = func(diffFile *gitdiff.DiffFile, baseCommit *git.Commit, headCommit *git.Commit) CsvDiffResult {
|
|
|
|
if diffFile == nil || baseCommit == nil || headCommit == nil {
|
|
|
|
return CsvDiffResult{nil, ""}
|
|
|
|
}
|
|
|
|
|
|
|
|
errTooLarge := errors.New(ctx.Locale.Tr("repo.error.csv.too_large"))
|
|
|
|
|
|
|
|
csvReaderFromCommit := func(c *git.Commit) (*csv.Reader, error) {
|
|
|
|
blob, err := c.GetBlobByPath(diffFile.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if setting.UI.CSV.MaxFileSize != 0 && setting.UI.CSV.MaxFileSize < blob.Size() {
|
|
|
|
return nil, errTooLarge
|
|
|
|
}
|
|
|
|
|
|
|
|
reader, err := blob.DataAsync()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
2021-04-20 07:25:08 +09:00
|
|
|
return csv_module.CreateReaderAndGuessDelimiter(charset.ToUTF8WithFallbackReader(reader))
|
2021-03-30 05:44:28 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
baseReader, err := csvReaderFromCommit(baseCommit)
|
|
|
|
if err == errTooLarge {
|
|
|
|
return CsvDiffResult{nil, err.Error()}
|
|
|
|
}
|
|
|
|
headReader, err := csvReaderFromCommit(headCommit)
|
|
|
|
if err == errTooLarge {
|
|
|
|
return CsvDiffResult{nil, err.Error()}
|
|
|
|
}
|
|
|
|
|
|
|
|
sections, err := gitdiff.CreateCsvDiff(diffFile, baseReader, headReader)
|
|
|
|
if err != nil {
|
|
|
|
errMessage, err := csv_module.FormatError(err, ctx.Locale)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("RenderCsvDiff failed: %v", err)
|
|
|
|
return CsvDiffResult{nil, ""}
|
|
|
|
}
|
|
|
|
return CsvDiffResult{nil, errMessage}
|
|
|
|
}
|
|
|
|
return CsvDiffResult{sections, ""}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 21:19:34 +09:00
|
|
|
// CompareInfo represents the collected results from ParseCompareInfo
|
|
|
|
type CompareInfo struct {
|
|
|
|
HeadUser *models.User
|
|
|
|
HeadRepo *models.Repository
|
|
|
|
HeadGitRepo *git.Repository
|
|
|
|
CompareInfo *git.CompareInfo
|
|
|
|
BaseBranch string
|
|
|
|
HeadBranch string
|
|
|
|
DirectComparison bool
|
|
|
|
}
|
|
|
|
|
2019-06-08 05:29:29 +09:00
|
|
|
// ParseCompareInfo parse compare info between two commit for preparing comparing references
|
2021-09-27 21:19:34 +09:00
|
|
|
func ParseCompareInfo(ctx *context.Context) *CompareInfo {
|
2019-06-08 05:29:29 +09:00
|
|
|
baseRepo := ctx.Repo.Repository
|
2021-09-27 21:19:34 +09:00
|
|
|
ci := &CompareInfo{}
|
2019-06-08 05:29:29 +09:00
|
|
|
|
|
|
|
// Get compared branches information
|
2020-05-12 14:52:46 +09:00
|
|
|
// A full compare url is of the form:
|
|
|
|
//
|
|
|
|
// 1. /{:baseOwner}/{:baseRepoName}/compare/{:baseBranch}...{:headBranch}
|
|
|
|
// 2. /{:baseOwner}/{:baseRepoName}/compare/{:baseBranch}...{:headOwner}:{:headBranch}
|
|
|
|
// 3. /{:baseOwner}/{:baseRepoName}/compare/{:baseBranch}...{:headOwner}/{:headRepoName}:{:headBranch}
|
|
|
|
//
|
|
|
|
// Here we obtain the infoPath "{:baseBranch}...[{:headOwner}/{:headRepoName}:]{:headBranch}" as ctx.Params("*")
|
|
|
|
// with the :baseRepo in ctx.Repo.
|
|
|
|
//
|
|
|
|
// Note: Generally :headRepoName is not provided here - we are only passed :headOwner.
|
|
|
|
//
|
|
|
|
// How do we determine the :headRepo?
|
|
|
|
//
|
|
|
|
// 1. If :headOwner is not set then the :headRepo = :baseRepo
|
|
|
|
// 2. If :headOwner is set - then look for the fork of :baseRepo owned by :headOwner
|
|
|
|
// 3. But... :baseRepo could be a fork of :headOwner's repo - so check that
|
|
|
|
// 4. Now, :baseRepo and :headRepos could be forks of the same repo - so check that
|
|
|
|
//
|
2019-06-08 05:29:29 +09:00
|
|
|
// format: <base branch>...[<head repo>:]<head branch>
|
|
|
|
// base<-head: master...head:feature
|
|
|
|
// same repo: master...feature
|
|
|
|
|
|
|
|
var (
|
|
|
|
isSameRepo bool
|
|
|
|
infoPath string
|
|
|
|
err error
|
|
|
|
)
|
2021-09-27 21:19:34 +09:00
|
|
|
|
2019-06-08 05:29:29 +09:00
|
|
|
infoPath = ctx.Params("*")
|
2020-05-12 14:52:46 +09:00
|
|
|
infos := strings.SplitN(infoPath, "...", 2)
|
2021-09-27 21:19:34 +09:00
|
|
|
|
|
|
|
if len(infos) != 2 {
|
|
|
|
infos = strings.SplitN(infoPath, "..", 2)
|
|
|
|
ci.DirectComparison = true
|
|
|
|
ctx.Data["PageIsComparePull"] = false
|
|
|
|
}
|
|
|
|
|
2019-06-08 05:29:29 +09:00
|
|
|
if len(infos) != 2 {
|
|
|
|
log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)
|
|
|
|
ctx.NotFound("CompareAndPullRequest", nil)
|
2021-09-27 21:19:34 +09:00
|
|
|
return nil
|
2019-06-08 05:29:29 +09:00
|
|
|
}
|
|
|
|
|
2020-05-12 14:52:46 +09:00
|
|
|
ctx.Data["BaseName"] = baseRepo.OwnerName
|
2021-09-27 21:19:34 +09:00
|
|
|
ci.BaseBranch = infos[0]
|
|
|
|
ctx.Data["BaseBranch"] = ci.BaseBranch
|
2019-06-08 05:29:29 +09:00
|
|
|
|
|
|
|
// If there is no head repository, it means compare between same repository.
|
|
|
|
headInfos := strings.Split(infos[1], ":")
|
|
|
|
if len(headInfos) == 1 {
|
|
|
|
isSameRepo = true
|
2021-09-27 21:19:34 +09:00
|
|
|
ci.HeadUser = ctx.Repo.Owner
|
|
|
|
ci.HeadBranch = headInfos[0]
|
2019-06-08 05:29:29 +09:00
|
|
|
|
|
|
|
} else if len(headInfos) == 2 {
|
2020-05-12 14:52:46 +09:00
|
|
|
headInfosSplit := strings.Split(headInfos[0], "/")
|
|
|
|
if len(headInfosSplit) == 1 {
|
2021-09-27 21:19:34 +09:00
|
|
|
ci.HeadUser, err = models.GetUserByName(headInfos[0])
|
2020-05-12 14:52:46 +09:00
|
|
|
if err != nil {
|
|
|
|
if models.IsErrUserNotExist(err) {
|
|
|
|
ctx.NotFound("GetUserByName", nil)
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("GetUserByName", err)
|
|
|
|
}
|
2021-09-27 21:19:34 +09:00
|
|
|
return nil
|
2019-06-08 05:29:29 +09:00
|
|
|
}
|
2021-09-27 21:19:34 +09:00
|
|
|
ci.HeadBranch = headInfos[1]
|
|
|
|
isSameRepo = ci.HeadUser.ID == ctx.Repo.Owner.ID
|
2020-05-12 14:52:46 +09:00
|
|
|
if isSameRepo {
|
2021-09-27 21:19:34 +09:00
|
|
|
ci.HeadRepo = baseRepo
|
2020-05-12 14:52:46 +09:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-27 21:19:34 +09:00
|
|
|
ci.HeadRepo, err = models.GetRepositoryByOwnerAndName(headInfosSplit[0], headInfosSplit[1])
|
2020-05-12 14:52:46 +09:00
|
|
|
if err != nil {
|
|
|
|
if models.IsErrRepoNotExist(err) {
|
|
|
|
ctx.NotFound("GetRepositoryByOwnerAndName", nil)
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("GetRepositoryByOwnerAndName", err)
|
|
|
|
}
|
2021-09-27 21:19:34 +09:00
|
|
|
return nil
|
2020-05-12 14:52:46 +09:00
|
|
|
}
|
2021-09-27 21:19:34 +09:00
|
|
|
if err := ci.HeadRepo.GetOwner(); err != nil {
|
2020-05-12 14:52:46 +09:00
|
|
|
if models.IsErrUserNotExist(err) {
|
|
|
|
ctx.NotFound("GetUserByName", nil)
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("GetUserByName", err)
|
|
|
|
}
|
2021-09-27 21:19:34 +09:00
|
|
|
return nil
|
2020-05-12 14:52:46 +09:00
|
|
|
}
|
2021-09-27 21:19:34 +09:00
|
|
|
ci.HeadBranch = headInfos[1]
|
|
|
|
ci.HeadUser = ci.HeadRepo.Owner
|
|
|
|
isSameRepo = ci.HeadRepo.ID == ctx.Repo.Repository.ID
|
2019-06-08 05:29:29 +09:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx.NotFound("CompareAndPullRequest", nil)
|
2021-09-27 21:19:34 +09:00
|
|
|
return nil
|
2019-06-08 05:29:29 +09:00
|
|
|
}
|
2021-09-27 21:19:34 +09:00
|
|
|
ctx.Data["HeadUser"] = ci.HeadUser
|
|
|
|
ctx.Data["HeadBranch"] = ci.HeadBranch
|
2019-06-08 05:29:29 +09:00
|
|
|
ctx.Repo.PullRequest.SameRepo = isSameRepo
|
|
|
|
|
|
|
|
// Check if base branch is valid.
|
2021-09-27 21:19:34 +09:00
|
|
|
baseIsCommit := ctx.Repo.GitRepo.IsCommitExist(ci.BaseBranch)
|
|
|
|
baseIsBranch := ctx.Repo.GitRepo.IsBranchExist(ci.BaseBranch)
|
|
|
|
baseIsTag := ctx.Repo.GitRepo.IsTagExist(ci.BaseBranch)
|
2019-06-08 05:29:29 +09:00
|
|
|
if !baseIsCommit && !baseIsBranch && !baseIsTag {
|
|
|
|
// Check if baseBranch is short sha commit hash
|
2021-09-27 21:19:34 +09:00
|
|
|
if baseCommit, _ := ctx.Repo.GitRepo.GetCommit(ci.BaseBranch); baseCommit != nil {
|
|
|
|
ci.BaseBranch = baseCommit.ID.String()
|
|
|
|
ctx.Data["BaseBranch"] = ci.BaseBranch
|
2019-06-08 05:29:29 +09:00
|
|
|
baseIsCommit = true
|
|
|
|
} else {
|
|
|
|
ctx.NotFound("IsRefExist", nil)
|
2021-09-27 21:19:34 +09:00
|
|
|
return nil
|
2019-06-08 05:29:29 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.Data["BaseIsCommit"] = baseIsCommit
|
|
|
|
ctx.Data["BaseIsBranch"] = baseIsBranch
|
|
|
|
ctx.Data["BaseIsTag"] = baseIsTag
|
2020-11-17 14:02:41 +09:00
|
|
|
ctx.Data["IsPull"] = true
|
2019-06-08 05:29:29 +09:00
|
|
|
|
2020-05-12 14:52:46 +09:00
|
|
|
// Now we have the repository that represents the base
|
|
|
|
|
|
|
|
// The current base and head repositories and branches may not
|
|
|
|
// actually be the intended branches that the user wants to
|
|
|
|
// create a pull-request from - but also determining the head
|
|
|
|
// repo is difficult.
|
|
|
|
|
|
|
|
// We will want therefore to offer a few repositories to set as
|
|
|
|
// our base and head
|
|
|
|
|
|
|
|
// 1. First if the baseRepo is a fork get the "RootRepo" it was
|
|
|
|
// forked from
|
|
|
|
var rootRepo *models.Repository
|
|
|
|
if baseRepo.IsFork {
|
|
|
|
err = baseRepo.GetBaseRepo()
|
|
|
|
if err != nil {
|
|
|
|
if !models.IsErrRepoNotExist(err) {
|
|
|
|
ctx.ServerError("Unable to find root repo", err)
|
2021-09-27 21:19:34 +09:00
|
|
|
return nil
|
2020-05-12 14:52:46 +09:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rootRepo = baseRepo.BaseRepo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Now if the current user is not the owner of the baseRepo,
|
|
|
|
// check if they have a fork of the base repo and offer that as
|
|
|
|
// "OwnForkRepo"
|
|
|
|
var ownForkRepo *models.Repository
|
2020-05-16 05:44:33 +09:00
|
|
|
if ctx.User != nil && baseRepo.OwnerID != ctx.User.ID {
|
2020-05-12 14:52:46 +09:00
|
|
|
repo, has := models.HasForkedRepo(ctx.User.ID, baseRepo.ID)
|
|
|
|
if has {
|
|
|
|
ownForkRepo = repo
|
|
|
|
ctx.Data["OwnForkRepo"] = ownForkRepo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 21:19:34 +09:00
|
|
|
has := ci.HeadRepo != nil
|
2020-05-12 14:52:46 +09:00
|
|
|
// 3. If the base is a forked from "RootRepo" and the owner of
|
|
|
|
// the "RootRepo" is the :headUser - set headRepo to that
|
2021-09-27 21:19:34 +09:00
|
|
|
if !has && rootRepo != nil && rootRepo.OwnerID == ci.HeadUser.ID {
|
|
|
|
ci.HeadRepo = rootRepo
|
2020-05-12 14:52:46 +09:00
|
|
|
has = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4. If the ctx.User has their own fork of the baseRepo and the headUser is the ctx.User
|
|
|
|
// set the headRepo to the ownFork
|
2021-09-27 21:19:34 +09:00
|
|
|
if !has && ownForkRepo != nil && ownForkRepo.OwnerID == ci.HeadUser.ID {
|
|
|
|
ci.HeadRepo = ownForkRepo
|
2020-05-12 14:52:46 +09:00
|
|
|
has = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5. If the headOwner has a fork of the baseRepo - use that
|
|
|
|
if !has {
|
2021-09-27 21:19:34 +09:00
|
|
|
ci.HeadRepo, has = models.HasForkedRepo(ci.HeadUser.ID, baseRepo.ID)
|
2020-05-12 14:52:46 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// 6. If the baseRepo is a fork and the headUser has a fork of that use that
|
|
|
|
if !has && baseRepo.IsFork {
|
2021-09-27 21:19:34 +09:00
|
|
|
ci.HeadRepo, has = models.HasForkedRepo(ci.HeadUser.ID, baseRepo.ForkID)
|
2020-05-12 14:52:46 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// 7. Otherwise if we're not the same repo and haven't found a repo give up
|
|
|
|
if !isSameRepo && !has {
|
2019-06-08 05:29:29 +09:00
|
|
|
ctx.Data["PageIsComparePull"] = false
|
|
|
|
}
|
|
|
|
|
2020-05-12 14:52:46 +09:00
|
|
|
// 8. Finally open the git repo
|
2019-06-08 05:29:29 +09:00
|
|
|
if isSameRepo {
|
2021-09-27 21:19:34 +09:00
|
|
|
ci.HeadRepo = ctx.Repo.Repository
|
|
|
|
ci.HeadGitRepo = ctx.Repo.GitRepo
|
2020-05-12 14:52:46 +09:00
|
|
|
} else if has {
|
2021-09-27 21:19:34 +09:00
|
|
|
ci.HeadGitRepo, err = git.OpenRepository(ci.HeadRepo.RepoPath())
|
2019-06-08 05:29:29 +09:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("OpenRepository", err)
|
2021-09-27 21:19:34 +09:00
|
|
|
return nil
|
2019-06-08 05:29:29 +09:00
|
|
|
}
|
2021-09-27 21:19:34 +09:00
|
|
|
defer ci.HeadGitRepo.Close()
|
2019-06-08 05:29:29 +09:00
|
|
|
}
|
|
|
|
|
2021-09-27 21:19:34 +09:00
|
|
|
ctx.Data["HeadRepo"] = ci.HeadRepo
|
2020-05-12 14:52:46 +09:00
|
|
|
|
|
|
|
// Now we need to assert that the ctx.User has permission to read
|
|
|
|
// the baseRepo's code and pulls
|
|
|
|
// (NOT headRepo's)
|
2019-06-08 05:29:29 +09:00
|
|
|
permBase, err := models.GetUserRepoPermission(baseRepo, ctx.User)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetUserRepoPermission", err)
|
2021-09-27 21:19:34 +09:00
|
|
|
return nil
|
2019-06-08 05:29:29 +09:00
|
|
|
}
|
|
|
|
if !permBase.CanRead(models.UnitTypeCode) {
|
|
|
|
if log.IsTrace() {
|
|
|
|
log.Trace("Permission Denied: User: %-v cannot read code in Repo: %-v\nUser in baseRepo has Permissions: %-+v",
|
|
|
|
ctx.User,
|
|
|
|
baseRepo,
|
|
|
|
permBase)
|
|
|
|
}
|
|
|
|
ctx.NotFound("ParseCompareInfo", nil)
|
2021-09-27 21:19:34 +09:00
|
|
|
return nil
|
2019-06-08 05:29:29 +09:00
|
|
|
}
|
|
|
|
|
2020-05-12 14:52:46 +09:00
|
|
|
// If we're not merging from the same repo:
|
2020-01-17 04:59:07 +09:00
|
|
|
if !isSameRepo {
|
2020-05-12 14:52:46 +09:00
|
|
|
// Assert ctx.User has permission to read headRepo's codes
|
2021-09-27 21:19:34 +09:00
|
|
|
permHead, err := models.GetUserRepoPermission(ci.HeadRepo, ctx.User)
|
2020-01-17 04:59:07 +09:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetUserRepoPermission", err)
|
2021-09-27 21:19:34 +09:00
|
|
|
return nil
|
2020-01-17 04:59:07 +09:00
|
|
|
}
|
|
|
|
if !permHead.CanRead(models.UnitTypeCode) {
|
|
|
|
if log.IsTrace() {
|
|
|
|
log.Trace("Permission Denied: User: %-v cannot read code in Repo: %-v\nUser in headRepo has Permissions: %-+v",
|
|
|
|
ctx.User,
|
2021-09-27 21:19:34 +09:00
|
|
|
ci.HeadRepo,
|
2020-01-17 04:59:07 +09:00
|
|
|
permHead)
|
|
|
|
}
|
|
|
|
ctx.NotFound("ParseCompareInfo", nil)
|
2021-09-27 21:19:34 +09:00
|
|
|
return nil
|
2019-06-08 05:29:29 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 14:52:46 +09:00
|
|
|
// If we have a rootRepo and it's different from:
|
|
|
|
// 1. the computed base
|
|
|
|
// 2. the computed head
|
|
|
|
// then get the branches of it
|
|
|
|
if rootRepo != nil &&
|
2021-09-27 21:19:34 +09:00
|
|
|
rootRepo.ID != ci.HeadRepo.ID &&
|
2020-05-12 14:52:46 +09:00
|
|
|
rootRepo.ID != baseRepo.ID {
|
2021-05-08 06:10:05 +09:00
|
|
|
perm, branches, tags, err := getBranchesAndTagsForRepo(ctx.User, rootRepo)
|
2020-05-12 14:52:46 +09:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetBranchesForRepo", err)
|
2021-09-27 21:19:34 +09:00
|
|
|
return nil
|
2020-05-12 14:52:46 +09:00
|
|
|
}
|
|
|
|
if perm {
|
|
|
|
ctx.Data["RootRepo"] = rootRepo
|
|
|
|
ctx.Data["RootRepoBranches"] = branches
|
2021-05-08 06:10:05 +09:00
|
|
|
ctx.Data["RootRepoTags"] = tags
|
2020-05-12 14:52:46 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a ownForkRepo and it's different from:
|
|
|
|
// 1. The computed base
|
2021-05-08 06:10:05 +09:00
|
|
|
// 2. The computed head
|
2020-05-12 14:52:46 +09:00
|
|
|
// 3. The rootRepo (if we have one)
|
|
|
|
// then get the branches from it.
|
|
|
|
if ownForkRepo != nil &&
|
2021-09-27 21:19:34 +09:00
|
|
|
ownForkRepo.ID != ci.HeadRepo.ID &&
|
2020-05-12 14:52:46 +09:00
|
|
|
ownForkRepo.ID != baseRepo.ID &&
|
|
|
|
(rootRepo == nil || ownForkRepo.ID != rootRepo.ID) {
|
2021-05-08 06:10:05 +09:00
|
|
|
perm, branches, tags, err := getBranchesAndTagsForRepo(ctx.User, ownForkRepo)
|
2020-05-12 14:52:46 +09:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetBranchesForRepo", err)
|
2021-09-27 21:19:34 +09:00
|
|
|
return nil
|
2020-05-12 14:52:46 +09:00
|
|
|
}
|
|
|
|
if perm {
|
|
|
|
ctx.Data["OwnForkRepo"] = ownForkRepo
|
|
|
|
ctx.Data["OwnForkRepoBranches"] = branches
|
2021-05-08 06:10:05 +09:00
|
|
|
ctx.Data["OwnForkRepoTags"] = tags
|
2020-05-12 14:52:46 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 05:29:29 +09:00
|
|
|
// Check if head branch is valid.
|
2021-09-27 21:19:34 +09:00
|
|
|
headIsCommit := ci.HeadGitRepo.IsCommitExist(ci.HeadBranch)
|
|
|
|
headIsBranch := ci.HeadGitRepo.IsBranchExist(ci.HeadBranch)
|
|
|
|
headIsTag := ci.HeadGitRepo.IsTagExist(ci.HeadBranch)
|
2019-06-08 05:29:29 +09:00
|
|
|
if !headIsCommit && !headIsBranch && !headIsTag {
|
|
|
|
// Check if headBranch is short sha commit hash
|
2021-09-27 21:19:34 +09:00
|
|
|
if headCommit, _ := ci.HeadGitRepo.GetCommit(ci.HeadBranch); headCommit != nil {
|
|
|
|
ci.HeadBranch = headCommit.ID.String()
|
|
|
|
ctx.Data["HeadBranch"] = ci.HeadBranch
|
2019-06-08 05:29:29 +09:00
|
|
|
headIsCommit = true
|
|
|
|
} else {
|
|
|
|
ctx.NotFound("IsRefExist", nil)
|
2021-09-27 21:19:34 +09:00
|
|
|
return nil
|
2019-06-08 05:29:29 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.Data["HeadIsCommit"] = headIsCommit
|
|
|
|
ctx.Data["HeadIsBranch"] = headIsBranch
|
|
|
|
ctx.Data["HeadIsTag"] = headIsTag
|
|
|
|
|
|
|
|
// Treat as pull request if both references are branches
|
|
|
|
if ctx.Data["PageIsComparePull"] == nil {
|
|
|
|
ctx.Data["PageIsComparePull"] = headIsBranch && baseIsBranch
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Data["PageIsComparePull"] == true && !permBase.CanReadIssuesOrPulls(true) {
|
|
|
|
if log.IsTrace() {
|
|
|
|
log.Trace("Permission Denied: User: %-v cannot create/read pull requests in Repo: %-v\nUser in baseRepo has Permissions: %-+v",
|
|
|
|
ctx.User,
|
|
|
|
baseRepo,
|
|
|
|
permBase)
|
|
|
|
}
|
|
|
|
ctx.NotFound("ParseCompareInfo", nil)
|
2021-09-27 21:19:34 +09:00
|
|
|
return nil
|
2019-06-08 05:29:29 +09:00
|
|
|
}
|
|
|
|
|
2021-09-27 21:19:34 +09:00
|
|
|
baseBranchRef := ci.BaseBranch
|
2020-06-12 08:49:47 +09:00
|
|
|
if baseIsBranch {
|
2021-09-27 21:19:34 +09:00
|
|
|
baseBranchRef = git.BranchPrefix + ci.BaseBranch
|
2020-06-12 08:49:47 +09:00
|
|
|
} else if baseIsTag {
|
2021-09-27 21:19:34 +09:00
|
|
|
baseBranchRef = git.TagPrefix + ci.BaseBranch
|
2020-06-12 08:49:47 +09:00
|
|
|
}
|
2021-09-27 21:19:34 +09:00
|
|
|
headBranchRef := ci.HeadBranch
|
2020-06-12 08:49:47 +09:00
|
|
|
if headIsBranch {
|
2021-09-27 21:19:34 +09:00
|
|
|
headBranchRef = git.BranchPrefix + ci.HeadBranch
|
2020-06-12 08:49:47 +09:00
|
|
|
} else if headIsTag {
|
2021-09-27 21:19:34 +09:00
|
|
|
headBranchRef = git.TagPrefix + ci.HeadBranch
|
2020-06-12 08:49:47 +09:00
|
|
|
}
|
|
|
|
|
2021-09-27 21:19:34 +09:00
|
|
|
ci.CompareInfo, err = ci.HeadGitRepo.GetCompareInfo(baseRepo.RepoPath(), baseBranchRef, headBranchRef, ci.DirectComparison)
|
2019-06-08 05:29:29 +09:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetCompareInfo", err)
|
2021-09-27 21:19:34 +09:00
|
|
|
return nil
|
2019-06-08 05:29:29 +09:00
|
|
|
}
|
2021-09-27 21:19:34 +09:00
|
|
|
ctx.Data["BeforeCommitID"] = ci.CompareInfo.MergeBase
|
2019-06-08 05:29:29 +09:00
|
|
|
|
2021-09-27 21:19:34 +09:00
|
|
|
return ci
|
2019-06-08 05:29:29 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareCompareDiff renders compare diff page
|
|
|
|
func PrepareCompareDiff(
|
|
|
|
ctx *context.Context,
|
2021-09-27 21:19:34 +09:00
|
|
|
ci *CompareInfo,
|
2021-02-13 13:35:43 +09:00
|
|
|
whitespaceBehavior string) bool {
|
2019-06-08 05:29:29 +09:00
|
|
|
|
|
|
|
var (
|
|
|
|
repo = ctx.Repo.Repository
|
|
|
|
err error
|
|
|
|
title string
|
|
|
|
)
|
|
|
|
|
|
|
|
// Get diff information.
|
2021-09-27 21:19:34 +09:00
|
|
|
ctx.Data["CommitRepoLink"] = ci.HeadRepo.Link()
|
2019-06-08 05:29:29 +09:00
|
|
|
|
2021-09-27 21:19:34 +09:00
|
|
|
headCommitID := ci.CompareInfo.HeadCommitID
|
2019-06-08 05:29:29 +09:00
|
|
|
|
|
|
|
ctx.Data["AfterCommitID"] = headCommitID
|
|
|
|
|
2021-09-27 21:19:34 +09:00
|
|
|
if (headCommitID == ci.CompareInfo.MergeBase && !ci.DirectComparison) ||
|
|
|
|
headCommitID == ci.CompareInfo.BaseCommitID {
|
2019-06-08 05:29:29 +09:00
|
|
|
ctx.Data["IsNothingToCompare"] = true
|
2021-03-04 12:41:23 +09:00
|
|
|
if unit, err := repo.GetUnit(models.UnitTypePullRequests); err == nil {
|
|
|
|
config := unit.PullRequestsConfig()
|
2021-03-29 15:58:48 +09:00
|
|
|
|
2021-03-04 12:41:23 +09:00
|
|
|
if !config.AutodetectManualMerge {
|
2021-09-27 21:19:34 +09:00
|
|
|
allowEmptyPr := !(ci.BaseBranch == ci.HeadBranch && ctx.Repo.Repository.Name == ci.HeadRepo.Name)
|
2021-03-29 15:58:48 +09:00
|
|
|
ctx.Data["AllowEmptyPr"] = allowEmptyPr
|
|
|
|
|
|
|
|
return !allowEmptyPr
|
2021-03-04 12:41:23 +09:00
|
|
|
}
|
2021-03-29 15:58:48 +09:00
|
|
|
|
|
|
|
ctx.Data["AllowEmptyPr"] = false
|
2021-03-04 12:41:23 +09:00
|
|
|
}
|
2019-06-08 05:29:29 +09:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-09-27 21:19:34 +09:00
|
|
|
beforeCommitID := ci.CompareInfo.MergeBase
|
|
|
|
if ci.DirectComparison {
|
|
|
|
beforeCommitID = ci.CompareInfo.BaseCommitID
|
|
|
|
}
|
|
|
|
|
|
|
|
diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(ci.HeadGitRepo,
|
|
|
|
beforeCommitID, headCommitID, setting.Git.MaxGitDiffLines,
|
|
|
|
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, whitespaceBehavior, ci.DirectComparison)
|
2019-06-08 05:29:29 +09:00
|
|
|
if err != nil {
|
2021-02-13 13:35:43 +09:00
|
|
|
ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err)
|
2019-06-08 05:29:29 +09:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
ctx.Data["Diff"] = diff
|
2020-05-26 14:58:07 +09:00
|
|
|
ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0
|
2019-06-08 05:29:29 +09:00
|
|
|
|
2021-09-27 21:19:34 +09:00
|
|
|
headCommit, err := ci.HeadGitRepo.GetCommit(headCommitID)
|
2019-06-08 05:29:29 +09:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetCommit", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-09-16 18:03:22 +09:00
|
|
|
baseGitRepo := ctx.Repo.GitRepo
|
2021-09-27 21:19:34 +09:00
|
|
|
baseCommitID := ci.CompareInfo.BaseCommitID
|
2019-09-16 18:03:22 +09:00
|
|
|
|
|
|
|
baseCommit, err := baseGitRepo.GetCommit(baseCommitID)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetCommit", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-27 21:19:34 +09:00
|
|
|
commits := models.ConvertFromGitCommit(ci.CompareInfo.Commits, ci.HeadRepo)
|
2021-08-10 03:08:51 +09:00
|
|
|
ctx.Data["Commits"] = commits
|
|
|
|
ctx.Data["CommitCount"] = len(commits)
|
|
|
|
|
|
|
|
if len(commits) == 1 {
|
|
|
|
c := commits[0]
|
2019-06-08 05:29:29 +09:00
|
|
|
title = strings.TrimSpace(c.UserCommit.Summary())
|
|
|
|
|
|
|
|
body := strings.Split(strings.TrimSpace(c.UserCommit.Message()), "\n")
|
|
|
|
if len(body) > 1 {
|
|
|
|
ctx.Data["content"] = strings.Join(body[1:], "\n")
|
|
|
|
}
|
|
|
|
} else {
|
2021-09-27 21:19:34 +09:00
|
|
|
title = ci.HeadBranch
|
2019-06-08 05:29:29 +09:00
|
|
|
}
|
2021-07-25 11:59:27 +09:00
|
|
|
if len(title) > 255 {
|
|
|
|
var trailer string
|
|
|
|
title, trailer = util.SplitStringAtByteN(title, 255)
|
|
|
|
if len(trailer) > 0 {
|
|
|
|
if ctx.Data["content"] != nil {
|
|
|
|
ctx.Data["content"] = fmt.Sprintf("%s\n\n%s", trailer, ctx.Data["content"])
|
|
|
|
} else {
|
|
|
|
ctx.Data["content"] = trailer + "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 05:29:29 +09:00
|
|
|
ctx.Data["title"] = title
|
2021-09-27 21:19:34 +09:00
|
|
|
ctx.Data["Username"] = ci.HeadUser.Name
|
|
|
|
ctx.Data["Reponame"] = ci.HeadRepo.Name
|
2019-06-08 05:29:29 +09:00
|
|
|
|
2021-09-27 21:19:34 +09:00
|
|
|
headTarget := path.Join(ci.HeadUser.Name, repo.Name)
|
2021-03-30 05:44:28 +09:00
|
|
|
setCompareContext(ctx, baseCommit, headCommit, headTarget)
|
2019-09-16 18:03:22 +09:00
|
|
|
|
2019-06-08 05:29:29 +09:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-08 06:10:05 +09:00
|
|
|
func getBranchesAndTagsForRepo(user *models.User, repo *models.Repository) (bool, []string, []string, error) {
|
2020-05-12 14:52:46 +09:00
|
|
|
perm, err := models.GetUserRepoPermission(repo, user)
|
|
|
|
if err != nil {
|
2021-05-08 06:10:05 +09:00
|
|
|
return false, nil, nil, err
|
2019-10-30 14:58:18 +09:00
|
|
|
}
|
2020-05-12 14:52:46 +09:00
|
|
|
if !perm.CanRead(models.UnitTypeCode) {
|
2021-05-08 06:10:05 +09:00
|
|
|
return false, nil, nil, nil
|
2019-10-30 14:58:18 +09:00
|
|
|
}
|
2020-05-12 14:52:46 +09:00
|
|
|
gitRepo, err := git.OpenRepository(repo.RepoPath())
|
2019-10-30 14:58:18 +09:00
|
|
|
if err != nil {
|
2021-05-08 06:10:05 +09:00
|
|
|
return false, nil, nil, err
|
2019-10-30 14:58:18 +09:00
|
|
|
}
|
2020-05-12 14:52:46 +09:00
|
|
|
defer gitRepo.Close()
|
2019-11-13 16:01:19 +09:00
|
|
|
|
2021-02-04 04:06:13 +09:00
|
|
|
branches, _, err := gitRepo.GetBranches(0, 0)
|
2019-10-30 14:58:18 +09:00
|
|
|
if err != nil {
|
2021-05-08 06:10:05 +09:00
|
|
|
return false, nil, nil, err
|
2019-10-30 14:58:18 +09:00
|
|
|
}
|
2021-09-11 02:30:37 +09:00
|
|
|
tags, err := gitRepo.GetTags(0, 0)
|
2021-05-08 06:10:05 +09:00
|
|
|
if err != nil {
|
|
|
|
return false, nil, nil, err
|
|
|
|
}
|
|
|
|
return true, branches, tags, nil
|
2019-10-30 14:58:18 +09:00
|
|
|
}
|
|
|
|
|
2019-06-08 05:29:29 +09:00
|
|
|
// CompareDiff show different from one commit to another commit
|
|
|
|
func CompareDiff(ctx *context.Context) {
|
2021-09-27 21:19:34 +09:00
|
|
|
ci := ParseCompareInfo(ctx)
|
2021-08-31 13:16:23 +09:00
|
|
|
defer func() {
|
2021-09-27 21:19:34 +09:00
|
|
|
if ci.HeadGitRepo != nil {
|
|
|
|
ci.HeadGitRepo.Close()
|
2021-08-31 13:16:23 +09:00
|
|
|
}
|
|
|
|
}()
|
2019-06-08 05:29:29 +09:00
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2019-11-13 16:01:19 +09:00
|
|
|
|
2021-09-27 21:19:34 +09:00
|
|
|
ctx.Data["DirectComparison"] = ci.DirectComparison
|
|
|
|
ctx.Data["OtherCompareSeparator"] = ".."
|
|
|
|
ctx.Data["CompareSeparator"] = "..."
|
|
|
|
if ci.DirectComparison {
|
|
|
|
ctx.Data["CompareSeparator"] = ".."
|
|
|
|
ctx.Data["OtherCompareSeparator"] = "..."
|
|
|
|
}
|
|
|
|
|
|
|
|
nothingToCompare := PrepareCompareDiff(ctx, ci,
|
2021-02-13 13:35:43 +09:00
|
|
|
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
|
2019-06-08 05:29:29 +09:00
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-08 06:10:05 +09:00
|
|
|
baseGitRepo := ctx.Repo.GitRepo
|
2021-09-11 02:30:37 +09:00
|
|
|
baseTags, err := baseGitRepo.GetTags(0, 0)
|
2021-05-08 06:10:05 +09:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetTags", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Tags"] = baseTags
|
2019-08-12 00:23:49 +09:00
|
|
|
|
2021-09-27 21:19:34 +09:00
|
|
|
headBranches, _, err := ci.HeadGitRepo.GetBranches(0, 0)
|
2021-05-08 06:10:05 +09:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetBranches", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["HeadBranches"] = headBranches
|
|
|
|
|
2021-09-27 21:19:34 +09:00
|
|
|
headTags, err := ci.HeadGitRepo.GetTags(0, 0)
|
2021-05-08 06:10:05 +09:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetTags", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["HeadTags"] = headTags
|
|
|
|
|
|
|
|
if ctx.Data["PageIsComparePull"] == true {
|
2021-09-27 21:19:34 +09:00
|
|
|
pr, err := models.GetUnmergedPullRequest(ci.HeadRepo.ID, ctx.Repo.Repository.ID, ci.HeadBranch, ci.BaseBranch, models.PullRequestFlowGithub)
|
2019-06-08 05:29:29 +09:00
|
|
|
if err != nil {
|
|
|
|
if !models.IsErrPullRequestNotExist(err) {
|
|
|
|
ctx.ServerError("GetUnmergedPullRequest", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx.Data["HasPullRequest"] = true
|
|
|
|
ctx.Data["PullRequest"] = pr
|
2021-04-06 00:30:52 +09:00
|
|
|
ctx.HTML(http.StatusOK, tplCompareDiff)
|
2019-06-08 05:29:29 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !nothingToCompare {
|
|
|
|
// Setup information for new form.
|
2020-01-19 15:43:38 +09:00
|
|
|
RetrieveRepoMetas(ctx, ctx.Repo.Repository, true)
|
2019-06-08 05:29:29 +09:00
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
beforeCommitID := ctx.Data["BeforeCommitID"].(string)
|
|
|
|
afterCommitID := ctx.Data["AfterCommitID"].(string)
|
|
|
|
|
2021-09-27 21:19:34 +09:00
|
|
|
separator := "..."
|
|
|
|
if ci.DirectComparison {
|
|
|
|
separator = ".."
|
|
|
|
}
|
|
|
|
ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitID) + separator + base.ShortSha(afterCommitID)
|
2019-06-08 05:29:29 +09:00
|
|
|
|
|
|
|
ctx.Data["IsRepoToolbarCommits"] = true
|
|
|
|
ctx.Data["IsDiffCompare"] = true
|
|
|
|
ctx.Data["RequireTribute"] = true
|
2019-12-17 04:57:30 +09:00
|
|
|
ctx.Data["RequireSimpleMDE"] = true
|
2019-06-08 05:29:29 +09:00
|
|
|
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
|
2020-09-11 23:48:39 +09:00
|
|
|
setTemplateIfExists(ctx, pullRequestTemplateKey, nil, pullRequestTemplateCandidates)
|
2020-10-05 14:49:33 +09:00
|
|
|
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
|
|
|
|
upload.AddUploadContext(ctx, "comment")
|
2019-06-08 05:29:29 +09:00
|
|
|
|
2020-04-04 14:39:48 +09:00
|
|
|
ctx.Data["HasIssuesOrPullsWritePermission"] = ctx.Repo.CanWrite(models.UnitTypePullRequests)
|
|
|
|
|
2021-04-06 00:30:52 +09:00
|
|
|
ctx.HTML(http.StatusOK, tplCompare)
|
2019-06-08 05:29:29 +09:00
|
|
|
}
|
2019-11-15 11:52:59 +09:00
|
|
|
|
|
|
|
// ExcerptBlob render blob excerpt contents
|
|
|
|
func ExcerptBlob(ctx *context.Context) {
|
|
|
|
commitID := ctx.Params("sha")
|
2021-07-29 10:42:15 +09:00
|
|
|
lastLeft := ctx.FormInt("last_left")
|
|
|
|
lastRight := ctx.FormInt("last_right")
|
|
|
|
idxLeft := ctx.FormInt("left")
|
|
|
|
idxRight := ctx.FormInt("right")
|
|
|
|
leftHunkSize := ctx.FormInt("left_hunk_size")
|
|
|
|
rightHunkSize := ctx.FormInt("right_hunk_size")
|
2021-08-11 09:31:13 +09:00
|
|
|
anchor := ctx.FormString("anchor")
|
|
|
|
direction := ctx.FormString("direction")
|
|
|
|
filePath := ctx.FormString("path")
|
2019-11-15 11:52:59 +09:00
|
|
|
gitRepo := ctx.Repo.GitRepo
|
2020-08-20 23:53:06 +09:00
|
|
|
chunkSize := gitdiff.BlobExcerptChunkSize
|
2019-11-15 11:52:59 +09:00
|
|
|
commit, err := gitRepo.GetCommit(commitID)
|
|
|
|
if err != nil {
|
2021-04-06 00:30:52 +09:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetCommit")
|
2019-11-15 11:52:59 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
section := &gitdiff.DiffSection{
|
2020-07-13 03:25:05 +09:00
|
|
|
FileName: filePath,
|
|
|
|
Name: filePath,
|
2019-11-15 11:52:59 +09:00
|
|
|
}
|
|
|
|
if direction == "up" && (idxLeft-lastLeft) > chunkSize {
|
|
|
|
idxLeft -= chunkSize
|
|
|
|
idxRight -= chunkSize
|
|
|
|
leftHunkSize += chunkSize
|
|
|
|
rightHunkSize += chunkSize
|
|
|
|
section.Lines, err = getExcerptLines(commit, filePath, idxLeft-1, idxRight-1, chunkSize)
|
|
|
|
} else if direction == "down" && (idxLeft-lastLeft) > chunkSize {
|
|
|
|
section.Lines, err = getExcerptLines(commit, filePath, lastLeft, lastRight, chunkSize)
|
|
|
|
lastLeft += chunkSize
|
|
|
|
lastRight += chunkSize
|
|
|
|
} else {
|
2021-06-25 00:47:46 +09:00
|
|
|
offset := -1
|
|
|
|
if direction == "down" {
|
|
|
|
offset = 0
|
|
|
|
}
|
|
|
|
section.Lines, err = getExcerptLines(commit, filePath, lastLeft, lastRight, idxRight-lastRight+offset)
|
2019-11-15 11:52:59 +09:00
|
|
|
leftHunkSize = 0
|
|
|
|
rightHunkSize = 0
|
|
|
|
idxLeft = lastLeft
|
|
|
|
idxRight = lastRight
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-04-06 00:30:52 +09:00
|
|
|
ctx.Error(http.StatusInternalServerError, "getExcerptLines")
|
2019-11-15 11:52:59 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if idxRight > lastRight {
|
|
|
|
lineText := " "
|
|
|
|
if rightHunkSize > 0 || leftHunkSize > 0 {
|
|
|
|
lineText = fmt.Sprintf("@@ -%d,%d +%d,%d @@\n", idxLeft, leftHunkSize, idxRight, rightHunkSize)
|
|
|
|
}
|
|
|
|
lineText = html.EscapeString(lineText)
|
|
|
|
lineSection := &gitdiff.DiffLine{
|
|
|
|
Type: gitdiff.DiffLineSection,
|
|
|
|
Content: lineText,
|
|
|
|
SectionInfo: &gitdiff.DiffLineSectionInfo{
|
|
|
|
Path: filePath,
|
|
|
|
LastLeftIdx: lastLeft,
|
|
|
|
LastRightIdx: lastRight,
|
|
|
|
LeftIdx: idxLeft,
|
|
|
|
RightIdx: idxRight,
|
|
|
|
LeftHunkSize: leftHunkSize,
|
|
|
|
RightHunkSize: rightHunkSize,
|
|
|
|
}}
|
|
|
|
if direction == "up" {
|
|
|
|
section.Lines = append([]*gitdiff.DiffLine{lineSection}, section.Lines...)
|
|
|
|
} else if direction == "down" {
|
|
|
|
section.Lines = append(section.Lines, lineSection)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.Data["section"] = section
|
|
|
|
ctx.Data["fileName"] = filePath
|
|
|
|
ctx.Data["AfterCommitID"] = commitID
|
|
|
|
ctx.Data["Anchor"] = anchor
|
2021-04-06 00:30:52 +09:00
|
|
|
ctx.HTML(http.StatusOK, tplBlobExcerpt)
|
2019-11-15 11:52:59 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func getExcerptLines(commit *git.Commit, filePath string, idxLeft int, idxRight int, chunkSize int) ([]*gitdiff.DiffLine, error) {
|
|
|
|
blob, err := commit.Tree.GetBlobByPath(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
reader, err := blob.DataAsync()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
scanner := bufio.NewScanner(reader)
|
|
|
|
var diffLines []*gitdiff.DiffLine
|
|
|
|
for line := 0; line < idxRight+chunkSize; line++ {
|
|
|
|
if ok := scanner.Scan(); !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if line < idxRight {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
lineText := scanner.Text()
|
|
|
|
diffLine := &gitdiff.DiffLine{
|
|
|
|
LeftIdx: idxLeft + (line - idxRight) + 1,
|
|
|
|
RightIdx: line + 1,
|
|
|
|
Type: gitdiff.DiffLinePlain,
|
|
|
|
Content: " " + lineText,
|
|
|
|
}
|
|
|
|
diffLines = append(diffLines, diffLine)
|
|
|
|
}
|
|
|
|
return diffLines, nil
|
|
|
|
}
|