问题 是否有一个良好的PHP git客户端与http支持? [关闭]


对于我正在进行的项目,我们希望将git用作我们经常修改的某些数据的修订跟踪器。我们使用php作为web前端,我们需要一个goo php git客户端来使用。我在互联网上遇到过一些人,他们都倾向于有同样的限制......

不支持HTTP。我们需要能够推/拉到远程存储库。我们还需要克隆。

理想情况下,我正在寻找一些不使用git命令的东西(即:exepers to exec())但是如果班级运作良好,我愿意解决。我看过一个看起来像我想做的C库,但是php语言绑定不完整,http函数被标记为实验。

有没有人有任何洞察通过PHP使用git和http?


9152
2018-02-14 21:16


起源

你为什么不打电话 git 同 exec() 只是为了你需要的命令?或者是什么问题 git-wrappers? - KingCrunch
您不需要HTTP支持 git 已经那样了。您应该将远程存储库克隆到Web服务器上的本地存储库并使用推/拉,因为这毕竟就是这种方式 git 作品。 - Ryan P
我不想做exec的调用,我正在寻找一个类接口。对exec的调用是草率的,它们不一定是跨平台的,并且引入了潜在的安全漏洞。在某些c lib绑定或某些“pure-php”代码上运行的类接口比运行shell命令更可取。我确实需要http支持,因为软件可能在多个网络服务器上运行,本地存储库在这种情况下是不够的。 - Radmilla Mustafa


答案:


https://github.com/kbjr/Git.php

Git.php是一个围绕git调用的包装类 proc_open 代替 exec 运行命令。虽然它没有推/拉方法,但确实有一般性 run 运行自定义git命令的方法,所以可以使用这样的方法:

$repo = Git::open('/path/to/repo');
$repo->run('push origin master');

它也有克隆方法(clone_to 和 clone_from 做本地克隆和 clone_remote 用于远程克隆)。


9
2018-02-17 05:30





一种可能性是使用PHP的SSH库通过连接回Web服务器来执行这些操作?

或者我发现了 这一组课程 它允许您通过HTTP克隆和读取其他元数据,但不能推送或拉取。然而,如果你足够勇敢地扩展它们,那么它可能是一个起点。我可以想象复制这些进程并使它们符合各种服务器版本等需要做很多工作。

[更新时间23/03/2014,收到upvote后 - 谢谢!]

我确实试图实现上面的一些方法(我正在寻找一个实现,画了一个空白,所以试着写我自己的),它  我想的很难!我实际上放弃了它,因为我发现在不同的架构中实现这一点的简单方法,但这是我写的类。它本质上是有效的,但对我工作的环境变化很脆弱(即它不能很好地处理错误或问题)。

它用:

  1. herzult / PHP-SSH
  2. ssh配置文件 - 简化在代码中设置身份验证凭据的技巧

(注意 - 我必须快速从代码中删除一些细节以发布它。所以你需要摆弄一下,将它集成到你的应用程序/命名空间等。)

<?php
/**
 * @author: scipilot
 * @since: 31/12/2013
 */

/**
 * Class GitSSH allows you to perform Git functions over an SSH session.
 * i.e. you are using the command-line Git commands after SSHing to a host which has the Git client.
 *
 * You don't need to know about the SSH to use the class, other than the fact you will need access
 * to a server via SSH which has the Git client installed. Its likely this is the local web server.
 *
 * This was made because PHP has no good native Git client.
 *
 * Requires herzult/php-ssh
 *
 * Example php-ssh config file would be
 *
 * <code>
 *  Host localhost
 *  User git
 *  IdentityFile id_rsa
 *
 *  Host your.host.domain.com
 *  User whoever
 *  IdentityFile ~/.ssh/WhoeverGit
 *</code>
 */
class GitSSH {
    protected $config;
    protected $session;
    protected $sPath;
    /**
     * @var string
     */
    protected $sConfigPath = '~/.ssh/config';

    /**
     * Connects to the specified host, ready for further commands.
     *
     * @param string $sHost         Host (entry in the config file) to connect to.
     * @param string $sConfigPath Optional; config file path. Defaults to ~/.ssh/config,
     *                            which is probably inaccessible for web apps.
     */
    function __construct($sHost, $sConfigPath=null){
        \Log::info('New GitSSH '.$sHost.', '.$sConfigPath);
        if(isset($sConfigPath)) $this->sConfigPath = $sConfigPath;

        $this->config = new \Ssh\SshConfigFileConfiguration($this->sConfigPath, $sHost);
        $this->session = new \Ssh\Session($this->config, $this->config->getAuthentication());
    }

    public function __destruct() {
        $this->disconnect();
    }

    /**
     * Thanks to Steve Kamerman, as there isn't a native disconnect.
     */
    public function disconnect() {
        $this->exec('echo "EXITING" && exit;');
        $this->session = null;
    }

    /**
     * Run a command (in the current working directory set by cd)
     * @param $sCommand
     * @return string
     */
    protected function exec($sCommand) {
        //echo "\n".$sCommand."\n";
        $exec = $this->session->getExec();
        $result = $exec->run('cd '.$this->sPath.'; '.$sCommand);
        // todo: parse/scrape the result, return a Result object?
        return $result;
    }

    /**
     * CD to a folder. (This not an 'incremental' cd!)
     * Devnote: we don't really execute the cd now, it's appended to other commands. Each command seems to re-login?
     *
     * @param string $sPath Absolute filesystem path, or relative from user home
     */
    public function cd($sPath){
        $this->sPath = $sPath;

        // @todo this is useless! each command seems to run in a separate login?
        //$result = $this->exec('cd'); // /; ls');
        //return $result;
    }

    /**
     * @return string
     */
    public function ls(){
        $result = $this->exec('ls ');

        return $result;
    }

    public function gitAdd($sOptions=null, array $aFiles=null){
        $result = $this->exec('git add '
            .(empty($sOptions) ? '' : ' '.$sOptions)
            .(empty($aFiles) ? '' : ' '.implode(' ', $aFiles))
        );

        return $result;
    }

    public function gitClone($sRepo, $sBranch=null, $sTarget=null){
        \Log::info('GitSSH::clone '.$sRepo.', '.$sBranch.', '.$sTarget);
        $result = $this->exec('git clone '
            .(empty($sBranch) ? '' : ' --branch '.$sBranch)
            .' '.$sRepo
            .' '.$sTarget);

        return $result;
    }

    public function gitCommit($sMessage, $sOptions=null, array $aFiles=null){
        $result = $this->exec('git commit '
            .'-m "'.addcslashes($sMessage, '"').'"'
            .(empty($sOptions) ? '' : ' '.$sOptions)
            .(empty($aFiles) ? '' : ' '.implode(' ', $aFiles))
        );

        return $result;
    }

    public function gitPull($sOptions=null, $sRepo=null, $sRefspec=null){
        $result = $this->exec('git pull '
            .(empty($sOptions) ? '' : ' '.$sOptions)
            .(empty($sRepo) ? '' : ' '.$sRepo)
            .(empty($sRefspec) ? '' : ' '.$sRefspec)
        );

        return $result;
    }

    public function gitPush($sOptions=null, $sRepo=null, $sRefspec=null){
        $result = $this->exec('git push '
            .(empty($sOptions) ? '' : ' '.$sOptions)
            .(empty($sRepo) ? '' : ' '.$sRepo)
            .(empty($sRefspec) ? '' : ' '.$sRefspec)
        );

        return $result;
    }

    /**
     * @return string the raw result from git status
     */
    public function gitStatus(){
        $result = $this->exec('git status');
        return $result;
    }

}

3
2017-12-30 10:56





这很有希望: http://gitphp.org (断开的链接;见一个 存档版本

我想那会为你做的。以下是对它的描述:

GitPHP是git存储库的Web前端。它模仿标准gitweb的外观,但是用PHP编写,并使用Smarty模板进行自定义。它有一些附加功能,包括通过GeSHi PHP类和项目类别支持的语法突出显示。它适用于Windows上的标准git和msysgit。

安装程序应该相当简单 - 只需要提取要安装它的tarball,将config / gitphp.conf.php.example复制到config / gitphp.conf.php,并将conf中的projectroot设置为指向您的目录裸git存储库是,并使templates_c目录可由webserver写入,如果它还没有。   您可以查看config / gitphp.conf.defaults.php中的所有可用选项和默认值,如果要覆盖默认值,则将选项复制到配置文件中。您还可以将config / projects.conf.php.example复制到config / projects.conf.php并编辑它,如果您想要对项目进行更高级的控制,例如为项目定义类别或从文本文件加载项目。更详细的说明在随附的自述文件中。

注意:如果您正在升级现有的gitphp.conf.php将不会被覆盖,但我建议您检查gitphp.conf.defaults.php以获取可能已添加的新配置选项。

您可以查看在此站点上运行的实时副本。


0
2018-02-14 22:01



对不起,我相信这只是一个存储库浏览器,就像gitweb一样。它不允许您执行任何命令,只需浏览文件和历史记录。 - scipilot