|
15 | 15 | $markerLen = $argv[4];
|
16 | 16 | // are we working with the lock file?
|
17 | 17 | $isLock = strtolower($argv[5]) === 'composer.lock';
|
| 18 | +// indentation type to use |
| 19 | +$indentation = ' '; |
18 | 20 |
|
19 | 21 | // grab the contents of each file state
|
20 | 22 | foreach ($states as $i => $state) {
|
| 23 | + $contents = file_get_contents($argv[$i + 1]); |
| 24 | + |
| 25 | + // use our file to base the indentation off of; |
| 26 | + // we ignore the possibility of indentation style changes |
| 27 | + if ($state === 'ours' && preg_match('/^{\n(\s+)/', $contents, $matches)) { |
| 28 | + $indentation = $matches[1]; |
| 29 | + } |
| 30 | + |
21 | 31 | // read and parse the composer file
|
22 |
| - $$state = json_decode(file_get_contents($argv[$i + 1]), true); |
| 32 | + $$state = json_decode($contents, true); |
23 | 33 |
|
24 | 34 | // check for malformed json
|
25 | 35 | if (json_last_error() !== JSON_ERROR_NONE || !is_array($$state)) {
|
@@ -170,6 +180,13 @@ function merge($ancestor, $ours, $theirs) {
|
170 | 180 | $parts[] = str_repeat('>', $markerLen);
|
171 | 181 | return implode("\n", $parts);
|
172 | 182 | }, $merged);
|
| 183 | + |
| 184 | + // fix the indentation per the user's preference, except for the lock |
| 185 | + if (!$isLock && $indentation !== ' ') { |
| 186 | + $merged = preg_replace_callback('/^(?: {4})+/m', function($matches) use ($indentation) { |
| 187 | + return str_repeat($indentation, strlen($matches[0]) / 4); |
| 188 | + }, $merged); |
| 189 | + } |
173 | 190 | }
|
174 | 191 |
|
175 | 192 | // update the file
|
|
0 commit comments