简体   繁体   English

从Docroot递归搜索.git目录

[英]Recursively Search for .git Directory From Docroot

I'm building a class in PHP for deploying via web hooks from GitLab. 我正在用PHP构建一个类,以通过GitLab的Web挂钩进行部署。

Obviously the file to hook needs to be available from the web and exist within the docroot. 显然,要挂接的文件需要可从Web上获得并且存在于docroot中。

But in order for this to pull the repo correctly, I need to recurse upwards from the docroot and find which directory contains the .git directory. 但是为了使此正确地拉回购协议,我需要从docroot向上递归,并找到哪个目录包含.git目录。

I can't find an iterator that recurses upward. 我找不到向上递归的迭代器。

I've tried Recursive Directory Iterator 我已经尝试过递归目录迭代器

but that recurses down from a top level directory and that won't work since I may have several directories that may contain .git directories. 但这是从顶级目录递归而来的,因为我可能有几个目录可能包含.git目录,所以该目录不起作用。

How would I go about recursing upwards until the first instance of a .git directory is found? 在找到.git目录的第一个实例之前,我将如何向上递归?

Ok this seems to do the trick, there may be an easier way out there but this serves the purpose pretty cleanly. 好的,这似乎可以解决问题,可能有一种更简单的方法,但是这样做很干净。

private function __seek() {
    $directories = array();
    $dirs        = explode('/', dirname(__FILE__));

    // remove empty first item
    array_shift($dirs);

    // remove the top/home directory as we won't be able to read it anyway
    $count = (count($dirs) - 1);

    for ($i = 0; $i < $count; $i++):
        $directories[] = '/' . implode('/', $dirs);
        array_pop($dirs);
    endfor;

    $paths = array();

    foreach ($directories as $directory):
        if (in_array('.git', scandir($directory))):
            $paths['parent']    = dirname($directory);
            $paths['directory'] = basename($directory);
        endif;
    endforeach;

    return $paths;
}

This returns a nice array that I can gives me both the directory (parent) which I need to chdir() into to pull, and the directory name to pull to (directory) 这将返回一个不错的数组,我可以同时给我要chdir()提取的目录(父目录)和要提取到的目录名(目录)

Array
(
    [parent] => /home/solution
    [directory] => app
)

Silly me ... 傻我...

as it turns out, GIT is so intuitive that it does the upstream searching for you. 事实证明,GIT非常直观,可以进行上游搜索。

I just tried to pull from 5 directories deep and it worked perfectly. 我只是尝试从5个目录中拉出,而且效果很好。

You can pull from any directory beneath your .git directory. 您可以从.git目录下的任何目录中提取。

GIT FTW! GIT FTW!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM