Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

execOrDie #48

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions entrypoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -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');


Expand All @@ -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);
Expand All @@ -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.

Expand All @@ -114,8 +109,8 @@
$message = sprintf('Pushing git commit with "%s" message to "%s"', $commitMessage, $config->getBranch());
note($message);

exec("git commit --message '{$commitMessage}'");
exec('git push --quiet origin ' . $config->getBranch());
execOrDie("git commit --message '{$commitMessage}'");
execOrDie('git push --quiet origin ' . $config->getBranch());
} else {
note('No files to change');
}
Expand All @@ -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] ?? '';
}

Expand All @@ -167,27 +162,38 @@ 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);
}


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 ' . $config->getUserEmail());
}
}