Skip to content

Commit 3c9467f

Browse files
authored
Merge pull request #6 from davidrjonas/feature/any-ref
Adds proper getopt with --to/from to load any file or ref
2 parents b7bad2c + cb09871 commit 3c9467f

File tree

2 files changed

+82
-15
lines changed

2 files changed

+82
-15
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ Or from vim, to insert the output into the commit message, type `:r!composer-loc
3030

3131
### Options
3232

33+
- `--from`: The file^, git ref, or git ref with filename to compare from (HEAD:composer.lock)
34+
- `--to`: The file^, git ref, or git ref with filename to compare to (composer.lock)
3335
- `--json`: json output
3436
- `--pretty`: pretty output when combined with `--json` (>=5.4 only)
3537

38+
^ File includes anything available as a [protocol stream wrapper](http://php.net/manual/en/wrappers.php) such as URLs.
39+
3640
Example Table Output
3741
====================
3842

composer-lock-diff

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
#!/usr/bin/env php
22
<?php
33

4-
$prod = diff('packages');
5-
$dev = diff('packages-dev');
4+
$opts = parseOpts();
65

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;
911
print json_encode(array('changes' => $prod, 'changes-dev' => $dev), $opts);
1012
return;
1113
}
1214

1315
print tableize('Production Changes', $prod);
1416
print tableize('Dev Changes', $dev);
1517

16-
function diff($key) {
18+
function diff($key, $from, $to) {
1719

1820
$pkgs = array();
1921

20-
$lines = '';
21-
exec('git show HEAD:composer.lock', $lines);
22-
$data = json_decode(implode("\n", $lines));
22+
$data = load($from);
2323

2424
foreach($data->$key as $pkg) {
2525
$pkgs[$pkg->name] = array(version($pkg), 'REMOVED');
2626
}
2727

28-
$data = json_decode(file_get_contents('composer.lock'));
28+
$data = load($to);
2929

3030
foreach($data->$key as $pkg) {
3131
if (! array_key_exists($pkg->name, $pkgs)) {
@@ -57,12 +57,6 @@ function version($pkg)
5757
return $version;
5858
}
5959

60-
function hasOpt($opt) {
61-
global $argv;
62-
$prefix = strlen($opt) === 1 ? '-' : '--';
63-
return in_array($prefix.$opt, $argv);
64-
}
65-
6660
function tableize($header, $data) {
6761
if (empty($data)) return '';
6862

@@ -102,3 +96,72 @@ function tabelizeLine($data, $widths) {
10296
return '| ' . implode(' | ', $fields) . ' |';
10397
}
10498

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

Comments
 (0)