Skip to content

Commit

Permalink
up: gitlab - migrate the gitlab:resolve command to a command class.
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 12, 2022
1 parent 06be919 commit e082447
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 46 deletions.
6 changes: 5 additions & 1 deletion app/Console/Attach/Gitlab/BranchDeleteCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ protected function execute(Input $input, Output $output): void

$force = $fs->getOpt('force');
$notMain = $fs->getOpt('not-main');
$dryRun = $this->flags->getOpt('dry-run');

$dryRun = false;
if ($p = $this->getParent()) {
$dryRun = $p->getFlags()->getOpt('dry-run');
}

$deletedNum = 0;
$mainRemote = $gl->getMainRemote();
Expand Down
52 changes: 7 additions & 45 deletions app/Console/Controller/Gitx/GitLabController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Inhere\Kite\Console\Attach\Gitlab\ProjectCmd;
use Inhere\Kite\Console\Component\RedirectToGitGroup;
use Inhere\Kite\Console\SubCmd\Gitflow\BranchCreateCmd;
use Inhere\Kite\Console\SubCmd\GitlabCmd\ResolveConflictCmd;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Kite;
use Throwable;
Expand Down Expand Up @@ -78,6 +79,7 @@ protected static function commandAliases(): array
'checkout' => ['co'],
'branch' => BranchCmd::aliases(),
MergeRequestCmd::getName() => MergeRequestCmd::aliases(),
ResolveConflictCmd::getName() => ResolveConflictCmd::aliases(),
];
}

Expand All @@ -90,6 +92,7 @@ protected function subCommands(): array
BranchCmd::class,
ProjectCmd::class,
MergeRequestCmd::class,
ResolveConflictCmd::class,
];
}

Expand Down Expand Up @@ -348,10 +351,11 @@ public function cloneCommand(FlagsParser $fs, Output $output): void
*/
public function checkoutCommand(FlagsParser $fs, Output $output): void
{
$co = Cmd::git('checkout')
->addArgs($fs->getArg('branch'))
->runAndPrint();
$br = $fs->getArg('branch');
// $repo = Repo::new();
// $repo->hasBranch(); TODO

$co = Cmd::git('checkout')->addArgs($br)->runAndPrint();
if ($co->isFail()) {
return;
}
Expand Down Expand Up @@ -495,48 +499,6 @@ public function openCommand(FlagsParser $fs, Output $output): void
$output->success('Complete');
}

/**
* Resolve conflicts preparing for current git branch.
*
* @help
* 1. will checkout to <cyan>branch</cyan>
* 2. will update code by <cyan>git pull</cyan>
* 3. update the <cyan>branch</cyan> codes from main repository
* 4. merge current-branch codes from main repository
* 5. please resolve conflicts by tools or manual
*
* @arguments
* branch string;The conflicts target branch name. eg: testing, qa, pre;required
*
* @param FlagsParser $fs
* @param Output $output
*/
public function resolveCommand(FlagsParser $fs, Output $output): void
{
$gitlab = $this->getGitlab();
$branch = $fs->getArg('branch');
$branch = $gitlab->getRealBranchName($branch);
$dryRun = $this->flags->getOpt('dry-run');

$curBranch = $gitlab->getCurBranch();
// $orgRemote = $gitlab->getForkRemote();

$runner = CmdRunner::new();
$runner->setDryRun($dryRun);
$runner->add('git fetch');
$runner->addf('git checkout %s', $branch);
// git checkout --track origin/BRANCH
// $runner->addf('git checkout --track %s/%s', $orgRemote, $branch);
$runner->addf('git pull');
$runner->addf('git pull %s %s', $gitlab->getMainRemote(), $branch);
$runner->addf('git pull %s %s', $gitlab->getMainRemote(), $curBranch);
$runner->run(true);

$output->success('Complete. please resolve conflicts by tools or manual');
$output->note('TIPS can exec this command after resolved for quick commit:');
$output->colored(" git add . && git commit && git push && kite gl pr -o head && git checkout $curBranch", 'mga');
}

/**
* parse link print information
*
Expand Down
74 changes: 74 additions & 0 deletions app/Console/SubCmd/GitlabCmd/ResolveConflictCmd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Console\SubCmd\GitlabCmd;

use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Common\CmdRunner;
use Inhere\Kite\Helper\AppHelper;

/**
* class ResolveConflictCmd
*
* @author inhere
* @date 2022/7/11
*/
class ResolveConflictCmd extends Command
{
protected static string $name = 'resolve';
protected static string $desc = 'create a new branch for git project';

public static function aliases(): array
{
return ['rc'];
}

/**
* Resolve conflicts preparing for current git branch.
*
* @help
* 1. will checkout to <cyan>branch</cyan>
* 2. will update code by <cyan>git pull</cyan>
* 3. update the <cyan>branch</cyan> codes from main repository
* 4. merge current-branch codes from main repository
* 5. please resolve conflicts by tools or manual
*
* @arguments
* branch string;The conflicts target branch name. eg: testing, qa, pre;required
*
* @param Input $input
* @param Output $output
*/
protected function execute(Input $input, Output $output)
{
$fs = $this->flags;
$gl = AppHelper::newGitlab();

$branch = $fs->getArg('branch');
$branch = $gl->getRealBranchName($branch);

$dryRun = false;
if ($p = $this->getParent()) {
$dryRun = $p->getFlags()->getOpt('dry-run');
}

$curBranch = $gl->getCurBranch();
// $orgRemote = $gl->getForkRemote();

$runner = CmdRunner::new();
$runner->setDryRun($dryRun);
$runner->add('git fetch');
$runner->addf('git checkout %s', $branch);
// git checkout --track origin/BRANCH
// $runner->addf('git checkout --track %s/%s', $orgRemote, $branch);
$runner->addf('git pull');
$runner->addf('git pull %s %s', $gl->getMainRemote(), $branch);
$runner->addf('git pull %s %s', $gl->getMainRemote(), $curBranch);
$runner->run(true);

$output->success('Complete. please resolve conflicts by tools or manual');
$output->note('can exec this command after resolved for quick commit:');
$output->colored(" git add . && git commit && git push && kite gl pr -o head && git checkout $curBranch", 'mga');
}
}

0 comments on commit e082447

Please sign in to comment.