Skip to content

Commit 45c9d7b

Browse files
committed
multi SCM support (adding SVN)
1 parent d16f1ce commit 45c9d7b

File tree

1 file changed

+58
-17
lines changed

1 file changed

+58
-17
lines changed

composer-lock-diff

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ $opts = parseOpts();
66
$changes = array();
77

88
if (! $opts['only-dev']) {
9-
$changes['changes'] = diff('packages', $opts['from'], $opts['to'], $opts['path']);
9+
$changes['changes'] = diff('packages', $opts['from'], $opts['to'], $opts['path'], $opts['scm-type']);
1010
}
1111

1212
if (! $opts['only-prod']) {
13-
$changes['changes-dev'] = diff('packages-dev', $opts['from'], $opts['to'], $opts['path']);
13+
$changes['changes-dev'] = diff('packages-dev', $opts['from'], $opts['to'], $opts['path'], $opts['scm-type']);
1414
}
1515

1616
if ($opts['json']) {
@@ -40,17 +40,17 @@ foreach($changes as $k => $diff) {
4040
print tableize($table_titles[$k], $diff, $table_opts);
4141
}
4242

43-
function diff($key, $from, $to, $base_path) {
43+
function diff($key, $from, $to, $base_path, $scm) {
4444

4545
$pkgs = array();
4646

47-
$data = load($from, $base_path);
47+
$data = load($from, $base_path, $scm);
4848

4949
foreach($data->$key as $pkg) {
5050
$pkgs[$pkg->name] = array(version($pkg), 'REMOVED', '');
5151
}
5252

53-
$data = load($to, $base_path);
53+
$data = load($to, $base_path, $scm);
5454

5555
foreach($data->$key as $pkg) {
5656
if (! array_key_exists($pkg->name, $pkgs)) {
@@ -150,7 +150,7 @@ function urlFormatterMd($url, $text) {
150150
return sprintf('[%s](%s)', $text, $url);
151151
}
152152

153-
function load($fileish, $base_path = '') {
153+
function load($fileish, $base_path = '', $scm) {
154154
$orig = $fileish;
155155

156156
if (empty($base_path)) {
@@ -176,20 +176,38 @@ function load($fileish, $base_path = '') {
176176
return mustDecodeJson(file_get_contents($fileish), $fileish);
177177
}
178178

179-
if (strpos($orig, ':') === false) {
180-
$fileish .= ':' . $base_path . 'composer.lock';
181-
}
179+
//try to find scm revision
180+
if (strtolower($scm) == 'git'){
181+
if (strpos($orig, ':') === false) {
182+
$fileish .= ':' . $base_path . 'composer.lock';
183+
}
182184

183-
$lines = array();
185+
$lines = array();
186+
exec('git show '. escapeshellarg($fileish), $lines, $exit);
184187

185-
exec('git show '. escapeshellarg($fileish), $lines, $exit);
188+
if ($exit !== 0) {
189+
error_log("Error: cannot open $orig or find it in git as $fileish");
190+
exit(1);
191+
}
186192

187-
if ($exit !== 0) {
188-
error_log("Error: cannot open $orig or find it in git as $fileish");
189-
exit(1);
193+
return mustDecodeJson(implode("\n", $lines), $fileish);
194+
}
195+
196+
elseif (strtolower($scm) == 'svn'){
197+
if (strpos($orig, '@') === false) {
198+
$fileish = $base_path . 'composer.lock@'.$fileish;
199+
}
200+
exec('svn cat '. escapeshellarg($fileish), $lines, $exit);
201+
202+
if ($exit !== 0) {
203+
error_log("Error: cannot open $orig or find it in svn as $fileish");
204+
exit(1);
205+
}
206+
return mustDecodeJson(implode("\n", $lines), $fileish);
190207
}
191208

192-
return mustDecodeJson(implode("\n", $lines), $fileish);
209+
error_log("Error: unhandled SCM $scm");
210+
exit(1);
193211
}
194212

195213
function isUrl($string) {
@@ -279,7 +297,7 @@ function formatCompareDrupal($url, $from, $to) {
279297
}
280298

281299
function parseOpts() {
282-
$given = getopt('hp:', array('path:', 'from:', 'to:', 'md', 'json', 'pretty', 'no-links', 'only-prod', 'only-dev', 'help'));
300+
$given = getopt('hp:', array('path:', 'from:', 'to:', 'md', 'json', 'pretty', 'no-links', 'only-prod', 'only-dev', 'help', 'scm:'));
283301

284302
foreach(array('help' => 'h', 'path' => 'p') as $long => $short) {
285303
if (array_key_exists($short, $given)) {
@@ -292,8 +310,10 @@ function parseOpts() {
292310
usage();
293311
}
294312

313+
$path = array_key_exists('path', $given) ? $given['path'] : '';
314+
295315
return array(
296-
'path' => array_key_exists('path', $given) ? $given['path'] : '',
316+
'path' => $path,
297317
'from' => array_key_exists('from', $given) ? $given['from'] : 'HEAD',
298318
'to' => array_key_exists('to', $given) ? $given['to'] : '',
299319
'md' => array_key_exists('md', $given),
@@ -302,9 +322,30 @@ function parseOpts() {
302322
'no-links' => array_key_exists('no-links', $given),
303323
'only-prod' => array_key_exists('only-prod', $given),
304324
'only-dev' => array_key_exists('only-dev', $given),
325+
'scm-type' => array_key_exists('scm', $given) ? $given['scm'] : scmDetect($path),
305326
);
306327
}
307328

329+
function scmDetect($base_path){
330+
if (empty($base_path)) {
331+
$base_path = '.' . DIRECTORY_SEPARATOR;
332+
} else {
333+
$base_path = rtrim($base_path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
334+
}
335+
336+
$base_path = realpath($base_path);
337+
while(!empty($base_path) && $base_path !== '/' ){
338+
if(is_dir($base_path.'/.svn')){
339+
return 'svn';
340+
}elseif(is_dir($base_path.'/.git')){
341+
return 'git';
342+
}
343+
$base_path = dirname($base_path);
344+
}
345+
346+
return 'unknown';
347+
}
348+
308349
function usage() {
309350
print <<<EOF
310351
Usage: composer-lock-diff [options]

0 commit comments

Comments
 (0)