Skip to content

Commit d371d6a

Browse files
committed
Preserve user-preferred indentation based on "our" file
1 parent 0e09aa4 commit d371d6a

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

merge.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@
1515
$markerLen = $argv[4];
1616
// are we working with the lock file?
1717
$isLock = strtolower($argv[5]) === 'composer.lock';
18+
// indentation type to use
19+
$indentation = ' ';
1820

1921
// grab the contents of each file state
2022
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+
2131
// read and parse the composer file
22-
$$state = json_decode(file_get_contents($argv[$i + 1]), true);
32+
$$state = json_decode($contents, true);
2333

2434
// check for malformed json
2535
if (json_last_error() !== JSON_ERROR_NONE || !is_array($$state)) {
@@ -170,6 +180,13 @@ function merge($ancestor, $ours, $theirs) {
170180
$parts[] = str_repeat('>', $markerLen);
171181
return implode("\n", $parts);
172182
}, $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+
}
173190
}
174191

175192
// update the file

0 commit comments

Comments
 (0)