是否有一个良好的PHP git客户端与http支持?

时间:2012-02-14 21:16:03

标签: php git http client-library

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

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

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

有没有人对通过php使用git和http有任何见解?

3 个答案:

答案 0 :(得分:9)

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

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

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

它也有克隆方法(clone_toclone_from进行本地克隆,clone_remote进行远程克隆。

答案 1 :(得分:3)

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

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

[更新于2014年3月23日,收到upvote后 - 谢谢!]

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

它使用:

  1. herzult / PHP-SSH
  2. 一个ssh配置文件 - 一种简化在代码中设置身份验证凭据的技巧
  3. (注意 - 我必须快速从代码中删除一些细节以发布它。所以你需要摆弄一下,将它集成到你的应用程序/命名空间等。)

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

答案 2 :(得分:0)

这看起来很有希望:http://gitphp.org(链接断开;请参阅archived version

我认为会为你做到这一点。以下是对它的描述:

  

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

     

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

     

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

     

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