Skip to content

Commit

Permalink
Fix force and improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gseidel committed May 1, 2021
1 parent ffed604 commit 6c4dac4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
17 changes: 11 additions & 6 deletions src/Git/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,18 @@ public function pushSubtreeBranch(string $remote, string $prefix, string $branch
$branchId = uniqid();
$this->execute(["git", "checkout", $commit]);
$this->execute(["git", "checkout", "-b", $branchId]);
if ($force) {
$this->execute(["git", "push", "--set-upstream", $remote, '--force', sprintf("%s:%s", $branchId, $branch)]);
} else {
$this->execute(["git", "push", "--set-upstream", $remote, sprintf("%s:%s", $branchId, $branch)]);
try {
if ($force) {
$this->execute(["git", "push", "--set-upstream", $remote, '--force', sprintf("%s:%s", $branchId, $branch)]);
} else {
$this->execute(["git", "push", "--set-upstream", $remote, sprintf("%s:%s", $branchId, $branch)]);
}
} catch (GitException $e) {
throw $e;
} finally {
$this->execute(["git", "checkout", $currentBranch]);
$this->execute(["git", "branch", "-D", $branchId]);
}
$this->execute(["git", "checkout", $currentBranch]);
$this->execute(["git", "branch", "-D", $branchId]);
} else {
$this->execute(["git", "checkout", $currentBranch]);
}
Expand Down
19 changes: 12 additions & 7 deletions src/Subroutine/PushSubtree.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ public function __invoke()
return Command::FAILURE;
}

$splitShPath = (new DownloadSplitSh($this->input, $this->output, $this->questionHelper, $this->yes))();
$git = new Git(getcwd());
if ($splitShPath !== null) {
$git->setSplitShBin($splitShPath);
}

$subtrees = [];
foreach ($this->configuration->getSubtrees() as $subtree) {
if ($this->name && $this->name === $subtree->getName()) {
Expand All @@ -87,6 +81,17 @@ public function __invoke()
}
}

if (!count($subtrees)) {
$this->output->writeln(sprintf('No subtrees found'));
return Command::FAILURE;
}

$splitShPath = (new DownloadSplitSh($this->input, $this->output, $this->questionHelper, $this->yes))();
$git = new Git(getcwd());
if ($splitShPath !== null) {
$git->setSplitShBin($splitShPath);
}

foreach ($subtrees as $subtree) {
if (!$git->hasRemote($subtree->getName())) {
$this->output->writeln(sprintf('Add remote "%s"', $subtree->getName()));
Expand All @@ -112,7 +117,7 @@ private function pushBranch(array $subtrees, Git $git)
$branch = $this->branch !== null ? $this->branch : $git->getCurrentBranch();
foreach ($subtrees as $subtree) {
$this->output->writeln(sprintf('Push branch "%s" to remote "%s"', $branch, $subtree->getName()));
$git->pushSubtreeBranch($subtree->getName(), $subtree->getPrefix(), $branch);
$git->pushSubtreeBranch($subtree->getName(), $subtree->getPrefix(), $branch, $this->force);
}
}

Expand Down

0 comments on commit 6c4dac4

Please sign in to comment.