diff --git a/src/CommandRunner/Git.php b/src/CommandRunner/Git.php index 13145e5e..98e5ba36 100644 --- a/src/CommandRunner/Git.php +++ b/src/CommandRunner/Git.php @@ -56,8 +56,20 @@ public function getRemoteUrl($remote = 'origin') return trim($this->commandRunner->run($command)); } + /** + * Check if the git repository in the provided directory has any uncommitted changes + * + * @param string $directory A directory containing a git repository + * @return bool True if there are changes, false otherwise + */ + public function hasUncommittedChanges($directory) + { + $changes = $this->run($directory, "git diff-index --name-only HEAD"); + return !(ctype_space($changes) || $changes = ''); + } + private function run($directory, $command) { - $this->commandRunner->run("cd \"$directory\" && $command"); + return $this->commandRunner->run("cd \"$directory\" && $command"); } } diff --git a/src/Deployer.php b/src/Deployer.php index 1c0b9d68..0c0ae1d9 100644 --- a/src/Deployer.php +++ b/src/Deployer.php @@ -51,9 +51,13 @@ public function deploy(Project $project, OutputInterface $output, $repositoryUrl $this->copyGeneratedFiles($output, $directory, $tmpDirectory); - $this->commitChanges($output, $tmpDirectory); + if ($this->git->hasUncommittedChanges($tmpDirectory)) { + $this->commitChanges($output, $tmpDirectory); - $this->pushBranch($output, $branch, $tmpDirectory); + $this->pushBranch($output, $branch, $tmpDirectory); + } else { + $output->writeln('Skipping deploy, no changes detected'); + } $this->deleteTempDirectory($tmpDirectory); } diff --git a/tests/UnitTest/CommandRunner/GitTest.php b/tests/UnitTest/CommandRunner/GitTest.php index 00e7de28..5d2d77ce 100644 --- a/tests/UnitTest/CommandRunner/GitTest.php +++ b/tests/UnitTest/CommandRunner/GitTest.php @@ -91,6 +91,34 @@ public function get_remote_url_should_return_url() $this->assertEquals('the-remote', $remote); } + /** + * @test + */ + public function has_uncommitted_changes_should_detect_changes() + { + $directory = 'directory'; + $changes = ' + changed_file_one.txt + changed_file_two.txt + '; + $this->expectCommandIsRun('cd "directory" && git diff-index --name-only HEAD', $changes); + + $this->assertTrue($this->git->hasUncommittedChanges($directory)); + } + + /** + * @test + */ + public function has_uncommitted_changes_should_detect_no_changes() + { + $directory = 'directory'; + $changes = ' + '; + $this->expectCommandIsRun('cd "directory" && git diff-index --name-only HEAD', $changes); + + $this->assertFalse($this->git->hasUncommittedChanges($directory)); + } + private function expectCommandIsRun($command, $return = null) { $this->commandRunner->expects($this->once())