@@ -1076,6 +1076,8 @@ private function applyGrouping(array $lines, string $eol): string
10761076 $ nonExportIgnoreLines = $ this ->collapseAndTrimBlankLines ($ nonExportIgnoreLines );
10771077 $ exportIgnoreLines = $ this ->collapseAndTrimBlankLines ($ exportIgnoreLines );
10781078
1079+ $ nonExportIgnoreLines = $ this ->subGroupNonExportIgnoreLines ($ nonExportIgnoreLines );
1080+
10791081 if ($ nonExportIgnoreLines === [] && $ exportIgnoreLines === []) {
10801082 return '' ;
10811083 }
@@ -1091,6 +1093,52 @@ private function applyGrouping(array $lines, string $eol): string
10911093 return \implode ($ eol , $ nonExportIgnoreLines ) . $ eol . $ eol . \implode ($ eol , $ exportIgnoreLines );
10921094 }
10931095
1096+ /**
1097+ * Sub-group non-export-ignore lines: glob/wildcard patterns first,
1098+ * then specific-file attribute directives, comments before both.
1099+ *
1100+ * @param array<int, string> $lines
1101+ * @return array<int, string>
1102+ */
1103+ private function subGroupNonExportIgnoreLines (array $ lines ): array
1104+ {
1105+ $ commentLines = [];
1106+ $ globPatternLines = [];
1107+ $ specificFileLines = [];
1108+
1109+ foreach ($ lines as $ line ) {
1110+ if (\str_starts_with (\ltrim ($ line ), '# ' )) {
1111+ $ commentLines [] = $ line ;
1112+ } elseif ($ line !== '' && \str_starts_with (\ltrim ($ line ), '* ' )) {
1113+ $ globPatternLines [] = $ line ;
1114+ } elseif ($ line !== '' ) {
1115+ $ specificFileLines [] = $ line ;
1116+ }
1117+ }
1118+
1119+ $ result = [];
1120+
1121+ if ($ commentLines !== []) {
1122+ $ result = \array_merge ($ result , $ commentLines );
1123+ if ($ globPatternLines !== [] || $ specificFileLines !== []) {
1124+ $ result [] = '' ;
1125+ }
1126+ }
1127+
1128+ if ($ globPatternLines !== []) {
1129+ $ result = \array_merge ($ result , $ globPatternLines );
1130+ if ($ specificFileLines !== []) {
1131+ $ result [] = '' ;
1132+ }
1133+ }
1134+
1135+ if ($ specificFileLines !== []) {
1136+ $ result = \array_merge ($ result , $ specificFileLines );
1137+ }
1138+
1139+ return $ result ;
1140+ }
1141+
10941142 /**
10951143 * Get the present export-ignore entries of
10961144 * the .gitattributes file.
0 commit comments