Skip to content

Commit 95a3a48

Browse files
committed
- Refactored the code to use a new trait WithCommitLog
- Added exception callback in makePipeline method - Changed return type of RegisterHooks::handle() and PrePush::handle() methods to void, because they don't have any returns anymore
1 parent 833be12 commit 95a3a48

File tree

9 files changed

+60
-85
lines changed

9 files changed

+60
-85
lines changed

src/Console/Commands/PostCommit.php

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
namespace Igorsgm\GitHooks\Console\Commands;
44

55
use Igorsgm\GitHooks\Contracts\HookCommand;
6-
use Igorsgm\GitHooks\Exceptions\HookFailException;
7-
use Igorsgm\GitHooks\Git\GitHelper;
8-
use Igorsgm\GitHooks\Git\Log;
9-
use Igorsgm\GitHooks\Traits\WithPipeline;
6+
use Igorsgm\GitHooks\Traits\WithCommitLog;
107
use Illuminate\Console\Command;
118

129
class PostCommit extends Command implements HookCommand
1310
{
14-
use WithPipeline;
11+
use WithCommitLog;
1512

1613
/**
1714
* The name and signature of the console command.
@@ -34,34 +31,4 @@ public function getHook(): string
3431
{
3532
return 'post-commit';
3633
}
37-
38-
/**
39-
* Execute the console command.
40-
*
41-
* @return mixed
42-
*/
43-
public function handle()
44-
{
45-
try {
46-
$this->sendLogCommitThroughHooks(
47-
new Log(
48-
GitHelper::getLastCommitFromLog()
49-
)
50-
);
51-
} catch (HookFailException $e) {
52-
return 1;
53-
}
54-
}
55-
56-
/**
57-
* Send the log commit through the pipes
58-
*
59-
* @param Log $log
60-
*/
61-
protected function sendLogCommitThroughHooks(Log $log): void
62-
{
63-
$this->makePipeline()
64-
->send($log)
65-
->thenReturn();
66-
}
6734
}

src/Console/Commands/PreCommit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function getHook(): string
3838
/**
3939
* Execute the console command.
4040
*
41-
* @return mixed
41+
* @return int|void
4242
*/
4343
public function handle()
4444
{

src/Console/Commands/PrePush.php

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
namespace Igorsgm\GitHooks\Console\Commands;
44

55
use Igorsgm\GitHooks\Contracts\HookCommand;
6-
use Igorsgm\GitHooks\Exceptions\HookFailException;
7-
use Igorsgm\GitHooks\Git\GitHelper;
8-
use Igorsgm\GitHooks\Git\Log;
9-
use Igorsgm\GitHooks\Traits\WithPipeline;
6+
use Igorsgm\GitHooks\Traits\WithCommitLog;
107
use Illuminate\Console\Command;
118

129
class PrePush extends Command implements HookCommand
1310
{
14-
use WithPipeline;
11+
use WithCommitLog;
1512

1613
/**
1714
* The name and signature of the console command.
@@ -34,34 +31,4 @@ public function getHook(): string
3431
{
3532
return 'pre-push';
3633
}
37-
38-
/**
39-
* Execute the console command.
40-
*
41-
* @return mixed
42-
*/
43-
public function handle()
44-
{
45-
try {
46-
$this->sendLogCommitThroughHooks(
47-
new Log(
48-
GitHelper::getLastCommitFromLog()
49-
)
50-
);
51-
} catch (HookFailException $e) {
52-
return 1;
53-
}
54-
}
55-
56-
/**
57-
* Send the log commit through the pipes
58-
*
59-
* @param Log $log
60-
*/
61-
protected function sendLogCommitThroughHooks(Log $log): void
62-
{
63-
$this->makePipeline()
64-
->send($log)
65-
->thenReturn();
66-
}
6734
}

src/Console/Commands/RegisterHooks.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class RegisterHooks extends Command
2525
* Execute the console command.
2626
*
2727
* @param GitHooks $gitHooks
28+
* @return void
29+
* @throws \Exception
2830
*/
2931
public function handle(GitHooks $gitHooks)
3032
{

src/Git/Log.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function __construct(string $log)
5656
*/
5757
private function parse(array $lines): void
5858
{
59-
foreach ($lines as $key => $line) {
59+
foreach ($lines as $line) {
6060
if (strpos($line, 'commit') === 0) {
6161
preg_match('/(?<hash>[a-z0-9]{40})/', $line, $matches);
6262
$this->hash = $matches['hash'] ?? null;

src/GitHooks.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Igorsgm\GitHooks;
44

5+
use Exception;
6+
57
class GitHooks
68
{
79
/**
@@ -37,12 +39,14 @@ public function getAvailableHooks()
3739
/**
3840
* Install git hook
3941
*
40-
* @param string $hook
42+
* @param string $hookName
43+
* @return void
44+
* @throws Exception
4145
*/
4246
public function install($hookName)
4347
{
4448
if (! is_dir($this->getGitHooksDir())) {
45-
throw new \Exception('Git not initialized in this project.');
49+
throw new Exception('Git not initialized in this project.');
4650
}
4751

4852
$command = 'git-hooks:'.$hookName;

src/HooksPipeline.php

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

55
use Closure;
6-
use Exception;
76
use Illuminate\Contracts\Container\Container;
87
use Illuminate\Pipeline\Pipeline;
98
use Throwable;
@@ -97,10 +96,8 @@ protected function carry()
9796
: $pipe(...$parameters);
9897

9998
return $this->handleCarry($carry);
100-
} catch (Exception $e) {
101-
$this->handleException($passable, $e);
10299
} catch (Throwable $e) {
103-
$this->handleException($passable, $e);
100+
return $this->handleException($passable, $e);
104101
}
105102
};
106103
};

src/Traits/WithCommitLog.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\Traits;
4+
5+
use Igorsgm\GitHooks\Exceptions\HookFailException;
6+
use Igorsgm\GitHooks\Git\GitHelper;
7+
use Igorsgm\GitHooks\Git\Log;
8+
9+
trait WithCommitLog
10+
{
11+
use WithPipeline;
12+
13+
/**
14+
* Execute the console command.
15+
*
16+
* @return int|void
17+
*/
18+
public function handle()
19+
{
20+
try {
21+
$this->sendLogCommitThroughHooks(
22+
new Log(
23+
GitHelper::getLastCommitFromLog()
24+
)
25+
);
26+
} catch (HookFailException $e) {
27+
return 1;
28+
}
29+
}
30+
31+
/**
32+
* Send the log commit through the pipes
33+
*
34+
* @param Log $log
35+
*/
36+
protected function sendLogCommitThroughHooks(Log $log): void
37+
{
38+
$this->makePipeline()
39+
->send($log)
40+
->thenReturn();
41+
}
42+
}

src/Traits/WithPipeline.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ protected function makePipeline(): Pipeline
2121

2222
return $pipeline
2323
->through($this->getRegisteredHooks())
24-
->withCallback($this->showInfoAboutHook());
25-
// ->withExceptionCallback($this->showHookErrorAndExit());
24+
->withCallback($this->showInfoAboutHook())
25+
->withExceptionCallback($this->showHookErrorAndExit());
2626
}
2727

2828
/**
@@ -61,11 +61,7 @@ public function getRegisteredHooks(): array
6161
$hooks = collect((array) config('git-hooks.'.$this->getHook()));
6262

6363
return $hooks->map(function ($hook, $i) {
64-
if (is_int($i)) {
65-
return $hook;
66-
}
67-
68-
return $i;
64+
return is_int($i) ? $hook : $i;
6965
})->all();
7066
}
7167
}

0 commit comments

Comments
 (0)