Created
January 30, 2017 09:34
-
-
Save printminion/3e4c099fbb9baa9393f0594ff9f5dd69 to your computer and use it in GitHub Desktop.
get git branch and hash
This file contains hidden or 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 | |
| /** | |
| * @desc Get git branch info/hash | |
| * @author Misha M.-Kupriyanov https://google.com/+MishaMKupriyanov | |
| * @link https://gist.github.com/printminion/3e4c099fbb9baa9393f0594ff9f5dd69 | |
| */ | |
| echo "[i]Git" . join(':', getSCID()); | |
| function getSCID($longHash = false) | |
| { | |
| $branchName = 'unknown'; | |
| $hash = 'unknown'; | |
| $repoPath = getcwd(); | |
| $headFilePath = $repoPath . '/.git/HEAD'; | |
| if (!file_exists($headFilePath)) { | |
| array($branchName, $hash); | |
| } | |
| $stringFromFile = file($headFilePath, FILE_USE_INCLUDE_PATH); | |
| $stringFromFile = $stringFromFile[0]; //get the string from the array | |
| $branchName = str_replace('ref: refs/heads/', '', $stringFromFile); | |
| $branchName = trim($branchName); | |
| $hashFilePath = $repoPath . '/.git/refs/heads/' . $branchName; | |
| if (!file_exists($hashFilePath)) { | |
| array($branchName, $hash); | |
| } | |
| $hash = file($hashFilePath, FILE_USE_INCLUDE_PATH); | |
| $hash = $hash[0]; //get the string from the array | |
| if (!$longHash) { | |
| $hash = substr($hash, 0, 7); | |
| } | |
| return array($branchName, $hash); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment