Skip to content

Commit 614a4b3

Browse files
committed
Update
1 parent fb3e801 commit 614a4b3

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

routers/web/repo/editor.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,10 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
297297
operation := "update"
298298
if isNewFile {
299299
operation = "create"
300-
}
301-
302-
var contentReader io.ReadSeeker
303-
// form content only has data if file is representable as text, is not too large and not in lfs
304-
if isNewFile || form.Content.Has() {
305-
contentReader = strings.NewReader(strings.ReplaceAll(form.Content.Value(), "\r", ""))
300+
} else if !form.Content.Has() {
301+
// The form content only has data if file is representable as text, is not too large and not in lfs. If it doesn't
302+
// have data, the only possible operation is a rename
303+
operation = "rename"
306304
}
307305

308306
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
315313
Operation: operation,
316314
FromTreePath: ctx.Repo.TreePath,
317315
TreePath: form.TreePath,
318-
ContentReader: contentReader,
316+
ContentReader: strings.NewReader(strings.ReplaceAll(form.Content.Value(), "\r", "")),
319317
},
320318
},
321319
Signoff: form.Signoff,

services/repository/files/update.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type ChangeRepoFile struct {
4343
Operation string
4444
TreePath string
4545
FromTreePath string
46-
ContentReader io.ReadSeeker // nil if the operation is a pure rename
46+
ContentReader io.ReadSeeker
4747
SHA string
4848
Options *RepoFileOptions
4949
}
@@ -246,7 +246,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
246246
contentStore := lfs.NewContentStore()
247247
for _, file := range opts.Files {
248248
switch file.Operation {
249-
case "create", "update":
249+
case "create", "update", "rename":
250250
if err := CreateOrUpdateFile(ctx, t, file, contentStore, repo.ID, hasOldBranch); err != nil {
251251
return nil, err
252252
}
@@ -490,9 +490,9 @@ func CreateOrUpdateFile(ctx context.Context, t *TemporaryUploadRepository, file
490490

491491
var treeObjectContentReader io.Reader = file.ContentReader
492492
var oldEntry *git.TreeEntry
493-
// If no new content is committed, which is only the case if file is renamed, use the old file from the last commit as
494-
// content
495-
if file.ContentReader == nil {
493+
// Assume that the file.ContentReader of a pure rename operation is invalid. Use the file content how it's present in
494+
// git instead
495+
if file.Operation == "rename" {
496496
lastCommitID, err := t.GetLastCommit(ctx)
497497
if err != nil {
498498
return err
@@ -523,15 +523,15 @@ func CreateOrUpdateFile(ctx context.Context, t *TemporaryUploadRepository, file
523523
}
524524

525525
var pointer lfs.Pointer
526-
// Get existing lfs pointer if the old path is in lfs
527-
if oldEntry != nil && attributesMap[file.Options.fromTreePath] != nil && attributesMap[file.Options.fromTreePath].Get(attribute.Filter).ToString().Value() == "lfs" {
526+
// Get existing lfs pointer if the operation is a pure rename and the old path is in lfs. This prevents the
527+
// re-generation/re-hash of a lfs pointer to the same data
528+
if file.Operation == "rename" && attributesMap[file.Options.fromTreePath] != nil && attributesMap[file.Options.fromTreePath].Get(attribute.Filter).ToString().Value() == "lfs" {
528529
if pointer, err = lfs.ReadPointer(treeObjectContentReader); err != nil {
529530
return err
530531
}
531532
}
532533

533534
if attributesMap[file.Options.treePath] != nil && attributesMap[file.Options.treePath].Get(attribute.Filter).ToString().Value() == "lfs" {
534-
// Only generate a new lfs pointer if the old path isn't in lfs
535535
if !pointer.IsValid() {
536536
if pointer, err = lfs.GeneratePointer(treeObjectContentReader); err != nil {
537537
return err
@@ -577,7 +577,7 @@ func CreateOrUpdateFile(ctx context.Context, t *TemporaryUploadRepository, file
577577
}
578578
if !exist {
579579
var lfsContentReader io.Reader
580-
if file.ContentReader != nil {
580+
if file.Operation != "rename" {
581581
if _, err := file.ContentReader.Seek(0, io.SeekStart); err != nil {
582582
return err
583583
}

0 commit comments

Comments
 (0)