1212
1313namespace PhpMerge ;
1414
15- use SebastianBergmann \Diff \Output \UnifiedDiffOutputBuilder ;
16- use Symplify \GitWrapper \GitWrapper ;
17- use Symplify \GitWrapper \Exception \GitException ;
15+ use Gitonomy \Git \Repository ;
16+ use PhpMerge \internal \AbstractMergeBase ;
17+ use PhpMerge \internal \Git \Gitonomy ;
18+ use PhpMerge \internal \Git \GitAbstractionInterface ;
19+ use PhpMerge \internal \Git \GitException ;
20+ use PhpMerge \internal \Git \Symplify ;
1821use PhpMerge \internal \Line ;
1922use PhpMerge \internal \Hunk ;
20- use PhpMerge \ internal \ AbstractMergeBase ;
23+ use Symplify \ GitWrapper \ GitWrapper ;
2124use SebastianBergmann \Diff \Differ ;
25+ use SebastianBergmann \Diff \Output \UnifiedDiffOutputBuilder ;
2226
2327/**
2428 * Class GitMerge merges three strings with git as the backend.
@@ -35,16 +39,9 @@ final class GitMerge extends AbstractMergeBase implements PhpMergeInterface
3539 /**
3640 * The git working directory.
3741 *
38- * @var \Symplify\GitWrapper\GitWorkingCopy|null
42+ * @var GitAbstractionInterface
3943 */
40- protected $ git ;
41-
42- /**
43- * The git wrapper to use for merging.
44- *
45- * @var \Symplify\GitWrapper\GitWrapper
46- */
47- protected $ wrapper ;
44+ protected GitAbstractionInterface $ git ;
4845
4946 /**
5047 * The temporary directory in which git can work.
@@ -61,17 +58,29 @@ final class GitMerge extends AbstractMergeBase implements PhpMergeInterface
6158 /**
6259 * Constructor, not setting anything up.
6360 *
64- * @param \Symplify\GitWrapper\GitWrapper|null $wrapper
61+ * @param GitAbstractionInterface| \Symplify\GitWrapper\GitWrapper|null $wrapper
6562 */
66- public function __construct (? GitWrapper $ wrapper = null )
63+ public function __construct ($ wrapper = null )
6764 {
68- if (!$ wrapper ) {
69- $ wrapper = new GitWrapper ('git ' );
70- }
71- $ this ->wrapper = $ wrapper ;
7265 $ this ->conflict = '' ;
73- $ this ->git = null ;
7466 $ this ->dir = null ;
67+
68+ // Set up the abstraction layer.
69+ if ($ wrapper instanceof GitAbstractionInterface) {
70+ // This allows the user to choose.
71+ $ this ->git = $ wrapper ;
72+ } elseif ($ wrapper instanceof GitWrapper) {
73+ // This is the backwards compatibility with an argument.
74+ $ this ->git = new Symplify ($ wrapper );
75+ } elseif (class_exists (Repository::class)) {
76+ // We use this if no wrapper is passed and the newer library exists.
77+ $ this ->git = new Gitonomy ();
78+ } elseif (class_exists (GitWrapper::class)) {
79+ // This is the backwards compatibility when only the deprecated class exists.
80+ $ this ->git = new Symplify (new GitWrapper ('git ' ));
81+ } else {
82+ throw new \InvalidArgumentException ('No git wrapper library found. use composer to install gitonomy/gitlib or symplify/git-wrapper ' );
83+ }
7584 }
7685
7786 /**
@@ -135,7 +144,7 @@ protected function mergeFile(string $file, string $base, string $remote, string
135144 $ this ->git ->add ($ file );
136145 $ this ->git ->commit ('Add base. ' );
137146
138- if (!in_array ( ' original ' , $ this ->git ->getBranches ()-> all () )) {
147+ if (!$ this ->git ->branchExists ( ' original ' )) {
139148 $ this ->git ->checkoutNewBranch ('original ' );
140149 } else {
141150 $ this ->git ->checkout ('original ' );
@@ -356,19 +365,14 @@ protected static function fixLastLine(array $lines, array $all): array
356365 protected function setup ()
357366 {
358367 if (!$ this ->dir ) {
359- // Greate a temporary directory.
368+ // Create a temporary directory.
360369 $ tempfile = tempnam (sys_get_temp_dir (), '' );
361370 mkdir ($ tempfile .'.git ' );
362371 if (file_exists ($ tempfile )) {
363372 unlink ($ tempfile );
364373 }
365374 $ this ->dir = $ tempfile .'.git ' ;
366- $ this ->git = $ this ->wrapper ->init ($ this ->dir );
367- }
368- if ($ this ->git ) {
369- $ this ->git ->config ('user.name ' , 'GitMerge ' );
370- $ this ->git ->config ('user.email ' , 'gitmerge@php-merge.example.com ' );
371- $ this ->git ->config ('merge.conflictStyle ' , 'diff3 ' );
375+ $ this ->git ->init ($ this ->dir );
372376 }
373377 }
374378
0 commit comments