mirror of https://github.com/MISP/MISP
chg: [internal] Current branch and commit checking
parent
736aba20a3
commit
0cc7804219
|
@ -37,4 +37,38 @@ class GitTool
|
|||
}
|
||||
return $data[0]['sha'];
|
||||
}
|
||||
|
||||
/**
|
||||
* `git rev-parse HEAD`
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function currentCommit()
|
||||
{
|
||||
$head = rtrim(FileAccessTool::readFromFile(ROOT . '/.git/HEAD'));
|
||||
if (substr($head, 0, 5) === 'ref: ') {
|
||||
$path = substr($head, 5);
|
||||
return rtrim(FileAccessTool::readFromFile(ROOT . '/.git/' . $path));
|
||||
} else if (strlen($head) === 40) {
|
||||
return $head;
|
||||
} else {
|
||||
throw new Exception("Invalid head $head");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* `git symbolic-ref HEAD`
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function currentBranch()
|
||||
{
|
||||
$head = rtrim(FileAccessTool::readFromFile(ROOT . '/.git/HEAD'));
|
||||
if (substr($head, 0, 5) === 'ref: ') {
|
||||
$path = substr($head, 5);
|
||||
return str_replace('refs/heads/', '', $path);
|
||||
} else {
|
||||
throw new Exception("ref HEAD is not a symbolic ref");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2764,8 +2764,11 @@ class AppModel extends Model
|
|||
{
|
||||
static $commit;
|
||||
if ($commit === null) {
|
||||
$commit = exec('git rev-parse HEAD');
|
||||
if (!$commit) {
|
||||
App::uses('GitTool', 'Tools');
|
||||
try {
|
||||
$commit = GitTool::currentCommit();
|
||||
} catch (Exception $e) {
|
||||
$this->logException('Could not get current git commit', $e, LOG_NOTICE);
|
||||
$commit = false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3863,11 +3863,11 @@ class Server extends AppModel
|
|||
|
||||
public function getCurrentBranch()
|
||||
{
|
||||
$ref = exec("git symbolic-ref HEAD");
|
||||
if ($ref) {
|
||||
return str_replace('refs/heads/', '', $ref);
|
||||
try {
|
||||
return GitTool::currentBranch();
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function checkoutMain()
|
||||
|
|
Loading…
Reference in New Issue