|
1 | 1 | #!/usr/bin/env php
|
2 | 2 | <?php
|
3 | 3 |
|
4 |
| -$prod = diff('packages'); |
5 |
| -$dev = diff('packages-dev'); |
| 4 | +$opts = parseOpts(); |
6 | 5 |
|
7 |
| -if (hasOpt('json')) { |
8 |
| - $opts = (hasOpt('pretty')) ? JSON_PRETTY_PRINT : 0; |
| 6 | +$prod = diff('packages', $opts['from'], $opts['to']); |
| 7 | +$dev = diff('packages-dev', $opts['from'], $opts['to']); |
| 8 | + |
| 9 | +if ($opts['json']) { |
| 10 | + $opts = ($opt['pretty']) ? JSON_PRETTY_PRINT : 0; |
9 | 11 | print json_encode(array('changes' => $prod, 'changes-dev' => $dev), $opts);
|
10 | 12 | return;
|
11 | 13 | }
|
12 | 14 |
|
13 | 15 | print tableize('Production Changes', $prod);
|
14 | 16 | print tableize('Dev Changes', $dev);
|
15 | 17 |
|
16 |
| -function diff($key) { |
| 18 | +function diff($key, $from, $to) { |
17 | 19 |
|
18 | 20 | $pkgs = array();
|
19 | 21 |
|
20 |
| - $lines = ''; |
21 |
| - exec('git show HEAD:composer.lock', $lines); |
22 |
| - $data = json_decode(implode("\n", $lines)); |
| 22 | + $data = load($from); |
23 | 23 |
|
24 | 24 | foreach($data->$key as $pkg) {
|
25 | 25 | $pkgs[$pkg->name] = array(version($pkg), 'REMOVED');
|
26 | 26 | }
|
27 | 27 |
|
28 |
| - $data = json_decode(file_get_contents('composer.lock')); |
| 28 | + $data = load($to); |
29 | 29 |
|
30 | 30 | foreach($data->$key as $pkg) {
|
31 | 31 | if (! array_key_exists($pkg->name, $pkgs)) {
|
@@ -57,12 +57,6 @@ function version($pkg)
|
57 | 57 | return $version;
|
58 | 58 | }
|
59 | 59 |
|
60 |
| -function hasOpt($opt) { |
61 |
| - global $argv; |
62 |
| - $prefix = strlen($opt) === 1 ? '-' : '--'; |
63 |
| - return in_array($prefix.$opt, $argv); |
64 |
| -} |
65 |
| - |
66 | 60 | function tableize($header, $data) {
|
67 | 61 | if (empty($data)) return '';
|
68 | 62 |
|
@@ -102,3 +96,72 @@ function tabelizeLine($data, $widths) {
|
102 | 96 | return '| ' . implode(' | ', $fields) . ' |';
|
103 | 97 | }
|
104 | 98 |
|
| 99 | +function load($fileish) { |
| 100 | + $orig = $fileish; |
| 101 | + |
| 102 | + if (empty($fileish)) { |
| 103 | + $fileish = 'composer.lock'; |
| 104 | + } |
| 105 | + |
| 106 | + if (file_exists($fileish)) { |
| 107 | + return mustDecodeJson(file_get_contents($fileish), $fileish); |
| 108 | + } |
| 109 | + |
| 110 | + if (strpos($fileish, ':') === false) { |
| 111 | + $fileish .= ':composer.lock'; |
| 112 | + } |
| 113 | + |
| 114 | + $lines = ''; |
| 115 | + |
| 116 | + exec('git show '. escapeshellarg($fileish), $lines, $exit); |
| 117 | + |
| 118 | + if ($exit !== 0) { |
| 119 | + error_log("Error: cannot open $orig or find it in git as $fileish"); |
| 120 | + exit(1); |
| 121 | + } |
| 122 | + |
| 123 | + return mustDecodeJson(implode("\n", $lines), $fileish); |
| 124 | +} |
| 125 | + |
| 126 | +function mustDecodeJson($json, $context) { |
| 127 | + $data = json_decode($json); |
| 128 | + |
| 129 | + if (empty($data)) { |
| 130 | + error_log("Error: contents from $context does not decode as json"); |
| 131 | + exit(1); |
| 132 | + } |
| 133 | + |
| 134 | + return $data; |
| 135 | +} |
| 136 | + |
| 137 | +function parseOpts() { |
| 138 | + $given = getopt('h', array('from:', 'to:', 'json', 'pretty', 'help')); |
| 139 | + |
| 140 | + if (array_key_exists('h', $given) || array_key_exists('help', $given)) { |
| 141 | + usage(); |
| 142 | + } |
| 143 | + |
| 144 | + return array( |
| 145 | + 'from' => array_key_exists('from', $given) ? $given['from'] : 'HEAD', |
| 146 | + 'to' => array_key_exists('to', $given) ? $given['to'] : '', |
| 147 | + 'json' => array_key_exists('json', $given), |
| 148 | + 'pretty' => version_compare(PHP_VERSION, '5.4.0', '>=') && array_key_exists('pretty', $given), |
| 149 | + ); |
| 150 | +} |
| 151 | + |
| 152 | +function usage() { |
| 153 | + print <<<EOF |
| 154 | +Usage: composer-lock-diff [options] |
| 155 | +
|
| 156 | +Options: |
| 157 | + -h --help Print this message |
| 158 | + --from The file, git ref, or git ref with filename to compare from (HEAD:composer.lock) |
| 159 | + --to The file, git ref, or git ref with filename to compare to (composer.lock) |
| 160 | + --json Format output as JSON |
| 161 | + --pretty Pretty print JSON output (PHP >= 5.4.0) |
| 162 | +
|
| 163 | +EOF; |
| 164 | + |
| 165 | + exit(0); |
| 166 | +} |
| 167 | + |
0 commit comments