From 614a4b34d98294827de5ada34904885dd7fc290c Mon Sep 17 00:00:00 2001 From: bytedream Date: Thu, 8 May 2025 12:51:58 +0200 Subject: [PATCH] Update --- routers/web/repo/editor.go | 12 +++++------- services/repository/files/update.go | 18 +++++++++--------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index db3e10e871..91b9c069d9 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -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, diff --git a/services/repository/files/update.go b/services/repository/files/update.go index 661b9bb09d..fdb28a35cb 100644 --- a/services/repository/files/update.go +++ b/services/repository/files/update.go @@ -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 }