0
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-07-21 18:54:39 +02:00
This commit is contained in:
bytedream 2025-05-08 12:51:58 +02:00
parent fb3e80106d
commit 614a4b34d9
2 changed files with 14 additions and 16 deletions

View File

@ -297,12 +297,10 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
operation := "update"
if isNewFile {
operation = "create"
}
var contentReader io.ReadSeeker
// form content only has data if file is representable as text, is not too large and not in lfs
if isNewFile || form.Content.Has() {
contentReader = strings.NewReader(strings.ReplaceAll(form.Content.Value(), "\r", ""))
} else if !form.Content.Has() {
// The form content only has data if file is representable as text, is not too large and not in lfs. If it doesn't
// have data, the only possible operation is a rename
operation = "rename"
}
if _, err := files_service.ChangeRepoFiles(ctx, ctx.Repo.Repository, ctx.Doer, &files_service.ChangeRepoFilesOptions{
@ -315,7 +313,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
Operation: operation,
FromTreePath: ctx.Repo.TreePath,
TreePath: form.TreePath,
ContentReader: contentReader,
ContentReader: strings.NewReader(strings.ReplaceAll(form.Content.Value(), "\r", "")),
},
},
Signoff: form.Signoff,

View File

@ -43,7 +43,7 @@ type ChangeRepoFile struct {
Operation string
TreePath string
FromTreePath string
ContentReader io.ReadSeeker // nil if the operation is a pure rename
ContentReader io.ReadSeeker
SHA string
Options *RepoFileOptions
}
@ -246,7 +246,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
contentStore := lfs.NewContentStore()
for _, file := range opts.Files {
switch file.Operation {
case "create", "update":
case "create", "update", "rename":
if err := CreateOrUpdateFile(ctx, t, file, contentStore, repo.ID, hasOldBranch); err != nil {
return nil, err
}
@ -490,9 +490,9 @@ func CreateOrUpdateFile(ctx context.Context, t *TemporaryUploadRepository, file
var treeObjectContentReader io.Reader = file.ContentReader
var oldEntry *git.TreeEntry
// If no new content is committed, which is only the case if file is renamed, use the old file from the last commit as
// content
if file.ContentReader == nil {
// Assume that the file.ContentReader of a pure rename operation is invalid. Use the file content how it's present in
// git instead
if file.Operation == "rename" {
lastCommitID, err := t.GetLastCommit(ctx)
if err != nil {
return err
@ -523,15 +523,15 @@ func CreateOrUpdateFile(ctx context.Context, t *TemporaryUploadRepository, file
}
var pointer lfs.Pointer
// Get existing lfs pointer if the old path is in lfs
if oldEntry != nil && attributesMap[file.Options.fromTreePath] != nil && attributesMap[file.Options.fromTreePath].Get(attribute.Filter).ToString().Value() == "lfs" {
// Get existing lfs pointer if the operation is a pure rename and the old path is in lfs. This prevents the
// re-generation/re-hash of a lfs pointer to the same data
if file.Operation == "rename" && attributesMap[file.Options.fromTreePath] != nil && attributesMap[file.Options.fromTreePath].Get(attribute.Filter).ToString().Value() == "lfs" {
if pointer, err = lfs.ReadPointer(treeObjectContentReader); err != nil {
return err
}
}
if attributesMap[file.Options.treePath] != nil && attributesMap[file.Options.treePath].Get(attribute.Filter).ToString().Value() == "lfs" {
// Only generate a new lfs pointer if the old path isn't in lfs
if !pointer.IsValid() {
if pointer, err = lfs.GeneratePointer(treeObjectContentReader); err != nil {
return err
@ -577,7 +577,7 @@ func CreateOrUpdateFile(ctx context.Context, t *TemporaryUploadRepository, file
}
if !exist {
var lfsContentReader io.Reader
if file.ContentReader != nil {
if file.Operation != "rename" {
if _, err := file.ContentReader.Seek(0, io.SeekStart); err != nil {
return err
}