Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions bin/composer-git-merge-driver
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,43 @@
define('CONFLICT_PLACEHOLDER', '=c=o=n=f=(\d+)=l=i=c=t=');
// options for json encode
define('JSON_ENCODE_OPTIONS', JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);

// Read arguments in order of %O %A %B %L %P
reset($argv);
// next skips first argv element
next($argv);
if (current($argv) == '--always-conflict-on-merge') {
$always_conflict_on_merge = true;
next($argv);
} else {
$always_conflict_on_merge = false;
}
$arg_ancestor = current($argv);
$arg_ours = next($argv);
$arg_theirs = next($argv);
$arg_markersize = next($argv);
$arg_pathname = next($argv);

// file states
$states = ['ancestor', 'ours', 'theirs'];
$states = [
'ancestor' => $arg_ancestor,
'ours' => $arg_ours,
'theirs' => $arg_theirs,
];
// unique object that represents a void
$void = (object) [];
// conflicts we have identified
$conflicts = [];
// length for conflict marker (<<<)
$markerLen = $argv[4];
$markerLen = $arg_markersize;
// are we working with the lock file?
$isLock = basename(strtolower($argv[5])) === 'composer.lock';
$isLock = basename(strtolower($arg_pathname)) === 'composer.lock';
// indentation type to use
$indentation = $defaultIndentation = ' ';

// grab the contents of each file state
foreach ($states as $i => $state) {
$contents = file_get_contents($argv[$i + 1]);
foreach ($states as $state => $statepath) {
$contents = file_get_contents($statepath);

// use our file to base the indentation off of;
// we ignore the possibility of indentation style changes
Expand Down Expand Up @@ -59,7 +80,7 @@ function merge($ancestor, $ours, $theirs) {
// reconcile each key
foreach ($keys as $key) {
// get the value of this key from each state
foreach ($states as $state) {
foreach ($states as $state => $_) {
${rtrim($state, 's') . 'Value'} = is_array($$state) && array_key_exists($key, $$state) ? ${$state}[$key] : $void;
}

Expand Down Expand Up @@ -144,7 +165,7 @@ if ($isLock) {
// @todo handle alias property as well
$packageKeys = ['packages', 'packages-dev'];

foreach ($states as $state) {
foreach ($states as $state => $_) {
// ensure we don't get a conflict on the content hash
${$state}['content-hash'] = null;

Expand Down Expand Up @@ -236,7 +257,7 @@ if (count($conflicts)) {
}

// update the file
file_put_contents($argv[2], $merged . "\n");
file_put_contents($arg_ours, $merged . "\n");

// determine exit status based on whether there were conflicts
exit(count($conflicts) ? 1 : 0);
exit($always_conflict_on_merge || count($conflicts) ? 1 : 0);