Skip to content

Commit 7402b37

Browse files
committed
update some for run git comand
1 parent 7d03345 commit 7402b37

File tree

5 files changed

+131
-69
lines changed

5 files changed

+131
-69
lines changed

.php-cs-fixer.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
$header = <<<'EOF'
4+
This file is part of Kite.
5+
6+
@link https://github.com/inhere
7+
@author https://github.com/inhere
8+
@license MIT
9+
EOF;
10+
11+
return (new PhpCsFixer\Config())
12+
->setRiskyAllowed(true)
13+
->setRules([
14+
'@PSR2' => true,
15+
'array_syntax' => [
16+
'syntax' => 'short'
17+
],
18+
'list_syntax' => [
19+
'syntax' => 'short'
20+
],
21+
'class_attributes_separation' => true,
22+
'declare_strict_types' => true,
23+
'global_namespace_import' => [
24+
'import_constants' => true,
25+
'import_functions' => true,
26+
],
27+
'header_comment' => [
28+
'comment_type' => 'PHPDoc',
29+
'header' => $header,
30+
'separate' => 'bottom'
31+
],
32+
'no_unused_imports' => true,
33+
'single_quote' => true,
34+
'standardize_not_equals' => true,
35+
'void_return' => true, // add :void for method
36+
])
37+
->setFinder(
38+
PhpCsFixer\Finder::create()
39+
->exclude('test')
40+
->exclude('runtime')
41+
->exclude('vendor')
42+
->in(__DIR__)
43+
)
44+
->setUsingCache(false);

.php_cs

-49
This file was deleted.

src/CmdBuilder.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ protected function execAndPrint(string $command, string $workDir): void
182182
public function run(bool $trimOutput = false): string
183183
{
184184
$cmdLine = $this->getCommandLine();
185-
Color::println("> $cmdLine", 'ylw');
185+
if ($this->printCmd) {
186+
Color::println("> $cmdLine", 'ylw');
187+
}
186188

187189
$process = $this->createProcess();
188190

src/Git.php

+40-9
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ class Git
129129
public const PROTO_SSH = 'ssh';
130130
public const PROTO_HTTP = 'http';
131131

132-
public const URL_GIT = 'git';
133-
public const URL_HTTP = 'http';
132+
public const URL_GIT = 'git';
133+
public const URL_HTTP = 'http';
134134
public const URL_HTTPS = 'https';
135135

136136
public const GITHUB_HOST = 'github.com';
@@ -179,6 +179,9 @@ class Git
179179
/** @var string The git repo dir path. */
180180
private $directory;
181181

182+
/** @var bool see CmdBuilder.printCmd */
183+
private $printCmd = true;
184+
182185
/**
183186
* @param string $repoDir
184187
*
@@ -231,9 +234,10 @@ public function newCmd(string $cmd, string ...$args): CmdBuilder
231234
public function getCommandBuilder(string $command = '', ...$args): CmdBuilder
232235
{
233236
return CmdBuilder::create($command, ...$args)
234-
->setBin($this->bin)
235-
->setWorkDir($this->directory)
236-
->setTimeout( $this->timeout);
237+
->setBin($this->bin)
238+
->setWorkDir($this->directory)
239+
->setTimeout($this->timeout)
240+
->setPrintCmd($this->printCmd);
237241
}
238242

239243
/**
@@ -261,6 +265,17 @@ public function getLastTag(): string
261265
return $this->runCmdLine($cmdLine, true);
262266
}
263267

268+
/**
269+
* @return string
270+
*/
271+
public function getLastCommit(): string
272+
{
273+
// latest commit id by: git log --pretty="%h %s" -n1 HEAD
274+
$cmdLine = 'git log --pretty="%h %s" -n1 HEAD';
275+
276+
return $this->runCmdLine($cmdLine, true);
277+
}
278+
264279
/**
265280
* @return string
266281
*/
@@ -300,10 +315,9 @@ public function getGit(): Git
300315
*
301316
* @return Git
302317
*/
303-
public function setBin($bin): Git
318+
public function setBin(string $bin): Git
304319
{
305320
$this->bin = $bin;
306-
307321
return $this;
308322
}
309323

@@ -317,7 +331,6 @@ public function setBin($bin): Git
317331
public function setRepository(string $directory): Git
318332
{
319333
$this->directory = $directory;
320-
321334
return $this;
322335
}
323336

@@ -331,7 +344,6 @@ public function setRepository(string $directory): Git
331344
public function setRepoDir(string $directory): Git
332345
{
333346
$this->directory = $directory;
334-
335347
return $this;
336348
}
337349

@@ -374,4 +386,23 @@ public function setTimeout(int $timeout): Git
374386
$this->timeout = $timeout;
375387
return $this;
376388
}
389+
390+
/**
391+
* @return bool
392+
*/
393+
public function isPrintCmd(): bool
394+
{
395+
return $this->printCmd;
396+
}
397+
398+
/**
399+
* @param bool $printCmd
400+
*
401+
* @return Git
402+
*/
403+
public function setPrintCmd(bool $printCmd): Git
404+
{
405+
$this->printCmd = $printCmd;
406+
return $this;
407+
}
377408
}

