Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Fix target .git repo handling when exporting starter kit with --clear #11509

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading