2019-06-08 01:59:29 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-06-08 01:59:29 +05:30
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2019-11-15 08:22:59 +05:30
|
|
|
"bufio"
|
2022-01-20 04:56:57 +05:30
|
|
|
gocontext "context"
|
2021-03-30 02:14:28 +05:30
|
|
|
"encoding/csv"
|
|
|
|
"errors"
|
2019-10-05 01:28:54 +05:30
|
|
|
"fmt"
|
2019-11-15 08:22:59 +05:30
|
|
|
"html"
|
2021-10-25 04:12:32 +05:30
|
|
|
"io"
|
2021-04-05 21:00:52 +05:30
|
|
|
"net/http"
|
2021-11-16 23:48:25 +05:30
|
|
|
"net/url"
|
2021-03-30 02:14:28 +05:30
|
|
|
"path/filepath"
|
2019-06-08 01:59:29 +05:30
|
|
|
"strings"
|
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-06-13 15:07:59 +05:30
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2022-05-11 15:39:36 +05:30
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 01:27:58 +05:30
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-06-08 01:59:29 +05:30
|
|
|
"code.gitea.io/gitea/modules/base"
|
2021-03-30 02:14:28 +05:30
|
|
|
"code.gitea.io/gitea/modules/charset"
|
2019-06-08 01:59:29 +05:30
|
|
|
"code.gitea.io/gitea/modules/context"
|
2021-03-30 02:14:28 +05:30
|
|
|
csv_module "code.gitea.io/gitea/modules/csv"
|
2019-06-08 01:59:29 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-10-30 21:20:40 +05:30
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2019-06-08 01:59:29 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2023-01-30 20:09:07 +05:30
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2020-10-05 11:19:33 +05:30
|
|
|
"code.gitea.io/gitea/modules/upload"
|
2021-07-25 08:29:27 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-09-06 07:50:09 +05:30
|
|
|
"code.gitea.io/gitea/services/gitdiff"
|
2019-06-08 01:59:29 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-11-15 08:22:59 +05:30
|
|
|
tplCompare base.TplName = "repo/diff/compare"
|
|
|
|
tplBlobExcerpt base.TplName = "repo/diff/blob_excerpt"
|
2021-10-15 21:35:33 +05:30
|
|
|
tplDiffBox base.TplName = "repo/diff/box"
|
2019-06-08 01:59:29 +05:30
|
|
|
)
|
|
|
|
|
2021-03-30 02:14:28 +05:30
|
|
|
// setCompareContext sets context data.
|
2023-02-20 09:26:07 +05:30
|
|
|
func setCompareContext(ctx *context.Context, before, head *git.Commit, headOwner, headName string) {
|
|
|
|
ctx.Data["BeforeCommit"] = before
|
2021-03-30 02:14:28 +05:30
|
|
|
ctx.Data["HeadCommit"] = head
|
|
|
|
|
2021-06-05 18:02:19 +05:30
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-20 09:26:07 +05:30
|
|
|
setPathsCompareContext(ctx, before, head, headOwner, headName)
|
2021-06-05 18:02:19 +05:30
|
|
|
setImageCompareContext(ctx)
|
2021-03-30 02:14:28 +05:30
|
|
|
setCsvCompareContext(ctx)
|
|
|
|
}
|
|
|
|
|
2021-11-16 23:48:25 +05:30
|
|
|
// SourceCommitURL creates a relative URL for a commit in the given repository
|
|
|
|
func SourceCommitURL(owner, name string, commit *git.Commit) string {
|
|
|
|
return setting.AppSubURL + "/" + url.PathEscape(owner) + "/" + url.PathEscape(name) + "/src/commit/" + url.PathEscape(commit.ID.String())
|
|
|
|
}
|
2019-10-05 01:28:54 +05:30
|
|
|
|
2021-11-16 23:48:25 +05:30
|
|
|
// RawCommitURL creates a relative URL for the raw commit in the given repository
|
|
|
|
func RawCommitURL(owner, name string, commit *git.Commit) string {
|
|
|
|
return setting.AppSubURL + "/" + url.PathEscape(owner) + "/" + url.PathEscape(name) + "/raw/commit/" + url.PathEscape(commit.ID.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// setPathsCompareContext sets context data for source and raw paths
|
2021-12-20 10:11:31 +05:30
|
|
|
func setPathsCompareContext(ctx *context.Context, base, head *git.Commit, headOwner, headName string) {
|
2021-11-16 23:48:25 +05:30
|
|
|
ctx.Data["SourcePath"] = SourceCommitURL(headOwner, headName, head)
|
|
|
|
ctx.Data["RawPath"] = RawCommitURL(headOwner, headName, head)
|
2019-10-05 01:28:54 +05:30
|
|
|
if base != nil {
|
2022-02-25 12:16:15 +05:30
|
|
|
ctx.Data["BeforeSourcePath"] = SourceCommitURL(headOwner, headName, base)
|
|
|
|
ctx.Data["BeforeRawPath"] = RawCommitURL(headOwner, headName, base)
|
2019-10-05 01:28:54 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setImageCompareContext sets context data that is required by image compare template
|
2021-06-05 18:02:19 +05:30
|
|
|
func setImageCompareContext(ctx *context.Context) {
|
|
|
|
ctx.Data["IsBlobAnImage"] = func(blob *git.Blob) bool {
|
|
|
|
if blob == nil {
|
|
|
|
return false
|
2019-10-05 01:28:54 +05:30
|
|
|
}
|
2021-06-05 18:02:19 +05:30
|
|
|
|
|
|
|
st, err := blob.GuessContentType()
|
2019-10-05 01:28:54 +05:30
|
|
|
if err != nil {
|
2021-06-05 18:02:19 +05:30
|
|
|
log.Error("GuessContentType failed: %v", err)
|
|
|
|
return false
|
2019-10-05 01:28:54 +05:30
|
|
|
}
|
2021-06-05 18:02:19 +05:30
|
|
|
return st.IsImage() && (setting.UI.SVG.Enabled || !st.IsSvgImage())
|
2019-10-05 01:28:54 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 02:14:28 +05:30
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-09-17 08:15:32 +05:30
|
|
|
ctx.Data["CreateCsvDiff"] = func(diffFile *gitdiff.DiffFile, baseBlob, headBlob *git.Blob) CsvDiffResult {
|
|
|
|
if diffFile == nil {
|
2021-03-30 02:14:28 +05:30
|
|
|
return CsvDiffResult{nil, ""}
|
|
|
|
}
|
|
|
|
|
|
|
|
errTooLarge := errors.New(ctx.Locale.Tr("repo.error.csv.too_large"))
|
|
|
|
|
2022-09-17 08:15:32 +05:30
|
|
|
csvReaderFromCommit := func(ctx *markup.RenderContext, blob *git.Blob) (*csv.Reader, io.Closer, error) {
|
|
|
|
if blob == nil {
|
|
|
|
// It's ok for blob to be nil (file added or deleted)
|
|
|
|
return nil, nil, nil
|
2021-03-30 02:14:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if setting.UI.CSV.MaxFileSize != 0 && setting.UI.CSV.MaxFileSize < blob.Size() {
|
2021-10-25 04:12:32 +05:30
|
|
|
return nil, nil, errTooLarge
|
2021-03-30 02:14:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
reader, err := blob.DataAsync()
|
|
|
|
if err != nil {
|
2021-10-25 04:12:32 +05:30
|
|
|
return nil, nil, err
|
2021-03-30 02:14:28 +05:30
|
|
|
}
|
|
|
|
|
2021-10-30 21:20:40 +05:30
|
|
|
csvReader, err := csv_module.CreateReaderAndDetermineDelimiter(ctx, charset.ToUTF8WithFallbackReader(reader))
|
2021-10-25 04:12:32 +05:30
|
|
|
return csvReader, reader, err
|
2021-03-30 02:14:28 +05:30
|
|
|
}
|
|
|
|
|
2022-09-17 08:15:32 +05:30
|
|
|
baseReader, baseBlobCloser, err := csvReaderFromCommit(&markup.RenderContext{Ctx: ctx, RelativePath: diffFile.OldName}, baseBlob)
|
2021-10-25 04:12:32 +05:30
|
|
|
if baseBlobCloser != nil {
|
|
|
|
defer baseBlobCloser.Close()
|
|
|
|
}
|
2022-05-02 22:16:50 +05:30
|
|
|
if err != nil {
|
2022-09-17 08:15:32 +05:30
|
|
|
if err == errTooLarge {
|
|
|
|
return CsvDiffResult{nil, err.Error()}
|
|
|
|
}
|
|
|
|
log.Error("error whilst creating csv.Reader from file %s in base commit %s in %s: %v", diffFile.Name, baseBlob.ID.String(), ctx.Repo.Repository.Name, err)
|
|
|
|
return CsvDiffResult{nil, "unable to load file"}
|
2022-05-02 22:16:50 +05:30
|
|
|
}
|
|
|
|
|
2022-09-17 08:15:32 +05:30
|
|
|
headReader, headBlobCloser, err := csvReaderFromCommit(&markup.RenderContext{Ctx: ctx, RelativePath: diffFile.Name}, headBlob)
|
2021-10-25 04:12:32 +05:30
|
|
|
if headBlobCloser != nil {
|
|
|
|
defer headBlobCloser.Close()
|
|
|
|
}
|
2022-05-02 22:16:50 +05:30
|
|
|
if err != nil {
|
2022-09-17 08:15:32 +05:30
|
|
|
if err == errTooLarge {
|
|
|
|
return CsvDiffResult{nil, err.Error()}
|
|
|
|
}
|
|
|
|
log.Error("error whilst creating csv.Reader from file %s in head commit %s in %s: %v", diffFile.Name, headBlob.ID.String(), ctx.Repo.Repository.Name, err)
|
|
|
|
return CsvDiffResult{nil, "unable to load file"}
|
2022-05-02 22:16:50 +05:30
|
|
|
}
|
2021-03-30 02:14:28 +05:30
|
|
|
|
|
|
|
sections, err := gitdiff.CreateCsvDiff(diffFile, baseReader, headReader)
|
|
|
|
if err != nil {
|
|
|
|
errMessage, err := csv_module.FormatError(err, ctx.Locale)
|
|
|
|
if err != nil {
|
2022-05-02 22:16:50 +05:30
|
|
|
log.Error("CreateCsvDiff FormatError failed: %v", err)
|
|
|
|
return CsvDiffResult{nil, "unknown csv diff error"}
|
2021-03-30 02:14:28 +05:30
|
|
|
}
|
|
|
|
return CsvDiffResult{nil, errMessage}
|
|
|
|
}
|
|
|
|
return CsvDiffResult{sections, ""}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 17:49:34 +05:30
|
|
|
// CompareInfo represents the collected results from ParseCompareInfo
|
|
|
|
type CompareInfo struct {
|
2021-11-24 15:19:20 +05:30
|
|
|
HeadUser *user_model.User
|
2021-12-10 06:57:50 +05:30
|
|
|
HeadRepo *repo_model.Repository
|
2021-09-27 17:49:34 +05:30
|
|
|
HeadGitRepo *git.Repository
|
|
|
|
CompareInfo *git.CompareInfo
|
|
|
|
BaseBranch string
|
|
|
|
HeadBranch string
|
|
|
|
DirectComparison bool
|
|
|
|
}
|
|
|
|
|
2019-06-08 01:59:29 +05:30
|
|
|
// ParseCompareInfo parse compare info between two commit for preparing comparing references
|
2021-09-27 17:49:34 +05:30
|
|
|
func ParseCompareInfo(ctx *context.Context) *CompareInfo {
|
2019-06-08 01:59:29 +05:30
|
|
|
baseRepo := ctx.Repo.Repository
|
2021-09-27 17:49:34 +05:30
|
|
|
ci := &CompareInfo{}
|
2019-06-08 01:59:29 +05:30
|
|
|
|
2021-10-15 21:35:33 +05:30
|
|
|
fileOnly := ctx.FormBool("file-only")
|
|
|
|
|
2019-06-08 01:59:29 +05:30
|
|
|
// Get compared branches information
|
2020-05-12 11:22:46 +05:30
|
|
|
// 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}
|
2021-12-06 22:34:07 +05:30
|
|
|
// 4. /{:baseOwner}/{:baseRepoName}/compare/{:headBranch}
|
|
|
|
// 5. /{:baseOwner}/{:baseRepoName}/compare/{:headOwner}:{:headBranch}
|
|
|
|
// 6. /{:baseOwner}/{:baseRepoName}/compare/{:headOwner}/{:headRepoName}:{:headBranch}
|
2020-05-12 11:22:46 +05:30
|
|
|
//
|
|
|
|
// 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 01:59:29 +05:30
|
|
|
// 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 17:49:34 +05:30
|
|
|
|
2019-06-08 01:59:29 +05:30
|
|
|
infoPath = ctx.Params("*")
|
2021-12-18 03:50:27 +05:30
|
|
|
var infos []string
|
|
|
|
if infoPath == "" {
|
|
|
|
infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch}
|
|
|
|
} else {
|
|
|
|
infos = strings.SplitN(infoPath, "...", 2)
|
|
|
|
if len(infos) != 2 {
|
|
|
|
if infos = strings.SplitN(infoPath, "..", 2); len(infos) == 2 {
|
|
|
|
ci.DirectComparison = true
|
|
|
|
ctx.Data["PageIsComparePull"] = false
|
|
|
|
} else {
|
|
|
|
infos = []string{baseRepo.DefaultBranch, infoPath}
|
|
|
|
}
|
2021-12-06 22:34:07 +05:30
|
|
|
}
|
2021-09-27 17:49:34 +05:30
|
|
|
}
|
|
|
|
|
2020-05-12 11:22:46 +05:30
|
|
|
ctx.Data["BaseName"] = baseRepo.OwnerName
|
2021-09-27 17:49:34 +05:30
|
|
|
ci.BaseBranch = infos[0]
|
|
|
|
ctx.Data["BaseBranch"] = ci.BaseBranch
|
2019-06-08 01:59:29 +05:30
|
|
|
|
|
|
|
// 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 17:49:34 +05:30
|
|
|
ci.HeadUser = ctx.Repo.Owner
|
|
|
|
ci.HeadBranch = headInfos[0]
|
2019-06-08 01:59:29 +05:30
|
|
|
|
|
|
|
} else if len(headInfos) == 2 {
|
2020-05-12 11:22:46 +05:30
|
|
|
headInfosSplit := strings.Split(headInfos[0], "/")
|
|
|
|
if len(headInfosSplit) == 1 {
|
2022-05-20 19:38:52 +05:30
|
|
|
ci.HeadUser, err = user_model.GetUserByName(ctx, headInfos[0])
|
2020-05-12 11:22:46 +05:30
|
|
|
if err != nil {
|
2021-11-24 15:19:20 +05:30
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2020-05-12 11:22:46 +05:30
|
|
|
ctx.NotFound("GetUserByName", nil)
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("GetUserByName", err)
|
|
|
|
}
|
2021-09-27 17:49:34 +05:30
|
|
|
return nil
|
2019-06-08 01:59:29 +05:30
|
|
|
}
|
2021-09-27 17:49:34 +05:30
|
|
|
ci.HeadBranch = headInfos[1]
|
|
|
|
isSameRepo = ci.HeadUser.ID == ctx.Repo.Owner.ID
|
2020-05-12 11:22:46 +05:30
|
|
|
if isSameRepo {
|
2021-09-27 17:49:34 +05:30
|
|
|
ci.HeadRepo = baseRepo
|
2020-05-12 11:22:46 +05:30
|
|
|
}
|
|
|
|
} else {
|
2022-12-03 08:18:26 +05:30
|
|
|
ci.HeadRepo, err = repo_model.GetRepositoryByOwnerAndName(ctx, headInfosSplit[0], headInfosSplit[1])
|
2020-05-12 11:22:46 +05:30
|
|
|
if err != nil {
|
2021-12-10 06:57:50 +05:30
|
|
|
if repo_model.IsErrRepoNotExist(err) {
|
2020-05-12 11:22:46 +05:30
|
|
|
ctx.NotFound("GetRepositoryByOwnerAndName", nil)
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("GetRepositoryByOwnerAndName", err)
|
|
|
|
}
|
2021-09-27 17:49:34 +05:30
|
|
|
return nil
|
2020-05-12 11:22:46 +05:30
|
|
|
}
|
2023-02-18 17:41:03 +05:30
|
|
|
if err := ci.HeadRepo.LoadOwner(ctx); err != nil {
|
2021-11-24 15:19:20 +05:30
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2020-05-12 11:22:46 +05:30
|
|
|
ctx.NotFound("GetUserByName", nil)
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("GetUserByName", err)
|
|
|
|
}
|
2021-09-27 17:49:34 +05:30
|
|
|
return nil
|
2020-05-12 11:22:46 +05:30
|
|
|
}
|
2021-09-27 17:49:34 +05:30
|
|
|
ci.HeadBranch = headInfos[1]
|
|
|
|
ci.HeadUser = ci.HeadRepo.Owner
|
|
|
|
isSameRepo = ci.HeadRepo.ID == ctx.Repo.Repository.ID
|
2019-06-08 01:59:29 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx.NotFound("CompareAndPullRequest", nil)
|
2021-09-27 17:49:34 +05:30
|
|
|
return nil
|
2019-06-08 01:59:29 +05:30
|
|
|
}
|
2021-09-27 17:49:34 +05:30
|
|
|
ctx.Data["HeadUser"] = ci.HeadUser
|
|
|
|
ctx.Data["HeadBranch"] = ci.HeadBranch
|
2019-06-08 01:59:29 +05:30
|
|
|
ctx.Repo.PullRequest.SameRepo = isSameRepo
|
|
|
|
|
|
|
|
// Check if base branch is valid.
|
2021-09-27 17:49:34 +05:30
|
|
|
baseIsCommit := ctx.Repo.GitRepo.IsCommitExist(ci.BaseBranch)
|
|
|
|
baseIsBranch := ctx.Repo.GitRepo.IsBranchExist(ci.BaseBranch)
|
|
|
|
baseIsTag := ctx.Repo.GitRepo.IsTagExist(ci.BaseBranch)
|
2019-06-08 01:59:29 +05:30
|
|
|
if !baseIsCommit && !baseIsBranch && !baseIsTag {
|
|
|
|
// Check if baseBranch is short sha commit hash
|
2021-09-27 17:49:34 +05:30
|
|
|
if baseCommit, _ := ctx.Repo.GitRepo.GetCommit(ci.BaseBranch); baseCommit != nil {
|
|
|
|
ci.BaseBranch = baseCommit.ID.String()
|
|
|
|
ctx.Data["BaseBranch"] = ci.BaseBranch
|
2019-06-08 01:59:29 +05:30
|
|
|
baseIsCommit = true
|
2022-03-23 19:10:12 +05:30
|
|
|
} else if ci.BaseBranch == git.EmptySHA {
|
|
|
|
if isSameRepo {
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ci.HeadBranch))
|
|
|
|
} else {
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ci.HeadRepo.FullName()) + ":" + util.PathEscapeSegments(ci.HeadBranch))
|
|
|
|
}
|
|
|
|
return nil
|
2019-06-08 01:59:29 +05:30
|
|
|
} else {
|
|
|
|
ctx.NotFound("IsRefExist", nil)
|
2021-09-27 17:49:34 +05:30
|
|
|
return nil
|
2019-06-08 01:59:29 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.Data["BaseIsCommit"] = baseIsCommit
|
|
|
|
ctx.Data["BaseIsBranch"] = baseIsBranch
|
|
|
|
ctx.Data["BaseIsTag"] = baseIsTag
|
2020-11-17 10:32:41 +05:30
|
|
|
ctx.Data["IsPull"] = true
|
2019-06-08 01:59:29 +05:30
|
|
|
|
2020-05-12 11:22:46 +05:30
|
|
|
// 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
|
2021-12-10 06:57:50 +05:30
|
|
|
var rootRepo *repo_model.Repository
|
2020-05-12 11:22:46 +05:30
|
|
|
if baseRepo.IsFork {
|
2022-12-03 08:18:26 +05:30
|
|
|
err = baseRepo.GetBaseRepo(ctx)
|
2020-05-12 11:22:46 +05:30
|
|
|
if err != nil {
|
2021-12-10 06:57:50 +05:30
|
|
|
if !repo_model.IsErrRepoNotExist(err) {
|
2020-05-12 11:22:46 +05:30
|
|
|
ctx.ServerError("Unable to find root repo", err)
|
2021-09-27 17:49:34 +05:30
|
|
|
return nil
|
2020-05-12 11:22:46 +05:30
|
|
|
}
|
|
|
|
} 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"
|
2021-12-10 06:57:50 +05:30
|
|
|
var ownForkRepo *repo_model.Repository
|
2022-03-22 12:33:22 +05:30
|
|
|
if ctx.Doer != nil && baseRepo.OwnerID != ctx.Doer.ID {
|
|
|
|
repo := repo_model.GetForkedRepo(ctx.Doer.ID, baseRepo.ID)
|
2021-11-22 20:51:55 +05:30
|
|
|
if repo != nil {
|
2020-05-12 11:22:46 +05:30
|
|
|
ownForkRepo = repo
|
|
|
|
ctx.Data["OwnForkRepo"] = ownForkRepo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 17:49:34 +05:30
|
|
|
has := ci.HeadRepo != nil
|
2020-05-12 11:22:46 +05:30
|
|
|
// 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 17:49:34 +05:30
|
|
|
if !has && rootRepo != nil && rootRepo.OwnerID == ci.HeadUser.ID {
|
|
|
|
ci.HeadRepo = rootRepo
|
2020-05-12 11:22:46 +05:30
|
|
|
has = true
|
|
|
|
}
|
|
|
|
|
2022-03-22 12:33:22 +05:30
|
|
|
// 4. If the ctx.Doer has their own fork of the baseRepo and the headUser is the ctx.Doer
|
2020-05-12 11:22:46 +05:30
|
|
|
// set the headRepo to the ownFork
|
2021-09-27 17:49:34 +05:30
|
|
|
if !has && ownForkRepo != nil && ownForkRepo.OwnerID == ci.HeadUser.ID {
|
|
|
|
ci.HeadRepo = ownForkRepo
|
2020-05-12 11:22:46 +05:30
|
|
|
has = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5. If the headOwner has a fork of the baseRepo - use that
|
|
|
|
if !has {
|
2021-12-12 21:18:20 +05:30
|
|
|
ci.HeadRepo = repo_model.GetForkedRepo(ci.HeadUser.ID, baseRepo.ID)
|
2021-11-22 20:51:55 +05:30
|
|
|
has = ci.HeadRepo != nil
|
2020-05-12 11:22:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// 6. If the baseRepo is a fork and the headUser has a fork of that use that
|
|
|
|
if !has && baseRepo.IsFork {
|
2021-12-12 21:18:20 +05:30
|
|
|
ci.HeadRepo = repo_model.GetForkedRepo(ci.HeadUser.ID, baseRepo.ForkID)
|
2021-11-22 20:51:55 +05:30
|
|
|
has = ci.HeadRepo != nil
|
2020-05-12 11:22:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// 7. Otherwise if we're not the same repo and haven't found a repo give up
|
|
|
|
if !isSameRepo && !has {
|
2019-06-08 01:59:29 +05:30
|
|
|
ctx.Data["PageIsComparePull"] = false
|
|
|
|
}
|
|
|
|
|
2020-05-12 11:22:46 +05:30
|
|
|
// 8. Finally open the git repo
|
2019-06-08 01:59:29 +05:30
|
|
|
if isSameRepo {
|
2021-09-27 17:49:34 +05:30
|
|
|
ci.HeadRepo = ctx.Repo.Repository
|
|
|
|
ci.HeadGitRepo = ctx.Repo.GitRepo
|
2020-05-12 11:22:46 +05:30
|
|
|
} else if has {
|
2022-03-30 00:43:41 +05:30
|
|
|
ci.HeadGitRepo, err = git.OpenRepository(ctx, ci.HeadRepo.RepoPath())
|
2019-06-08 01:59:29 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("OpenRepository", err)
|
2021-09-27 17:49:34 +05:30
|
|
|
return nil
|
2019-06-08 01:59:29 +05:30
|
|
|
}
|
2021-09-27 17:49:34 +05:30
|
|
|
defer ci.HeadGitRepo.Close()
|
2019-06-08 01:59:29 +05:30
|
|
|
}
|
|
|
|
|
2021-09-27 17:49:34 +05:30
|
|
|
ctx.Data["HeadRepo"] = ci.HeadRepo
|
2022-04-28 21:15:33 +05:30
|
|
|
ctx.Data["BaseCompareRepo"] = ctx.Repo.Repository
|
2020-05-12 11:22:46 +05:30
|
|
|
|
2022-03-22 12:33:22 +05:30
|
|
|
// Now we need to assert that the ctx.Doer has permission to read
|
2020-05-12 11:22:46 +05:30
|
|
|
// the baseRepo's code and pulls
|
|
|
|
// (NOT headRepo's)
|
2022-05-11 15:39:36 +05:30
|
|
|
permBase, err := access_model.GetUserRepoPermission(ctx, baseRepo, ctx.Doer)
|
2019-06-08 01:59:29 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetUserRepoPermission", err)
|
2021-09-27 17:49:34 +05:30
|
|
|
return nil
|
2019-06-08 01:59:29 +05:30
|
|
|
}
|
2021-11-10 01:27:58 +05:30
|
|
|
if !permBase.CanRead(unit.TypeCode) {
|
2019-06-08 01:59:29 +05:30
|
|
|
if log.IsTrace() {
|
|
|
|
log.Trace("Permission Denied: User: %-v cannot read code in Repo: %-v\nUser in baseRepo has Permissions: %-+v",
|
2022-03-22 12:33:22 +05:30
|
|
|
ctx.Doer,
|
2019-06-08 01:59:29 +05:30
|
|
|
baseRepo,
|
|
|
|
permBase)
|
|
|
|
}
|
|
|
|
ctx.NotFound("ParseCompareInfo", nil)
|
2021-09-27 17:49:34 +05:30
|
|
|
return nil
|
2019-06-08 01:59:29 +05:30
|
|
|
}
|
|
|
|
|
2020-05-12 11:22:46 +05:30
|
|
|
// If we're not merging from the same repo:
|
2020-01-17 01:29:07 +05:30
|
|
|
if !isSameRepo {
|
2022-03-22 12:33:22 +05:30
|
|
|
// Assert ctx.Doer has permission to read headRepo's codes
|
2022-05-11 15:39:36 +05:30
|
|
|
permHead, err := access_model.GetUserRepoPermission(ctx, ci.HeadRepo, ctx.Doer)
|
2020-01-17 01:29:07 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetUserRepoPermission", err)
|
2021-09-27 17:49:34 +05:30
|
|
|
return nil
|
2020-01-17 01:29:07 +05:30
|
|
|
}
|
2021-11-10 01:27:58 +05:30
|
|
|
if !permHead.CanRead(unit.TypeCode) {
|
2020-01-17 01:29:07 +05:30
|
|
|
if log.IsTrace() {
|
|
|
|
log.Trace("Permission Denied: User: %-v cannot read code in Repo: %-v\nUser in headRepo has Permissions: %-+v",
|
2022-03-22 12:33:22 +05:30
|
|
|
ctx.Doer,
|
2021-09-27 17:49:34 +05:30
|
|
|
ci.HeadRepo,
|
2020-01-17 01:29:07 +05:30
|
|
|
permHead)
|
|
|
|
}
|
|
|
|
ctx.NotFound("ParseCompareInfo", nil)
|
2021-09-27 17:49:34 +05:30
|
|
|
return nil
|
2019-06-08 01:59:29 +05:30
|
|
|
}
|
2022-04-28 21:15:33 +05:30
|
|
|
ctx.Data["CanWriteToHeadRepo"] = permHead.CanWrite(unit.TypeCode)
|
2019-06-08 01:59:29 +05:30
|
|
|
}
|
|
|
|
|
2020-05-12 11:22:46 +05:30
|
|
|
// 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 17:49:34 +05:30
|
|
|
rootRepo.ID != ci.HeadRepo.ID &&
|
2020-05-12 11:22:46 +05:30
|
|
|
rootRepo.ID != baseRepo.ID {
|
2022-08-25 08:01:57 +05:30
|
|
|
canRead := access_model.CheckRepoUnitUser(ctx, rootRepo, ctx.Doer, unit.TypeCode)
|
2021-10-15 21:35:33 +05:30
|
|
|
if canRead {
|
2020-05-12 11:22:46 +05:30
|
|
|
ctx.Data["RootRepo"] = rootRepo
|
2021-10-15 21:35:33 +05:30
|
|
|
if !fileOnly {
|
2022-01-20 04:56:57 +05:30
|
|
|
branches, tags, err := getBranchesAndTagsForRepo(ctx, rootRepo)
|
2021-10-15 21:35:33 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetBranchesForRepo", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["RootRepoBranches"] = branches
|
|
|
|
ctx.Data["RootRepoTags"] = tags
|
|
|
|
}
|
2020-05-12 11:22:46 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a ownForkRepo and it's different from:
|
|
|
|
// 1. The computed base
|
2021-05-08 02:40:05 +05:30
|
|
|
// 2. The computed head
|
2020-05-12 11:22:46 +05:30
|
|
|
// 3. The rootRepo (if we have one)
|
|
|
|
// then get the branches from it.
|
|
|
|
if ownForkRepo != nil &&
|
2021-09-27 17:49:34 +05:30
|
|
|
ownForkRepo.ID != ci.HeadRepo.ID &&
|
2020-05-12 11:22:46 +05:30
|
|
|
ownForkRepo.ID != baseRepo.ID &&
|
|
|
|
(rootRepo == nil || ownForkRepo.ID != rootRepo.ID) {
|
2022-08-25 08:01:57 +05:30
|
|
|
canRead := access_model.CheckRepoUnitUser(ctx, ownForkRepo, ctx.Doer, unit.TypeCode)
|
2021-10-15 21:35:33 +05:30
|
|
|
if canRead {
|
2020-05-12 11:22:46 +05:30
|
|
|
ctx.Data["OwnForkRepo"] = ownForkRepo
|
2021-10-15 21:35:33 +05:30
|
|
|
if !fileOnly {
|
2022-01-20 04:56:57 +05:30
|
|
|
branches, tags, err := getBranchesAndTagsForRepo(ctx, ownForkRepo)
|
2021-10-15 21:35:33 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetBranchesForRepo", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ctx.Data["OwnForkRepoBranches"] = branches
|
|
|
|
ctx.Data["OwnForkRepoTags"] = tags
|
|
|
|
}
|
2020-05-12 11:22:46 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-08 01:59:29 +05:30
|
|
|
// Check if head branch is valid.
|
2021-09-27 17:49:34 +05:30
|
|
|
headIsCommit := ci.HeadGitRepo.IsCommitExist(ci.HeadBranch)
|
|
|
|
headIsBranch := ci.HeadGitRepo.IsBranchExist(ci.HeadBranch)
|
|
|
|
headIsTag := ci.HeadGitRepo.IsTagExist(ci.HeadBranch)
|
2019-06-08 01:59:29 +05:30
|
|
|
if !headIsCommit && !headIsBranch && !headIsTag {
|
|
|
|
// Check if headBranch is short sha commit hash
|
2021-09-27 17:49:34 +05:30
|
|
|
if headCommit, _ := ci.HeadGitRepo.GetCommit(ci.HeadBranch); headCommit != nil {
|
|
|
|
ci.HeadBranch = headCommit.ID.String()
|
|
|
|
ctx.Data["HeadBranch"] = ci.HeadBranch
|
2019-06-08 01:59:29 +05:30
|
|
|
headIsCommit = true
|
|
|
|
} else {
|
|
|
|
ctx.NotFound("IsRefExist", nil)
|
2021-09-27 17:49:34 +05:30
|
|
|
return nil
|
2019-06-08 01:59:29 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
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",
|
2022-03-22 12:33:22 +05:30
|
|
|
ctx.Doer,
|
2019-06-08 01:59:29 +05:30
|
|
|
baseRepo,
|
|
|
|
permBase)
|
|
|
|
}
|
|
|
|
ctx.NotFound("ParseCompareInfo", nil)
|
2021-09-27 17:49:34 +05:30
|
|
|
return nil
|
2019-06-08 01:59:29 +05:30
|
|
|
}
|
|
|
|
|
2021-09-27 17:49:34 +05:30
|
|
|
baseBranchRef := ci.BaseBranch
|
2020-06-12 05:19:47 +05:30
|
|
|
if baseIsBranch {
|
2021-09-27 17:49:34 +05:30
|
|
|
baseBranchRef = git.BranchPrefix + ci.BaseBranch
|
2020-06-12 05:19:47 +05:30
|
|
|
} else if baseIsTag {
|
2021-09-27 17:49:34 +05:30
|
|
|
baseBranchRef = git.TagPrefix + ci.BaseBranch
|
2020-06-12 05:19:47 +05:30
|
|
|
}
|
2021-09-27 17:49:34 +05:30
|
|
|
headBranchRef := ci.HeadBranch
|
2020-06-12 05:19:47 +05:30
|
|
|
if headIsBranch {
|
2021-09-27 17:49:34 +05:30
|
|
|
headBranchRef = git.BranchPrefix + ci.HeadBranch
|
2020-06-12 05:19:47 +05:30
|
|
|
} else if headIsTag {
|
2021-09-27 17:49:34 +05:30
|
|
|
headBranchRef = git.TagPrefix + ci.HeadBranch
|
2020-06-12 05:19:47 +05:30
|
|
|
}
|
|
|
|
|
2021-10-15 21:35:33 +05:30
|
|
|
ci.CompareInfo, err = ci.HeadGitRepo.GetCompareInfo(baseRepo.RepoPath(), baseBranchRef, headBranchRef, ci.DirectComparison, fileOnly)
|
2019-06-08 01:59:29 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetCompareInfo", err)
|
2021-09-27 17:49:34 +05:30
|
|
|
return nil
|
2019-06-08 01:59:29 +05:30
|
|
|
}
|
2021-09-27 17:49:34 +05:30
|
|
|
ctx.Data["BeforeCommitID"] = ci.CompareInfo.MergeBase
|
2019-06-08 01:59:29 +05:30
|
|
|
|
2021-09-27 17:49:34 +05:30
|
|
|
return ci
|
2019-06-08 01:59:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareCompareDiff renders compare diff page
|
|
|
|
func PrepareCompareDiff(
|
|
|
|
ctx *context.Context,
|
2021-09-27 17:49:34 +05:30
|
|
|
ci *CompareInfo,
|
Refactor git command package to improve security and maintainability (#22678)
This PR follows #21535 (and replace #22592)
## Review without space diff
https://github.com/go-gitea/gitea/pull/22678/files?diff=split&w=1
## Purpose of this PR
1. Make git module command completely safe (risky user inputs won't be
passed as argument option anymore)
2. Avoid low-level mistakes like
https://github.com/go-gitea/gitea/pull/22098#discussion_r1045234918
3. Remove deprecated and dirty `CmdArgCheck` function, hide the `CmdArg`
type
4. Simplify code when using git command
## The main idea of this PR
* Move the `git.CmdArg` to the `internal` package, then no other package
except `git` could use it. Then developers could never do
`AddArguments(git.CmdArg(userInput))` any more.
* Introduce `git.ToTrustedCmdArgs`, it's for user-provided and already
trusted arguments. It's only used in a few cases, for example: use git
arguments from config file, help unit test with some arguments.
* Introduce `AddOptionValues` and `AddOptionFormat`, they make code more
clear and simple:
* Before: `AddArguments("-m").AddDynamicArguments(message)`
* After: `AddOptionValues("-m", message)`
* -
* Before: `AddArguments(git.CmdArg(fmt.Sprintf("--author='%s <%s>'",
sig.Name, sig.Email)))`
* After: `AddOptionFormat("--author='%s <%s>'", sig.Name, sig.Email)`
## FAQ
### Why these changes were not done in #21535 ?
#21535 is mainly a search&replace, it did its best to not change too
much logic.
Making the framework better needs a lot of changes, so this separate PR
is needed as the second step.
### The naming of `AddOptionXxx`
According to git's manual, the `--xxx` part is called `option`.
### How can it guarantee that `internal.CmdArg` won't be not misused?
Go's specification guarantees that. Trying to access other package's
internal package causes compilation error.
And, `golangci-lint` also denies the git/internal package. Only the
`git/command.go` can use it carefully.
### There is still a `ToTrustedCmdArgs`, will it still allow developers
to make mistakes and pass untrusted arguments?
Generally speaking, no. Because when using `ToTrustedCmdArgs`, the code
will be very complex (see the changes for examples). Then developers and
reviewers can know that something might be unreasonable.
### Why there was a `CmdArgCheck` and why it's removed?
At the moment of #21535, to reduce unnecessary changes, `CmdArgCheck`
was introduced as a hacky patch. Now, almost all code could be written
as `cmd := NewCommand(); cmd.AddXxx(...)`, then there is no need for
`CmdArgCheck` anymore.
### Why many codes for `signArg == ""` is deleted?
Because in the old code, `signArg` could never be empty string, it's
either `-S[key-id]` or `--no-gpg-sign`. So the `signArg == ""` is just
dead code.
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-02-04 08:00:43 +05:30
|
|
|
whitespaceBehavior git.TrustedCmdArgs,
|
2022-01-20 23:16:10 +05:30
|
|
|
) bool {
|
2019-06-08 01:59:29 +05:30
|
|
|
var (
|
|
|
|
repo = ctx.Repo.Repository
|
|
|
|
err error
|
|
|
|
title string
|
|
|
|
)
|
|
|
|
|
|
|
|
// Get diff information.
|
2021-09-27 17:49:34 +05:30
|
|
|
ctx.Data["CommitRepoLink"] = ci.HeadRepo.Link()
|
2019-06-08 01:59:29 +05:30
|
|
|
|
2021-09-27 17:49:34 +05:30
|
|
|
headCommitID := ci.CompareInfo.HeadCommitID
|
2019-06-08 01:59:29 +05:30
|
|
|
|
|
|
|
ctx.Data["AfterCommitID"] = headCommitID
|
|
|
|
|
2021-09-27 17:49:34 +05:30
|
|
|
if (headCommitID == ci.CompareInfo.MergeBase && !ci.DirectComparison) ||
|
|
|
|
headCommitID == ci.CompareInfo.BaseCommitID {
|
2019-06-08 01:59:29 +05:30
|
|
|
ctx.Data["IsNothingToCompare"] = true
|
2022-12-10 08:16:31 +05:30
|
|
|
if unit, err := repo.GetUnit(ctx, unit.TypePullRequests); err == nil {
|
2021-03-04 09:11:23 +05:30
|
|
|
config := unit.PullRequestsConfig()
|
2021-03-29 12:28:48 +05:30
|
|
|
|
2021-03-04 09:11:23 +05:30
|
|
|
if !config.AutodetectManualMerge {
|
2021-09-27 17:49:34 +05:30
|
|
|
allowEmptyPr := !(ci.BaseBranch == ci.HeadBranch && ctx.Repo.Repository.Name == ci.HeadRepo.Name)
|
2021-03-29 12:28:48 +05:30
|
|
|
ctx.Data["AllowEmptyPr"] = allowEmptyPr
|
|
|
|
|
|
|
|
return !allowEmptyPr
|
2021-03-04 09:11:23 +05:30
|
|
|
}
|
2021-03-29 12:28:48 +05:30
|
|
|
|
|
|
|
ctx.Data["AllowEmptyPr"] = false
|
2021-03-04 09:11:23 +05:30
|
|
|
}
|
2019-06-08 01:59:29 +05:30
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-09-27 17:49:34 +05:30
|
|
|
beforeCommitID := ci.CompareInfo.MergeBase
|
|
|
|
if ci.DirectComparison {
|
|
|
|
beforeCommitID = ci.CompareInfo.BaseCommitID
|
|
|
|
}
|
|
|
|
|
2021-11-21 22:21:08 +05:30
|
|
|
maxLines, maxFiles := setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffFiles
|
|
|
|
files := ctx.FormStrings("files")
|
|
|
|
if len(files) == 2 || len(files) == 1 {
|
|
|
|
maxLines, maxFiles = -1, -1
|
|
|
|
}
|
|
|
|
|
|
|
|
diff, err := gitdiff.GetDiff(ci.HeadGitRepo,
|
|
|
|
&gitdiff.DiffOptions{
|
|
|
|
BeforeCommitID: beforeCommitID,
|
|
|
|
AfterCommitID: headCommitID,
|
|
|
|
SkipTo: ctx.FormString("skip-to"),
|
|
|
|
MaxLines: maxLines,
|
|
|
|
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
|
|
|
|
MaxFiles: maxFiles,
|
|
|
|
WhitespaceBehavior: whitespaceBehavior,
|
|
|
|
DirectComparison: ci.DirectComparison,
|
|
|
|
}, ctx.FormStrings("files")...)
|
2019-06-08 01:59:29 +05:30
|
|
|
if err != nil {
|
2021-02-13 10:05:43 +05:30
|
|
|
ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err)
|
2019-06-08 01:59:29 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
ctx.Data["Diff"] = diff
|
2020-05-26 11:28:07 +05:30
|
|
|
ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0
|
2019-06-08 01:59:29 +05:30
|
|
|
|
2021-09-27 17:49:34 +05:30
|
|
|
headCommit, err := ci.HeadGitRepo.GetCommit(headCommitID)
|
2019-06-08 01:59:29 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetCommit", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-09-16 14:33:22 +05:30
|
|
|
baseGitRepo := ctx.Repo.GitRepo
|
|
|
|
|
2023-02-20 09:26:07 +05:30
|
|
|
beforeCommit, err := baseGitRepo.GetCommit(beforeCommitID)
|
2019-09-16 14:33:22 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetCommit", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-01-09 09:20:54 +05:30
|
|
|
commits := git_model.ConvertFromGitCommit(ctx, ci.CompareInfo.Commits, ci.HeadRepo)
|
2021-08-09 23:38:51 +05:30
|
|
|
ctx.Data["Commits"] = commits
|
|
|
|
ctx.Data["CommitCount"] = len(commits)
|
|
|
|
|
|
|
|
if len(commits) == 1 {
|
|
|
|
c := commits[0]
|
2019-06-08 01:59:29 +05:30
|
|
|
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 17:49:34 +05:30
|
|
|
title = ci.HeadBranch
|
2019-06-08 01:59:29 +05:30
|
|
|
}
|
2021-07-25 08:29:27 +05:30
|
|
|
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 01:59:29 +05:30
|
|
|
ctx.Data["title"] = title
|
2021-09-27 17:49:34 +05:30
|
|
|
ctx.Data["Username"] = ci.HeadUser.Name
|
|
|
|
ctx.Data["Reponame"] = ci.HeadRepo.Name
|
2019-06-08 01:59:29 +05:30
|
|
|
|
2023-02-20 09:26:07 +05:30
|
|
|
setCompareContext(ctx, beforeCommit, headCommit, ci.HeadUser.Name, repo.Name)
|
2019-09-16 14:33:22 +05:30
|
|
|
|
2019-06-08 01:59:29 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
func getBranchesAndTagsForRepo(ctx gocontext.Context, repo *repo_model.Repository) (branches, tags []string, err error) {
|
2022-03-30 00:43:41 +05:30
|
|
|
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
|
2019-10-30 11:28:18 +05:30
|
|
|
if err != nil {
|
2021-10-15 21:35:33 +05:30
|
|
|
return nil, nil, err
|
2019-10-30 11:28:18 +05:30
|
|
|
}
|
2020-05-12 11:22:46 +05:30
|
|
|
defer gitRepo.Close()
|
2019-11-13 12:31:19 +05:30
|
|
|
|
2021-12-09 00:38:16 +05:30
|
|
|
branches, _, err = gitRepo.GetBranchNames(0, 0)
|
2019-10-30 11:28:18 +05:30
|
|
|
if err != nil {
|
2021-10-15 21:35:33 +05:30
|
|
|
return nil, nil, err
|
2019-10-30 11:28:18 +05:30
|
|
|
}
|
2021-10-15 21:35:33 +05:30
|
|
|
tags, err = gitRepo.GetTags(0, 0)
|
2021-05-08 02:40:05 +05:30
|
|
|
if err != nil {
|
2021-10-15 21:35:33 +05:30
|
|
|
return nil, nil, err
|
2021-05-08 02:40:05 +05:30
|
|
|
}
|
2021-10-15 21:35:33 +05:30
|
|
|
return branches, tags, nil
|
2019-10-30 11:28:18 +05:30
|
|
|
}
|
|
|
|
|
2019-06-08 01:59:29 +05:30
|
|
|
// CompareDiff show different from one commit to another commit
|
|
|
|
func CompareDiff(ctx *context.Context) {
|
2021-09-27 17:49:34 +05:30
|
|
|
ci := ParseCompareInfo(ctx)
|
2021-08-31 09:46:23 +05:30
|
|
|
defer func() {
|
2021-10-01 01:01:02 +05:30
|
|
|
if ci != nil && ci.HeadGitRepo != nil {
|
2021-09-27 17:49:34 +05:30
|
|
|
ci.HeadGitRepo.Close()
|
2021-08-31 09:46:23 +05:30
|
|
|
}
|
|
|
|
}()
|
2019-06-08 01:59:29 +05:30
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2019-11-13 12:31:19 +05:30
|
|
|
|
2021-12-17 00:31:14 +05:30
|
|
|
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
|
2021-09-27 17:49:34 +05:30
|
|
|
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 10:05:43 +05:30
|
|
|
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
|
2019-06-08 01:59:29 +05:30
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-16 22:31:10 +05:30
|
|
|
baseTags, err := repo_model.GetTagNamesByRepoID(ctx, ctx.Repo.Repository.ID)
|
2021-05-08 02:40:05 +05:30
|
|
|
if err != nil {
|
2023-03-16 22:31:10 +05:30
|
|
|
ctx.ServerError("GetTagNamesByRepoID", err)
|
2021-05-08 02:40:05 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Tags"] = baseTags
|
2019-08-11 20:53:49 +05:30
|
|
|
|
2021-10-15 21:35:33 +05:30
|
|
|
fileOnly := ctx.FormBool("file-only")
|
|
|
|
if fileOnly {
|
|
|
|
ctx.HTML(http.StatusOK, tplDiffBox)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-09 00:38:16 +05:30
|
|
|
headBranches, _, err := ci.HeadGitRepo.GetBranchNames(0, 0)
|
2021-05-08 02:40:05 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetBranches", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["HeadBranches"] = headBranches
|
|
|
|
|
2023-03-16 22:31:10 +05:30
|
|
|
headTags, err := repo_model.GetTagNamesByRepoID(ctx, ci.HeadRepo.ID)
|
2021-05-08 02:40:05 +05:30
|
|
|
if err != nil {
|
2023-03-16 22:31:10 +05:30
|
|
|
ctx.ServerError("GetTagNamesByRepoID", err)
|
2021-05-08 02:40:05 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["HeadTags"] = headTags
|
|
|
|
|
|
|
|
if ctx.Data["PageIsComparePull"] == true {
|
2022-11-19 13:42:33 +05:30
|
|
|
pr, err := issues_model.GetUnmergedPullRequest(ctx, ci.HeadRepo.ID, ctx.Repo.Repository.ID, ci.HeadBranch, ci.BaseBranch, issues_model.PullRequestFlowGithub)
|
2019-06-08 01:59:29 +05:30
|
|
|
if err != nil {
|
2022-06-13 15:07:59 +05:30
|
|
|
if !issues_model.IsErrPullRequestNotExist(err) {
|
2019-06-08 01:59:29 +05:30
|
|
|
ctx.ServerError("GetUnmergedPullRequest", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx.Data["HasPullRequest"] = true
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
2021-12-24 17:44:42 +05:30
|
|
|
ctx.ServerError("LoadIssue", err)
|
|
|
|
return
|
|
|
|
}
|
2019-06-08 01:59:29 +05:30
|
|
|
ctx.Data["PullRequest"] = pr
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplCompareDiff)
|
2019-06-08 01:59:29 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !nothingToCompare {
|
|
|
|
// Setup information for new form.
|
2020-01-19 12:13:38 +05:30
|
|
|
RetrieveRepoMetas(ctx, ctx.Repo.Repository, true)
|
2019-06-08 01:59:29 +05:30
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
beforeCommitID := ctx.Data["BeforeCommitID"].(string)
|
|
|
|
afterCommitID := ctx.Data["AfterCommitID"].(string)
|
|
|
|
|
2021-09-27 17:49:34 +05:30
|
|
|
separator := "..."
|
|
|
|
if ci.DirectComparison {
|
|
|
|
separator = ".."
|
|
|
|
}
|
|
|
|
ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitID) + separator + base.ShortSha(afterCommitID)
|
2019-06-08 01:59:29 +05:30
|
|
|
|
|
|
|
ctx.Data["IsRepoToolbarCommits"] = true
|
|
|
|
ctx.Data["IsDiffCompare"] = true
|
|
|
|
ctx.Data["RequireTribute"] = true
|
2022-09-02 13:28:49 +05:30
|
|
|
templateErrs := setTemplateIfExists(ctx, pullRequestTemplateKey, pullRequestTemplateCandidates)
|
|
|
|
|
|
|
|
if len(templateErrs) > 0 {
|
|
|
|
ctx.Flash.Warning(renderErrorOfTemplates(ctx, templateErrs), true)
|
|
|
|
}
|
2022-07-24 09:15:33 +05:30
|
|
|
|
2023-01-30 20:09:07 +05:30
|
|
|
if content, ok := ctx.Data["content"].(string); ok && content != "" {
|
|
|
|
// If a template content is set, prepend the "content". In this case that's only
|
|
|
|
// applicable if you have one commit to compare and that commit has a message.
|
|
|
|
// In that case the commit message will be prepend to the template body.
|
|
|
|
if templateContent, ok := ctx.Data[pullRequestTemplateKey].(string); ok && templateContent != "" {
|
2022-07-24 09:15:33 +05:30
|
|
|
// Re-use the same key as that's priortized over the "content" key.
|
|
|
|
// Add two new lines between the content to ensure there's always at least
|
|
|
|
// one empty line between them.
|
|
|
|
ctx.Data[pullRequestTemplateKey] = content + "\n\n" + templateContent
|
|
|
|
}
|
2023-01-30 20:09:07 +05:30
|
|
|
|
|
|
|
// When using form fields, also add content to field with id "body".
|
|
|
|
if fields, ok := ctx.Data["Fields"].([]*api.IssueFormField); ok {
|
|
|
|
for _, field := range fields {
|
|
|
|
if field.ID == "body" {
|
|
|
|
if fieldValue, ok := field.Attributes["value"].(string); ok && fieldValue != "" {
|
|
|
|
field.Attributes["value"] = content + "\n\n" + fieldValue
|
|
|
|
} else {
|
|
|
|
field.Attributes["value"] = content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-24 09:15:33 +05:30
|
|
|
}
|
|
|
|
|
2020-10-05 11:19:33 +05:30
|
|
|
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
|
|
|
|
upload.AddUploadContext(ctx, "comment")
|
2019-06-08 01:59:29 +05:30
|
|
|
|
2021-11-10 01:27:58 +05:30
|
|
|
ctx.Data["HasIssuesOrPullsWritePermission"] = ctx.Repo.CanWrite(unit.TypePullRequests)
|
2020-04-04 11:09:48 +05:30
|
|
|
|
2023-02-13 11:39:52 +05:30
|
|
|
if unit, err := ctx.Repo.Repository.GetUnit(ctx, unit.TypePullRequests); err == nil {
|
|
|
|
config := unit.PullRequestsConfig()
|
|
|
|
ctx.Data["AllowMaintainerEdit"] = config.DefaultAllowMaintainerEdit
|
|
|
|
} else {
|
|
|
|
ctx.Data["AllowMaintainerEdit"] = false
|
|
|
|
}
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplCompare)
|
2019-06-08 01:59:29 +05:30
|
|
|
}
|
2019-11-15 08:22:59 +05:30
|
|
|
|
|
|
|
// ExcerptBlob render blob excerpt contents
|
|
|
|
func ExcerptBlob(ctx *context.Context) {
|
|
|
|
commitID := ctx.Params("sha")
|
2021-07-29 07:12:15 +05:30
|
|
|
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 06:01:13 +05:30
|
|
|
anchor := ctx.FormString("anchor")
|
|
|
|
direction := ctx.FormString("direction")
|
|
|
|
filePath := ctx.FormString("path")
|
2019-11-15 08:22:59 +05:30
|
|
|
gitRepo := ctx.Repo.GitRepo
|
2022-02-05 23:56:12 +05:30
|
|
|
if ctx.FormBool("wiki") {
|
|
|
|
var err error
|
2022-03-30 00:43:41 +05:30
|
|
|
gitRepo, err = git.OpenRepository(ctx, ctx.Repo.Repository.WikiPath())
|
2022-02-05 23:56:12 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("OpenRepository", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
}
|
2020-08-20 20:23:06 +05:30
|
|
|
chunkSize := gitdiff.BlobExcerptChunkSize
|
2019-11-15 08:22:59 +05:30
|
|
|
commit, err := gitRepo.GetCommit(commitID)
|
|
|
|
if err != nil {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "GetCommit")
|
2019-11-15 08:22:59 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
section := &gitdiff.DiffSection{
|
2020-07-12 23:55:05 +05:30
|
|
|
FileName: filePath,
|
|
|
|
Name: filePath,
|
2019-11-15 08:22:59 +05:30
|
|
|
}
|
|
|
|
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-24 21:17:46 +05:30
|
|
|
offset := -1
|
|
|
|
if direction == "down" {
|
|
|
|
offset = 0
|
|
|
|
}
|
|
|
|
section.Lines, err = getExcerptLines(commit, filePath, lastLeft, lastRight, idxRight-lastRight+offset)
|
2019-11-15 08:22:59 +05:30
|
|
|
leftHunkSize = 0
|
|
|
|
rightHunkSize = 0
|
|
|
|
idxLeft = lastLeft
|
|
|
|
idxRight = lastRight
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "getExcerptLines")
|
2019-11-15 08:22:59 +05:30
|
|
|
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,
|
2022-01-20 23:16:10 +05:30
|
|
|
},
|
|
|
|
}
|
2019-11-15 08:22:59 +05:30
|
|
|
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
|
2022-05-09 03:59:50 +05:30
|
|
|
ctx.Data["FileNameHash"] = base.EncodeSha1(filePath)
|
2019-11-15 08:22:59 +05:30
|
|
|
ctx.Data["AfterCommitID"] = commitID
|
|
|
|
ctx.Data["Anchor"] = anchor
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplBlobExcerpt)
|
2019-11-15 08:22:59 +05:30
|
|
|
}
|
|
|
|
|
2021-12-20 10:11:31 +05:30
|
|
|
func getExcerptLines(commit *git.Commit, filePath string, idxLeft, idxRight, chunkSize int) ([]*gitdiff.DiffLine, error) {
|
2019-11-15 08:22:59 +05:30
|
|
|
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
|
|
|
|
}
|