@@ -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