Skip to content

Commit 627bd75

Browse files
committed
Remove empty files
1 parent 6078f63 commit 627bd75

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/Patch.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ class Patch
5353
*/
5454
private $method = PatchApplicator::METHOD_PATCH;
5555

56+
/**
57+
* @var bool
58+
*/
59+
private $keepEmptyFiles;
60+
5661
/**
5762
* @param $sourcePackage
5863
* @param string $targetPackage
@@ -61,6 +66,7 @@ class Patch
6166
* @param string $description
6267
* @param int $stripPathComponents
6368
* @param string $method
69+
* @param bool $keepEmptyFiles
6470
*/
6571
public function __construct(
6672
$sourcePackage,
@@ -69,7 +75,8 @@ public function __construct(
6975
$filename,
7076
$description,
7177
$stripPathComponents = 1,
72-
$method = PatchApplicator::METHOD_PATCH
78+
$method = PatchApplicator::METHOD_PATCH,
79+
$keepEmptyFiles = false
7380
) {
7481
$this->sourcePackage = $sourcePackage;
7582
$this->targetPackage = $targetPackage;
@@ -78,6 +85,7 @@ public function __construct(
7885
$this->description = $description;
7986
$this->stripPathComponents = $stripPathComponents;
8087
$this->method = $method;
88+
$this->keepEmptyFiles = $keepEmptyFiles;
8189
}
8290

8391
/**
@@ -92,7 +100,8 @@ public static function createFromConfig($sourcePackage, $targetPackage, array $c
92100
'version-constraint' => '*',
93101
'description' => null,
94102
'strip-path-components' => 1,
95-
'method' => PatchApplicator::METHOD_PATCH
103+
'method' => PatchApplicator::METHOD_PATCH,
104+
'keep-empty-files' => false,
96105
], $config);
97106

98107
if (!in_array($config['method'], PatchApplicator::METHODS)) {
@@ -110,7 +119,8 @@ public static function createFromConfig($sourcePackage, $targetPackage, array $c
110119
$config['filename'],
111120
$config['description'],
112121
$config['strip-path-components'],
113-
$config['method']
122+
$config['method'],
123+
$config['keep-empty-files']
114124
);
115125
}
116126

@@ -124,7 +134,8 @@ public static function createFromArray(array $data)
124134
$data['filename'],
125135
$data['description'],
126136
$data['strip_path_components'],
127-
$data['method']
137+
$data['method'],
138+
$data['keep_empty_files']
128139
);
129140
}
130141

@@ -193,6 +204,14 @@ public function getStripPathComponents()
193204
return $this->stripPathComponents;
194205
}
195206

207+
/**
208+
* @return bool
209+
*/
210+
public function getKeepEmptyFiles()
211+
{
212+
return $this->keepEmptyFiles;
213+
}
214+
196215
/**
197216
* @return string
198217
*/
@@ -210,7 +229,8 @@ public function toArray()
210229
'filename' => $this->filename,
211230
'description' => $this->description,
212231
'strip_path_components' => $this->stripPathComponents,
213-
'method' => $this->method
232+
'method' => $this->method,
233+
'keep_empty_files' => $this->keepEmptyFiles,
214234
];
215235
}
216236
}

src/PatchApplicator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,20 @@ private function hasPatchCommand()
119119
* @param string $targetDirectory
120120
* @param string $patchFile
121121
* @param int $stripPathComponents
122+
* @param bool $keepEmptyFiles
122123
* @return bool
123124
*/
124-
private function executePatchCommand($method, $targetDirectory, $patchFile, $stripPathComponents)
125+
private function executePatchCommand($method, $targetDirectory, $patchFile, $stripPathComponents, $keepEmptyFiles = false)
125126
{
126127
$cwd = null;
127128

128129
if ($method === self::METHOD_PATCH && $this->hasPatchCommand()) {
129130
$cmd = ['patch', '--posix', '--batch', '--forward', '--strip=' . $stripPathComponents, '--input='.$patchFile, '--directory='.$targetDirectory];
131+
132+
if (!$keepEmptyFiles) {
133+
$cmd[] = '--remove-empty-files';
134+
}
135+
130136
} else {
131137
$cmd = ['git', 'apply', '-v', '-p' . $stripPathComponents, $patchFile];
132138

0 commit comments

Comments
 (0)