diff --git a/entrypoint.php b/entrypoint.php index 24ab6a1..1af61f4 100755 --- a/entrypoint.php +++ b/entrypoint.php @@ -43,11 +43,11 @@ note(sprintf('Trying to checkout %s branch', $config->getBranch())); // if the given branch doesn't exist it returns empty string -$branchSwitchedSuccessfully = exec(sprintf('git checkout %s', $config->getBranch())) !== ''; +$branchSwitchedSuccessfully = execOrDie(sprintf('git checkout %s', $config->getBranch())) !== ''; // if the branch doesn't exist we creat it and push to origin // otherwise we just checkout to the given branch -if (! $branchSwitchedSuccessfully) { +if (!$branchSwitchedSuccessfully) { note(sprintf('Creating branch "%s" as it doesn\'t exist', $config->getBranch())); exec_with_output_print(sprintf('git checkout -b %s', $config->getBranch())); @@ -60,16 +60,11 @@ // We're only interested in the .git directory, move it to $TARGET_DIR and use it from now on mkdir($buildDirectory . '/.git', 0777, true); -$copyGitDirectoryCommandLine = sprintf('cp -r %s %s', $cloneDirectory . '/.git', $buildDirectory); -exec($copyGitDirectoryCommandLine, $outputLines, $exitCode); - -if ($exitCode === 1) { - die('Command failed'); -} +execOrDie(sprintf('cp -r %s %s', $cloneDirectory . '/.git', $buildDirectory)); // cleanup old unused data to avoid pushing them -exec('rm -rf ' . $cloneDirectory); +execOrDie('rm -rf ' . $cloneDirectory); // exec('rm -rf .git'); @@ -78,7 +73,7 @@ $copyMessage = sprintf('Copying contents to git repo of "%s" branch', $config->getCommitHash()); note($copyMessage); $commandLine = sprintf('cp -ra %s %s', $config->getPackageDirectory() . '/.', $buildDirectory); -exec($commandLine); +execOrDie($commandLine); note('Files that will be pushed'); list_directory_files($buildDirectory); @@ -102,7 +97,7 @@ // "status --porcelain" retrieves all modified files, no matter if they are newly created or not, // when "diff-index --quiet HEAD" only checks files that were already present in the project. -exec('git status --porcelain', $changedFiles); +execOrDie('git status --porcelain', $changedFiles); // $changedFiles is an array that contains the list of modified files, and is empty if there are no changes. @@ -114,8 +109,8 @@ $message = sprintf('Pushing git commit with "%s" message to "%s"', $commitMessage, $config->getBranch()); note($message); - exec('git commit --message ' . escapeshellarg($commitMessage)); - exec('git push --quiet origin ' . $config->getBranch()); + execOrDie('git commit --message ' . escapeshellarg($commitMessage)); + execOrDie('git push --quiet origin ' . $config->getBranch()); } else { note('No files to change'); } @@ -141,7 +136,7 @@ function createCommitMessage(string $commitSha): string { - exec("git show -s --format=%B {$commitSha}", $outputLines); + execOrDie("git show -s --format=%B {$commitSha}", $outputLines); return $outputLines[0] ?? ''; } @@ -167,16 +162,27 @@ function list_directory_files(string $directory): void /********************* helper functions *********************/ +function execOrDie(string $command, ?array &$output = []): string|false +{ + $result = exec($command, $output, $errorCode); + if (0 === $errorCode) { + return $result; + } + + echo implode(PHP_EOL, $output); + exit(1); +} + function exec_with_note(string $commandLine): void { note('Running: ' . $commandLine); - exec($commandLine); + execOrDie($commandLine); } function exec_with_output_print(string $commandLine): void { - exec($commandLine, $outputLines); + execOrDie($commandLine, $outputLines); echo implode(PHP_EOL, $outputLines); } @@ -184,10 +190,10 @@ function exec_with_output_print(string $commandLine): void function setupGitCredentials(Config $config): void { if ($config->getUserName()) { - exec('git config --global user.name ' . $config->getUserName()); + execOrDie('git config --global user.name ' . $config->getUserName()); } if ($config->getUserEmail()) { - exec('git config --global user.email ' . $config->getUserEmail()); + execOrDie('git config --global user.email ' . escapeshellarg($config->getUserEmail())); } }