From 49b5bcf9cd3c6761590db003ec22bbd4cd900db7 Mon Sep 17 00:00:00 2001 From: magdev Date: Sat, 1 Jun 2019 12:26:33 +0200 Subject: [PATCH] Introduced GitService --- app/conf/services.yaml | 4 ++ src/Service/GitService.php | 115 +++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 src/Service/GitService.php diff --git a/app/conf/services.yaml b/app/conf/services.yaml index f00dadc..06c362c 100644 --- a/app/conf/services.yaml +++ b/app/conf/services.yaml @@ -19,6 +19,10 @@ services: class: '\Magdev\Dossier\Service\MinifierService' arguments: ['@monolog'] + git: + class: '\Magdev\Dossier\Service\GitService' + arguments: ['@config', '@monolog'] + translator: class: '\Magdev\Dossier\Service\TranslatorService' arguments: ['@config', '@monolog'] diff --git a/src/Service/GitService.php b/src/Service/GitService.php new file mode 100644 index 0000000..ba86392 --- /dev/null +++ b/src/Service/GitService.php @@ -0,0 +1,115 @@ +config = $config; + $this->logger = $logger; + } + + + /** + * Initialize a new repository + * + * @return bool + */ + public function init(): bool + { + if (!$this->isGitRepository()) { + $this->exec('git init'); + } + return $this->isGitRepository(); + } + + + /** + * Get the current branch name + * + * @return string + */ + public function getCurrentBranchName(): string + { + if (!$this->isGitRepository()) { + return $this->config->get('docname'); + } + $output = $this->exec('git branch | grep \\\* | cut -d \' \' -f2'); + return $output[0]; + } + + + /** + * Check if repository is initialized + * + * @return bool + */ + public function isGitRepository(): bool + { + return is_dir(PROJECT_ROOT.'/.git') && file_exists(PROJECT_ROOT.'/.git/config'); + } + + + /** + * Execute a system command + * + * @param string $cmd + * @return array + */ + protected function exec(string $cmd): array + { + $output = array(); + exec($cmd, $output); + return $output; + } +}