diff --git a/src/StarterKits/Exporter.php b/src/StarterKits/Exporter.php index f3f635d4cb..fd2bbf0d55 100644 --- a/src/StarterKits/Exporter.php +++ b/src/StarterKits/Exporter.php @@ -153,7 +153,9 @@ protected function clearExportPath() return $this; } - $this->files->cleanDirectory($this->exportPath); + $this->preserveGitRepository(function () { + $this->files->cleanDirectory($this->exportPath); + }); return $this; } @@ -257,4 +259,20 @@ protected function exportPackage(): self return $this; } + + /** + * Prevent filesystem callback from affecting .git repository. + */ + protected function preserveGitRepository($callback): void + { + $this->files->makeDirectory(storage_path('statamic/tmp'), 0777, true, true); + + $this->files->moveDirectory($this->exportPath.'/.git', storage_path('statamic/tmp/.git')); + + $callback(); + + $this->files->moveDirectory(storage_path('statamic/tmp/.git'), $this->exportPath.'/.git'); + + $this->files->deleteDirectory(storage_path('statamic/tmp')); + } } diff --git a/tests/StarterKits/ExportTest.php b/tests/StarterKits/ExportTest.php index 7a536a84ee..6f51a373fa 100644 --- a/tests/StarterKits/ExportTest.php +++ b/tests/StarterKits/ExportTest.php @@ -173,7 +173,11 @@ public function it_can_clear_target_export_path_with_clear_option() base_path('two'), ]); - // Imagine this exists from previous export + // Imagine we already have a target a git repo + $this->files->makeDirectory($this->targetPath('.git'), 0777, true, true); + $this->files->put($this->targetPath('.git/config'), 'Config.'); + + // And imagine this exists from previous export $this->files->makeDirectory($this->exportPath('one'), 0777, true, true); $this->files->put($this->exportPath('one/file.md'), 'One.'); @@ -195,10 +199,13 @@ public function it_can_clear_target_export_path_with_clear_option() $this->exportCoolRunnings(['--clear' => true]); - // But 'one' folder should exist after exporting with `--clear` option + // Our 'one' folder shouldn't exist after exporting with `--clear` option $this->assertFileDoesNotExist($this->exportPath('one')); $this->assertFileExists($this->exportPath('two')); + // But it should not clear `.git` directory + $this->assertFileExists($this->targetPath('.git/config')); + $this->exportCoolRunnings(); $this->cleanPaths($paths);