-
Notifications
You must be signed in to change notification settings - Fork 1
/
GitDiffer.php
241 lines (196 loc) · 8.13 KB
/
GitDiffer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
declare(strict_types=1);
/*
* This file is part of the Composer package "cpsit/migrator".
*
* Copyright (C) 2023 Elias Häußler <e.haeussler@familie-redlich.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace CPSIT\Migrator\Diff\Differ;
use CPSIT\Migrator\Diff;
use CPSIT\Migrator\Exception;
use CPSIT\Migrator\Helper;
use CPSIT\Migrator\Resource;
use GitElephant\Objects;
use GitElephant\Repository;
use GitElephant\Status;
use Symfony\Component\Filesystem;
use function end;
use function explode;
/**
* GitDiffer.
*
* @author Elias Häußler <e.haeussler@familie-redlich.de>
* @license GPL-3.0-or-later
*/
final class GitDiffer implements Differ
{
private const MAIN_BRANCH = 'main';
private const BASE_BRANCH = 'base';
private const DIFF_MODES = [
Status\StatusFile::ADDED => Diff\DiffMode::Added,
Status\StatusFile::COPIED => Diff\DiffMode::Copied,
Status\StatusFile::DELETED => Diff\DiffMode::Deleted,
Status\StatusFile::IGNORED => Diff\DiffMode::Ignored,
Status\StatusFile::MODIFIED => Diff\DiffMode::Modified,
Status\StatusFile::RENAMED => Diff\DiffMode::Renamed,
Status\StatusFile::UNTRACKED => Diff\DiffMode::Untracked,
Status\StatusFile::UPDATED_BUT_UNMERGED => Diff\DiffMode::Conflicted,
];
private readonly Filesystem\Filesystem $filesystem;
private readonly Repository $repository;
public function __construct()
{
$this->filesystem = new Filesystem\Filesystem();
$this->repository = $this->initializeRepository();
}
public function generateDiff(
Resource\Collector\CollectorInterface $source,
Resource\Collector\CollectorInterface $target,
Resource\Collector\DirectoryCollector $base,
): Diff\DiffResult {
// Generate Git tree
$this->commitSourceFiles($source);
$this->commitBaseFiles($base);
$this->commitTargetFiles($target);
// Perform three-way merge
$outcome = $this->merge();
// Calculate diff objects from current status
$status = $this->repository->stage()->getStatus();
$diffObjects = $this->calculateDiffObjects($status);
// Generate applicable patch
$patch = $this->repository->getCaller()->execute('diff --cached')->getOutput();
return new Diff\DiffResult($diffObjects, $patch, $outcome);
}
public function applyDiff(
Diff\DiffResult $diffResult,
Resource\Collector\DirectoryCollector $base,
): bool {
if (!$diffResult->getOutcome()->isSuccessful()) {
throw Exception\PatchFailureException::forConflictedDiff($diffResult);
}
$repo = new Resource\Collector\DirectoryCollector($this->repository->getPath());
$this->filesystem->remove($base->collectFiles());
$this->filesystem->mirror($this->repository->getPath(), $base->getBaseDirectory(), $repo->collectFiles());
return true;
}
private function commitSourceFiles(Resource\Collector\CollectorInterface $source): void
{
foreach ($source as $path => $contents) {
$this->filesystem->dumpFile(
Filesystem\Path::join($this->repository->getPath(), $path),
$contents,
);
}
$this->repository->commit('Add source files', true, allowEmpty: true);
}
private function commitBaseFiles(Resource\Collector\DirectoryCollector $base): void
{
$repo = new Resource\Collector\DirectoryCollector($this->repository->getPath());
// Go to base branch
$this->repository->checkout(self::BASE_BRANCH, true);
// Add base files
$this->filesystem->remove($repo->collectFiles());
$this->filesystem->mirror($base->getBaseDirectory(), $this->repository->getPath(), $base->collectFiles());
// Commit base files
$this->repository->commit('Add base files', true, allowEmpty: true);
// Go back to main branch
$this->repository->checkout(self::MAIN_BRANCH);
}
private function commitTargetFiles(Resource\Collector\CollectorInterface $target): void
{
$repo = new Resource\Collector\DirectoryCollector($this->repository->getPath());
// Clean up repository
$this->filesystem->remove($repo->collectFiles());
// Add target files
foreach ($target as $path => $contents) {
$this->filesystem->dumpFile(
Filesystem\Path::join($this->repository->getPath(), $path),
$contents,
);
}
// Commit target files
$this->repository->commit('Add target files', true, allowEmpty: true);
}
private function merge(): Diff\Outcome
{
$mainBranch = $this->repository->getBranch(self::MAIN_BRANCH) ?? $this->repository->getMainBranch();
try {
$this->repository->checkout(self::BASE_BRANCH);
$this->repository->merge($mainBranch, 'Migrate base files', 'no-ff');
// Enforce working directory to be dirty (we need this to correctly
// determine the current Git status in the DiffResult class)
$this->repository->reset('HEAD~1', []);
$outcome = Diff\Outcome::successful();
} catch (\Exception $exception) {
$errorMessage = explode('with reason: ', $exception->getMessage());
$outcome = Diff\Outcome::failed(end($errorMessage));
}
return $outcome;
}
/**
* @return list<Diff\DiffObject>
*/
private function calculateDiffObjects(Status\Status $status): array
{
$diff = $this->repository->commit('Add changed files', allowEmpty: true)->getDiff('HEAD', 'HEAD~1');
$diffObjects = [];
/** @var Status\StatusFile $statusFile */
foreach ($status->all() as $statusFile) {
$diffMode = self::DIFF_MODES[$statusFile->getWorkingTreeStatus()] ?? self::DIFF_MODES[$statusFile->getIndexStatus()] ?? null;
$destinationPath = $statusFile->getRenamed() ?? $statusFile->getName();
// Skip irrelevant statuses
if (null === $diffMode) {
continue;
}
/** @var Objects\Diff\DiffObject $diffObject */
foreach ($diff as $diffObject) {
if ($destinationPath === $diffObject->getDestinationPath()) {
/** @var list<Objects\Diff\DiffChunk> $chunks */
$chunks = $diffObject->getChunks();
$diffObjects[] = new Diff\DiffObject(
$diffMode,
$diffObject->getOriginalPath(),
$diffObject->getDestinationPath(),
$chunks,
);
}
}
}
return $diffObjects;
}
private function initializeRepository(): Repository
{
$repoDir = Helper\FilesystemHelper::getNewTemporaryDirectory();
// Create temporary directory for Git repository
$this->filesystem->mkdir($repoDir);
// Create repository
$repository = new Repository($repoDir);
// Initialize repository
$repository->addGlobalConfig('commit.gpgsign', false);
$repository->addGlobalConfig('user.name', 'CPS Migrator');
$repository->addGlobalConfig('user.email', '');
$repository->init(false, self::MAIN_BRANCH);
return $repository;
}
public function __destruct()
{
try {
$this->filesystem->remove($this->repository->getPath());
} catch (Filesystem\Exception\IOException) {
// Ignore failures
}
}
}