Sometimes it’s interesting to get the hash of the head of the current git repository, with it you can check if you are running an up-to-date version or postfix it in URLs with ‘?version=(hash)’ so you won’t have problems with browser/proxies caches.
Bellow is a snippet that either returns the hash of the branch, by default master, or false in case you aren’t running the project with a git repo.
<?php
/**
* Get the hash of the current git HEAD
* @param str $branch The git branch to check
* @return mixed Either the hash or a boolean false
*/
function get_current_git_commit( $branch='master' ) {
if ( $hash = file_get_contents( sprintf( '.git/refs/heads/%s', $branch ) ) ) {
return trim($hash);
} else {
return false;
}
}
?>