Created
October 10, 2016 05:41
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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