Skip to content

Commit 829047c

Browse files
authored
Merge pull request #48 from hakre/patch-1
Exit in error if VCS cannot be forced, closes #47
2 parents d8f6352 + 5a99c5d commit 829047c

File tree

4 files changed

+37
-22
lines changed

4 files changed

+37
-22
lines changed

Makefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
OPTS ?= ""
2+
PHP = php
23

3-
all: test md no-links only-dev only-prod json json-pretty
4+
all: test test-help test-help-vcs-error md no-links only-dev only-prod json json-pretty
45

56
test:
6-
php ./composer-lock-diff --from test-data/composer.from.lock --to test-data/composer.to.lock $(OPTS)
7+
$(PHP) ./composer-lock-diff --from test-data/composer.from.lock --to test-data/composer.to.lock $(OPTS)
8+
9+
test-help:
10+
$(PHP) ./composer-lock-diff --help
11+
12+
test-help-vcs-error:
13+
$(PHP) ./composer-lock-diff --vcs UNKNOWN ; test $$? -eq 1
714

815
md:
916
$(MAKE) test OPTS=--md

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,18 @@ Or from vim, to insert the output into the commit message, type `:r!composer-loc
3838

3939
### Options
4040

41-
- `--path, -p`: Base to with which to prefix paths. Default "./"
42-
- `--from`: The file^, git ref, or git ref with filename to compare from (HEAD:composer.lock)
41+
- `-h, --help`: Print this message
42+
- `-p, --path`: Base to with which to prefix paths. Default "./"
43+
E.g. `-p app` would look for HEAD:app/composer.lock and app/composer.lock
44+
- `--from`: The file^, git ref, or git ref with filename to compare from (git: HEAD:composer.lock, svn: composer.lock@BASE)
4345
- `--to`: The file^, git ref, or git ref with filename to compare to (composer.lock)
44-
- `--md`: Markdown table output
45-
- `--json`: json output
46-
- `--pretty`: pretty output when combined with `--json` (>=5.4 only)
46+
- `--json`: Format output as JSON
47+
- `--pretty`: Pretty print JSON output (PHP >= 5.4.0)
48+
- `--md`: Use markdown instead of plain text
4749
- `--no-links`: Don't include Compare links in plain text or any links in markdown
4850
- `--only-prod`: Only include changes from `packages`
4951
- `--only-dev`: Only include changes from `packages-dev`
50-
- `--vcs`: Force vcs (git, svn, ...). Default auto-detect from path
52+
- `--vcs`: Force vcs (git, svn, ...). Default: attempt to auto-detect
5153

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

composer-lock-diff

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ if (! $opts['only-prod']) {
1616
}
1717

1818
if ($opts['json']) {
19-
$json_opts = ($opts['pretty']) ? JSON_PRETTY_PRINT : 0;
20-
print json_encode($changes, $json_opts);
19+
$json_opts = ($opts['pretty']) ? JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT : 0;
20+
echo json_encode($changes, $json_opts), PHP_EOL;
2121
return;
2222
}
2323

@@ -69,7 +69,7 @@ function diff($key, $data_from, $data_to) {
6969

7070
function version($pkg)
7171
{
72-
if((substr($pkg->version,0,4) == 'dev-' || '-dev' === substr($pkg->version, -4)) && isset($pkg->source) && isset($pkg->source->reference)) {
72+
if((substr($pkg->version,0,4) == 'dev-' || '-dev' === substr($pkg->version, -4)) && isset($pkg->source->reference)) {
7373
$version = substr($pkg->source->reference,0,7) ?: '';
7474
} else {
7575
$version = (string) $pkg->version;
@@ -444,8 +444,8 @@ function parseOpts() {
444444

445445
$vcs = array_key_exists('vcs', $given) ? $given['vcs'] : '';
446446
if ($vcs && !function_exists('vcsLoad' . ucfirst($vcs))) {
447-
error_log("Unsupported vcs '$vcs'\n");
448-
usage();
447+
error_log(sprintf("Unsupported VCS '$vcs'; supported are: '%s'\n", implode("', '", getVcses())));
448+
usage(1);
449449
}
450450

451451
return array(
@@ -462,14 +462,14 @@ function parseOpts() {
462462
);
463463
}
464464

465-
function usage() {
465+
function usage($status = 0) {
466466
$vcses = implode(', ', getVcses());
467-
print <<<EOF
467+
$help = <<<EOF
468468
Usage: composer-lock-diff [options]
469469
470470
Options:
471-
-h --help Print this message
472-
--path, -p Base to with which to prefix paths. Default "./"
471+
-h, --help Print this message
472+
-p, --path Base to with which to prefix paths. Default "./"
473473
E.g. `-p app` would look for HEAD:app/composer.lock and app/composer.lock
474474
--from The file, git ref, or git ref with filename to compare from
475475
(git: HEAD:composer.lock, svn: composer.lock@BASE)
@@ -480,11 +480,16 @@ Options:
480480
--no-links Don't include Compare links in plain text or any links in markdown
481481
--only-prod Only include changes from `packages`
482482
--only-dev Only include changes from `packages-dev`
483-
--vcs Force vcs ($vcses). Default: attempt to auto-detect
484-
483+
--vcs Force VCS ($vcses). Default: attempt to auto-detect
485484
EOF;
486485

487-
exit(0);
486+
if ($status) {
487+
error_log($help);
488+
} else {
489+
echo $help, "\n";
490+
}
491+
492+
exit($status);
488493
}
489494
# vim: ff=unix ts=4 ss=4 sr et
490495

test-svn.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#!/bin/bash
22

3-
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
3+
DIR="$( CDPATH='' cd -- "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
4+
[ "$DIR" ] || exit
45

56
svn --help > /dev/null || { echo "Fail: could not find 'svn' executable"; exit 1; }
67
svnadmin --help > /dev/null || { echo "Fail: could not find 'svnadmin' executable"; exit 1; }
78

89
trap cleanup INT ERR
910

1011
function cleanup() {
11-
cd "$DIR/test-data"
12+
cd "$DIR/test-data" || exit
1213
rm -rf proj proj-working svnrepo
1314
}
1415

0 commit comments

Comments
 (0)