Skip to content

Commit

Permalink
[5.x] Fix target .git repo handling when exporting starter kit with…
Browse files Browse the repository at this point in the history
… `--clear` (#11509)
  • Loading branch information
jesseleite authored Feb 27, 2025
1 parent 80c0b89 commit 9d62d6c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/StarterKits/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ protected function clearExportPath()
return $this;
}

$this->files->cleanDirectory($this->exportPath);
$this->preserveGitRepository(function () {
$this->files->cleanDirectory($this->exportPath);
});

return $this;
}
Expand Down Expand Up @@ -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'));
}
}
11 changes: 9 additions & 2 deletions tests/StarterKits/ExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');

Expand All @@ -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);
Expand Down

0 comments on commit 9d62d6c

Please sign in to comment.