Esempio n. 1
0
 public function testMixFlagsAndJokers()
 {
     $expr = new Expression('~^.*abc.*$~is');
     $expr->getRegex()->setStartFlag(false)->setEndFlag(false)->setStartJoker(false)->setEndJoker(false);
     $this->assertEquals('~abc~is', $expr->render());
     $expr->getRegex()->setStartFlag(true)->setEndFlag(true)->setStartJoker(true)->setEndJoker(true);
     $this->assertEquals('~^.*abc.*$~is', $expr->render());
 }
Esempio n. 2
0
 protected function buildContentFiltering(Command $command, array $contains, $not = false)
 {
     foreach ($contains as $contain) {
         $expr = Expression::create($contain);
         $command->add('| xargs -I{} -r grep -I')->add($expr->isCaseSensitive() ? null : '-i')->add($not ? '-L' : '-l')->add('-Ee')->arg($expr->renderPattern())->add('{}');
     }
 }
Esempio n. 3
0
 /**
  * Adds rules that filenames must not match.
  *
  * You can use patterns (delimited with / sign) or simple strings.
  *
  * $collection->notPath('/^spec\/')
  *
  * @param $pattern
  *
  * @return FilesCollection
  */
 public function notPath($pattern)
 {
     $regex = Expression::create($pattern)->getRegex()->render();
     return $this->filter(function (SplFileInfo $file) use($regex) {
         return !preg_match($regex, $file->getPath());
     });
 }
 /**
  * {@inheritdoc}
  */
 protected function buildContentFiltering(Command $command, array $contains, $not = false)
 {
     foreach ($contains as $contain) {
         $expr = Expression::create($contain);
         // todo: avoid forking process for each $pattern by using multiple -e options
         $command->add('| grep -v \'^$\'')->add('| xargs -I{} grep -I')->add($expr->isCaseSensitive() ? null : '-i')->add($not ? '-L' : '-l')->add('-Ee')->arg($expr->renderPattern())->add('{}');
     }
 }
Esempio n. 5
0
 /**
  * @dataProvider getToRegexData
  */
 public function testGlobToRegex($glob, $match, $noMatch)
 {
     foreach ($match as $m) {
         $this->assertRegExp(Expression::create($glob)->getRegex()->render(), $m, '::toRegex() converts a glob to a regexp');
     }
     foreach ($noMatch as $m) {
         $this->assertNotRegExp(Expression::create($glob)->getRegex()->render(), $m, '::toRegex() converts a glob to a regexp');
     }
 }
Esempio n. 6
0
 /**
  * Converts glob to regexp.
  *
  * PCRE patterns are left unchanged.
  * Glob strings are transformed with Glob::toRegex().
  *
  * @param string $str Pattern: glob or regexp
  *
  * @return string regexp corresponding to a given glob or regexp
  */
 protected function toRegex($str)
 {
     $value = Expression::create($str);
     if ($value->isGlob()) {
         $value = $value->getRegex();
         if (false === strpos($str, '/')) {
             $value->setStartFlag(false);
         }
     }
     return $value->render();
 }
Esempio n. 7
0
 /**
  * @param ContextInterface|GitCommitMsgContext $context
  */
 public function run(ContextInterface $context)
 {
     $config = $this->getConfiguration();
     $commitMessage = $context->getCommitMessage();
     foreach ($config['matchers'] as $rule) {
         $expression = Expression::create($rule);
         $regex = $expression->getRegex();
         if ((bool) $config['case_insensitive']) {
             $regex->addOption('i');
         }
         if ((bool) $config['multiline']) {
             $regex->addOption('m');
         }
         if (!preg_match($regex->render(), $commitMessage)) {
             throw new RuntimeException(sprintf('The commit message does not match the rule: %s', $rule));
         }
     }
 }
 /**
  * Checks whether the string is a regex.
  *
  * @param string $str
  *
  * @return bool Whether the given string is a regex
  */
 protected function isRegex($str)
 {
     return Expression::create($str)->isRegex();
 }
 /**
  * Converts glob to regexp.
  *
  * PCRE patterns are left unchanged.
  * Glob strings are transformed with Glob::toRegex().
  *
  * @param string $str Pattern: glob or regexp
  *
  * @return string regexp corresponding to a given glob or regexp
  */
 protected function toRegex($str)
 {
     return Expression::create($str)->getRegex()->render();
 }
Esempio n. 10
0
 private function buildPathsFiltering(Command $command, $dir, array $paths, $not = false)
 {
     if (0 === count($paths)) {
         return;
     }
     $command->add($not ? '-not' : null)->cmd('(');
     foreach ($paths as $i => $path) {
         $expr = Expression::create($path);
         if ($expr->isGlob() && $expr->getGlob()->isExpandable()) {
             $expr = Expression::create($expr->getGlob()->toRegex(false));
         }
         if ($expr->isRegex()) {
             $regex = $expr->getRegex();
             $regex->prepend($regex->hasStartFlag() ? preg_quote($dir) . DIRECTORY_SEPARATOR : '.*')->setEndJoker(!$regex->hasEndFlag());
         } else {
             $expr->prepend('*')->append('*');
         }
         $command->add($i > 0 ? '-or' : null)->add($expr->isRegex() ? $expr->isCaseSensitive() ? '-regex' : '-iregex' : ($expr->isCaseSensitive() ? '-path' : '-ipath'))->arg($expr->renderPattern());
     }
     $command->cmd(')');
 }
 /**
  * @dataProvider getRegexRenderingData
  */
 public function testRegexRendering($expr, $body)
 {
     $this->assertEquals($body, Expression::create($expr)->renderPattern());
 }
    /**
     * @param Command  $command
     * @param string   $dir
     * @param string[] $paths
     * @param Boolean  $not
     */
    private function buildPathsFiltering(Command $command, $dir, array $paths, $not = false)
    {
        if (0 === count($paths)) {
            return;
        }

        $command->add($not ? '-not' : null)->cmd('(');

        foreach ($paths as $i => $path) {
            $expr = Expression::create($path);

            // Find does not support expandable globs ("*.{a,b}" syntax).
            if ($expr->isGlob() && $expr->getGlob()->isExpandable()) {
                $expr = Expression::create($expr->getGlob()->toRegex(false));
            }

            // Fixes 'not search' regex problems.
            if ($expr->isRegex()) {
                $regex = $expr->getRegex();
                $regex->prepend($regex->hasStartFlag() ? $dir.DIRECTORY_SEPARATOR : '.*')->setEndJoker(!$regex->hasEndFlag());
            } else {
                $expr->prepend('*')->append('*');
            }

            $command
                ->add($i > 0 ? '-or' : null)
                ->add($expr->isRegex()
                    ? ($expr->isCaseSensitive() ? '-regex' : '-iregex')
                    : ($expr->isCaseSensitive() ? '-path' : '-ipath')
                )
                ->arg($expr->renderPattern());
        }

        $command->cmd(')');
    }