Skip to content

Commit b505628

Browse files
committed
Adds git and svn vcs* functions
1 parent 9eb5b9a commit b505628

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

composer-lock-diff

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,120 @@ function formatCompareDrupal($url, $from, $to) {
300300
return sprintf('%s/compare/8.x-%s...8.x-%s', $url, substr(urlencode($from), 0, -2), substr(urlencode($to), 0, -2));
301301
}
302302

303+
//
304+
// ## VCSes ####################
305+
//
306+
307+
function getVcses() {
308+
return array('git', 'svn');
309+
}
310+
311+
function vcsDetectGit($_fileish) {
312+
// Is there a git executable?
313+
exec('git --version', null, $exit);
314+
if ($exit !== 0) return array(false, "'git --version' exited with non-zero code '$exit'");
315+
316+
// Does this look like a git repo?
317+
$path = findUp('.', '.git');
318+
return array(!! $path, ($path) ? false : "Could not find .git in current directory or parents");
319+
}
320+
321+
function vcsLoadGit($fileish, $base_path, $_default_fileish) {
322+
// We don't care about $default_fileish here - we are expected to load from
323+
// git and we must make a filename to do that.
324+
if (empty($fileish)) {
325+
$fileish = 'HEAD';
326+
}
327+
328+
if (strpos($fileish, ':') === false) {
329+
$fileish .= ':' . $base_path . 'composer.lock';
330+
}
331+
332+
$lines = array();
333+
exec('git show ' . escapeshellarg($fileish), $lines, $exit);
334+
335+
if ($exit !== 0) {
336+
return array('', "'git show $fileish' exited with non-zero code '$exit'");
337+
}
338+
339+
return array(mustDecodeJson(implode("\n", $lines), $fileish), false);
340+
}
341+
342+
function vcsDetectSvn($fileish, $base_path, $default_fileish) {
343+
// Is there a git executable?
344+
exec('svn --version', null, $exit);
345+
if ($exit !== 0) return array(false, "'svn --version' exited with non-zero code '$exit'");
346+
347+
if (strpos('svn://', $fileish) === 0) {
348+
return array(true, false);
349+
}
350+
351+
// Does this look like a svn repo?
352+
$path = findUp('.', '.svn');
353+
return array(!! $path, ($path) ? false : "Could not find .svn in current directory or parents");
354+
}
355+
356+
function vcsLoadSvn($fileish, $base_path, $_default_fileish) {
357+
// We don't care about $default_fileish here - we are expected to load from
358+
// svn and we must make a filename to do that.
359+
if (empty($fileish)) {
360+
$fileish = 'BASE';
361+
}
362+
363+
// If $fileish starts with a url scheme that 'svn cat' can handle or ^, or
364+
// if it contains a @, assume it is already a proper svn identifier.
365+
// - file:// http:// https:// svn:// svn+ssh:// => absolute url of
366+
// repository (file/http/https may have been handled with stream wrappers
367+
// if '--vcs svn' wasn't specified)
368+
// - ^ => relative url from current workspace repository
369+
// - @ => repository url with revision
370+
if (preg_match('#^\^|^(file|http|https|svn|svn\+ssh)://|@#i', $fileish) === 0) {
371+
$fileish = $base_path . 'composer.lock@'.$fileish;
372+
}
373+
374+
exec('svn cat ' . escapeshellarg($fileish), $lines, $exit);
375+
376+
if ($exit !== 0) {
377+
return array('', "'svn cat $fileish' exited with non-zero code '$exit'");
378+
}
379+
380+
return array(mustDecodeJson(implode("\n", $lines), $fileish), false);
381+
}
382+
383+
function findUp($path, $filename, $tries = 10) {
384+
if (empty($path)) {
385+
$path = '.';
386+
}
387+
388+
// > Trailing delimiters, such as \ and /, are also removed
389+
// > returns false on failure, e.g. if the file does not exist.
390+
$path = realpath($path);
391+
if ($path === false) return false;
392+
393+
do {
394+
$candidate = joinPath($path, $filename);
395+
396+
if (file_exists($candidate)) {
397+
return $candidate;
398+
}
399+
400+
$path = dirnameSafe($path);
401+
} while ($path !== false && --$tries > 0);
402+
403+
return false;
404+
}
405+
406+
function dirnameSafe($path) {
407+
$parent = dirname($path);
408+
return ($parent != $path && !empty($parent)) ? $parent : false;
409+
}
410+
411+
function joinPath(/* path parts */) {
412+
return implode(DIRECTORY_SEPARATOR, array_map(function($part) {
413+
return trim($part, DIRECTORY_SEPARATOR);
414+
}, func_get_args()));
415+
}
416+
303417
function parseOpts() {
304418
$given = getopt('hp:', array('path:', 'from:', 'to:', 'md', 'json', 'pretty', 'no-links', 'only-prod', 'only-dev', 'help', 'vcs:'));
305419

0 commit comments

Comments
 (0)