Skip to content

Instantly share code, notes, and snippets.

@webkod3r
Last active March 29, 2020 18:25
Show Gist options
  • Save webkod3r/bbbec0813d594c6a3786e5e8f8e4c15d to your computer and use it in GitHub Desktop.
Save webkod3r/bbbec0813d594c6a3786e5e8f8e4c15d to your computer and use it in GitHub Desktop.
write current git hash to file
<?php
if (!function_exists('get_commit_hash')) {
/**
* Checks to see if we have a .commit_hash file or .git repo and return the hash if we do.
*
* @return null|string
*/
function get_commit_hash()
{
$commitHash = base_path('.commit_hash');
if (file_exists($commitHash)) {
// See if we have the .commit_hash file
return trim(exec(sprintf('cat %s', $commitHash)));
} elseif (is_dir(base_path('.git'))) {
// Do we have a .git repo?
return trim(exec('git log --pretty="%h" -n1 HEAD'));
} else {
// ¯\_(ツ)_/¯
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment