Skip to content

Instantly share code, notes, and snippets.

@danb-humaan
Created October 10, 2016 05:41
Show Gist options
  • Save danb-humaan/05b4f7299a6dc95e84ed60f4788b4e7c to your computer and use it in GitHub Desktop.
Save danb-humaan/05b4f7299a6dc95e84ed60f4788b4e7c to your computer and use it in GitHub Desktop.
Helper function to get a git commit hash from a few sources.
<?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