【问题标题】:Checkout of git repository in pure PHP在纯 PHP 中签出 git 存储库
【发布时间】:2011-10-29 04:02:24
【问题描述】:

我需要在纯 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),但它似乎没有这样的功能。 基本上我需要

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

-“解压”master分支文件到指定目录

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

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

【问题讨论】:

标签: php git git-checkout


【解决方案1】:

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

看起来像这样:

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

【讨论】:

  • 这不是也需要在系统上安装git吗?
  • @Alexandre Salume 我曾经尝试过您的库,但无法使其正常工作。这个想法集中在在本地操作我的存储库。假设我在 wamp www 文件夹中有一个 php 应用程序,上面初始化了 git。是否可以使用您的库来操作该存储库,而不是使用 BitBucket 或 Github 作为源路径?
【解决方案2】:
<?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;
    }
  }
?>

【讨论】:

  • 如果你安装了 git 那就发疯
【解决方案3】:

你可以试试

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

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

【讨论】:

  • 对不起,我忘了提,但是 - git 没有安装,我无法安装它(共享空间):(
  • 我有同样的困难,有人建议我使用 FTPloy,对我来说不够好,但也许值得一看
猜你喜欢
  • 1970-01-01
  • 2013-09-22
  • 2020-07-09
  • 1970-01-01
  • 2023-03-22
  • 2014-09-04
  • 2013-02-15
  • 2012-11-16
  • 1970-01-01
相关资源
最近更新 更多