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

Exclude some paths from .git folder removal #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,40 @@ While not a best practice, sometimes it's simply a requirement.
## How to use the plugin

When required in composer.json, the plugin will automatically
search for any .git directories in installed or updated
search for any .git directories in installed or updated
dependencies and delete them immediately.

There's nothing else you need to do after requiring this plugin
in your main composer.json file.

Optionally, if you wish to develop on one of your project's dependencies and keep its .git folder, you can tell composer-cleanup-vcs-dirs to exclude that dependency's path from deletion. In your project's composer.json, set the `extras > cleanup-vcs-dirs > exclude` entry with a pattern that matches the dependency installation directories you wish to keep the .git folder for. For example, the following would keep .git folders located in a `custom` folder and inside the `symfony/debug` folder:

```json
{
"name": "my-project",
"description": "This is the composer.json file",
"type": "project",
"extra": {
"cleanup-vcs-dirs": {
"exclude": [
"symfony/debug",
"/custom\\/*/"
]
}
}
}
```

The `extras > cleanup-vcs-dirs > exclude` directory path can be a string or an array of strings. You can use patterns (delimited with `/` sign) or simple strings.

* `"some/special/dir"`
* `["some/directory", "another/directory"]`
* `"/some\\/special\\/dir/"`

(Note that any backslash characters in a regex string need to be escaped with an extra backslash when used in a JSON file like composer.json. In other words, PHP's `'/custom\/*/'` becomes `"/custom\\*/"` in JSON.)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(Note that any backslash characters in a regex string need to be escaped with an extra backslash when used in a JSON file like composer.json. In other words, PHP's `'/custom\/*/'` becomes `"/custom\\*/"` in JSON.)
(Note that any backslash characters in a regex string need to be escaped with an extra backslash when used in a JSON file like composer.json. In other words, PHP's `'/custom\/*/'` becomes `"/custom\\/*/"` in JSON.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good eyes! Fixed.


After adding an `extras > cleanup-vcs-dirs > exclude` entry, you will need to run `composer reinstall --prefer-source [package-needing-git]` to force composer to re-add the .git folder.

## Running the command directly

You can also run the cleanup-vcs-dirs command directly in Composer.
Expand Down
14 changes: 12 additions & 2 deletions src/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,24 @@ public function getVcsDirs($parentDir, $excludeRoot = false) {

$iterator = $finder
->directories()
->in($parentDir)
->in($excludeRoot ? $parentDir : './')
->ignoreVCS(false)
->ignoreDotFiles(false)
->exclude(['node_modules', '.git/*'])
->name('.git');

if ($excludeRoot) {
$iterator = $iterator->depth('> 0');
$iterator->depth('> 0');
}
else {
$iterator->path($parentDir);
}

$extra = $this->composer->getPackage()->getExtra();
if (isset($extra['cleanup-vcs-dirs']) && !empty($extra['cleanup-vcs-dirs']['exclude'])) {
foreach ((array) $extra['cleanup-vcs-dirs']['exclude'] as $pattern) {
$iterator->notPath($pattern);
}
}

return $iterator;
Expand Down