Skip to content

Instantly share code, notes, and snippets.

@printminion
Created January 30, 2017 09:34
Show Gist options
  • Select an option

  • Save printminion/3e4c099fbb9baa9393f0594ff9f5dd69 to your computer and use it in GitHub Desktop.

Select an option

Save printminion/3e4c099fbb9baa9393f0594ff9f5dd69 to your computer and use it in GitHub Desktop.
get git branch and hash
<?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