在纯PHP中检出git存储库

时间:2011-08-17 18:04:38

标签: php git git-checkout

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

-replicate / clone远程git存储库

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

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

/编辑: 没有安装git,我也无法安装git

3 个答案:

答案 0 :(得分:4)

您可以尝试使用

  

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

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

答案 1 :(得分:3)

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

看起来像这样:

$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;

答案 2 :(得分:-3)

<?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;
    }
  }
?>