1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright Andrea Heigl <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Licenses under the MIT-license. For details see the included file LICENSE.md |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Org_Heigl\CaptainHook\Hooks\AddTime; |
12
|
|
|
|
13
|
|
|
use CaptainHook\App\Config; |
14
|
|
|
use CaptainHook\App\Console\IO; |
15
|
|
|
use CaptainHook\App\Hook\Action; |
16
|
|
|
use DateTimeImmutable; |
17
|
|
|
use Exception; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
use Org_Heigl\CaptainHook\Hooks\AddTime\CommitMessage\CommitMessageHandler; |
20
|
|
|
use Org_Heigl\CaptainHook\Hooks\AddTime\Formatter\TimeDifference; |
21
|
|
|
use Org_Heigl\CaptainHook\Hooks\AddTime\Fuzzier\TenMinutesCeiling; |
22
|
|
|
use SebastianFeldmann\Git\Repository; |
23
|
|
|
|
24
|
|
|
class AddTime implements Action |
25
|
|
|
{ |
26
|
|
|
private $formatter; |
27
|
|
|
|
28
|
|
|
public function __construct(TimeDifference $formatter) |
29
|
|
|
{ |
30
|
|
|
$this->formatter = $formatter; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Executes the action. |
35
|
|
|
* |
36
|
|
|
* @param \CaptainHook\App\Config $config |
37
|
|
|
* @param \CaptainHook\App\Console\IO $io |
38
|
|
|
* @param \SebastianFeldmann\Git\Repository $repository |
39
|
|
|
* @param \CaptainHook\App\Config\Action $action |
40
|
|
|
* @throws \Exception |
41
|
|
|
*/ |
42
|
|
|
public function execute(Config $config, IO $io, Repository $repository, Config\Action $action) |
43
|
|
|
{ |
44
|
|
|
$time = new DateTimeImmutable(); |
45
|
|
|
try { |
46
|
|
|
$lastCommitDate = $this->getLastCommitDateTime(); |
47
|
|
|
$timeDiff = $this->formatter->format( |
48
|
|
|
$time->diff($lastCommitDate) |
49
|
|
|
); |
50
|
|
|
} catch (Exception $e) { |
51
|
|
|
$io->write($e->getMessage()); |
52
|
|
|
$timeDiff = '??'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$messageHandler = new CommitMessageHandler($repository->getCommitMsg()); |
56
|
|
|
|
57
|
|
|
$message = $messageHandler->appendContent(sprintf( |
58
|
|
|
'Time: %s', |
59
|
|
|
$timeDiff |
60
|
|
|
)); |
61
|
|
|
|
62
|
|
|
$repository->setCommitMsg($message); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function getLastCommitDateTime() : DateTimeImmutable |
66
|
|
|
{ |
67
|
|
|
exec('git log -1 --format="%at"', $result); |
68
|
|
|
|
69
|
|
|
if (! is_numeric($result[0])) { |
70
|
|
|
throw new InvalidArgumentException('No last commit-Date found'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return new DateTimeImmutable('@' . $result[0]); |
74
|
|
|
} |
75
|
|
|
} |