Skip to content

Commit 970ce6b

Browse files
committed
Use new git-wrapper library
1 parent 37eaf64 commit 970ce6b

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/PhpMerge/GitMerge.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
namespace PhpMerge;
1212

13-
use GitWrapper\GitWrapper;
14-
use GitWrapper\GitException;
13+
use Symplify\GitWrapper\GitWrapper;
14+
use Symplify\GitWrapper\Exception\GitException;
1515
use PhpMerge\internal\Line;
1616
use PhpMerge\internal\Hunk;
1717
use PhpMerge\internal\PhpMergeBase;
@@ -40,20 +40,20 @@ final class GitMerge extends PhpMergeBase implements PhpMergeInterface
4040
/**
4141
* The git working directory.
4242
*
43-
* @var \GitWrapper\GitWorkingCopy
43+
* @var \Symplify\GitWrapper\GitWorkingCopy|null
4444
*/
4545
protected $git;
4646

4747
/**
4848
* The git wrapper to use for merging.
4949
*
50-
* @var \GitWrapper\GitWrapper
50+
* @var \Symplify\GitWrapper\GitWrapper
5151
*/
5252
protected $wrapper;
5353

5454
/**
5555
* The temporary directory in which git can work.
56-
* @var string
56+
* @var string|null
5757
*/
5858
protected $dir;
5959

@@ -154,14 +154,15 @@ protected function mergeFile(string $file, string $base, string $remote, string
154154
*/
155155
protected static function getConflicts($file, $baseText, $remoteText, $localText, &$conflicts, &$merged)
156156
{
157+
$content = file_get_contents($file);
157158
$raw = new \ArrayObject(self::splitStringByLines(file_get_contents($file)));
158159
$lineIterator = $raw->getIterator();
159160
$state = 'unchanged';
160161
$conflictIndicator = [
161-
'<<<<<<< HEAD' => 'local',
162-
'||||||| merged common ancestors' => 'base',
162+
'<<<<<<<' => 'local',
163+
'|||||||' => 'base',
163164
'=======' => 'remote',
164-
'>>>>>>> original' => 'end conflict',
165+
'>>>>>>>' => 'end conflict',
165166
];
166167

167168
// Create hunks from the text diff.
@@ -185,9 +186,10 @@ protected static function getConflicts($file, $baseText, $remoteText, $localText
185186
// Loop over all the lines in the file.
186187
while ($lineIterator->valid()) {
187188
$line = $lineIterator->current();
188-
if (array_key_exists(trim($line), $conflictIndicator)) {
189+
$gitKey = substr(trim($line), 0, 7);
190+
if (array_key_exists($gitKey, $conflictIndicator)) {
189191
// Check for a line matching a conflict indicator.
190-
$state = $conflictIndicator[trim($line)];
192+
$state = $conflictIndicator[$gitKey];
191193
$skipedLines++;
192194
if ($state == 'end conflict') {
193195
// We just treated a merge conflict.
@@ -234,9 +236,9 @@ protected static function getConflicts($file, $baseText, $remoteText, $localText
234236
}
235237
$merged[] = $line;
236238

237-
/** @var Hunk $r */
239+
/** @var Hunk|null $r */
238240
$r = $remoteIterator->current();
239-
/** @var Hunk $l */
241+
/** @var Hunk|null $l */
240242
$l = $localIterator->current();
241243

242244
if ($r == $l) {
@@ -247,7 +249,7 @@ protected static function getConflicts($file, $baseText, $remoteText, $localText
247249

248250
// A hunk has been successfully merged, so we can just
249251
// tally the lines added and removed and skip forward.
250-
if ($r && $r->getStart() == $lineNumber) {
252+
if (!is_null($r) && $r->getStart() == $lineNumber) {
251253
if (!$r->hasIntersection($l)) {
252254
$lineNumber += count($r->getRemovedLines());
253255
$newLine += count($r->getAddedLines());
@@ -263,7 +265,7 @@ protected static function getConflicts($file, $baseText, $remoteText, $localText
263265
$newLine++;
264266
}
265267
}
266-
} elseif ($l && $l->getStart() == $lineNumber) {
268+
} elseif (!is_null($l) && $l->getStart() == $lineNumber) {
267269
if (!$l->hasIntersection($r)) {
268270
$lineNumber += count($l->getRemovedLines());
269271
$newLine += count($l->getAddedLines());
@@ -329,12 +331,12 @@ protected static function fixLastLine(array $lines, array $all): array
329331
/**
330332
* Constructor, not setting anything up.
331333
*
332-
* @param \GitWrapper\GitWrapper $wrapper
334+
* @param \Symplify\GitWrapper\GitWrapper|null $wrapper
333335
*/
334336
public function __construct(GitWrapper $wrapper = null)
335337
{
336338
if (!$wrapper) {
337-
$wrapper = new GitWrapper();
339+
$wrapper = new GitWrapper('git');
338340
}
339341
$this->wrapper = $wrapper;
340342
$this->conflict = '';
@@ -369,7 +371,7 @@ protected function setup()
369371
*/
370372
protected function cleanup()
371373
{
372-
if (is_dir($this->dir)) {
374+
if (isset($this->dir) && is_dir($this->dir)) {
373375
// Recursively delete all files and folders.
374376
$files = new \RecursiveIteratorIterator(
375377
new \RecursiveDirectoryIterator($this->dir, \RecursiveDirectoryIterator::SKIP_DOTS),

0 commit comments

Comments
 (0)