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