Skip to content

Commit d20233f

Browse files
authored
Merge pull request #3 from igorsgm/chore/adjustments-2
wip
2 parents fe75ace + 9064f89 commit d20233f

19 files changed

+183
-143
lines changed

src/Console/Commands/PostCommit.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Igorsgm\GitHooks\Contracts\HookCommand;
66
use Igorsgm\GitHooks\Exceptions\HookFailException;
7-
use Igorsgm\GitHooks\Git\GetLasCommitFromLog;
7+
use Igorsgm\GitHooks\Git\GitHelper;
88
use Igorsgm\GitHooks\Git\Log;
99
use Igorsgm\GitHooks\Traits\WithPipeline;
1010
use Illuminate\Console\Command;
@@ -54,15 +54,14 @@ public function getHook(): string
5454
/**
5555
* Execute the console command.
5656
*
57-
* @param GetLasCommitFromLog $command
5857
* @return mixed
5958
*/
60-
public function handle(GetLasCommitFromLog $command)
59+
public function handle()
6160
{
6261
try {
6362
$this->sendLogCommitThroughHooks(
6463
new Log(
65-
$command->exec()->getOutput()
64+
GitHelper::getLastCommitFromLog()
6665
)
6766
);
6867
} catch (HookFailException $e) {

src/Console/Commands/PreCommit.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Igorsgm\GitHooks\Contracts\HookCommand;
66
use Igorsgm\GitHooks\Exceptions\HookFailException;
77
use Igorsgm\GitHooks\Git\ChangedFiles;
8-
use Igorsgm\GitHooks\Git\GetListOfChangedFiles;
8+
use Igorsgm\GitHooks\Git\GitHelper;
99
use Igorsgm\GitHooks\Traits\WithPipeline;
1010
use Illuminate\Console\Command;
1111
use Illuminate\Contracts\Config\Repository;
@@ -54,15 +54,14 @@ public function getHook(): string
5454
/**
5555
* Execute the console command.
5656
*
57-
* @param GetListOfChangedFiles $command
5857
* @return mixed
5958
*/
60-
public function handle(GetListOfChangedFiles $command)
59+
public function handle()
6160
{
6261
try {
6362
$this->sendChangedFilesThroughHooks(
6463
new ChangedFiles(
65-
$command->exec()->getOutput()
64+
GitHelper::getListOfChangedFiles()
6665
)
6766
);
6867
} catch (HookFailException $e) {

src/Console/Commands/PrePush.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Igorsgm\GitHooks\Contracts\HookCommand;
66
use Igorsgm\GitHooks\Exceptions\HookFailException;
7-
use Igorsgm\GitHooks\Git\GetLasCommitFromLog;
7+
use Igorsgm\GitHooks\Git\GitHelper;
88
use Igorsgm\GitHooks\Git\Log;
99
use Igorsgm\GitHooks\Traits\WithPipeline;
1010
use Illuminate\Console\Command;
@@ -54,15 +54,14 @@ public function getHook(): string
5454
/**
5555
* Execute the console command.
5656
*
57-
* @param GetLasCommitFromLog $command
5857
* @return mixed
5958
*/
60-
public function handle(GetLasCommitFromLog $command)
59+
public function handle()
6160
{
6261
try {
6362
$this->sendLogCommitThroughHooks(
6463
new Log(
65-
$command->exec()->getOutput()
64+
GitHelper::getLastCommitFromLog()
6665
)
6766
);
6867
} catch (HookFailException $e) {

src/Contracts/GitCommand.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/Git/GetLasCommitFromLog.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/Git/GetListOfChangedFiles.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/Git/GitHelper.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Igorsgm\GitHooks\Git;
4+
5+
use Igorsgm\GitHooks\Traits\ProcessHelper;
6+
use Symfony\Component\Process\Exception\ProcessFailedException;
7+
8+
class GitHelper
9+
{
10+
use ProcessHelper;
11+
12+
/**
13+
* @return string
14+
*/
15+
public static function getListOfChangedFiles()
16+
{
17+
return self::execAndGetCommandOutput('git status --short');
18+
}
19+
20+
/**
21+
* @return string
22+
*/
23+
public static function getLastCommitFromLog()
24+
{
25+
return self::execAndGetCommandOutput('git log -1 HEAD');
26+
}
27+
28+
/**
29+
* @param string|array $commands
30+
* @return string
31+
*/
32+
private static function execAndGetCommandOutput($commands)
33+
{
34+
$process = self::execCommands($commands);
35+
36+
if (! $process->isSuccessful()) {
37+
throw new ProcessFailedException($process);
38+
}
39+
40+
return $process->getOutput();
41+
}
42+
}

src/Traits/ProcessHelper.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace Igorsgm\GitHooks\Traits;
4+
5+
use RuntimeException;
6+
use Symfony\Component\Process\Process;
7+
8+
trait ProcessHelper
9+
{
10+
/**
11+
* Run the given commands.
12+
*
13+
* @param array|string $commands
14+
* @param array $params
15+
* @return \Symfony\Component\Process\Process
16+
*/
17+
public function runCommands($commands, $params = [])
18+
{
19+
$input = $this->input ?? null;
20+
$output = method_exists($this, 'getOutput') ? $this->getOutput() : null;
21+
22+
if ($output && ! $output->isDecorated()) {
23+
$commands = $this->transformCommands($commands, function ($value) {
24+
return $value.' --no-ansi';
25+
});
26+
}
27+
28+
if ($input && $input->getOption('quiet')) {
29+
$commands = $this->transformCommands($commands, function ($value) {
30+
return $value.' --quiet';
31+
});
32+
}
33+
34+
if (data_get($params, 'silent')) {
35+
$commands = $this->transformCommands($commands, function ($value) {
36+
return $this->buildNoOutputCommand($value);
37+
});
38+
}
39+
40+
$process = Process::fromShellCommandline(
41+
implode(' && ', (array) $commands),
42+
data_get($params, 'cwd'),
43+
data_get($params, 'env'),
44+
data_get($params, 'input'),
45+
data_get($params, 'timeout')
46+
);
47+
48+
$showOutput = data_get($params, 'tty') === true || data_get($params, 'show-output') === true;
49+
if ($showOutput && '\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
50+
try {
51+
$process->setTty(true);
52+
} catch (RuntimeException $e) {
53+
$output->writeln(' <bg=yellow;fg=black> WARN </> '.$e->getMessage().PHP_EOL);
54+
}
55+
}
56+
57+
$process->run(! $showOutput ? null : function ($type, $line) use ($output) {
58+
$output->write(' '.$line);
59+
});
60+
61+
return $process;
62+
}
63+
64+
/**
65+
* Static alias for runCommands.
66+
*
67+
* @param array|string $commands
68+
* @param array $params
69+
* @return Process
70+
*/
71+
public static function execCommands($commands, $params = [])
72+
{
73+
return (new self)->runCommands($commands, $params);
74+
}
75+
76+
/**
77+
* @param array $commands
78+
* @param callable $callback
79+
* @return array
80+
*/
81+
public function transformCommands($commands, $callback)
82+
{
83+
return array_map(function ($value) use ($callback) {
84+
if (substr($value, 0, 5) === 'chmod') {
85+
return $value;
86+
}
87+
88+
return $callback($value);
89+
}, $commands);
90+
}
91+
92+
/**
93+
* Builds the string for a command without console output
94+
*
95+
* @param string $command
96+
* @return string
97+
*/
98+
public function buildNoOutputCommand($command = '')
99+
{
100+
return trim($command).' > '.(PHP_OS_FAMILY == 'Windows' ? 'NUL' : '/dev/null 2>&1');
101+
}
102+
}

src/Traits/WithCommitMessage.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Igorsgm\GitHooks\Exceptions\HookFailException;
88
use Igorsgm\GitHooks\Git\ChangedFiles;
99
use Igorsgm\GitHooks\Git\CommitMessage;
10-
use Igorsgm\GitHooks\Git\GetListOfChangedFiles;
10+
use Igorsgm\GitHooks\Git\GitHelper;
1111
use Illuminate\Contracts\Config\Repository;
1212

1313
trait WithCommitMessage
@@ -26,10 +26,8 @@ trait WithCommitMessage
2626

2727
/**
2828
* Execute the console command.
29-
*
30-
* @param GetListOfChangedFiles $command
3129
*/
32-
public function handle(GetListOfChangedFiles $command)
30+
public function handle()
3331
{
3432
$file = $this->argument('file');
3533

@@ -42,7 +40,7 @@ public function handle(GetListOfChangedFiles $command)
4240
new CommitMessage(
4341
$message,
4442
new ChangedFiles(
45-
$command->exec()->getOutput()
43+
GitHelper::getListOfChangedFiles()
4644
)
4745
)
4846
);

tests/CommitMessageStorageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Igorsgm\GitHooks\Tests;
44

55
use Igorsgm\GitHooks\CommitMessageStorage;
6-
use Igorsgm\GitHooks\Tests\Concerns\WithTmpFiles;
6+
use Igorsgm\GitHooks\Tests\Traits\WithTmpFiles;
77

88
class CommitMessageStorageTest extends TestCase
99
{

0 commit comments

Comments
 (0)