chg: [internal] Current branch and commit checking

pull/7959/head
Jakub Onderka 2021-11-14 11:30:17 +01:00
parent 736aba20a3
commit 0cc7804219
3 changed files with 43 additions and 6 deletions

View File

@ -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");
}
}
}

View File

@ -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;
}
}

View File

@ -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()