开发者

Checkout of git repository in pure PHP

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

-replicate/clone a remote git repository

-"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.

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


You can try with

  • https://github.com/teqneers/PHP-Stream-Wrapper-for-Git

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.

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


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

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;


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