Skip to content

Commit 8767afd

Browse files
committed
Fixes generated or modified header
1 parent 4b565f6 commit 8767afd

10 files changed

+170
-104
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
88
## [Unreleased]
99

1010

11+
## [v5.2.1] - 2025-10-17
12+
13+
### Fixed
14+
- The correct file `HEADERS` (generated|modified) are written.
15+
1116
## [v5.2.0] - 2025-09-26
1217

1318
### Added
@@ -430,7 +435,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
430435

431436
- Initial release.
432437

433-
[Unreleased]: https://github.com/raphaelstolt/lean-package-validator/compare/v5.2.0...HEAD
438+
[Unreleased]: https://github.com/raphaelstolt/lean-package-validator/compare/v5.2.1...HEAD
439+
[v5.2.1]: https://github.com/raphaelstolt/lean-package-validator/compare/v5.2.0...v5.2.1
434440
[v5.2.0]: https://github.com/raphaelstolt/lean-package-validator/compare/v5.1.0...v5.2.0
435441
[v5.1.0]: https://github.com/raphaelstolt/lean-package-validator/compare/v5.0.0...v5.1.0
436442
[v5.0.0]: https://github.com/raphaelstolt/lean-package-validator/compare/v4.7.1...v5.0.0

src/Analyser.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,7 @@ public function getExpectedGitattributesContent(array $postfixLessExportIgnores
597597
. $exportIgnoreContent;
598598
}
599599
} else {
600-
$content = "* text=auto eol=lf"
601-
. \str_repeat($this->preferredEol, 2)
602-
. $content;
600+
$content = "* text=auto eol=lf" . \str_repeat($this->preferredEol, 2) . $content;
603601
}
604602

605603
return $content;

src/Commands/ValidateCommand.php

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
288288
$verboseOutput = '+ Scanning directory ' . $directory . '.';
289289
$output->writeln($verboseOutput, OutputInterface::VERBOSITY_VERBOSE);
290290

291-
// Print deprecation notices for legacy options but do NOT change exit code.
291+
// Print deprecation notices for legacy options but do NOT change the exit code.
292292
if ($input->hasOption('create') && (bool) $input->getOption('create')) {
293293
$output->writeln('<comment>The --create option is deprecated. Please use the dedicated <info>create</info> command.</comment>');
294294
}
@@ -306,7 +306,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
306306
$validateArchive = $input->getOption('validate-git-archive');
307307
$globPattern = $input->getOption('glob-pattern');
308308
$globPatternFile = (string) $input->getOption('glob-pattern-file');
309-
$omitHeader = $input->getOption('omit-header');
309+
$omitHeader = (boolean) $input->getOption('omit-header');
310310
$showDifference = $input->getOption('diff');
311311
$reportStaleExportIgnores = $input->getOption('report-stale-export-ignores');
312312

@@ -455,24 +455,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
455455

456456
$verboseOutput = '+ Checking .gitattribute file existence in ' . $directory . '.';
457457
$output->writeln($verboseOutput, OutputInterface::VERBOSITY_VERBOSE);
458+
$outputContent = '';
458459

