Skip to content

Commit 59945bd

Browse files
committed
Fixes generated or modified header
1 parent 4b565f6 commit 59945bd

File tree

5 files changed

+127
-41
lines changed

5 files changed

+127
-41
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/Commands/ValidateCommand.php

Lines changed: 7 additions & 15 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
}
@@ -307,6 +307,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
307307
$globPattern = $input->getOption('glob-pattern');
308308
$globPatternFile = (string) $input->getOption('glob-pattern-file');
309309
$omitHeader = $input->getOption('omit-header');
310+
310311
$showDifference = $input->getOption('diff');
311312
$reportStaleExportIgnores = $input->getOption('report-stale-export-ignores');
312313

@@ -457,9 +458,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
457458
$output->writeln($verboseOutput, OutputInterface::VERBOSITY_VERBOSE);
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>';
461+
if ($createGitattributesFile === false) {
462+
$warning = 'Warning: There is no .gitattributes file present in '
463+
. $this->analyser->getDirectory() . '.';
464+
$outputContent = '<error>' . $warning . '</error>';
465+
}
463466

464467
$expectedGitattributesFileContent = $this->analyser
465468
->getExpectedGitattributesContent();
@@ -562,17 +565,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
562565
}
563566
$output->writeln($verboseOutput, OutputInterface::VERBOSITY_VERBOSE);
564567

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-
576568
$outputContent .= $this->gitattributesFileRepository->overwriteGitattributesFile(
577569
$expectedGitattributesFileContent
578570
);

src/GitattributesFileRepository.php

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@ public function createGitattributesFile(string $content, bool $withHeader = true
3131
{
3232
// Ensure the "generated 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) && !\str_contains($contentToWrite, self::GENERATED_HEADER)) {
100+
return self::MODIFIED_HEADER . PHP_EOL . PHP_EOL . $contentToWrite;
119101
}
120102

121103
return $contentToWrite;

tests/Commands/ValidateCommandTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Stolt\LeanPackage\Commands\ValidateCommand;
2020
use Stolt\LeanPackage\Exceptions\InvalidGlobPattern;
2121
use Stolt\LeanPackage\Exceptions\NoLicenseFilePresent;
22+
use Stolt\LeanPackage\GitattributesFileRepository;
2223
use Stolt\LeanPackage\Helpers\Str as OsHelper;
2324
use Stolt\LeanPackage\Presets\Finder;
2425
use Stolt\LeanPackage\Presets\PhpPreset;
@@ -1617,13 +1618,24 @@ public function incompleteGitattributesFileIsOverwritten(string $option): void
16171618
['specs']
16181619
);
16191620

1621+
$header = GitattributesFileRepository::GENERATED_HEADER . PHP_EOL . PHP_EOL;
1622+
1623+
if ($option === '--overwrite') {
1624+
$header = GitattributesFileRepository::MODIFIED_HEADER . PHP_EOL;
1625+
}
1626+
1627+
if ($option === '--create') {
1628+
$header = GitattributesFileRepository::GENERATED_HEADER . PHP_EOL . PHP_EOL;
1629+
\unlink(WORKING_DIRECTORY . DIRECTORY_SEPARATOR . '.gitattributes');
1630+
}
1631+
16201632
$command = $this->application->find('validate');
16211633
$commandTester = new CommandTester($command);
16221634
$commandTester->execute([
16231635
'command' => $command->getName(),
16241636
'directory' => WORKING_DIRECTORY,
16251637
$option => true,
1626-
'--omit-header' => true,
1638+
'--omit-header' => false,
16271639
]);
16281640

16291641
$dedicatedCommand = $option === '--create' ? 'create' : 'update';
@@ -1633,6 +1645,7 @@ public function incompleteGitattributesFileIsOverwritten(string $option): void
16331645
The present .gitattributes file is considered invalid.
16341646
16351647
Overwrote it with the shown content:
1648+
$header
16361649
* text=auto eol=lf
16371650
16381651
.gitattributes export-ignore
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Stolt\LeanPackage\Tests;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use Stolt\LeanPackage\Analyser;
7+
use Stolt\LeanPackage\GitattributesFileRepository;
8+
use Stolt\LeanPackage\Presets\Finder;
9+
use Stolt\LeanPackage\Presets\PhpPreset;
10+
11+
final class GitattributesFileRepositoryTest extends TestCase
12+
{
13+
public function setUp(): void
14+
{
15+
$this->setUpTemporaryDirectory();
16+
}
17+
18+
/**
19+
* Tear down the test environment.
20+
*
21+
* @return void
22+
*/
23+
protected function tearDown(): void
24+
{
25+
if (\is_dir($this->temporaryDirectory)) {
26+
$this->removeDirectory($this->temporaryDirectory);
27+
}
28+
}
29+
30+
#[Test]
31+
public function addsExpectedFileHeaders(): void
32+
{
33+
$analyser = (new Analyser(new Finder(new PhpPreset())))->setDirectory($this->temporaryDirectory);
34+
35+
$repository = new GitattributesFileRepository($analyser);
36+
37+
$fakeGitattributesContent = <<<CONTENT
38+
* text=auto eol=lf
39+
40+
.gitattributes export-ignore
41+
phpspec.yml.dist export-ignore
42+
specs/ export-ignore
43+
version-increase-command export-ignore
44+
CONTENT;
45+
$contentWithFileHeader = $repository->applyOverwriteHeaderPolicy($fakeGitattributesContent);
46+
47+
$this->assertStringContainsString(GitattributesFileRepository::GENERATED_HEADER, $contentWithFileHeader);
48+
49+
$generatedHeader = GitattributesFileRepository::GENERATED_HEADER . PHP_EOL . PHP_EOL;
50+
$fakeGitattributesContent = <<<CONTENT
51+
$generatedHeader
52+
* text=auto eol=lf
53+
54+
.gitattributes export-ignore
55+
phpspec.yml.dist export-ignore
56+
specs/ export-ignore
57+
version-increase-command export-ignore
58+
CONTENT;
59+
60+
\touch($this->temporaryDirectory . DIRECTORY_SEPARATOR . '.gitattributes');
61+
62+
$contentWithFileHeader = $repository->applyOverwriteHeaderPolicy($fakeGitattributesContent);
63+
64+
$this->assertStringContainsString(GitattributesFileRepository::MODIFIED_HEADER, $contentWithFileHeader);
65+
66+
$modifiedHeader = GitattributesFileRepository::MODIFIED_HEADER . PHP_EOL . PHP_EOL;
67+
$fakeGitattributesContent = <<<CONTENT
68+
$modifiedHeader
69+
* text=auto eol=lf
70+
71+
.gitattributes export-ignore
72+
phpspec.yml.dist export-ignore
73+
specs/ export-ignore
74+
version-increase-command export-ignore
75+
CONTENT;
76+
$contentWithFileHeader = $repository->applyOverwriteHeaderPolicy($fakeGitattributesContent);
77+
78+
$this->assertStringContainsString(GitattributesFileRepository::MODIFIED_HEADER, $contentWithFileHeader);
79+
80+
$fakeGitattributesContent = <<<CONTENT
81+
* text=auto eol=lf
82+
83+
.gitattributes export-ignore
84+
phpspec.yml.dist export-ignore
85+
specs/ export-ignore
86+
version-increase-command export-ignore
87+
CONTENT;
88+
89+
$contentWithFileHeader = $repository->applyOverwriteHeaderPolicy($fakeGitattributesContent);
90+
91+
$this->assertStringContainsString(GitattributesFileRepository::MODIFIED_HEADER, $contentWithFileHeader);
92+
}
93+
}

0 commit comments

Comments
 (0)