在纯 PHP 中签出 git 存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7097360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:50:54  来源:igfitidea点击:

Checkout of git repository in pure PHP

phpgitgit-checkout

提问by dom0

I need to to a git checkout in pure PHP. I already tried this ( http://www.phpclasses.org/package/5310-PHP-Retrieve-project-files-from-GIT-repositories.html) with HTTP and SASL, but I didn't really work. Then I took a look at GLIP ( https://github.com/patrikf/glip), but that doesn't seem to have any functionality like this. Basically I need to

我需要使用纯 PHP 进行 git checkout。我已经用 HTTP 和 SASL尝试过这个(http://www.phpclasses.org/package/5310-PHP-Retrieve-project-files-from-GIT-repositories.html),但我并没有真正工作。然后我查看了 GLIP ( https://github.com/patrikf/glip),但它似乎没有任何这样的功能。基本上我需要

-replicate/clone a remote git repository

-复制/克隆远程 git 存储库

-"extract" master branch files into a specified directory

-“提取”主分支文件到指定目录

The main problem with PHP GIT is, that it just didn't supported all possible changes you could do in a commit. Only new files, no moving around of files. And it was also unable to extract files.

PHP GIT 的主要问题是,它只是不支持您可以在提交中进行的所有可能的更改。只有新文件,没有文件移动。它也无法提取文件。

/edit: git is not installed and I also cannot install git

/edit: git 未安装,我也无法安装 git

采纳答案by Gordon

You can try with

你可以试试

Git Streamwrapper for PHP is a PHP library that allows PHP code to interact with one or multiple Git repositories from within an application. The library consists of a Git repository abstraction that can be used to programatically access Git repositories and of a stream wrapper that can be hooked into the PHP stream infrastructure to allow the developer to use file and directory access functions directly on files in a Git repository. The library provides means to access status information on a Git repository, such as the log, the current repository status or commit information, as well.

用于 PHP 的 Git Streamwrapper 是一个 PHP 库,它允许 PHP 代码从应用程序内与一个或多个 Git 存储库进行交互。该库由可用于以编程方式访问 Git 存储库的 Git 存储库抽象和可连接到 PHP 流基础架构以允许开发人员直接对 Git 存储库中的文件使用文件和目录访问功能的流包装器组成。该库还提供访问 Git 存储库状态信息的方法,例如日志、当前存储库状态或提交信息。

It requires Git to be installed on the machine though and is in beta as of this writing.

它需要在机器上安装 Git,并且在撰写本文时处于测试阶段。

回答by Alexandre Salomé

I've developed a pretty good PHP library to manipulate git repositories, you should consider it: http://gitonomy.com/doc/gitlib/master/

我开发了一个很好的 PHP 库来操作 git 存储库,你应该考虑一下:http: //gitonomy.com/doc/gitlib/master/

Looks like this:

看起来像这样:

$repository = new Gitonomy\Git\Repository('/path/to/repository');
$master     = $repository->getReferences()->getBranch('master');

$author = $master->getCommit()->getAuthorName();

echo "Last modification on master made by ".$author;

回答by taggartJ

<?php
  /**
   * This function handles the pull / init / clone of a git repo
   *
   * @param $git_url 
   *  Example of git clone url git://github.com/someuser/somerepo.git
   *
   * @return bool true 
   */
  public static function pullOrCloneRepo($git_url) {
    if (!isset($git_url)) {
      return false;
    }
    // validate contains git://github.com/
    if (strpos($git_url, 'git://github.com/') !== FALSE) {
      // create a directory and change permissions 
      $uri = 'public://somedir'; // change this if not in drupal 
      //check the dir
      $file_path = drupal_realpath($uri);  // change this if not in drupal 
      if (isset($file_path)) {
        $first_dir =  getcwd();
        // change dir to the new path
        $new_dir = chdir($file_path);
        // Git init
        $git_init = shell_exec('git init');
        // Git clone
        $git_clone = shell_exec('git clone '. $git_url);
        // Git pull
        $git_pull = shell_exec('git pull');
        // change dir back
        $change_dir_back = chdir($first_dir);
        return true;
      }
    }
    else {
      return false;
    }
  }
?>