Skip to content

Commit

Permalink
Merge pull request #6 from davidrjonas/feature/any-ref
Browse files Browse the repository at this point in the history
Adds proper getopt with --to/from to load any file or ref
  • Loading branch information
davidrjonas authored Jun 27, 2017
2 parents b7bad2c + cb09871 commit 3c9467f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 15 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ Or from vim, to insert the output into the commit message, type `:r!composer-loc

### Options

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

^ File includes anything available as a [protocol stream wrapper](http://php.net/manual/en/wrappers.php) such as URLs.

Example Table Output
====================

Expand Down
93 changes: 78 additions & 15 deletions composer-lock-diff
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
#!/usr/bin/env php
<?php

$prod = diff('packages');
$dev = diff('packages-dev');
$opts = parseOpts();

if (hasOpt('json')) {
$opts = (hasOpt('pretty')) ? JSON_PRETTY_PRINT : 0;
$prod = diff('packages', $opts['from'], $opts['to']);
$dev = diff('packages-dev', $opts['from'], $opts['to']);

if ($opts['json']) {
$opts = ($opt['pretty']) ? JSON_PRETTY_PRINT : 0;
print json_encode(array('changes' => $prod, 'changes-dev' => $dev), $opts);
return;
}

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

function diff($key) {
function diff($key, $from, $to) {

$pkgs = array();

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

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

$data = json_decode(file_get_contents('composer.lock'));
$data = load($to);

foreach($data->$key as $pkg) {
if (! array_key_exists($pkg->name, $pkgs)) {
Expand Down Expand Up @@ -57,12 +57,6 @@ function version($pkg)
return $version;
}

function hasOpt($opt) {
global $argv;
$prefix = strlen($opt) === 1 ? '-' : '--';
return in_array($prefix.$opt, $argv);
}

function tableize($header, $data) {
if (empty($data)) return '';

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

function load($fileish) {
$orig = $fileish;

if (empty($fileish)) {
$fileish = 'composer.lock';
}

if (file_exists($fileish)) {
return mustDecodeJson(file_get_contents($fileish), $fileish);
}

if (strpos($fileish, ':') === false) {
$fileish .= ':composer.lock';
}

$lines = '';

exec('git show '. escapeshellarg($fileish), $lines, $exit);

if ($exit !== 0) {
error_log("Error: cannot open $orig or find it in git as $fileish");
exit(1);
}

return mustDecodeJson(implode("\n", $lines), $fileish);
}

function mustDecodeJson($json, $context) {
$data = json_decode($json);

if (empty($data)) {
error_log("Error: contents from $context does not decode as json");
exit(1);
}

return $data;
}

function parseOpts() {
$given = getopt('h', array('from:', 'to:', 'json', 'pretty', 'help'));

if (array_key_exists('h', $given) || array_key_exists('help', $given)) {
usage();
}

return array(
'from' => array_key_exists('from', $given) ? $given['from'] : 'HEAD',
'to' => array_key_exists('to', $given) ? $given['to'] : '',
'json' => array_key_exists('json', $given),
'pretty' => version_compare(PHP_VERSION, '5.4.0', '>=') && array_key_exists('pretty', $given),
);
}

function usage() {
print <<<EOF
Usage: composer-lock-diff [options]
Options:
-h --help Print this message
--from The file, git ref, or git ref with filename to compare from (HEAD:composer.lock)
--to The file, git ref, or git ref with filename to compare to (composer.lock)
--json Format output as JSON
--pretty Pretty print JSON output (PHP >= 5.4.0)
EOF;

exit(0);
}

0 comments on commit 3c9467f

Please sign in to comment.