|
10 | 10 |
|
11 | 11 | class Analyser |
12 | 12 | { |
| 13 | + const EXPORT_IGNORE_CLASSIC = 'classic'; |
| 14 | + const EXPORT_IGNORE_NEGATED = 'negated'; |
| 15 | + |
13 | 16 | const EXPORT_IGNORES_PLACEMENT_PLACEHOLDER = '{{ export_ignores_placement }}'; |
14 | 17 | /** |
15 | 18 | * The directory to analyse |
@@ -575,18 +578,83 @@ public function getGitignoredPatterns(): array |
575 | 578 | return $this->getGitignorePatterns($gitignoreFile); |
576 | 579 | } |
577 | 580 |
|
| 581 | + private function getNegatedGitattributesContent(): string |
| 582 | + { |
| 583 | + $postfixLessExportIgnores = $this->collectExpectedNegatedExportIgnores(); |
| 584 | + |
| 585 | + \sort($postfixLessExportIgnores, SORT_STRING | SORT_FLAG_CASE); |
| 586 | + |
| 587 | + $postfixLessExportIgnores = \array_map(function (string $fileToNegate): string { |
| 588 | + if (\is_dir($this->directory . DIRECTORY_SEPARATOR . $fileToNegate)) { |
| 589 | + $fileToNegate .= DIRECTORY_SEPARATOR; |
| 590 | + } |
| 591 | + |
| 592 | + return $fileToNegate; |
| 593 | + }, $postfixLessExportIgnores); |
| 594 | + |
| 595 | + if (\count($postfixLessExportIgnores) > 0) { |
| 596 | + if ($this->sortFromDirectoriesToFiles === false && ($this->isAlignExportIgnoresEnabled() || $this->isStrictAlignmentComparisonEnabled())) { |
| 597 | + $postfixLessExportIgnores = $this->getAlignedExportIgnoreArtifacts( |
| 598 | + $postfixLessExportIgnores |
| 599 | + ); |
| 600 | + } |
| 601 | + |
| 602 | + if ($this->sortFromDirectoriesToFiles) { |
| 603 | + $postfixLessExportIgnores = $this->getByDirectoriesToFilesExportIgnoreArtifacts( |
| 604 | + $postfixLessExportIgnores |
| 605 | + ); |
| 606 | + } |
| 607 | + |
| 608 | + $content = "* export-ignore" . PHP_EOL . PHP_EOL |
| 609 | + . \implode(" -export-ignore" . $this->preferredEol, $postfixLessExportIgnores) |
| 610 | + . " -export-ignore" . $this->preferredEol; |
| 611 | + |
| 612 | + if ($this->hasGitattributesFile()) { |
| 613 | + $exportIgnoreContent = \rtrim($content); |
| 614 | + $content = $this->getPresentNonExportIgnoresContent(); |
| 615 | + |
| 616 | + if (\strstr($content, self::EXPORT_IGNORES_PLACEMENT_PLACEHOLDER)) { |
| 617 | + $content = \str_replace( |
| 618 | + self::EXPORT_IGNORES_PLACEMENT_PLACEHOLDER, |
| 619 | + $exportIgnoreContent, |
| 620 | + $content |
| 621 | + ); |
| 622 | + } else { |
| 623 | + $content = $content |
| 624 | + . \str_repeat($this->preferredEol, 2) |
| 625 | + . $exportIgnoreContent; |
| 626 | + } |
| 627 | + } else { |
| 628 | + $content = "* text=auto eol=lf" . \str_repeat($this->preferredEol, 2) . $content; |
| 629 | + } |
| 630 | + |
| 631 | + return $content; |
| 632 | + } |
| 633 | + |
| 634 | + return ''; |
| 635 | + } |
| 636 | + |
578 | 637 | /** |
579 | 638 | * Return the expected .gitattributes content. |
580 | 639 | * |
581 | 640 | * @param array $postfixLessExportIgnores Expected patterns without an export-ignore postfix. |
| 641 | + * @param string $flavour The flavour of the .gitattributes file content. Possible values are classic and negated. |
582 | 642 | * @return string |
583 | 643 | */ |
584 | | - public function getExpectedGitattributesContent(array $postfixLessExportIgnores = []): string |
| 644 | + public function getExpectedGitattributesContent(array $postfixLessExportIgnores = [], string $flavour = self::EXPORT_IGNORE_CLASSIC): string |
585 | 645 | { |
586 | | - if ($postfixLessExportIgnores === []) { |
| 646 | + if ($flavour !== self::EXPORT_IGNORE_CLASSIC && $flavour !== self::EXPORT_IGNORE_NEGATED) { |
| 647 | + throw new \InvalidArgumentException("Invalid flavour provided. Expected 'classic' or 'negated'."); |
| 648 | + } |
| 649 | + |
| 650 | + if ($postfixLessExportIgnores === [] && $flavour === self::EXPORT_IGNORE_CLASSIC) { |
587 | 651 | $postfixLessExportIgnores = $this->collectExpectedExportIgnores(); |
588 | 652 | } |
589 | 653 |
|
| 654 | + if ($flavour === self::EXPORT_IGNORE_NEGATED) { |
| 655 | + return $this->getNegatedGitattributesContent(); |
| 656 | + } |
| 657 | + |
590 | 658 | if (!$this->hasGitattributesFile() && \count($postfixLessExportIgnores) > 0) { |
591 | 659 | $postfixLessExportIgnores[] = '.gitattributes'; |
592 | 660 | } |
@@ -691,6 +759,41 @@ public function getPresentExportIgnoresToPreserve(array $globPatternMatchingExpo |
691 | 759 | return $exportIgnoresToPreserve; |
692 | 760 | } |
693 | 761 |
|
| 762 | + public function collectExpectedNegatedExportIgnores(): array |
| 763 | + { |
| 764 | + $expectedNegatedExportIgnores = []; |
| 765 | + |
| 766 | + \chdir($this->directory); |
| 767 | + |
| 768 | + $globMatches = Glob::glob($this->globPattern, Glob::GLOB_BRACE); |
| 769 | + |
| 770 | + if (!\is_array($globMatches)) { |
| 771 | + return $expectedNegatedExportIgnores; |
| 772 | + } |
| 773 | + |
| 774 | + $globMatches = \array_values( |
| 775 | + \array_filter($globMatches, function (string $fileToIgnore): bool { |
| 776 | + if ($this->isKeepLicenseEnabled() && \preg_match('/(License.*)/i', $fileToIgnore)) { |
| 777 | + return false; |
| 778 | + } |
| 779 | + |
| 780 | + if ($this->isKeepReadmeEnabled() && \preg_match('/(Readme.*)/i', $fileToIgnore)) { |
| 781 | + return false; |
| 782 | + } |
| 783 | + |
| 784 | + return true; |
| 785 | + }) |
| 786 | + ); |
| 787 | + |
| 788 | + $allFiles = Glob::glob('{*}', Glob::GLOB_BRACE); |
| 789 | + |
| 790 | + if (!\is_array($allFiles) || count ($allFiles) === 0) { |
| 791 | + return $expectedNegatedExportIgnores; |
| 792 | + } |
| 793 | + |
| 794 | + return array_diff($allFiles, $globMatches); |
| 795 | + } |
| 796 | + |
694 | 797 | /** |
695 | 798 | * Collect the expected export-ignored files. |
696 | 799 | * |
@@ -773,7 +876,7 @@ public function collectExpectedExportIgnores(): array |
773 | 876 | } |
774 | 877 |
|
775 | 878 | /** |
776 | | - * Detect most frequently used end of line sequence. |
| 879 | + * Detect the most frequently used end-of-line sequence. |
777 | 880 | * |
778 | 881 | * @param string $content The content to detect the eol in. |
779 | 882 | * |
@@ -930,7 +1033,7 @@ public function getReformattedGitattributesContent(): string |
930 | 1033 | } |
931 | 1034 |
|
932 | 1035 | /** |
933 | | - * Get the present non export-ignore entries of |
| 1036 | + * Get the present non-export-ignore entries of |
934 | 1037 | * the .gitattributes file. |
935 | 1038 | * |
936 | 1039 | * @return string |
|
0 commit comments