Skip to content

Instantly share code, notes, and snippets.

@dfelton
Last active July 10, 2020 18:42
Show Gist options
  • Save dfelton/b3a74b24a914de4341864fd79dac6b37 to your computer and use it in GitHub Desktop.
Save dfelton/b3a74b24a914de4341864fd79dac6b37 to your computer and use it in GitHub Desktop.
Bash function that pushes current branch to upstream development branch
pushToDev() {
php -nr '
function getCurrentBranchName(): ?string {
$branches = shell_exec("git branch 2>/dev/null");
$branchName = null;
if (is_string($branches)) {
$branches = explode(PHP_EOL, trim($branches));
foreach ($branches as $branch) {
if (strpos($branch, "*") === 0) {
$branchName = trim(substr($branch, 1));
break;
}
}
}
return $branchName;
}
$branches = shell_exec("git branch 2>/dev/null");
if ($branches === null) {
echo "Error: not a git repository (or any of the parent directories)\n";
exit(1);
}
if (
strpos(shell_exec("pwd"), "/m2/") === false ||
strpos(shell_exec("pwd"), "/m2/makerportal") !== false ||
strpos(shell_exec("pwd"), "/m2/pim") !== false
) {
$devBranchName = "development";
} else {
$devBranchName = "development-mp";
}
$currentBranch = getCurrentBranchName();
if ($currentBranch === null) {
echo "Error: unable to determine current branch\n";
exit(1);
} elseif ($currentBranch === "development" || $currentBranch === "development-mp") {
echo "Error: Currently on $currentBranch. Nothing to do.\n";
exit(1);
}
$branches = shell_exec("git branch -r 2>/dev/null");
if ($branches === null) {
echo "No upstream set\n";
exit(1);
}
$branches = explode(PHP_EOL, trim($branches));
$doesUpstreamHaveDevelopment = false;
foreach ($branches as $branch) {
$branch = explode("/", trim($branch));
unset($branch[0]);
$branch = implode("/", $branch);
if ($branch === $devBranchName) {
$doesUpstreamHaveDevelopment = true;
break;
}
}
if (!$doesUpstreamHaveDevelopment) {
echo "Error: could not detect that upstream has a \"$devBranchName\" branch.\n";
exit(1);
}
echo "You are about to merge and push to \033[1m$devBranchName\033[0m, are you sure? (answer \"yes\" to continue) ";
$handle = fopen("php://stdin", "r");
if (strtolower(trim(fgets($handle))) !== "yes") {
fclose($handle);
echo "Aborting. No operations performed.\n";
exit(1);
}
fclose($handle);
echo "Looking for modified files...\n";
if (strpos(shell_exec("git status"), "modified:") !== false) {
$stashed = true;
echo "Modified files detected, stashing...\n";
shell_exec("git stash");
} else {
$stashed = false;
echo "No modified files detected, proceeding.";
}
echo "Pushing $currentBranch to $devBranchName environment...\n";
shell_exec("git branch -D $devBranchName 2>/dev/null");
echo shell_exec("git fetch && git switch $devBranchName && git merge $currentBranch --no-edit && git push && git switch $currentBranch");
if ($devBranchName === getCurrentBranchName()) {
echo "Auto merge and push to $devBranchName failed. Please resolve merge conflicts and continue manually.\n";
if ($stashed) {
echo "\033[1mNOTICE:\033[0m changes to your branch were stashed.\n";
echo "Remember retrieve them with \"git stash pop\" from within the $currentBranch when you are done.\n";
}
exit(1);
}
if ($stashed) {
echo "Retrieving stashed files...\n";
shell_exec("git stash pop");
echo "Stashed files retrieved...\n";
}
echo "Complete! Happy coding :-)\n";
exit(0);
'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment