Skip to content

Commit 0e09aa4

Browse files
committed
Properly handle a conflict containing a void
1 parent 0ba6dc0 commit 0e09aa4

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

merge.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ function merge($ancestor, $ours, $theirs) {
126126
foreach ($merged[$key] as $package) {
127127
// if we have a conflict, convert the conflicting values back to package definition arrays
128128
if (preg_match('/^' . CONFLICT_PLACEHOLDER . '$/', $package, $matches)) {
129-
$conflicts[$matches[1]] = array_map(function($json) {
130-
return json_decode($json, true);
129+
$conflicts[$matches[1]] = array_map(function($json) use ($void) {
130+
return $json === $void ? $void : json_decode($json, true);
131131
}, $conflicts[$matches[1]]);
132132
$packageArray[] = $package;
133133
}
@@ -150,14 +150,25 @@ function merge($ancestor, $ours, $theirs) {
150150

151151
// if we have conflicts, replace the conflict markers with the actual conflicting values
152152
if (count($conflicts)) {
153-
$merged = preg_replace_callback('/^(\s+)(.+)"' . CONFLICT_PLACEHOLDER . '"(,?)$/m', function ($matches) use ($conflicts, $markerLen) {
153+
$merged = preg_replace_callback('/^(\s+)(.+)"' . CONFLICT_PLACEHOLDER . '"(,?)$/m', function ($matches) use ($conflicts, $markerLen, $void) {
154154
list(, $space, $property, $conflictNum, $comma) = $matches;
155-
// replace the conflict placeholder with theirs/ours values surrounded by conflict markers
156-
return str_repeat('<', $markerLen) . " HEAD\n"
157-
. $space . $property . preg_replace('/\n/', "\n$space", json_encode($conflicts[$conflictNum][1], JSON_ENCODE_OPTIONS)) . $comma . "\n"
158-
. str_repeat('=', $markerLen) . "\n"
159-
. $space . $property . preg_replace('/\n/', "\n$space", json_encode($conflicts[$conflictNum][0], JSON_ENCODE_OPTIONS)) . $comma . "\n"
160-
. str_repeat('>', $markerLen);
155+
// build the replacement
156+
$parts = [];
157+
// add opening conflict marker
158+
$parts[] = str_repeat('<', $markerLen) . ' HEAD';
159+
// add their value, if not void
160+
if ($conflicts[$conflictNum][1] !== $void) {
161+
$parts[] = $space . $property . preg_replace('/\n/', "\n$space", json_encode($conflicts[$conflictNum][1], JSON_ENCODE_OPTIONS)) . $comma;
162+
}
163+
// add conflict marker divider
164+
$parts[] = str_repeat('=', $markerLen);
165+
// add our value, if not void
166+
if ($conflicts[$conflictNum][0] !== $void) {
167+
$parts[] = $space . $property . preg_replace('/\n/', "\n$space", json_encode($conflicts[$conflictNum][0], JSON_ENCODE_OPTIONS)) . $comma;
168+
}
169+
// add closing conflict marker
170+
$parts[] = str_repeat('>', $markerLen);
171+
return implode("\n", $parts);
161172
}, $merged);
162173
}
163174

0 commit comments

Comments
 (0)