Last active
August 29, 2015 14:13
-
-
Save ethaizone/54c5b6efdf57563d4987 to your computer and use it in GitHub Desktop.
Add new logic for git that deploy via jenkin (multi-scm) and add as original code that I use on Laravel
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 | |
/** | |
* Get Git Status via PHP (No execute function) | |
* @author Nimit Suwannagate <ethaizone@hotmail.com> | |
* Laravel Version | |
*/ | |
function getGitStatus() | |
{ | |
$branch = null; | |
$commit = null; | |
$lastCheckoutTime = null; | |
$headFile = app_path().'/../.git/HEAD'; | |
if (file_exists($headFile)) | |
{ | |
$head = file_get_contents($headFile); | |
preg_match("#refs/heads/(.+)$#", $head, $matches); | |
if (! empty($matches[1])) | |
{ | |
$branch = $matches[1]; | |
$headLogsFile = app_path().'/../.git/logs/refs/heads/'.$branch; | |
if (file_exists($headLogsFile)) | |
{ | |
$heads = file($headLogsFile); | |
$heads = array_reverse($heads); | |
if ( preg_match("#^[0-9a-f]+ ([0-9a-f]+) ([^<]+) <([^>]+)> ([0-9]+) (\+[0-9]{4})#", $heads[0], $matches)) | |
{ | |
if (! empty($matches[1])) | |
{ | |
$commit = $matches[1]; | |
$timestamp = $matches[4]; | |
$lastCheckoutTime = date("Y-m-d H:i:s", $timestamp); | |
} | |
} | |
} | |
$headFile = app_path().'/../.git/refs/heads/'.$branch; | |
if (! $commit && file_exists($headFile)) | |
{ | |
$head = file_get_contents($headFile); | |
preg_match("#([0-9a-f]+)#", $head, $matches); | |
if (! empty($matches[1])) | |
{ | |
$commit = $matches[1]; | |
} | |
} | |
} else { | |
// new logic - for code that build via jenkin multiple scms | |
$commit = trim($head); | |
$headFile = app_path().'/../.git/FETCH_HEAD'; | |
if ($commit && file_exists($headFile)) | |
{ | |
$head = file_get_contents($headFile); | |
// sd($head); | |
preg_match("#{$commit}\t\tbranch '([^']+)'#", $head, $matches); | |
if (! empty($matches[1])) | |
{ | |
$branch = $matches[1]; | |
} | |
} | |
if ($branch) | |
{ | |
$headLogsFile = app_path().'/../.git/logs/HEAD'; | |
if (file_exists($headLogsFile)) | |
{ | |
$heads = file($headLogsFile); | |
$heads = array_reverse($heads); | |
if ( preg_match("#^[0-9a-f]+ ([0-9a-f]+) ([^<]+) <([^>]+)> ([0-9]+) (\+[0-9]{4})#", $heads[0], $matches)) | |
{ | |
if (! empty($matches[1]) && $commit == $matches[1]) | |
{ | |
$commit = $matches[1]; | |
$timestamp = $matches[4]; | |
$lastCheckoutTime = date("Y-m-d H:i:s", $timestamp); | |
} | |
} | |
} | |
} | |
} | |
} | |
return compact('branch', 'commit', 'lastCheckoutTime'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment