Skip to content

Commit e4f7372

Browse files
committed
Adds compare links and --no-links options
1 parent 5d2af3f commit e4f7372

File tree

1 file changed

+78
-5
lines changed

1 file changed

+78
-5
lines changed

composer-lock-diff

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,17 @@ if ($opts['json']) {
1212
return;
1313
}
1414

15-
$table_opts = ($opts['md']) ? array('capped' => false, 'joint' => '|') : array();
15+
$table_opts = array(
16+
'no-links' => $opts['no-links'],
17+
);
18+
19+
if ($opts['md']) {
20+
$table_opts = array_merge($table_opts, array(
21+
'capped' => false,
22+
'joint' => '|',
23+
'url_formatter' => 'urlFormatterMd',
24+
));
25+
}
1626

1727
print tableize('Production Changes', $prod, $table_opts);
1828
print tableize('Dev Changes', $dev, $table_opts);
@@ -24,21 +34,22 @@ function diff($key, $from, $to, $base_path) {
2434
$data = load($from, $base_path);
2535

2636
foreach($data->$key as $pkg) {
27-
$pkgs[$pkg->name] = array(version($pkg), 'REMOVED');
37+
$pkgs[$pkg->name] = array(version($pkg), 'REMOVED', '');
2838
}
2939

3040
$data = load($to, $base_path);
3141

3242
foreach($data->$key as $pkg) {
3343
if (! array_key_exists($pkg->name, $pkgs)) {
34-
$pkgs[$pkg->name] = array('NEW', version($pkg));
44+
$pkgs[$pkg->name] = array('NEW', version($pkg), '');
3545
continue;
3646
}
3747

3848
if ($pkgs[$pkg->name][0] == version($pkg)) {
3949
unset($pkgs[$pkg->name]);
4050
} else {
4151
$pkgs[$pkg->name][1] = version($pkg);
52+
$pkgs[$pkg->name][2] = makeCompareUrl($pkg, $pkgs);
4253
}
4354
}
4455

@@ -61,6 +72,19 @@ function tableize($header, $data, $opts = array()) {
6172

6273
$opts = array_merge(array('capped' => true, 'joint' => '+'), $opts);
6374

75+
if ($opts['no-links']) {
76+
array_walk($data, function(&$values) {
77+
unset($values[2]);
78+
});
79+
} else {
80+
if (array_key_exists('url_formatter', $opts)) {
81+
$formatter = $opts['url_formatter'];
82+
array_walk($data, function(&$values) use ($formatter) {
83+
$values[2] = call_user_func($formatter, $values[2], '...');
84+
});
85+
}
86+
}
87+
6488
$widths = array(maxLength(array_merge(array($header), array_keys($data))));
6589

6690
for($i = 0; $i < count(reset($data)); $i++) {
@@ -71,7 +95,10 @@ function tableize($header, $data, $opts = array()) {
7195
$lines[] = separatorLine($widths, $opts['joint']);
7296
}
7397

74-
$lines[] = tabelizeLine(array($header, 'From', 'To'), $widths);
98+
$titles = array($header, 'From', 'To');
99+
if (! $opts['no-links']) array_push($titles, 'Compare');
100+
101+
$lines[] = tabelizeLine($titles, $widths);
75102
$lines[] = separatorLine($widths, $opts['joint']);
76103

77104
foreach($data as $key => $v) {
@@ -104,6 +131,11 @@ function tabelizeLine($data, $widths) {
104131
return '| ' . implode(' | ', $fields) . ' |';
105132
}
106133

134+
function urlFormatterMd($url, $text) {
135+
if (empty($url)) return '';
136+
return sprintf('[%s](%s)', $text, $url);
137+
}
138+
107139
function load($fileish, $base_path = '') {
108140
$orig = $fileish;
109141

@@ -148,8 +180,47 @@ function mustDecodeJson($json, $context) {
148180
return $data;
149181
}
150182

183+
function makeCompareUrl($pkg, $diff) {
184+
$func = 'formatCompare' . ucfirst(getSourceRepoType($pkg->source->url));
185+
return call_user_func($func, $pkg->source->url, $diff[$pkg->name][0], $diff[$pkg->name][1]);
186+
}
187+
188+
function getSourceRepoType($url) {
189+
if (! preg_match('/^http/i', $url)) {
190+
return 'unknown';
191+
}
192+
193+
$host = strtolower(parse_url($url, PHP_URL_HOST));
194+
195+
if (strpos($host, 'github') !== false) {
196+
return 'github';
197+
} elseif (strpos($host, 'bitbucket') !== false) {
198+
return 'bitbucket';
199+
} elseif (strpos($host, 'gitlab') !== false) {
200+
return 'gitlab';
201+
}
202+
203+
return 'unknown';
204+
}
205+
206+
function formatCompareUnknown($url, $from, $to) {
207+
return '';
208+
}
209+
210+
function formatCompareGithub($url, $from, $to) {
211+
return sprintf('%s/compare/%s...%s', preg_replace('/\.git$/', '', $url), urlencode($from), urlencode($to));
212+
}
213+
214+
function formatCompareBitbucket($url, $from, $to) {
215+
return sprintf('%s/branches/compare/%s%%0D%s', preg_replace('/\.git$/', '', $url), urlencode($from), urlencode($to));
216+
}
217+
218+
function formatCompareGitlab($url, $from, $to) {
219+
return sprintf('%s/compare/%s...%s', preg_replace('/\.git$/', '', $url), urlencode($from), urlencode($to));
220+
}
221+
151222
function parseOpts() {
152-
$given = getopt('hp:', array('path:', 'from:', 'to:', 'md', 'json', 'pretty', 'help'));
223+
$given = getopt('hp:', array('path:', 'from:', 'to:', 'md', 'json', 'pretty', 'no-links', 'help'));
153224

154225
foreach(array('help' => 'h', 'path' => 'p') as $long => $short) {
155226
if (array_key_exists($short, $given)) {
@@ -169,6 +240,7 @@ function parseOpts() {
169240
'md' => array_key_exists('md', $given),
170241
'json' => array_key_exists('json', $given),
171242
'pretty' => version_compare(PHP_VERSION, '5.4.0', '>=') && array_key_exists('pretty', $given),
243+
'no-links' => array_key_exists('no-links', $given),
172244
);
173245
}
174246

@@ -185,6 +257,7 @@ Options:
185257
--json Format output as JSON
186258
--pretty Pretty print JSON output (PHP >= 5.4.0)
187259
--md Use markdown instead of plain text
260+
--no-links Don't include Compare links in plain text or any links in markdown
188261
189262
EOF;
190263

0 commit comments

Comments
 (0)