Skip to content

Commit 623c5c3

Browse files
committed
Exclude some paths from .git folder removal
1 parent dfa7c8c commit 623c5c3

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,40 @@ While not a best practice, sometimes it's simply a requirement.
99
## How to use the plugin
1010

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

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

18+
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:
19+
20+
```json
21+
{
22+
"name": "my-project",
23+
"description": "This is the composer.json file",
24+
"type": "project",
25+
"extra": {
26+
"cleanup-vcs-dirs": {
27+
"exclude": [
28+
"symfony/debug",
29+
"/custom\\/*/"
30+
]
31+
}
32+
}
33+
}
34+
```
35+
36+
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.
37+
38+
* `"some/special/dir"`
39+
* `["some/directory", "another/directory"]`
40+
* `"/some\\/special\\/dir/"`
41+
42+
(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.)
43+
44+
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.
45+
1846
## Running the command directly
1947

2048
You can also run the cleanup-vcs-dirs command directly in Composer.

src/Handler.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,24 @@ public function getVcsDirs($parentDir, $excludeRoot = false) {
4646

4747
$iterator = $finder
4848
->directories()
49-
->in($parentDir)
49+
->in($excludeRoot ? $parentDir : './')
5050
->ignoreVCS(false)
5151
->ignoreDotFiles(false)
5252
->exclude(['node_modules', '.git/*'])
5353
->name('.git');
5454

5555
if ($excludeRoot) {
56-
$iterator = $iterator->depth('> 0');
56+
$iterator->depth('> 0');
57+
}
58+
else {
59+
$iterator->path($parentDir);
60+
}
61+
62+
$extra = $this->composer->getPackage()->getExtra();
63+
if (isset($extra['cleanup-vcs-dirs']) && !empty($extra['cleanup-vcs-dirs']['exclude'])) {
64+
foreach ((array) $extra['cleanup-vcs-dirs']['exclude'] as $pattern) {
65+
$iterator->notPath($pattern);
66+
}
5767
}
5868

5969
return $iterator;

0 commit comments

Comments
 (0)