PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/CoreConsole/Commands/GitCommit.php

https://github.com/CodeYellowBV/piwik
PHP | 142 lines | 103 code | 25 blank | 14 comment | 12 complexity | 6a5690831ecafe23030009506a9cf9bd MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\Plugins\CoreConsole\Commands;
  10. use Piwik\Plugin\ConsoleCommand;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. /**
  16. */
  17. class GitCommit extends ConsoleCommand
  18. {
  19. protected function configure()
  20. {
  21. $this->setName('git:commit')
  22. ->setDescription('Commit')
  23. ->addOption('message', 'm', InputOption::VALUE_REQUIRED, 'Commit Message');
  24. }
  25. protected function execute(InputInterface $input, OutputInterface $output)
  26. {
  27. $submodules = $this->getSubmodulePaths();
  28. foreach ($submodules as $submodule) {
  29. if (empty($submodule)) {
  30. continue;
  31. }
  32. $status = $this->getStatusOfSubmodule($submodule);
  33. if (false !== strpos($status, '?? ')) {
  34. $output->writeln(sprintf('<error>%s has untracked files or folders. Delete or add them and try again.</error>', $submodule));
  35. $output->writeln('<error>Status:</error>');
  36. $output->writeln(sprintf('<comment>%s</comment>', $status));
  37. return;
  38. }
  39. }
  40. $commitMessage = $input->getOption('message');
  41. if (empty($commitMessage)) {
  42. $output->writeln('No message specified. Use option -m or --message.');
  43. return;
  44. }
  45. if (!$this->hasChangesToBeCommitted()) {
  46. $dialog = $this->getHelperSet()->get('dialog');
  47. $question = '<question>There are no changes to be commited in the super repo, do you just want to commit and converge submodules?</question>';
  48. if (!$dialog->askConfirmation($output, $question, false)) {
  49. $output->writeln('<info>Cool, nothing done. Stage files using "git add" and try again.</info>');
  50. return;
  51. }
  52. }
  53. foreach ($submodules as $submodule) {
  54. if (empty($submodule)) {
  55. continue;
  56. }
  57. $status = $this->getStatusOfSubmodule($submodule);
  58. if (empty($status)) {
  59. $output->writeln(sprintf('%s has no changes, will ignore', $submodule));
  60. continue;
  61. }
  62. $cmd = sprintf('cd %s/%s && git pull && git add . && git commit -am "%s"', PIWIK_DOCUMENT_ROOT, $submodule, $commitMessage);
  63. $this->passthru($cmd, $output);
  64. }
  65. if ($this->hasChangesToBeCommitted()) {
  66. $cmd = sprintf('cd %s && git commit -m "%s"', PIWIK_DOCUMENT_ROOT, $commitMessage);
  67. $this->passthru($cmd, $output);
  68. }
  69. foreach ($submodules as $submodule) {
  70. if (empty($submodule)) {
  71. continue;
  72. }
  73. $cmd = sprintf('cd %s && git add %s', PIWIK_DOCUMENT_ROOT, $submodule);
  74. $this->passthru($cmd, $output);
  75. }
  76. if ($this->hasChangesToBeCommitted()) {
  77. $cmd = sprintf('cd %s && git commit -m "Updating submodules"', PIWIK_DOCUMENT_ROOT);
  78. $this->passthru($cmd, $output);
  79. }
  80. }
  81. private function passthru($cmd, OutputInterface $output)
  82. {
  83. $output->writeln('Executing command: ' . $cmd);
  84. passthru($cmd);
  85. }
  86. private function hasChangesToBeCommitted()
  87. {
  88. $cmd = sprintf('cd %s && git status --porcelain', PIWIK_DOCUMENT_ROOT);
  89. $result = shell_exec($cmd);
  90. $result = trim($result);
  91. if (false !== strpos($result, 'M ')) {
  92. // stages
  93. return true;
  94. }
  95. if (false !== strpos($result, 'MM ')) {
  96. // staged and modified
  97. return true;
  98. }
  99. return false;
  100. }
  101. /**
  102. * @return array
  103. */
  104. private function getSubmodulePaths()
  105. {
  106. $cmd = sprintf("grep path .gitmodules | sed 's/.*= //'");
  107. $submodules = shell_exec($cmd);
  108. $submodules = explode("\n", $submodules);
  109. return $submodules;
  110. }
  111. protected function getStatusOfSubmodule($submodule)
  112. {
  113. $cmd = sprintf('cd %s/%s && git status --porcelain', PIWIK_DOCUMENT_ROOT, $submodule);
  114. $status = trim(shell_exec($cmd));
  115. return $status;
  116. }
  117. }