Skip to content

Commit

Permalink
Merge branch 'buffcode-3.0' into 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Flyingmana committed Mar 8, 2016
2 parents bf7aea2 + cd4bf61 commit 46e3afe
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased][unreleased]
- Fixed an issue where symlinks were follwed when removing a module, causing files in vendor to be removed
- Added PHP 7.0, HHVM to Travis CI. Removed allow_failures for HHVM.
- Changes the way gitignore files are being processed. Retains the layout and other duplicates (comments, empty lines, etc.)

## [3.0.6] - 2015-10-21
- Fix problems with magento connect packages referencing non existent files
Expand Down
58 changes: 51 additions & 7 deletions src/MagentoHackathon/Composer/Magento/GitIgnore.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct($fileLocation)
{
$this->gitIgnoreLocation = $fileLocation;
if (file_exists($fileLocation)) {
$this->lines = array_flip(file($fileLocation, FILE_IGNORE_NEW_LINES));
$this->lines = $this->removeDuplicates(file($fileLocation, FILE_IGNORE_NEW_LINES));
}
}

Expand All @@ -41,8 +41,8 @@ public function __construct($fileLocation)
public function addEntry($file)
{
$file = $this->prependSlashIfNotExist($file);
if (!isset($this->lines[$file])) {
$this->lines[$file] = $file;
if (!in_array($file, $this->lines)) {
$this->lines[] = $file;
}
$this->hasChanges = true;
}
Expand All @@ -63,9 +63,13 @@ public function addMultipleEntries(array $files)
public function removeEntry($file)
{
$file = $this->prependSlashIfNotExist($file);
if (isset($this->lines[$file])) {
unset($this->lines[$file]);
$key = array_search($file, $this->lines);
if (false !== $key) {
unset($this->lines[$key]);
$this->hasChanges = true;

// renumber array
$this->lines = array_values($this->lines);
}
}

Expand All @@ -84,7 +88,7 @@ public function removeMultipleEntries(array $files)
*/
public function getEntries()
{
return array_values(array_flip($this->lines));
return $this->lines;
}

/**
Expand All @@ -93,7 +97,7 @@ public function getEntries()
public function write()
{
if ($this->hasChanges) {
file_put_contents($this->gitIgnoreLocation, implode("\n", array_flip($this->lines)));
file_put_contents($this->gitIgnoreLocation, implode("\n", $this->lines));
}
}

Expand All @@ -108,4 +112,44 @@ private function prependSlashIfNotExist($file)
{
return sprintf('/%s', ltrim($file, '/'));
}

/**
* Removes duplicate patterns from the input array, without touching comments, line breaks etc.
* Will remove the last duplicate pattern.
*
* @param array $lines
* @return array
*/
private function removeDuplicates($lines)
{
// remove empty lines
$duplicates = array_filter($lines);

// remove comments
$duplicates = array_filter($duplicates, function ($line) {
return strpos($line, '#') !== 0;
});

// check if duplicates exist
if (count($duplicates) !== count(array_unique($duplicates))) {
$duplicates = array_filter(array_count_values($duplicates), function ($count) {
return $count > 1;
});

// search from bottom to top
$lines = array_reverse($lines);
foreach ($duplicates as $duplicate => $count) {
// remove all duplicates, except the first one
for ($i = 1; $i < $count; $i++) {
$key = array_search($duplicate, $lines);
unset($lines[$key]);
}
}

// restore original order
$lines = array_values(array_reverse($lines));
}

return $lines;
}
}
18 changes: 18 additions & 0 deletions tests/MagentoHackathon/Composer/Magento/GitIgnoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,22 @@ public function testForwardSlashIsPrePendedToPath()
$gitIgnore->addEntry('/file2.txt');
$this->assertSame(['/file1.txt', '/file2.txt'], $gitIgnore->getEntries());
}

public function testDuplicatesRemoved()
{
$lines = array('/line1', '/line1');
file_put_contents($this->gitIgnoreFile, implode("\n", $lines));

$gitIgnore = new GitIgnore($this->gitIgnoreFile);
$this->assertCount(1, $gitIgnore->getEntries());
}

public function testEmptyLinesNotRemoved()
{
$lines = array('/line1', '', '/line1');
file_put_contents($this->gitIgnoreFile, implode("\n", $lines));

$gitIgnore = new GitIgnore($this->gitIgnoreFile);
$this->assertSame(array('/line1', ''), $gitIgnore->getEntries());
}
}

0 comments on commit 46e3afe

Please sign in to comment.