Skip to content

Commit 7ae9ad9

Browse files
committed
Handels preceding slashes and directories
1 parent 7276458 commit 7ae9ad9

3 files changed

Lines changed: 67 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
77

88
## [Unreleased]
99

10+
## [v5.8.1] - 2026-05-07
11+
12+
### Fixed
13+
- Improved the output of the `reformat` command. Preceding slashes are removed and directories are postfixed with a slash.
14+
1015
## [v5.8.0] - 2026-05-07
1116

1217
### Added

src/Commands/ReformatCommand.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(private readonly Analyser $analyser,
3434
*/
3535
protected function configure(): void
3636
{
37-
$this->analyser->setDirectory(WORKING_DIRECTORY);
37+
$this->analyser->setDirectory(\getcwd());
3838

3939
$this
4040
->setName('reformat')
@@ -67,16 +67,13 @@ protected function configure(): void
6767
*/
6868
protected function execute(InputInterface $input, OutputInterface $output): int
6969
{
70-
$directory = (string)$input->getArgument('directory') ?: WORKING_DIRECTORY;
70+
$directory = (string)$input->getArgument('directory') ?: \getcwd();
7171
$this->analyser->setDirectory($directory);
7272
$isAgenticRun = $this->isAgenticRun($input);
7373

7474
return $this->reformatPresentExportIgnores($input, $output, $directory, $isAgenticRun);
7575
}
7676

77-
78-
79-
8077
private function reformatPresentExportIgnores(
8178
InputInterface $input,
8279
OutputInterface $output,
@@ -174,10 +171,18 @@ private function getPresentGitattributesContentWithAlignedExportIgnores(): strin
174171
}
175172

176173
[$pattern, $suffix] = \explode('export-ignore', $line, 2);
177-
$pattern = \rtrim($pattern);
174+
$pattern = \trim($pattern);
175+
176+
if (\str_starts_with($pattern, '/') && \str_ends_with($pattern, '/') === false) {
177+
$pattern = \ltrim($pattern, '/');
178+
}
179+
180+
if (\is_dir(\getcwd() . '/' . $pattern) && \str_ends_with($pattern, '/') === false) {
181+
$pattern = $pattern . DIRECTORY_SEPARATOR;
182+
}
178183

179-
if (str_starts_with($pattern, '/') && str_ends_with($pattern, '/') === false) {
180-
$pattern = str_replace('/', '', $pattern);
184+
if (\strlen($pattern) > $longestPattern) {
185+
$longestPattern = \strlen($pattern);
181186
}
182187

183188
return $pattern . \str_repeat(' ', $longestPattern - \strlen($pattern) + 1) . 'export-ignore' . $suffix;

tests/Commands/ReformatCommandTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,55 @@ protected function tearDown(): void
2525
$this->removeDirectory($this->temporaryDirectory);
2626
}
2727

28+
#[Test]
29+
public function addASlashToExportIgnoredDirectories(): void
30+
{
31+
$analyser = (new Analyser(new Finder(new PhpPreset())))->setDirectory($this->temporaryDirectory);
32+
$repository = new GitattributesFileRepository($analyser);
33+
$command = new ReformatCommand($analyser, $repository);
34+
35+
$gitattributesContent = <<<CONTENT
36+
/.gitattributes export-ignore
37+
/.gitignore export-ignore
38+
/ChangeLog export-ignore
39+
/example export-ignore
40+
/README.rst export-ignore
41+
/tests export-ignore
42+
/.editorconfig export-ignore
43+
/.github export-ignore
44+
/contributing.rst export-ignore
45+
CONTENT;
46+
47+
$this->createTemporaryGitattributesFile($gitattributesContent);
48+
49+
$this->createTemporaryFiles(['.gitignore'], ['example', 'tests', '.github']);
50+
51+
$result = TestCommand::for($command)
52+
->addArgument($this->temporaryDirectory)
53+
->addOption('dry-run')
54+
->execute()
55+
->assertSuccessful();
56+
57+
$expectedGitattributesContent = <<<CONTENT
58+
.gitattributes export-ignore
59+
.gitignore export-ignore
60+
ChangeLog export-ignore
61+
example/ export-ignore
62+
README.rst export-ignore
63+
tests/ export-ignore
64+
.editorconfig export-ignore
65+
.github/ export-ignore
66+
contributing.rst export-ignore
67+
CONTENT;
68+
69+
$this->assertSame(trim($expectedGitattributesContent), trim($result->output()));
70+
71+
$this->assertStringEqualsFile(
72+
$this->temporaryDirectory . DIRECTORY_SEPARATOR . '.gitattributes',
73+
$gitattributesContent
74+
);
75+
}
76+
2877
#[Test]
2978
public function alignsOnlyExistingExportIgnoresAndRemovesPrecedingSlashes(): void
3079
{

0 commit comments

Comments
 (0)