1
1
#!/usr/bin/env php
2
2
<?php
3
3
4
+ // placeholder to denote a conflict in a json property
4
5
define ('CONFLICT_PLACEHOLDER ' , '=c=o=n=f=(\d+)=l=i=c=t= ' );
6
+ // options for json encode
5
7
define ('JSON_ENCODE_OPTIONS ' , JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE );
6
8
// file states
7
9
$ states = ['ancestor ' , 'ours ' , 'theirs ' ];
8
10
// unique object that represents a void
9
11
$ void = (object ) [];
10
12
// conflicts we have identified
11
13
$ conflicts = [];
12
- // options for json encode
14
+ // length for conflict marker (<<<)
13
15
$ markerLen = $ argv [4 ];
16
+ // are we working with the lock file?
17
+ $ isLock = strtolower ($ argv [5 ]) === 'composer.lock ' ;
14
18
15
19
// grab the contents of each file state
16
20
foreach ($ states as $ i => $ state ) {
@@ -92,21 +96,21 @@ function merge($ancestor, $ours, $theirs) {
92
96
}
93
97
94
98
// special handling for lock files
95
- if (strtolower ($ argv [5 ]) === 'composer.lock ' ) {
96
- $ packageKeys = ['packages ' , 'packages-dev ' , 'aliases ' ];
99
+ if ($ isLock ) {
100
+ // @todo handle alias property as well
101
+ $ packageKeys = ['packages ' , 'packages-dev ' ];
97
102
98
103
foreach ($ states as $ state ) {
99
104
// ensure we don't get a conflict on the content hash
100
105
$ {$ state }['content-hash ' ] = null ;
101
106
102
107
// convert package array to assoc array, mapping package name to json-encoded package definition
103
108
foreach ($ packageKeys as $ key ) {
104
- $ newArray = [];
109
+ $ packageArray = [];
105
110
foreach ($ {$ state }[$ key ] as $ package ) {
106
- // @todo this is different for alias
107
- $ newArray [$ package ['name ' ]] = json_encode ($ package );
111
+ $ packageArray [$ package ['name ' ]] = json_encode ($ package );
108
112
}
109
- $ {$ state }[$ key ] = $ newArray ;
113
+ $ {$ state }[$ key ] = $ packageArray ;
110
114
}
111
115
}
112
116
@@ -118,25 +122,26 @@ function merge($ancestor, $ours, $theirs) {
118
122
// sort the packages
119
123
ksort ($ merged [$ key ]);
120
124
121
- $ newArray = [];
125
+ $ packageArray = [];
122
126
foreach ($ merged [$ key ] as $ package ) {
123
127
// if we have a conflict, convert the conflicting values back to package definition arrays
124
128
if (preg_match ('/^ ' . CONFLICT_PLACEHOLDER . '$/ ' , $ package , $ matches )) {
125
129
$ conflicts [$ matches [1 ]] = array_map (function ($ json ) {
126
130
return json_decode ($ json , true );
127
131
}, $ conflicts [$ matches [1 ]]);
128
- $ newArray [] = $ package ;
132
+ $ packageArray [] = $ package ;
129
133
}
130
134
// otherwise just convert the value back to a package definition array
131
135
else {
132
- $ newArray [] = json_decode ($ package , true );
136
+ $ packageArray [] = json_decode ($ package , true );
133
137
}
134
138
}
135
- $ merged [$ key ] = $ newArray ;
139
+ $ merged [$ key ] = $ packageArray ;
136
140
}
137
141
138
- // update the content-hash key
139
- $ merged ['content-hash ' ] = (count ($ conflicts ) ? 'Merge conflict! ' : 'Auto-merged! ' ) . ' Run `composer update --lock` to regenerate ' ;
142
+ // update the content-hash key with a helpful message
143
+ $ merged ['content-hash ' ] = sprintf (count ($ conflicts ) ? 'Merge conflict! %s after fixing conflict(s) ' : 'Auto-merged! %s ' ,
144
+ 'Run `composer update --lock` to regenerate ' );
140
145
141
146
$ merged = json_encode ($ merged , JSON_ENCODE_OPTIONS );
142
147
} else {
0 commit comments