1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Churn\Assessors\GitCommitCount; |
4
|
|
|
|
5
|
|
|
use Churn\Services\CommandService; |
6
|
|
|
use Churn\Exceptions\CommandServiceException; |
7
|
|
|
|
8
|
|
|
class GitCommitCountAssessor |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The command service. |
12
|
|
|
* @var CommandService |
13
|
|
|
*/ |
14
|
|
|
protected $commandService; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class constructor. |
18
|
|
|
* @param CommandService $commandService Command Service. |
19
|
|
|
*/ |
20
|
|
|
public function __construct(CommandService $commandService) |
21
|
|
|
{ |
22
|
|
|
$this->commandService = $commandService; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* See how many commits the file at $filePath has. |
27
|
|
|
* @param string $filePath Path and filename. |
28
|
|
|
* @throws CommandServiceException If command resulted in an error. |
29
|
|
|
* @return integer |
30
|
|
|
*/ |
31
|
|
|
public function assess($filePath): int |
32
|
|
|
{ |
33
|
|
|
$command = $this->buildCommand($filePath); |
34
|
|
|
$result = $this->commandService->execute($command); |
35
|
|
|
if (! isset($result[0])) { |
36
|
|
|
throw new CommandServiceException('Command resulted in an error (Does specified file exist?)'); |
37
|
|
|
} |
38
|
|
|
$result = trim($result[0]); |
39
|
|
|
$explodedResult = explode(' ', $result); |
40
|
|
|
return (integer) $explodedResult[0]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Build the command to get the number of commits for the file at $filePath. |
45
|
|
|
* @param string $filePath Patha nd filename. |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
protected function buildCommand($filePath): string |
49
|
|
|
{ |
50
|
|
|
$commandTemplate = "git log --name-only --pretty=format: %s | sort | uniq -c | sort -nr | grep %s"; |
51
|
|
|
return sprintf($commandTemplate, $filePath, $filePath); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|