2019-06-07 22:29:29 +02:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-06-07 22:29:29 +02:00
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2019-11-15 10:52:59 +08:00
|
|
|
"bufio"
|
2022-01-19 23:26:57 +00:00
|
|
|
gocontext "context"
|
2021-03-29 22:44:28 +02:00
|
|
|
"encoding/csv"
|
|
|
|
"errors"
|
2019-10-04 21:58:54 +02:00
|
|
|
"fmt"
|
2019-11-15 10:52:59 +08:00
|
|
|
"html"
|
2021-10-25 00:42:32 +02:00
|
|
|
"io"
|
2021-04-05 17:30:52 +02:00
|
|
|
"net/http"
|
2021-11-16 18:18:25 +00:00
|
|
|
"net/url"
|
2021-03-29 22:44:28 +02:00
|
|
|
"path/filepath"
|
2019-06-07 22:29:29 +02:00
|
|
|
"strings"
|
|
|
|
|
2023-06-29 18:03:20 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 23:51:54 +08:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-06-13 17:37:59 +08:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2022-05-11 18:09:36 +08:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 03:57:58 +08:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-06-07 22:29:29 +02:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
2021-03-29 22:44:28 +02:00
|
|
|
"code.gitea.io/gitea/modules/charset"
|
|
|
|
csv_module "code.gitea.io/gitea/modules/csv"
|
2019-06-07 22:29:29 +02:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 04:09:51 +08:00
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2019-06-07 22:29:29 +02:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-10-30 09:50:40 -06:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2024-02-23 03:18:33 +01:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2019-06-07 22:29:29 +02:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2023-01-30 15:39:07 +01:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2023-10-11 14:34:21 +02:00
|
|
|
"code.gitea.io/gitea/modules/typesniffer"
|
2021-07-25 03:59:27 +01:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2024-04-16 11:45:04 +08:00
|
|
|
"code.gitea.io/gitea/routers/common"
|
2024-02-27 15:12:22 +08:00
|
|
|
"code.gitea.io/gitea/services/context"
|
|
|
|
"code.gitea.io/gitea/services/context/upload"
|
2019-09-06 10:20:09 +08:00
|
|
|
"code.gitea.io/gitea/services/gitdiff"
|
2019-06-07 22:29:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-11-15 10:52:59 +08:00
|
|
|
tplCompare base.TplName = "repo/diff/compare"
|
|
|
|
tplBlobExcerpt base.TplName = "repo/diff/blob_excerpt"
|
2021-10-15 17:05:33 +01:00
|
|
|
tplDiffBox base.TplName = "repo/diff/box"
|
2019-06-07 22:29:29 +02:00
|
|
|
)
|
|
|
|
|
2021-03-29 22:44:28 +02:00
|
|
|
// setCompareContext sets context data.
|
2023-02-19 20:56:07 -07:00
|
|
|
func setCompareContext(ctx *context.Context, before, head *git.Commit, headOwner, headName string) {
|
|
|
|
ctx.Data["BeforeCommit"] = before
|
2021-03-29 22:44:28 +02:00
|
|
|
ctx.Data["HeadCommit"] = head
|
|
|
|
|
2021-06-05 14:32:19 +02: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
|
|
|
|
}
|
|
|
|
|
2023-10-11 14:34:21 +02:00
|
|
|
ctx.Data["GetSniffedTypeForBlob"] = func(blob *git.Blob) typesniffer.SniffedType {
|
|
|
|
st := typesniffer.SniffedType{}
|
|
|
|
|
|
|
|
if blob == nil {
|
|
|
|
return st
|
|
|
|
}
|
|
|
|
|
|
|
|
st, err := blob.GuessContentType()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GuessContentType failed: %v", err)
|
|
|
|
return st
|
|
|
|
}
|
|
|
|
return st
|
|
|
|
}
|
|
|
|
|
2023-02-19 20:56:07 -07:00
|
|
|
setPathsCompareContext(ctx, before, head, headOwner, headName)
|
2021-06-05 14:32:19 +02:00
|
|
|
setImageCompareContext(ctx)
|
2021-03-29 22:44:28 +02:00
|
|
|
setCsvCompareContext(ctx)
|
|
|
|
}
|
|
|
|
|
2021-11-16 18:18:25 +00:00
|
|
|
// 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-04 21:58:54 +02:00
|
|
|
|
2021-11-16 18:18:25 +00:00
|
|
|
// 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 05:41:31 +01:00
|
|
|
func setPathsCompareContext(ctx *context.Context, base, head *git.Commit, headOwner, headName string) {
|
2021-11-16 18:18:25 +00:00
|
|
|
ctx.Data["SourcePath"] = SourceCommitURL(headOwner, headName, head)
|
|
|
|
ctx.Data["RawPath"] = RawCommitURL(headOwner, headName, head)
|
2019-10-04 21:58:54 +02:00
|
|
|
if base != nil {
|
2022-02-25 07:46:15 +01:00
|
|
|
ctx.Data["BeforeSourcePath"] = SourceCommitURL(headOwner, headName, base)
|
|
|
|
ctx.Data["BeforeRawPath"] = RawCommitURL(headOwner, headName, base)
|
2019-10-04 21:58:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setImageCompareContext sets context data that is required by image compare template
|
2021-06-05 14:32:19 +02:00
|
|
|
func setImageCompareContext(ctx *context.Context) {
|
2023-10-11 14:34:21 +02:00
|
|
|
ctx.Data["IsSniffedTypeAnImage"] = func(st typesniffer.SniffedType) bool {
|
2021-06-05 14:32:19 +02:00
|
|
|
return st.IsImage() && (setting.UI.SVG.Enabled || !st.IsSvgImage())
|
2019-10-04 21:58:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 22:44:28 +02: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
|
|
|
|
}
|
|
|
|
|
2022-09-17 04:45:32 +02:00
|
|
|
ctx.Data["CreateCsvDiff"] = func(diffFile *gitdiff.DiffFile, baseBlob, headBlob *git.Blob) CsvDiffResult {
|
|
|
|
if diffFile == nil {
|
2021-03-29 22:44:28 +02:00
|
|
|
return CsvDiffResult{nil, ""}
|
|
|
|
}
|
|
|
|
|
2024-02-15 05:48:45 +08:00
|
|
|
errTooLarge := errors.New(ctx.Locale.TrString("repo.error.csv.too_large"))
|
2021-03-29 22:44:28 +02:00
|
|
|
|
2022-09-17 04:45:32 +02:00
|
|
|
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-29 22:44:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if setting.UI.CSV.MaxFileSize != 0 && setting.UI.CSV.MaxFileSize < blob.Size() {
|
2021-10-25 00:42:32 +02:00
|
|
|
return nil, nil, errTooLarge
|
2021-03-29 22:44:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
reader, err := blob.DataAsync()
|
|
|
|
if err != nil {
|
2021-10-25 00:42:32 +02:00
|
|
|
return nil, nil, err
|
2021-03-29 22:44:28 +02:00
|
|
|
}
|
|
|
|
|
2024-01-27 19:02:51 +01:00
|
|
|
csvReader, err := csv_module.CreateReaderAndDetermineDelimiter(ctx, charset.ToUTF8WithFallbackReader(reader, charset.ConvertOpts{}))
|
2021-10-25 00:42:32 +02:00
|
|
|
return csvReader, reader, err
|
2021-03-29 22:44:28 +02:00
|
|
|
}
|
|
|
|
|
2024-11-22 13:48:09 +08:00
|
|
|
baseReader, baseBlobCloser, err := csvReaderFromCommit(markup.NewRenderContext(ctx).WithRelativePath(diffFile.OldName), baseBlob)
|
2021-10-25 00:42:32 +02:00
|
|
|
if baseBlobCloser != nil {
|
|
|
|
defer baseBlobCloser.Close()
|
|
|
|
}
|
2022-05-02 18:46:50 +02:00
|
|
|
if err != nil {
|
2022-09-17 04:45:32 +02:00
|
|
|
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 18:46:50 +02:00
|
|
|
}
|
|
|
|
|
2024-11-22 13:48:09 +08:00
|
|
|
headReader, headBlobCloser, err := csvReaderFromCommit(markup.NewRenderContext(ctx).WithRelativePath(diffFile.Name), headBlob)
|
2021-10-25 00:42:32 +02:00
|
|
|
if headBlobCloser != nil {
|
|
|
|
defer headBlobCloser.Close()
|
|
|
|
}
|
2022-05-02 18:46:50 +02:00
|
|
|
if err != nil {
|
2022-09-17 04:45:32 +02:00
|
|
|
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 18:46:50 +02:00
|
|
|
}
|
2021-03-29 22:44:28 +02:00
|
|
|
|
|
|
|
sections, err := gitdiff.CreateCsvDiff(diffFile, baseReader, headReader)
|
|
|
|
if err != nil {
|
|
|
|
errMessage, err := csv_module.FormatError(err, ctx.Locale)
|
|
|
|
if err != nil {
|
2022-05-02 18:46:50 +02:00
|
|
|
log.Error("CreateCsvDiff FormatError failed: %v", err)
|
|
|
|
return CsvDiffResult{nil, "unknown csv diff error"}
|
2021-03-29 22:44:28 +02:00
|
|
|
}
|
|
|
|
return CsvDiffResult{nil, errMessage}
|
|
|
|
}
|
|
|
|
return CsvDiffResult{sections, ""}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 22:29:29 +02:00
|
|
|
// ParseCompareInfo parse compare info between two commit for preparing comparing references
|
2024-12-10 21:54:57 -08:00
|
|
|
// Permission check for base repository's code read should be checked before invoking this function
|
2024-04-16 11:45:04 +08:00
|
|
|
func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
|
2024-12-10 21:54:57 -08:00
|
|
|
fileOnly := ctx.FormBool("file-only")
|
|
|
|
pathParam := ctx.PathParam("*")
|
|
|
|
baseRepo := ctx.Repo.Repository
|
2021-09-27 13:19:34 +01:00
|
|
|
|
2024-12-10 21:54:57 -08:00
|
|
|
ci, err := common.ParseComparePathParams(ctx, pathParam, baseRepo, ctx.Repo.GitRepo)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case user_model.IsErrUserNotExist(err):
|
|
|
|
ctx.NotFound("GetUserByName", nil)
|
|
|
|
case repo_model.IsErrRepoNotExist(err):
|
|
|
|
ctx.NotFound("GetRepositoryByOwnerAndName", nil)
|
|
|
|
case errors.Is(err, util.ErrInvalidArgument):
|
|
|
|
ctx.NotFound("ParseComparePathParams", nil)
|
|
|
|
default:
|
|
|
|
ctx.ServerError("GetRepositoryByOwnerAndName", err)
|
2021-12-06 11:04:07 -06:00
|
|
|
}
|
2024-12-10 21:54:57 -08:00
|
|
|
return nil
|
2021-09-27 13:19:34 +01:00
|
|
|
}
|
2024-12-10 21:54:57 -08:00
|
|
|
defer ci.Close()
|
2021-09-27 13:19:34 +01:00
|
|
|
|
2024-12-10 21:54:57 -08:00
|
|
|
// remove the check when we support compare with carets
|
|
|
|
if ci.CaretTimes > 0 {
|
|
|
|
ctx.NotFound("Unsupported compare", nil)
|
2021-09-27 13:19:34 +01:00
|
|
|
return nil
|
2019-06-07 22:29:29 +02:00
|
|
|
}
|
2024-12-10 21:54:57 -08:00
|
|
|
|
|
|
|
if ci.BaseOriRef == ctx.Repo.GetObjectFormat().EmptyObjectID().String() {
|
|
|
|
if ci.IsSameRepo() {
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ci.HeadOriRef))
|
2019-06-07 22:29:29 +02:00
|
|
|
} else {
|
2024-12-10 21:54:57 -08:00
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ci.HeadRepo.FullName()) + ":" + util.PathEscapeSegments(ci.HeadOriRef))
|
2019-06-07 22:29:29 +02:00
|
|
|
}
|
2024-12-10 21:54:57 -08:00
|
|
|
return nil
|
2019-06-07 22:29:29 +02:00
|
|
|
}
|
2024-12-10 21:54:57 -08:00
|
|
|
|
2020-05-12 06:52:46 +01:00
|
|
|
// If we're not merging from the same repo:
|
2024-12-10 21:54:57 -08:00
|
|
|
if !ci.IsSameRepo() {
|
2022-03-22 08:03:22 +01:00
|
|
|
// Assert ctx.Doer has permission to read headRepo's codes
|
2022-05-11 18:09:36 +08:00
|
|
|
permHead, err := access_model.GetUserRepoPermission(ctx, ci.HeadRepo, ctx.Doer)
|
2020-01-17 03:59:07 +08:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetUserRepoPermission", err)
|
2021-09-27 13:19:34 +01:00
|
|
|
return nil
|
2020-01-17 03:59:07 +08:00
|
|
|
}
|
2021-11-10 03:57:58 +08:00
|
|
|
if !permHead.CanRead(unit.TypeCode) {
|
2020-01-17 03:59:07 +08:00
|
|
|
if log.IsTrace() {
|
|
|
|
log.Trace("Permission Denied: User: %-v cannot read code in Repo: %-v\nUser in headRepo has Permissions: %-+v",
|
2022-03-22 08:03:22 +01:00
|
|
|
ctx.Doer,
|
2021-09-27 13:19:34 +01:00
|
|
|
ci.HeadRepo,
|
2020-01-17 03:59:07 +08:00
|
|
|
permHead)
|
|
|
|
}
|
|
|
|
ctx.NotFound("ParseCompareInfo", nil)
|
2021-09-27 13:19:34 +01:00
|
|
|
return nil
|
2019-06-07 22:29:29 +02:00
|
|
|
}
|
2022-04-28 17:45:33 +02:00
|
|
|
ctx.Data["CanWriteToHeadRepo"] = permHead.CanWrite(unit.TypeCode)
|
2019-06-07 22:29:29 +02:00
|
|
|
}
|
|
|
|
|
2024-12-11 00:23:12 -08:00
|
|
|
// TODO: prepareRepos, branches and tags for dropdowns
|
2020-05-12 06:52:46 +01:00
|
|
|
|
2024-12-11 00:23:12 -08:00
|
|
|
ctx.Data["PageIsComparePull"] = ci.IsPull() && ctx.Repo.CanReadIssuesOrPulls(true)
|
|
|
|
ctx.Data["BaseName"] = baseRepo.OwnerName
|
|
|
|
ctx.Data["BaseBranch"] = ci.BaseOriRef
|
|
|
|
ctx.Data["HeadUser"] = ci.HeadUser
|
|
|
|
ctx.Data["HeadBranch"] = ci.HeadOriRef
|
|
|
|
ctx.Repo.PullRequest.SameRepo = ci.IsSameRepo()
|
2020-05-12 06:52:46 +01:00
|
|
|
|
2024-12-11 00:23:12 -08:00
|
|
|
ctx.Data["BaseIsCommit"] = ci.IsBaseCommit
|
|
|
|
ctx.Data["BaseIsBranch"] = ci.BaseFullRef.IsBranch()
|
|
|
|
ctx.Data["BaseIsTag"] = ci.BaseFullRef.IsTag()
|
|
|
|
ctx.Data["IsPull"] = true
|
|
|
|
// ctx.Data["OwnForkRepo"] = ownForkRepo FIXME: This is not used
|
|
|
|
|
|
|
|
ctx.Data["HeadRepo"] = ci.HeadRepo
|
|
|
|
ctx.Data["BaseCompareRepo"] = ctx.Repo.Repository
|
2024-12-10 21:54:57 -08:00
|
|
|
ctx.Data["HeadIsCommit"] = ci.IsHeadCommit
|
|
|
|
ctx.Data["HeadIsBranch"] = ci.HeadFullRef.IsBranch()
|
|
|
|
ctx.Data["HeadIsTag"] = ci.HeadFullRef.IsTag()
|
2020-06-12 00:49:47 +01:00
|
|
|
|
2024-12-10 21:54:57 -08:00
|
|
|
ci.CompareInfo, err = ci.HeadGitRepo.GetCompareInfo(baseRepo.RepoPath(), ci.BaseFullRef.String(), ci.HeadFullRef.String(), ci.DirectComparison(), fileOnly)
|
2019-06-07 22:29:29 +02:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetCompareInfo", err)
|
2021-09-27 13:19:34 +01:00
|
|
|
return nil
|
2019-06-07 22:29:29 +02:00
|
|
|
}
|
2024-12-10 21:54:57 -08:00
|
|
|
if ci.DirectComparison() {
|
2023-04-16 03:27:23 -04:00
|
|
|
ctx.Data["BeforeCommitID"] = ci.CompareInfo.BaseCommitID
|
|
|
|
} else {
|
|
|
|
ctx.Data["BeforeCommitID"] = ci.CompareInfo.MergeBase
|
|
|
|
}
|
2019-06-07 22:29:29 +02:00
|
|
|
|
2021-09-27 13:19:34 +01:00
|
|
|
return ci
|
2019-06-07 22:29:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareCompareDiff renders compare diff page
|
|
|
|
func PrepareCompareDiff(
|
|
|
|
ctx *context.Context,
|
2024-04-16 11:45:04 +08:00
|
|
|
ci *common.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 10:30:43 +08:00
|
|
|
whitespaceBehavior git.TrustedCmdArgs,
|
2022-01-20 18:46:10 +01:00
|
|
|
) bool {
|
2019-06-07 22:29:29 +02:00
|
|
|
var (
|
|
|
|
repo = ctx.Repo.Repository
|
|
|
|
err error
|
|
|
|
title string
|
|
|
|
)
|
|
|
|
|
|
|
|
// Get diff information.
|
2021-09-27 13:19:34 +01:00
|
|
|
ctx.Data["CommitRepoLink"] = ci.HeadRepo.Link()
|
2019-06-07 22:29:29 +02:00
|
|
|
|
2021-09-27 13:19:34 +01:00
|
|
|
headCommitID := ci.CompareInfo.HeadCommitID
|
2019-06-07 22:29:29 +02:00
|
|
|
|
|
|
|
ctx.Data["AfterCommitID"] = headCommitID
|
|
|
|
|
2024-12-10 21:54:57 -08:00
|
|
|
if (headCommitID == ci.CompareInfo.MergeBase && !ci.DirectComparison()) ||
|
2021-09-27 13:19:34 +01:00
|
|
|
headCommitID == ci.CompareInfo.BaseCommitID {
|
2019-06-07 22:29:29 +02:00
|
|
|
ctx.Data["IsNothingToCompare"] = true
|
2022-12-10 10:46:31 +08:00
|
|
|
if unit, err := repo.GetUnit(ctx, unit.TypePullRequests); err == nil {
|
2021-03-04 11:41:23 +08:00
|
|
|
config := unit.PullRequestsConfig()
|
2021-03-29 14:58:48 +08:00
|
|
|
|
2021-03-04 11:41:23 +08:00
|
|
|
if !config.AutodetectManualMerge {
|
2024-12-10 21:54:57 -08:00
|
|
|
allowEmptyPr := !(ci.BaseOriRef == ci.HeadOriRef && ctx.Repo.Repository.Name == ci.HeadRepo.Name)
|
2021-03-29 14:58:48 +08:00
|
|
|
ctx.Data["AllowEmptyPr"] = allowEmptyPr
|
|
|
|
|
|
|
|
return !allowEmptyPr
|
2021-03-04 11:41:23 +08:00
|
|
|
}
|
2021-03-29 14:58:48 +08:00
|
|
|
|
|
|
|
ctx.Data["AllowEmptyPr"] = false
|
2021-03-04 11:41:23 +08:00
|
|
|
}
|
2019-06-07 22:29:29 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-09-27 13:19:34 +01:00
|
|
|
beforeCommitID := ci.CompareInfo.MergeBase
|
2024-12-10 21:54:57 -08:00
|
|
|
if ci.DirectComparison() {
|
2021-09-27 13:19:34 +01:00
|
|
|
beforeCommitID = ci.CompareInfo.BaseCommitID
|
|
|
|
}
|
|
|
|
|
2021-11-21 16:51:08 +00:00
|
|
|
maxLines, maxFiles := setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffFiles
|
|
|
|
files := ctx.FormStrings("files")
|
|
|
|
if len(files) == 2 || len(files) == 1 {
|
|
|
|
maxLines, maxFiles = -1, -1
|
|
|
|
}
|
|
|
|
|
2024-11-01 22:29:37 -05:00
|
|
|
fileOnly := ctx.FormBool("file-only")
|
|
|
|
|
2023-10-03 12:30:41 +02:00
|
|
|
diff, err := gitdiff.GetDiff(ctx, ci.HeadGitRepo,
|
2021-11-21 16:51:08 +00:00
|
|
|
&gitdiff.DiffOptions{
|
|
|
|
BeforeCommitID: beforeCommitID,
|
|
|
|
AfterCommitID: headCommitID,
|
|
|
|
SkipTo: ctx.FormString("skip-to"),
|
|
|
|
MaxLines: maxLines,
|
|
|
|
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
|
|
|
|
MaxFiles: maxFiles,
|
|
|
|
WhitespaceBehavior: whitespaceBehavior,
|
2024-12-10 21:54:57 -08:00
|
|
|
DirectComparison: ci.DirectComparison(),
|
2024-11-01 22:29:37 -05:00
|
|
|
FileOnly: fileOnly,
|
2021-11-21 16:51:08 +00:00
|
|
|
}, ctx.FormStrings("files")...)
|
2019-06-07 22:29:29 +02:00
|
|
|
if err != nil {
|
2021-02-13 05:35:43 +01:00
|
|
|
ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err)
|
2019-06-07 22:29:29 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
ctx.Data["Diff"] = diff
|
2020-05-26 06:58:07 +01:00
|
|
|
ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0
|
2019-06-07 22:29:29 +02:00
|
|
|
|
2021-09-27 13:19:34 +01:00
|
|
|
headCommit, err := ci.HeadGitRepo.GetCommit(headCommitID)
|
2019-06-07 22:29:29 +02:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetCommit", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-09-16 11:03:22 +02:00
|
|
|
baseGitRepo := ctx.Repo.GitRepo
|
|
|
|
|
2023-02-19 20:56:07 -07:00
|
|
|
beforeCommit, err := baseGitRepo.GetCommit(beforeCommitID)
|
2019-09-16 11:03:22 +02:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetCommit", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-07-28 23:11:40 +08:00
|
|
|
commits := processGitCommits(ctx, ci.CompareInfo.Commits)
|
2021-08-09 20:08:51 +02:00
|
|
|
ctx.Data["Commits"] = commits
|
|
|
|
ctx.Data["CommitCount"] = len(commits)
|
|
|
|
|
|
|
|
if len(commits) == 1 {
|
|
|
|
c := commits[0]
|
2019-06-07 22:29:29 +02: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 {
|
2024-12-10 21:54:57 -08:00
|
|
|
title = ci.HeadOriRef
|
2019-06-07 22:29:29 +02:00
|
|
|
}
|
2021-07-25 03:59:27 +01: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-07 22:29:29 +02:00
|
|
|
ctx.Data["title"] = title
|
2021-09-27 13:19:34 +01:00
|
|
|
ctx.Data["Username"] = ci.HeadUser.Name
|
|
|
|
ctx.Data["Reponame"] = ci.HeadRepo.Name
|
2019-06-07 22:29:29 +02:00
|
|
|
|
2023-02-19 20:56:07 -07:00
|
|
|
setCompareContext(ctx, beforeCommit, headCommit, ci.HeadUser.Name, repo.Name)
|
2019-09-16 11:03:22 +02:00
|
|
|
|
2019-06-07 22:29:29 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-01-19 23:26:57 +00:00
|
|
|
func getBranchesAndTagsForRepo(ctx gocontext.Context, repo *repo_model.Repository) (branches, tags []string, err error) {
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 04:09:51 +08:00
|
|
|
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
|
2019-10-30 13:58:18 +08:00
|
|
|
if err != nil {
|
2021-10-15 17:05:33 +01:00
|
|
|
return nil, nil, err
|
2019-10-30 13:58:18 +08:00
|
|
|
}
|
2020-05-12 06:52:46 +01:00
|
|
|
defer gitRepo.Close()
|
2019-11-13 07:01:19 +00:00
|
|
|
|
2023-06-29 18:03:20 +08:00
|
|
|
branches, err = git_model.FindBranchNames(ctx, git_model.FindBranchOptions{
|
2024-03-22 20:53:52 +08:00
|
|
|
RepoID: repo.ID,
|
|
|
|
ListOptions: db.ListOptionsAll,
|
2024-02-23 03:18:33 +01:00
|
|
|
IsDeletedBranch: optional.Some(false),
|
2023-06-29 18:03:20 +08:00
|
|
|
})
|
2019-10-30 13:58:18 +08:00
|
|
|
if err != nil {
|
2021-10-15 17:05:33 +01:00
|
|
|
return nil, nil, err
|
2019-10-30 13:58:18 +08:00
|
|
|
}
|
2021-10-15 17:05:33 +01:00
|
|
|
tags, err = gitRepo.GetTags(0, 0)
|
2021-05-07 17:10:05 -04:00
|
|
|
if err != nil {
|
2021-10-15 17:05:33 +01:00
|
|
|
return nil, nil, err
|
2021-05-07 17:10:05 -04:00
|
|
|
}
|
2021-10-15 17:05:33 +01:00
|
|
|
return branches, tags, nil
|
2019-10-30 13:58:18 +08:00
|
|
|
}
|
|
|
|
|
2019-06-07 22:29:29 +02:00
|
|
|
// CompareDiff show different from one commit to another commit
|
|
|
|
func CompareDiff(ctx *context.Context) {
|
2021-09-27 13:19:34 +01:00
|
|
|
ci := ParseCompareInfo(ctx)
|
2021-08-31 06:16:23 +02:00
|
|
|
defer func() {
|
2021-09-30 20:31:02 +01:00
|
|
|
if ci != nil && ci.HeadGitRepo != nil {
|
2021-09-27 13:19:34 +01:00
|
|
|
ci.HeadGitRepo.Close()
|
2021-08-31 06:16:23 +02:00
|
|
|
}
|
|
|
|
}()
|
2019-06-07 22:29:29 +02:00
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2019-11-13 07:01:19 +00:00
|
|
|
|
2021-12-16 19:01:14 +00:00
|
|
|
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
|
2021-09-27 13:19:34 +01:00
|
|
|
ctx.Data["DirectComparison"] = ci.DirectComparison
|
|
|
|
ctx.Data["OtherCompareSeparator"] = ".."
|
|
|
|
ctx.Data["CompareSeparator"] = "..."
|
2024-12-10 21:54:57 -08:00
|
|
|
if ci.DirectComparison() {
|
2021-09-27 13:19:34 +01:00
|
|
|
ctx.Data["CompareSeparator"] = ".."
|
|
|
|
ctx.Data["OtherCompareSeparator"] = "..."
|
|
|
|
}
|
|
|
|
|
|
|
|
nothingToCompare := PrepareCompareDiff(ctx, ci,
|
2021-02-13 05:35:43 +01:00
|
|
|
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))
|
2019-06-07 22:29:29 +02:00
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-17 01:01:10 +08:00
|
|
|
baseTags, err := repo_model.GetTagNamesByRepoID(ctx, ctx.Repo.Repository.ID)
|
2021-05-07 17:10:05 -04:00
|
|
|
if err != nil {
|
2023-03-17 01:01:10 +08:00
|
|
|
ctx.ServerError("GetTagNamesByRepoID", err)
|
2021-05-07 17:10:05 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Tags"] = baseTags
|
2019-08-11 17:23:49 +02:00
|
|
|
|
2021-10-15 17:05:33 +01:00
|
|
|
fileOnly := ctx.FormBool("file-only")
|
|
|
|
if fileOnly {
|
|
|
|
ctx.HTML(http.StatusOK, tplDiffBox)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-29 18:03:20 +08:00
|
|
|
headBranches, err := git_model.FindBranchNames(ctx, git_model.FindBranchOptions{
|
2024-03-22 20:53:52 +08:00
|
|
|
RepoID: ci.HeadRepo.ID,
|
|
|
|
ListOptions: db.ListOptionsAll,
|
2024-02-23 03:18:33 +01:00
|
|
|
IsDeletedBranch: optional.Some(false),
|
2023-06-29 18:03:20 +08:00
|
|
|
})
|
2021-05-07 17:10:05 -04:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetBranches", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["HeadBranches"] = headBranches
|
|
|
|
|
2023-07-21 19:20:04 +08:00
|
|
|
// For compare repo branches
|
|
|
|
PrepareBranchList(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-17 01:01:10 +08:00
|
|
|
headTags, err := repo_model.GetTagNamesByRepoID(ctx, ci.HeadRepo.ID)
|
2021-05-07 17:10:05 -04:00
|
|
|
if err != nil {
|
2023-03-17 01:01:10 +08:00
|
|
|
ctx.ServerError("GetTagNamesByRepoID", err)
|
2021-05-07 17:10:05 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["HeadTags"] = headTags
|
|
|
|
|
|
|
|
if ctx.Data["PageIsComparePull"] == true {
|
2024-12-10 21:54:57 -08:00
|
|
|
pr, err := issues_model.GetUnmergedPullRequest(ctx, ci.HeadRepo.ID, ctx.Repo.Repository.ID, ci.HeadOriRef, ci.BaseOriRef, issues_model.PullRequestFlowGithub)
|
2019-06-07 22:29:29 +02:00
|
|
|
if err != nil {
|
2022-06-13 17:37:59 +08:00
|
|
|
if !issues_model.IsErrPullRequestNotExist(err) {
|
2019-06-07 22:29:29 +02:00
|
|
|
ctx.ServerError("GetUnmergedPullRequest", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx.Data["HasPullRequest"] = true
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
2021-12-24 20:14:42 +08:00
|
|
|
ctx.ServerError("LoadIssue", err)
|
|
|
|
return
|
|
|
|
}
|
2019-06-07 22:29:29 +02:00
|
|
|
ctx.Data["PullRequest"] = pr
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, tplCompareDiff)
|
2019-06-07 22:29:29 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !nothingToCompare {
|
|
|
|
// Setup information for new form.
|
2024-11-11 04:07:54 +08:00
|
|
|
pageMetaData := retrieveRepoIssueMetaData(ctx, ctx.Repo.Repository, nil, true)
|
2024-11-10 16:26:42 +08:00
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2024-11-11 04:07:54 +08:00
|
|
|
_, templateErrs := setTemplateIfExists(ctx, pullRequestTemplateKey, pullRequestTemplateCandidates, pageMetaData)
|
2024-11-10 16:26:42 +08:00
|
|
|
if len(templateErrs) > 0 {
|
|
|
|
ctx.Flash.Warning(renderErrorOfTemplates(ctx, templateErrs), true)
|
|
|
|
}
|
2019-06-07 22:29:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
beforeCommitID := ctx.Data["BeforeCommitID"].(string)
|
|
|
|
afterCommitID := ctx.Data["AfterCommitID"].(string)
|
|
|
|
|
2021-09-27 13:19:34 +01:00
|
|
|
separator := "..."
|
2024-12-10 21:54:57 -08:00
|
|
|
if ci.DirectComparison() {
|
2021-09-27 13:19:34 +01:00
|
|
|
separator = ".."
|
|
|
|
}
|
|
|
|
ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitID) + separator + base.ShortSha(afterCommitID)
|
2019-06-07 22:29:29 +02:00
|
|
|
|
|
|
|
ctx.Data["IsDiffCompare"] = true
|
2022-07-24 05:45:33 +02:00
|
|
|
|
2023-01-30 15:39:07 +01:00
|
|
|
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 != "" {
|
2024-04-27 10:03:49 +02:00
|
|
|
// Re-use the same key as that's prioritized over the "content" key.
|
2022-07-24 05:45:33 +02:00
|
|
|
// 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 15:39:07 +01:00
|
|
|
|
|
|
|
// 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 05:45:33 +02:00
|
|
|
}
|
|
|
|
|
2024-01-12 16:25:15 +01:00
|
|
|
ctx.Data["IsProjectsEnabled"] = ctx.Repo.CanWrite(unit.TypeProjects)
|
2020-10-05 07:49:33 +02:00
|
|
|
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
|
|
|
|
upload.AddUploadContext(ctx, "comment")
|
2019-06-07 22:29:29 +02:00
|
|
|
|
2021-11-10 03:57:58 +08:00
|
|
|
ctx.Data["HasIssuesOrPullsWritePermission"] = ctx.Repo.CanWrite(unit.TypePullRequests)
|
2020-04-04 13:39:48 +08:00
|
|
|
|
2023-02-13 07:09:52 +01:00
|
|
|
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 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, tplCompare)
|
2019-06-07 22:29:29 +02:00
|
|
|
}
|
2019-11-15 10:52:59 +08:00
|
|
|
|
|
|
|
// ExcerptBlob render blob excerpt contents
|
|
|
|
func ExcerptBlob(ctx *context.Context) {
|
2024-06-19 06:32:45 +08:00
|
|
|
commitID := ctx.PathParam("sha")
|
2021-07-29 09:42:15 +08: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 02:31:13 +02:00
|
|
|
anchor := ctx.FormString("anchor")
|
|
|
|
direction := ctx.FormString("direction")
|
|
|
|
filePath := ctx.FormString("path")
|
2019-11-15 10:52:59 +08:00
|
|
|
gitRepo := ctx.Repo.GitRepo
|
2022-02-05 18:26:12 +00:00
|
|
|
if ctx.FormBool("wiki") {
|
|
|
|
var err error
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 04:09:51 +08:00
|
|
|
gitRepo, err = gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
|
2022-02-05 18:26:12 +00:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("OpenRepository", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
}
|
2020-08-20 16:53:06 +02:00
|
|
|
chunkSize := gitdiff.BlobExcerptChunkSize
|
2019-11-15 10:52:59 +08:00
|
|
|
commit, err := gitRepo.GetCommit(commitID)
|
|
|
|
if err != nil {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetCommit")
|
2019-11-15 10:52:59 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
section := &gitdiff.DiffSection{
|
2020-07-12 14:25:05 -04:00
|
|
|
FileName: filePath,
|
|
|
|
Name: filePath,
|
2019-11-15 10:52:59 +08: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-24 17:47:46 +02:00
|
|
|
offset := -1
|
|
|
|
if direction == "down" {
|
|
|
|
offset = 0
|
|
|
|
}
|
|
|
|
section.Lines, err = getExcerptLines(commit, filePath, lastLeft, lastRight, idxRight-lastRight+offset)
|
2019-11-15 10:52:59 +08:00
|
|
|
leftHunkSize = 0
|
|
|
|
rightHunkSize = 0
|
|
|
|
idxLeft = lastLeft
|
|
|
|
idxRight = lastRight
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.Error(http.StatusInternalServerError, "getExcerptLines")
|
2019-11-15 10:52:59 +08: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,
|
2022-01-20 18:46:10 +01:00
|
|
|
},
|
|
|
|
}
|
2019-11-15 10:52:59 +08:00
|
|
|
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
|
2024-05-20 23:12:50 +08:00
|
|
|
ctx.Data["FileNameHash"] = git.HashFilePathForWebUI(filePath)
|
2019-11-15 10:52:59 +08:00
|
|
|
ctx.Data["AfterCommitID"] = commitID
|
|
|
|
ctx.Data["Anchor"] = anchor
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, tplBlobExcerpt)
|
2019-11-15 10:52:59 +08:00
|
|
|
}
|
|
|
|
|
2021-12-20 05:41:31 +01:00
|
|
|
func getExcerptLines(commit *git.Commit, filePath string, idxLeft, idxRight, chunkSize int) ([]*gitdiff.DiffLine, error) {
|
2019-11-15 10:52:59 +08:00
|
|
|
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)
|
|
|
|
}
|
2024-03-22 19:17:30 +08:00
|
|
|
if err = scanner.Err(); err != nil {
|
|
|
|
return nil, fmt.Errorf("getExcerptLines scan: %w", err)
|
2024-03-19 10:20:36 +08:00
|
|
|
}
|
2019-11-15 10:52:59 +08:00
|
|
|
return diffLines, nil
|
|
|
|
}
|