Skip to content

Commit

Permalink
fix: remote branch line parse fail, update some command logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 5, 2022
1 parent 2b3f268 commit 45b1a18
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 17 deletions.
8 changes: 8 additions & 0 deletions src/CmdBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,12 @@ public function isFail(): bool
{
return $this->code !== 0;
}

/**
* @return bool
*/
public function isSuccess(): bool
{
return $this->code === 0;
}
}
3 changes: 1 addition & 2 deletions src/Command/Commit.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public function __invoke(string $message, array $options = []): bool
$this->addFlags($builder, $options, ['all', 'amend']);
$this->addValues($builder, $options, ['reuse-message', 'squash', 'author', 'date', 'cleanup']);

// $this->run($builder);
$builder->run();
$this->run($builder);

return true;
}
Expand Down
10 changes: 4 additions & 6 deletions src/Command/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function list(string $sort = ''): array
$builder = $this->getCommandBuilder()
->addIf("--sort=$sort", $sort);

$output = $builder->run();
$output = $this->run($builder);
return $this->split($output);
}

Expand Down Expand Up @@ -119,10 +119,9 @@ public function create(string $tag, $commit = null, array $options = []): bool
$builder->add($commit);
}

// $this->run($builder);
$builder->run();
$this->run($builder);

return true;
return $builder->isSuccess();
}

/**
Expand All @@ -144,8 +143,7 @@ public function delete(Traversable|array|string $tag): bool
$builder->add($value);
}

// $this->run($builder);
$builder->run();
$this->run($builder);
return true;
}

Expand Down
38 changes: 37 additions & 1 deletion src/Concern/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ abstract class AbstractCommand
*/
protected Git $git;

/**
* @var bool
*/
protected bool $printCmd = false;

/**
* @param Git $git
*/
Expand All @@ -40,6 +45,25 @@ public function __construct(Git $git)
$this->git = $git;
}

/**
* @return bool
*/
public function isPrintCmd(): bool
{
return $this->printCmd;
}

/**
* @param bool $printCmd
*
* @return static
*/
public function setPrintCmd(bool $printCmd): static
{
$this->printCmd = $printCmd;
return $this;
}

/**
* @return string
*/
Expand Down Expand Up @@ -119,6 +143,18 @@ protected function getCommandBuilder(string ...$args): CmdBuilder
return $this->git->getCommandBuilder($cmd, ...$args);
}

/**
* build git command for run
*
* @param mixed ...$args
*
* @return CmdBuilder
*/
public function builder(string ...$args): CmdBuilder
{
return $this->getCommandBuilder(...$args);
}

/**
* Executes a process
*
Expand All @@ -128,7 +164,7 @@ protected function getCommandBuilder(string ...$args): CmdBuilder
*/
public function run(CmdBuilder $builder): string
{
return $builder->run();
return $builder->setPrintCmd($this->printCmd)->run();
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/GitUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ public static function parseRemoteUrl(string $url): array
/**
* '/(?<current>\*| ) (?<name>[^\s]+) +((?:->) (?<alias>[^\s]+)|(?<hash>[0-9a-z]{7}) (?<message>.*))/'
*/
public const PATTERN_BR_LINE = '/(?<current>\*| ) (?<name>\S+) +((?:->) (?<alias>\S+)|(?<hash>[0-9a-z]{4,41}) (?<message>.*))/';
// public const PATTERN_BR_LINE = '/(?<current>\* )(?<name>\S+) +((?:->) (?<alias>\S+)|(?<hash>[0-9a-z]{4,41}) (?<message>.*))/';
public const PATTERN_BR_LINE = '/(?<current>\* )?(?<name>\S+) +(?<hash>[0-9a-z]{4,41}) (?<message>.*)/';

/**
* @param string $line
Expand Down Expand Up @@ -167,7 +168,7 @@ public static function parseBranchLine(string $line, bool $verbose = true): arra

// up from: https://github.com/kzykhys/PHPGit/pull/15/files
if (isset($matches['current'])) {
$branch['current'] = $matches['current'] === '*';
$branch['current'] = trim($matches['current']) === '*';
}

// full name with remote. eg: remotes/origin/NAME
Expand Down
24 changes: 18 additions & 6 deletions src/Repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class Repo
*/
private ?Git $git = null;

/*
* @var Info
/**
* @var bool
*/
// private $info;
private bool $debug = false;

/**
* @var string
Expand Down Expand Up @@ -178,6 +178,7 @@ public function getStatusInfo(bool $refresh = false): StatusInfo

$text = $this->ensureGit()
->newCmd('status', '-bs', '-u')
->setPrintCmd($this->debug)
->run(true);

$this->statusInfo = StatusInfo::fromString($text);
Expand Down Expand Up @@ -291,7 +292,7 @@ public function getLastTagName(bool $refresh = false): string
$git = $this->ensureGit();
$val = $git->isQuietRun();

$str = $git->setQuietRun(true)->getLastTagName($refresh);
$str = $git->setPrintCmd($this->debug)->getLastTagName($refresh);
$git->setQuietRun($val);

return $str;
Expand Down Expand Up @@ -321,8 +322,8 @@ public function getCurrentBranch(bool $refresh = false): string
public function getBranchInfos(bool $refresh = false): BranchInfos
{
if ($refresh || !$this->branchInfos) {
$str = $this->newCmd('branch', '-v', '--abbrev=7')
->setQuietRun(true)
$str = $this->newCmd('branch', '-av', '--abbrev=7')
->setPrintCmd($this->debug)
->run(true);

$this->branchInfos = BranchInfos::fromString($str);
Expand Down Expand Up @@ -523,4 +524,15 @@ public function getPlatform(): string
return $this->platform;
}

/**
* @param bool $debug
*
* @return Repo
*/
public function setDebug(bool $debug): self
{
$this->debug = $debug;
return $this;
}

}
2 changes: 2 additions & 0 deletions test/Info/BranchInfosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ public function testBranchInfos_verbose(): void

$this->assertNotEmpty($cur = $brInfos->getCurrentBranch());
$this->assertTrue($cur->current);
$this->assertNotEmpty($brInfos->getRemoteBranches());
$this->assertNotEmpty($brInfos->getRemoteBranch('my_new_br'));
}
}

0 comments on commit 45b1a18

Please sign in to comment.