99use Stolt \LeanPackage \Exceptions \NonExistentGlobPatternFile ;
1010use Stolt \LeanPackage \Exceptions \PresetNotAvailable ;
1111use Stolt \LeanPackage \Gitattributes \ValueObject as GitattributesValueObject ;
12+ use Stolt \LeanPackage \Gitattributes \FileRepository as GitattributesFileRepository ;
1213use Stolt \LeanPackage \Glob ;
1314use Stolt \LeanPackage \Helpers \Str ;
1415use Stolt \LeanPackage \Presets \Finder ;
@@ -150,14 +151,15 @@ abstract class AbstractExportIgnoreAnalyser
150151
151152 public bool $ groupNonExportIgnores = false ;
152153
153- private Finder $ finder ;
154-
155154 /**
156155 * Initialize.
157156 */
158- public function __construct (Finder $ finder , string $ directory = '' , ?ExportIgnoreConfiguration $ configuration = null )
157+ public function __construct (
158+ protected readonly Finder $ finder ,
159+ protected readonly GitattributesFileRepository $ gitattributesFileRepository ,
160+ string $ directory = '' ,
161+ ?ExportIgnoreConfiguration $ configuration = null )
159162 {
160- $ this ->finder = $ finder ;
161163 $ this ->defaultGlobPattern = $ finder ->getDefaultPreset ();
162164
163165 $ configuration ??= new ExportIgnoreConfiguration (
@@ -168,6 +170,11 @@ public function __construct(Finder $finder, string $directory = '', ?ExportIgnor
168170 $ this ->configuration = $ configuration ;
169171
170172 $ this ->directory = $ configuration ->directory ;
173+
174+ if (!is_dir ($ this ->directory ) && defined ('WORKING_DIRECTORY ' )) {
175+ $ this ->directory = WORKING_DIRECTORY ;
176+ }
177+
171178 $ this ->gitattributesFile = $ this ->directory . DIRECTORY_SEPARATOR . '.gitattributes ' ;
172179
173180 $ this ->globPattern = $ configuration ->globPattern ;
@@ -200,7 +207,6 @@ public function getDirectory(): string
200207 *
201208 * @param string $directory The directory to analyse.
202209 * @return AbstractExportIgnoreAnalyser
203- * @return Analyser
204210 *
205211 * @throws RuntimeException
206212 */
@@ -210,11 +216,14 @@ public function setDirectory(string $directory = __DIR__): AbstractExportIgnoreA
210216 $ message = "Directory {$ directory } doesn't exist. " ;
211217 throw new \RuntimeException ($ message );
212218 }
219+
213220 $ this ->directory = $ directory ;
214221 $ this ->gitattributesFile = $ directory
215222 . DIRECTORY_SEPARATOR
216223 . '.gitattributes ' ;
217224
225+ $ this ->gitattributesFileRepository ->setWorkingDirectory ($ directory );
226+
218227 return $ this ;
219228 }
220229
@@ -507,7 +516,7 @@ public function getPresentGitAttributesContent(): string
507516 return '' ;
508517 }
509518
510- return ( string ) \file_get_contents ( $ this ->gitattributesFile );
519+ return $ this ->gitattributesFileRepository -> getGitattributesContent ( );
511520 }
512521
513522 /**
@@ -520,6 +529,11 @@ public function getFinder(): Finder
520529 return $ this ->finder ;
521530 }
522531
532+ public function getGitattributesFileRepository (): GitattributesFileRepository
533+ {
534+ return $ this ->gitattributesFileRepository ;
535+ }
536+
523537 /**
524538 * Set the glob pattern file.
525539 *
@@ -567,11 +581,10 @@ public function setGlobPatternFromFile(string $file): AbstractExportIgnoreAnalys
567581 * @param string $pattern The glob pattern to use to detect expected export-ignores files.
568582 *
569583 * @return AbstractExportIgnoreAnalyser
570- * @return Analyser
571584 *
572585 * @throws InvalidGlobPattern
573586 */
574- public function setGlobPattern ($ pattern ): AbstractExportIgnoreAnalyser
587+ public function setGlobPattern (string $ pattern ): AbstractExportIgnoreAnalyser
575588 {
576589 $ this ->globPattern = \trim ($ pattern );
577590 $ this ->guardGlobPattern ($ this ->globPattern );
@@ -684,6 +697,61 @@ protected function patternHasMatch(string $globPattern): bool
684697 return \is_array ($ matches ) && \count ($ matches ) > 0 ;
685698 }
686699
700+ /**
701+ * Return export ignores in a .gitattributes file to preserve.
702+ *
703+ * @param array $globPatternMatchingExportIgnores Export ignores matching glob pattern.
704+ *
705+ * @return array
706+ */
707+ public function getPresentExportIgnoresToPreserve (array $ globPatternMatchingExportIgnores ): array
708+ {
709+ $ gitattributesContent = $ this ->gitattributesFileRepository ->getGitattributesContent ();
710+
711+ if (\preg_match ("/(\*\h*)(text\h*)(=\h*auto)/ " , $ gitattributesContent )) {
712+ $ this ->textAutoconfiguration ();
713+ }
714+
715+ $ gitattributesLines = \preg_split (
716+ '/ \\r \\n| \\r| \\n/ ' ,
717+ $ gitattributesContent
718+ );
719+
720+ $ basenamedGlobPatternMatchingExportIgnores = \array_map (
721+ 'basename ' ,
722+ $ globPatternMatchingExportIgnores
723+ );
724+
725+ $ exportIgnoresToPreserve = [];
726+
727+ \array_filter ($ gitattributesLines , function ($ line ) use (
728+ &$ exportIgnoresToPreserve ,
729+ &$ globPatternMatchingExportIgnores ,
730+ &$ basenamedGlobPatternMatchingExportIgnores
731+ ) {
732+ if (\strstr ($ line , 'export-ignore ' ) && !\str_contains ($ line , '-export-ignore ' ) && \strpos ($ line , '# ' ) === false ) {
733+ list ($ pattern , $ void ) = \explode ('export-ignore ' , $ line );
734+ if (\substr ($ pattern , 0 , 1 ) === '/ ' ) {
735+ $ pattern = \substr ($ pattern , 1 );
736+ $ this ->hasPrecedingSlashesInExportIgnorePattern = true ;
737+ }
738+ $ patternMatches = $ this ->patternHasMatch ($ pattern );
739+ $ pattern = \trim ($ pattern );
740+
741+ if ($ patternMatches
742+ && !\in_array ($ pattern , $ globPatternMatchingExportIgnores , strict: true )
743+ && !\in_array ($ pattern , $ basenamedGlobPatternMatchingExportIgnores , strict: true )
744+ ) {
745+ if (\file_exists ($ this ->directory . DIRECTORY_SEPARATOR . $ pattern )) {
746+ return $ exportIgnoresToPreserve [] = \trim ($ pattern );
747+ }
748+ }
749+ }
750+ });
751+
752+ return $ exportIgnoresToPreserve ;
753+ }
754+
687755 protected function mergeWithExistingGitattributes (string $ content ): string
688756 {
689757 $ exportIgnoreContent = \rtrim ($ content );
@@ -717,7 +785,7 @@ public function getPresentNonExportIgnoresContent(): string
717785 return '' ;
718786 }
719787
720- $ gitattributesContent = ( string ) \file_get_contents ( $ this ->gitattributesFile );
788+ $ gitattributesContent = $ this ->gitattributesFileRepository -> getGitattributesContent ( );
721789 $ eol = Str::detectEol ($ gitattributesContent );
722790 $ this ->preferredEol = $ eol ;
723791
@@ -786,9 +854,9 @@ protected function getAlignedExportIgnoreArtifacts(array $artifacts): array
786854 return \array_map (static function (string $ artifact ) use (&$ longestArtifact ) {
787855 if (\strlen ($ artifact ) < $ longestArtifact ) {
788856 return $ artifact . \str_repeat (
789- ' ' ,
790- $ longestArtifact - \strlen ($ artifact )
791- );
857+ ' ' ,
858+ $ longestArtifact - \strlen ($ artifact )
859+ );
792860 }
793861 return $ artifact ;
794862 }, $ artifacts );
0 commit comments