Get the latest version info of a github project
The functions takes a github repo uri and returns version information.
For example we could pass in git://github.com/jquery/jquery.git
and the function would return
jquery/jquery:
Latest: 2.0.0b1
Stable: 1.9.1
Quite something for so little code, the secret is the usage of http://www.version.is/
function getLatestVersion($githubUrl)
{
$pathPart = parse_url($githubUrl, PHP_URL_PATH);
$repo = str_replace('.git', "", $pathPart);
$curl = curl_init('http://api.version.is/' . $repo);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
$versionInfo = curl_exec($curl);
curl_close($curl);
return $versionInfo;
}
Written by Markus Hausammann
Related protips
3 Responses
From here: http://www.version.is/docs/introduction/ it says that it relies on git tags for recognizing versioning (mind you, I just skimmed the docs). So, it wouldn't work on untagged repos, is that right? And just for clarity, by tags we mean tagged using git tag
command?
I've just never used that, that's why I'm asking. Wondering if I shouldn't start
Yes, that's correct! Since these days most projects want to be composer/packagist installable, they need to have tags for their versions so most projects actually have tags. But if there are no tags, version.is will return the string 'Repo has not tags.' You can look for this string in the return and return null or even implement some fallback method. I think the solution presented here is just very lean and easy but has its limitations.
Thanks, good tip. Also, I tried this out after finding the secret formula hidden here (it is an odd place to have that, no? Transitioning O.o).
Anyway, using api.version.is/jquery/jquery
yields
jquery/jquery:
Latest: 2.0.0b1
Stable: 1.9.1
Nifty.