1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** Created by PhpStorm, User: jonphipps, Date: 2018-02-15, Time: 4:19 PM */ |
4
|
|
|
|
5
|
|
|
namespace App\Services\Publish; |
6
|
|
|
|
7
|
|
|
use App\Jobs\GenerateRdf; |
8
|
|
|
use App\Models\Project; |
9
|
|
|
use GitWrapper\GitException; |
10
|
|
|
use GitWrapper\GitWrapper; |
11
|
|
|
use Illuminate\Support\Facades\Storage; |
12
|
|
|
|
13
|
|
|
class Git |
14
|
|
|
{ |
15
|
|
|
public static function getProjectPath(int $projectId): string |
16
|
|
|
{ |
17
|
|
|
return GenerateRdf::PROJECT_ROOT . "/{$projectId}/"; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public static function getProjectRepo(Project $project): ?string |
21
|
|
|
{ |
22
|
|
|
$repo = $project->repo; |
23
|
|
|
|
24
|
|
|
return $repo ? "https://github.com/{$repo}.git" : null; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \App\Models\Project $project |
29
|
|
|
* @param string $disk |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
* @throws \GitWrapper\GitException |
33
|
|
|
*/ |
34
|
|
|
public static function initDir(Project $project, $disk = GenerateRdf::REPO_ROOT): void |
35
|
|
|
{ |
36
|
|
|
$projectPath = self::getProjectPath($project->id); |
37
|
|
|
if (! Storage::disk($disk)->exists($projectPath)) { |
38
|
|
|
$dir = Storage::disk($disk)->path($projectPath); |
39
|
|
|
|
40
|
|
|
/** @var GitWrapper $wrapper */ |
41
|
|
|
$wrapper = static::getWrapper(); |
42
|
|
|
$repo = self::getProjectRepo($project); |
43
|
|
|
if ($repo) { |
|
|
|
|
44
|
|
|
$git = $wrapper->cloneRepository($repo, $dir); |
45
|
|
|
if (! $git->isCloned()) { |
46
|
|
|
throw new GitException($git); |
47
|
|
|
} |
48
|
|
|
} else { |
49
|
|
|
Storage::disk($disk)->createDir($projectPath); |
|
|
|
|
50
|
|
|
$wrapper->init($dir); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param \App\Models\Project $project |
57
|
|
|
* @param string $disk |
58
|
|
|
* @param $message |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
* @throws \GitWrapper\GitException |
62
|
|
|
*/ |
63
|
|
|
public static function commitDir(Project $project, $disk = GenerateRdf::REPO_ROOT, $message): void |
64
|
|
|
{ |
65
|
|
|
$projectPath = self::getProjectPath($project->id); |
66
|
|
|
$dir = Storage::disk($disk)->path($projectPath); |
67
|
|
|
|
68
|
|
|
/** @var GitWrapper $wrapper */ |
69
|
|
|
$wrapper = static::getWrapper(); |
70
|
|
|
$git = $wrapper->workingCopy($dir); |
71
|
|
|
|
72
|
|
|
if ($git->hasChanges()) { |
73
|
|
|
$git->add('.'); |
74
|
|
|
$git->commit($message); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return \GitWrapper\GitWrapper |
80
|
|
|
* @throws \GitWrapper\GitException |
81
|
|
|
*/ |
82
|
|
|
private static function getWrapper(): GitWrapper |
83
|
|
|
{ |
84
|
|
|
return new GitWrapper('/usr/bin/git'); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: