From e91edfeb1e62935a57140f38f949a1afd95f20ea Mon Sep 17 00:00:00 2001 From: Roman Date: Wed, 7 Apr 2021 13:09:50 +0200 Subject: [PATCH] CLI + version 1.2.4 --- cli.php | 42 +++++++++++++++++++++++++++++++++--------- core/core.php | 2 +- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/cli.php b/cli.php index 73de6f8..6439613 100644 --- a/cli.php +++ b/cli.php @@ -160,6 +160,24 @@ function handleDatabase(array $argv) { } } +function findPullBranch(array $output): ?string { + foreach ($output as $line) { + $parts = preg_split('/\s+/', $line); + if (count($parts) >= 3 && $parts[2] === '(fetch)') { + $remoteName = $parts[0]; + $url = $parts[1]; + if (endsWith($url, "@github.com:rhergenreder/web-base.git") || + endsWith($url, "@romanh.de:Projekte/web-base.git") || + $url === 'https://github.com/rhergenreder/web-base.git' || + $url === 'https://git.romanh.de/Projekte/web-base.git') { + return "$remoteName/master"; + } + } + } + + return null; +} + function onMaintenance(array $argv) { $action = $argv[2] ?? "status"; $maintenanceFile = "MAINTENANCE"; @@ -180,9 +198,21 @@ function onMaintenance(array $argv) { _exit("Maintenance disabled"); } else if ($action === "update") { - // TODO: find that dynamically - $pullBranch = "root/master"; - $pushBranch = "origin/master"; + printLine("$ git remote -v"); + exec("git remote -v", $gitRemote, $ret); + if ($ret !== 0) { + die(); + } + + $pullBranch = findPullBranch($gitRemote); + if ($pullBranch === null) { + $pullBranch = 'origin/master'; + printLine("Unable to find remote update branch. Make sure, you are still in a git repository, and one of the remote branches " . + "have the original fetch url"); + printLine("Trying to continue with '$pullBranch'"); + } else { + printLine("Using remote update branch: $pullBranch"); + } printLine("$ git fetch " . str_replace("/", " ", $pullBranch)); exec("git fetch " . str_replace("/", " ", $pullBranch), $gitFetch, $ret); @@ -205,12 +235,6 @@ function onMaintenance(array $argv) { _exit("You have uncommitted changes. Please commit them before updating."); } - printLine("$ git rev-list HEAD...$pushBranch --ignore-submodules --count"); - exec("git rev-list HEAD...$pushBranch --ignore-submodules --count", $gitRevList, $ret); - if ($ret !== 0) { - _exit("HEAD is behind your local branch. Please push your commits before updating."); - } - // enable maintenance mode if it wasn't turned on before if (!$isMaintenanceEnabled) { printLine("Turning on maintenance mode"); diff --git a/core/core.php b/core/core.php index ff01d87..c41d07e 100644 --- a/core/core.php +++ b/core/core.php @@ -1,6 +1,6 @@