I want to display the Git version on my site.
How can I display a semantic version number from Git, that non-technical users of a site can easily reference when raising
Run git tag in terminal to preview your tags and say you got i.e:
v1.0.0
v1.1.0
v1.2.4
here's how to get the latest version v1.2.4
function getVersion() {
$hash = exec("git rev-list --tags --max-count=1");
return exec("git describe --tags $hash");
}
echo getVersion(); // "v1.2.4"
Coincidentally (if your tags are ordered), since exec returns only the last row we could just do:
function getVersion() {
return exec("git tag");
}
echo getVersion(); // "v1.2.4"
To get all the rows string use shell_exec:
function getVersions() {
return shell_exec("git tag");
}
echo getVersions(); // "v1.0.0
// v1.1.0
// v1.2.4"
To get an Array:
$tagsArray = explode(PHP_EOL, shell_exec("git tag"));
To sort tags by date:
git tag --sort=committerdate
Docs: git-for-each-ref#_field_names
For sorting purposes, fields with numeric values sort in numeric order (objectsize, authordate, committerdate, creatordate, taggerdate). All other fields are used to sort in their byte-value order.