Commit 9eb26548 authored by mickadoo's avatar mickadoo

68: Add GitTools service, renaming method

parent de70959e
......@@ -3,7 +3,6 @@
namespace AppBundle\Model;
use AppBundle\Utils\LocaleTools;
use \AppBundle\Model\Version;
class Language {
......@@ -162,7 +161,7 @@ class Language {
* @return string
* e.g. "Spanish"
*/
public function englishName() {
public function getEnglishName() {
return LocaleTools::getLanguageNameInLocale($this->code, 'en');
}
......@@ -185,10 +184,10 @@ class Language {
*/
public function descriptiveName() {
if ($this->code == 'en') {
return $this->englishName();
return $this->getEnglishName();
}
else {
return $this->nativeName() . " (" . $this->englishName() . ")";
return $this->nativeName() . " (" . $this->getEnglishName() . ")";
}
}
......
<?php
namespace AppBundle\Utils;
use Symfony\Component\Process\Process;
class GitTools {
/**
* @param $repoPath
*
* @return string
*/
public function getCurrentBranch($repoPath): string {
$checkBranch = new Process('git rev-parse --abbrev-ref HEAD', $repoPath);
$checkBranch->run();
$currentBranch = trim($checkBranch->getOutput());
return $currentBranch;
}
/**
* @param $repoPath
* @param $branch
*/
public function checkoutBranch($repoPath, $branch) {
$gitCheckoutBranch = new Process("git checkout {$branch}", $repoPath);
$gitCheckoutBranch->run();
}
/**
* @param string $repoPath
*/
public function pull($repoPath) {
$gitPull = new Process('git pull', $repoPath);
$gitPull->run();
if (!$gitPull->isSuccessful()) {
$msg = "Unable to run 'git pull'. Output: " . $gitPull->getErrorOutput();
throw new \Exception($msg);
}
}
/**
* @param $repoURL
* @param $repoPath
*
* @throws \Exception
*/
public function clone($repoURL, $repoPath) {
$process = new Process("git clone {$repoURL} {$repoPath}");
$process->run();
if (!$process->isSuccessful()) {
$msg = "Unable to run 'git clone'. Output: " . $process->getErrorOutput();
throw new \Exception($msg);
}
}
/**
* If we are on the not on the correct branch, attempt to check it out
* (first locally, then remotely).
*
* @param string $repoPath
* @param string $branch
*/
public function checkout(string $repoPath, string $branch) {
$currentBranch = $this->getCurrentBranch($repoPath);
if ($currentBranch != $branch) {
if (!$this->localBranchExists($repoPath, $branch)) {
if (!$this->remoteBranchExists($repoPath, $branch)) {
$err = "'{$branch}' branch does not exist " . "remotely or locally.";
throw new \Exception($err);
}
}
$this->checkoutBranch($repoPath, $branch);
}
}
/**
* @param $repoPath
* @param $branch
*
* @return bool
*/
private function localBranchExists($repoPath, $branch) {
$cmd = "git show-ref --verify refs/heads/{$branch}";
$process = new Process($cmd, $repoPath);
$process->run();
return $process->isSuccessful();
}
/**
* @param $repoPath
* @param $branch
*
* @return bool
*/
private function remoteBranchExists($repoPath, $branch) {
$cmd = sprintf("git show-ref --verify refs/remotes/origin/{$branch}");
$process = new Process($cmd, $repoPath);
$process->run();
return $process->isSuccessful();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment