Skip to content

Commit 4d093d5

Browse files
committed
Adds --path and defaults to ./ (inspired by @paxal, GH-2)
1 parent 3c9467f commit 4d093d5

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Or from vim, to insert the output into the commit message, type `:r!composer-loc
3030

3131
### Options
3232

33+
- `--path, -p`: Base to with which to prefix paths. Default "./"
3334
- `--from`: The file^, git ref, or git ref with filename to compare from (HEAD:composer.lock)
3435
- `--to`: The file^, git ref, or git ref with filename to compare to (composer.lock)
3536
- `--json`: json output

composer-lock-diff

+27-11
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
$opts = parseOpts();
55

6-
$prod = diff('packages', $opts['from'], $opts['to']);
7-
$dev = diff('packages-dev', $opts['from'], $opts['to']);
6+
$prod = diff('packages', $opts['from'], $opts['to'], $opts['path']);
7+
$dev = diff('packages-dev', $opts['from'], $opts['to'], $opts['path']);
88

99
if ($opts['json']) {
1010
$opts = ($opt['pretty']) ? JSON_PRETTY_PRINT : 0;
@@ -15,17 +15,17 @@ if ($opts['json']) {
1515
print tableize('Production Changes', $prod);
1616
print tableize('Dev Changes', $dev);
1717

18-
function diff($key, $from, $to) {
18+
function diff($key, $from, $to, $base_path) {
1919

2020
$pkgs = array();
2121

22-
$data = load($from);
22+
$data = load($from, $base_path);
2323

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

28-
$data = load($to);
28+
$data = load($to, $base_path);
2929

3030
foreach($data->$key as $pkg) {
3131
if (! array_key_exists($pkg->name, $pkgs)) {
@@ -96,19 +96,25 @@ function tabelizeLine($data, $widths) {
9696
return '| ' . implode(' | ', $fields) . ' |';
9797
}
9898

99-
function load($fileish) {
99+
function load($fileish, $base_path = '') {
100100
$orig = $fileish;
101101

102+
if (empty($base_path)) {
103+
$base_path = '.' . DIRECTORY_SEPARATOR;
104+
} else {
105+
$base_path = rtrim($base_path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
106+
}
107+
102108
if (empty($fileish)) {
103-
$fileish = 'composer.lock';
109+
$fileish = $base_path . 'composer.lock';
104110
}
105111

106112
if (file_exists($fileish)) {
107113
return mustDecodeJson(file_get_contents($fileish), $fileish);
108114
}
109115

110-
if (strpos($fileish, ':') === false) {
111-
$fileish .= ':composer.lock';
116+
if (strpos($orig, ':') === false) {
117+
$fileish .= ':' . $base_path . 'composer.lock';
112118
}
113119

114120
$lines = '';
@@ -135,13 +141,21 @@ function mustDecodeJson($json, $context) {
135141
}
136142

137143
function parseOpts() {
138-
$given = getopt('h', array('from:', 'to:', 'json', 'pretty', 'help'));
144+
$given = getopt('hp:', array('path:', 'from:', 'to:', 'json', 'pretty', 'help'));
145+
146+
foreach(array('help' => 'h', 'path' => 'p') as $long => $short) {
147+
if (array_key_exists($short, $given)) {
148+
$given[$long] = $given[$short];
149+
unset($given[$short]);
150+
}
151+
}
139152

140-
if (array_key_exists('h', $given) || array_key_exists('help', $given)) {
153+
if (array_key_exists('help', $given)) {
141154
usage();
142155
}
143156

144157
return array(
158+
'path' => array_key_exists('path', $given) ? $given['path'] : '',
145159
'from' => array_key_exists('from', $given) ? $given['from'] : 'HEAD',
146160
'to' => array_key_exists('to', $given) ? $given['to'] : '',
147161
'json' => array_key_exists('json', $given),
@@ -155,6 +169,8 @@ Usage: composer-lock-diff [options]
155169
156170
Options:
157171
-h --help Print this message
172+
--path, -p Base to with which to prefix paths. Default "./"
173+
E.g. `-p app` would look for HEAD:app/composer.lock and app/composer.lock
158174
--from The file, git ref, or git ref with filename to compare from (HEAD:composer.lock)
159175
--to The file, git ref, or git ref with filename to compare to (composer.lock)
160176
--json Format output as JSON

0 commit comments

Comments
 (0)