459460
if (!$this->analyser->hasGitattributesFile()) {
460-
$warning = 'Warning: There is no .gitattributes file present in '
461-
. $this->analyser->getDirectory() . '.';
462-
$outputContent = '<error>' . $warning . '</error>';
463-
464-
$expectedGitattributesFileContent = $this->analyser
465-
->getExpectedGitattributesContent();
461+
if ($createGitattributesFile === false) {
462+
$warning = 'Warning: There is no .gitattributes file present in '
463+
. $this->analyser->getDirectory() . '.';
464+
$outputContent.= '<error>' . $warning . '</error>';
465+
}
466466

467467
$verboseOutput = '+ Getting expected .gitattribute file content.';
468468
$output->writeln($verboseOutput, OutputInterface::VERBOSITY_VERBOSE);
469469

470+
$expectedGitattributesFileContent = $this->analyser
471+
->getExpectedGitattributesContent();
472+
470473
if ($expectedGitattributesFileContent !== '') {
474+
471475
if ($createGitattributesFile || $overwriteGitattributesFile) {
472476
try {
473477
$outputContent .= $this->gitattributesFileRepository->createGitattributesFile(
474478
$expectedGitattributesFileContent,
475-
$omitHeader === false
479+
$omitHeader === true ? false : true
476480
);
477481

478482
$output->writeln($outputContent);
@@ -562,17 +566,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
562566
}
563567
$output->writeln($verboseOutput, OutputInterface::VERBOSITY_VERBOSE);
564568

565-
if ($omitHeader === false) {
566-
if (\str_contains($expectedGitattributesFileContent, GitattributesFileRepository::GENERATED_HEADER)) {
567-
$expectedGitattributesFileContent = \str_replace(
568-
GitattributesFileRepository::GENERATED_HEADER . PHP_EOL . PHP_EOL,
569-
'',
570-
$expectedGitattributesFileContent
571-
);
572-
}
573-
$expectedGitattributesFileContent = GitattributesFileRepository::MODIFIED_HEADER . PHP_EOL . PHP_EOL . $expectedGitattributesFileContent;
574-
}
575-
576569
$outputContent .= $this->gitattributesFileRepository->overwriteGitattributesFile(
577570
$expectedGitattributesFileContent
578571
);

src/GitattributesFileRepository.php

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,9 @@ public function __construct(Analyser $analyser)
2929
*/
3030
public function createGitattributesFile(string $content, bool $withHeader = true): string
3131
{
32-
// Ensure the "generated by" header is present when requested.
32+
// Ensure the generated or modified by header is present when requested
3333
if ($withHeader) {
34-
$headerPrefix = self::GENERATED_HEADER . PHP_EOL . PHP_EOL;
35-
36-
if (!\str_starts_with($content, self::GENERATED_HEADER . PHP_EOL)
37-
&& !\str_starts_with($content, $headerPrefix)
38-
) {
39-
$content = $headerPrefix . $content;
40-
}
34+
$content = $this->applyOverwriteHeaderPolicy($content);
4135
}
4236

4337
$bytesWritten = file_put_contents(
@@ -95,27 +89,15 @@ public function applyOverwriteHeaderPolicy(string $contentToWrite): string
9589
$gitattributesPath = $this->analyser->getGitattributesFilePath();
9690

9791
if (!\is_file($gitattributesPath)) {
98-
return $contentToWrite;
92+
return self::GENERATED_HEADER . PHP_EOL . PHP_EOL . $contentToWrite;
9993
}
10094

101-
$presentContent = (string) @\file_get_contents($gitattributesPath);
102-
if ($presentContent === '') {
103-
return $contentToWrite;
95+
if (\str_contains($contentToWrite, self::GENERATED_HEADER)) {
96+
return \str_replace(self::GENERATED_HEADER, self::MODIFIED_HEADER, $contentToWrite);
10497
}
10598

106-
if (\str_contains($presentContent, self::GENERATED_HEADER)) {
107-
$generatedPrefix = self::GENERATED_HEADER . PHP_EOL;
108-
$modifiedPrefix = self::MODIFIED_HEADER . PHP_EOL;
109-
110-
// If the new content starts with the "generated by" header, replace it.
111-
if (\str_starts_with($contentToWrite, $generatedPrefix)) {
112-
return $modifiedPrefix . \substr($contentToWrite, \strlen($generatedPrefix));
113-
}
114-
115-
// If no header is present at the top, prepend the "partly modified" header.
116-
if (!\str_starts_with($contentToWrite, $modifiedPrefix)) {
117-
return self::MODIFIED_HEADER . PHP_EOL . PHP_EOL . $contentToWrite;
118-
}
99+
if ((\str_contains($contentToWrite, self::MODIFIED_HEADER) === false) && (\str_contains($contentToWrite, self::GENERATED_HEADER) === false)) {
100+
return self::MODIFIED_HEADER . PHP_EOL . PHP_EOL . $contentToWrite;
119101
}
120102

121103
return $contentToWrite;

tests/Commands/CreateCommandTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ public function failsIfGitattributesAlreadyExists(): void
103103
}
104104

105105
$gitattributesContent = <<<CONTENT
106-
* text=auto eol=lf
107106
108107
phpspec.yml.dist export-ignore
109108
specs/ export-ignore

tests/Commands/UpdateCommandTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ public function updatesExistingGitattributesAndReplacesHeader(): void
4848
$gitattributesContent = <<<CONTENT
4949
# This file was generated by the lean package validator (http://git.io/lean-package-validator).
5050
51-
* text=auto eol=lf
52-
5351
.gitattributes export-ignore
5452
.github/ export-ignore
5553
tests/ export-ignore

tests/Commands/ValidateCommandStdinOptionsTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public function stdinHonorsStrictOrderAndReportsInvalidOnShuffledOrder(): void
6161

6262
// Shuffled order: README before dotfiles, etc.
6363
$stdinContent = <<<GITATTR
64-
* text=auto eol=lf
6564
6665
README.md export-ignore
6766
tests/ export-ignore
@@ -94,7 +93,6 @@ public function stdinHonorsStrictOrderAndReportsValidOnExpectedOrder(): void
9493

9594
// Likely expected order: .gitattributes, .gitignore, README.md, tests/
9695
$stdinContent = <<<GITATTR
97-
* text=auto eol=lf
9896
9997
.gitattributes export-ignore
10098
.gitignore export-ignore
@@ -127,7 +125,6 @@ public function stdinHonorsEnforceAlignmentAndReportsValidOnExpectedOrder(): voi
127125

128126
// Likely expected order: .gitattributes, .gitignore, README.md, tests/
129127
$stdinContent = <<<GITATTR
130-
* text=auto eol=lf
131128
132129
.gitattributes export-ignore
133130
.gitignore export-ignore

0 commit comments

Comments
 (0)