src/Repo.php

+44-10
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ class Repo
7676
*/
7777
private $currentBranch;
7878

79+
/**
80+
* @var string 'commitId message'
81+
*/
82+
private $lastCommit;
83+
7984
/**
8085
* @var string
8186
*/
@@ -159,7 +164,7 @@ public function exec(string $cmd, ...$args)
159164
public function getRemoteInfo(string $name = '', string $type = 'fetch'): RemoteInfo
160165
{
161166
$name = $name ?: $this->defaultRemote;
162-
$key = $name . '.' . $type;
167+
$key = $name . '.' . $type;
163168
if (isset($this->remoteInfos[$key])) {
164169
return $this->remoteInfos[$key];
165170
}
@@ -245,14 +250,17 @@ protected function loadDefaultRemoteInfo(): void
245250
}
246251

247252
/**
253+
* @param bool $refresh
254+
*
248255
* @return string
249256
*/
250-
public function getLastTagName(): string
257+
public function getLastTagName(bool $refresh = false): string
251258
{
252259
$git = $this->ensureGit();
253260

254-
$cmdLine = 'git fetch --tags';
255-
$git->runCmdLine($cmdLine);
261+
if ($refresh) {
262+
$git->runCmdLine('git fetch --tags');
263+
}
256264

257265
// $cmdLine = 'git describe --tags $(git rev-list --tags --max-count=1)';
258266
$cmdLine = 'git describe --abbrev=0 --tags';
@@ -276,6 +284,22 @@ public function getCurrentBranch(bool $refresh = false): string
276284
return $this->currentBranch;
277285
}
278286

287+
/**
288+
* @param bool $refresh
289+
*
290+
* @return string
291+
*/
292+
public function getLastCommit(bool $refresh = false): string
293+
{
294+
if (false === $refresh && null !== $this->lastCommit) {
295+
return $this->lastCommit;
296+
}
297+
298+
$this->lastCommit = $this->ensureGit()->getLastCommit();
299+
300+
return $this->lastCommit;
301+
}
302+
279303
/**
280304
* @param bool $refresh
281305
*
@@ -316,31 +340,33 @@ public function getChangedFiles(): ?Generator
316340
} elseif (strpos($file, 'D ') === 0) {
317341
yield substr($file, 3);
318342

319-
// new files
343+
// new files
320344
} elseif (strpos($file, '?? ') === 0) {
321345
yield substr($file, 3);
322346
}
323347
}
324348
}
325349

326350
/**
351+
* @param bool $refresh
352+
*
327353
* @return string[]
328354
*/
329-
public function getInfo(): array
355+
public function getInfo(bool $refresh = false): array
330356
{
331357
$remoteUrls = [];
332358
foreach ($this->getRemotes() as $name => $urls) {
333359
$remoteUrls[$name] = $urls['fetch'];
334360
}
335361

336362
$repoInfo = [
337-
'platformName' => $this->getPlatform(),
338-
'currentBranch' => $this->getCurrentBranch(),
339-
'lastCommitId' => $this->getLastCommitId(),
363+
'platformName' => $this->getPlatform(),
364+
'currentBranch' => $this->getCurrentBranch($refresh),
365+
'lastCommit' => $this->getLastCommit($refresh),
340366
'remoteList' => $remoteUrls,
341367
];
342368

343-
if ($tagName = $this->getLastTagName()) {
369+
if ($tagName = $this->getLastTagName($refresh)) {
344370
$repoInfo['lastTagName'] = $tagName;
345371
}
346372

@@ -386,6 +412,14 @@ public function getGit(): Git
386412
return $this->ensureGit();
387413
}
388414

415+
/**
416+
* @param bool $printCmd
417+
*/
418+
public function setPrintCmd(bool $printCmd): void
419+
{
420+
$this->ensureGit()->setPrintCmd($printCmd);
421+
}
422+
389423
/**
390424
* @return string
391425
*/

0 commit comments

Comments
 (0)