fix crash when renaming file
[phorkie.git] / src / phorkie / Repository / Post.php
index 511a83c569c40cd5d1ff1f6e7c7955f9819ba3e2..7a373aaf9005e65343c831700db81d2ef327403f 100644 (file)
@@ -58,8 +58,8 @@ class Repository_Post
                 continue;
             }
 
-            $orignalName = Tools::sanitizeFilename($arFile['original_name']);
-            $name        = Tools::sanitizeFilename($arFile['name']);
+            $originalName = Tools::sanitizeFilename($arFile['original_name']);
+            $name         = Tools::sanitizeFilename($arFile['name']);
 
             if ($arFile['type'] == '_auto_') {
                 //FIXME: upload
@@ -79,7 +79,7 @@ class Repository_Post
 
             $bNew = false;
             $bDelete = false;
-            if (!isset($orignalName) || $orignalName == '') {
+            if (!isset($originalName) || $originalName == '') {
                 //new file
                 $bNew = true;
                 if (strpos($name, '.') === false) {
@@ -87,27 +87,30 @@ class Repository_Post
                     $name .= '.' . $arFile['type'];
                 }
                 $this->newfileName = $name;
-            } else if (!$this->repo->hasFile($orignalName)) {
+            } else if (!$this->repo->hasFile($originalName)) {
                 //unknown file
                 //FIXME: Show error message
                 continue;
             } else if (isset($arFile['delete']) && $arFile['delete'] == 1) {
                 $bDelete = true;
-            } else if ($orignalName != $name) {
+            } else if ($originalName != $name) {
                 if (strpos($name, '/') === false) {
                     //ignore names with a slash in it, would be new directory
                     //FIXME: what to do with overwrites?
                     $vc->getCommand('mv')
-                        ->addArgument($orignalName)
+                        ->addArgument($originalName)
                         ->addArgument($name)
                         ->execute();
                     $bCommit = true;
                 } else {
-                    $name = $orignalName;
+                    $name = $originalName;
                 }
             }
 
             $file = $this->repo->getFileByName($name, false);
+            if ($originalName !== '') {
+                $originalFile = $this->repo->getFileByName($originalName, false);
+            }
             if ($bDelete) {
                 $command = $vc->getCommand('rm')
                     ->addArgument($file->getFilename())
@@ -123,8 +126,8 @@ class Repository_Post
                     ->execute();
                 $bCommit = true;
             } else if ($bNew
-                || (isset($arFile['content'])
-                    && $file->getContent() != $arFile['content']
+                || (isset($arFile['content']) && isset($originalFile)
+                    && $originalFile->getContent() != $arFile['content']
                 )
             ) {
                 $dir = dirname($file->getFullPath());
@@ -166,6 +169,9 @@ class Repository_Post
             //the post-update hook should do that IMO, but does not somehow
             $vc->getCommand('update-server-info')->execute();
 
+            //we changed the hash by committing, so reload it
+            $this->repo->reloadHash();
+
             $bChanged = true;
         }