PHP根据.gitignore过滤文件和路径 [英] PHP filtering files and paths according .gitignore

查看:626
本文介绍了PHP根据.gitignore过滤文件和路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用PHP读取所有被.gitignore配置忽略的文件和路径。就像git一样。



可以重复读取目录,并使用正则表达式对每个文件进行过滤。但是如果路径中有太多文件,那么效率太低。



读取.gitignore忽略的目标文件和路径的任何好的和最有效的方法?

解决方案

您需要分几步进行操作: 文件

每个文件夹都可以有一个文件夹,因此不要假设只有一个文件夹。



子模块有一个.git链接到主.git文件夹,所以要小心停止太早。



它会像这样:

 函数find_gitignore_files($ dir){
$ files = array();
while(true){
$ file =$ dir / .gitignore;
if(is_file($ file))$ files [] = $ file;
if(is_dir($ dir / .git)&&!is_link($ dir / .git))break; #stop here
if(dirname($ dir)==='。')break; #和这里
$ dir = dirname($ dir);
}
返回$文件;
}



2 - 解析每个.gitignore文件



您需要忽略注释,介意否定运算符(),并且介意这些小块。



 函数parse_git_ignore_file($ file){# $ file ='/absolute/path/to/.gitignore'
$ dir = dirname($ file);
$ matches = array();
$ lines = file($ file);
foreach($ lines as $ line){
$ line = trim($ line);
if($ line ==='')continue; #空行
if(substr($ line,0,1)=='#')continue; #$ comment
if(substr($ line,0,1)=='!'){#negated glob
$ line = substr($ line,1);
$ files = array_diff(glob($ dir / *),glob($ dir / $ line));
} else {#normal glob
$ files = glob($ dir / $ line);
}
$ matches = array_merge($ matches,$ files);
}
返回$匹配;

$ / code>

(注意:以上都没有经过测试,但它们应该让你进入正确的方向。)


I want to use PHP to read all files and paths ignored by .gitignore configuration. Just like how git does.

It's possible to read directory repeatedly and use regular expression for each file to filter. But it`s so ineffective if the path have too much files.

Any good and most effective way to read target files and path ignored by .gitignore?

解决方案

You need to proceed in several steps:

1 - Find the .gitignore files

Each folder can have one, so don't assume there's a single one.

And submodules have a .git link to the main .git folder, so be wary about stopping too early as well.

It'll go something like:

function find_gitignore_files($dir) {
  $files = array();
  while (true) {
    $file = "$dir/.gitignore";
    if (is_file($file)) $files[] = $file;
    if (is_dir("$dir/.git") && !is_link("$dir/.git")) break;  # stop here
    if (dirname($dir) === '.') break;                         # and here
    $dir = dirname($dir);
  }
  return $files;
}

2 - Parse each .gitignore file

You need to ignore comments, mind the negation operator (!), and mind the globs.

This one is, give or take, is going to go something like:

function parse_git_ignore_file($file) { # $file = '/absolute/path/to/.gitignore'
  $dir = dirname($file);
  $matches = array();
  $lines = file($file);
  foreach ($lines as $line) {
    $line = trim($line);
    if ($line === '') continue;                 # empty line
    if (substr($line, 0, 1) == '#') continue;   # a comment
    if (substr($line, 0, 1) == '!') {           # negated glob
      $line = substr($line, 1);
      $files = array_diff(glob("$dir/*"), glob("$dir/$line"));
    } else {                                    # normal glob
      $files = glob("$dir/$line");
    }
    $matches = array_merge($matches, $files);
  }
  return $matches;
}

(Note: none of the above is tested, but they should put you in the right direction.)

这篇关于PHP根据.gitignore过滤文件和路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