GitRepository.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Intl\Util;
  11. use Symfony\Component\Filesystem\Filesystem;
  12. use Symfony\Component\Intl\Exception\RuntimeException;
  13. /**
  14. * @internal
  15. */
  16. final class GitRepository
  17. {
  18. private $path;
  19. public function __construct(string $path)
  20. {
  21. $this->path = $path;
  22. $this->getUrl();
  23. }
  24. public static function download(string $remote, string $targetDir): self
  25. {
  26. self::exec('which git', 'The command "git" is not installed.');
  27. $filesystem = new Filesystem();
  28. if (!$filesystem->exists($targetDir.'/.git')) {
  29. $filesystem->remove($targetDir);
  30. $filesystem->mkdir($targetDir);
  31. self::exec(sprintf('git clone %s %s', escapeshellarg($remote), escapeshellarg($targetDir)));
  32. }
  33. return new self(realpath($targetDir));
  34. }
  35. public function getPath(): string
  36. {
  37. return $this->path;
  38. }
  39. public function getUrl(): string
  40. {
  41. return $this->getLastLine($this->execInPath('git config --get remote.origin.url'));
  42. }
  43. public function getLastCommitHash(): string
  44. {
  45. return $this->getLastLine($this->execInPath('git log -1 --format="%H"'));
  46. }
  47. public function getLastAuthor(): string
  48. {
  49. return $this->getLastLine($this->execInPath('git log -1 --format="%an"'));
  50. }
  51. public function getLastAuthoredDate(): \DateTime
  52. {
  53. return new \DateTime($this->getLastLine($this->execInPath('git log -1 --format="%ai"')));
  54. }
  55. public function getLastTag(callable $filter = null): string
  56. {
  57. $tags = $this->execInPath('git tag -l --sort=v:refname');
  58. if (null !== $filter) {
  59. $tags = array_filter($tags, $filter);
  60. }
  61. return $this->getLastLine($tags);
  62. }
  63. public function checkout(string $branch)
  64. {
  65. $this->execInPath(sprintf('git checkout %s', escapeshellarg($branch)));
  66. }
  67. private function execInPath(string $command): array
  68. {
  69. return self::exec(sprintf('cd %s && %s', escapeshellarg($this->path), $command));
  70. }
  71. private static function exec(string $command, string $customErrorMessage = null): array
  72. {
  73. exec(sprintf('%s 2>&1', $command), $output, $result);
  74. if (0 !== $result) {
  75. throw new RuntimeException(null !== $customErrorMessage ? $customErrorMessage : sprintf('The "%s" command failed.', $command));
  76. }
  77. return $output;
  78. }
  79. private function getLastLine(array $output): string
  80. {
  81. return array_pop($output);
  82. }
  83. }