2016-05-03 11 views
0

git pullgit pushのコマンドをPHPに渡すことは可能ですか?これによりチャネル `git on the server`はPHPを経由して呼び出します

私はgit originとしてhttps://example.com/projectname?credentials=xxxを設定します、と私はpullまたはpushを実行するとき、彼らは大丈夫だ場合、その後、PHPスクリプトは、資格情報を分析し、Gitのレポへのgitにより送信された元のコマンドを送信する意味フォルダに移動してから、ユーザーに応答が返されますか?

これを達成するためにgit-http-backendを使用することができますか、または他の方法がありますか?

+0

コマンドではないが、端末のようなPHPの仕事を作る方法を探してください。 'shell_exec'コマンドは、 – Autor69

+0

が[git alias](https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases)の仕事のように聞こえるはずです。 – DevDonkey

答えて

0

私は最終的にそれがプル関連のコマンドのために働くようになったが、プッシュのため

static $basegitdir = '/path/to/where/bare/git/repos/live'; 

/** 
* A PHP wrapper for git-http-backend 
*/ 
static function httpBackend() { 
    $packagename = 'gitrepo'; 
    $gitdir = self::$basegitdir . "{$packagename}.git"; 
    $gitcoredir = '/usr/lib/git-core'; // True for Ubuntu systems 
    try { 
     // In my implementation, func_get_args will return e.g. ['info', 'refs'], basically, all of the stuff git appends to the remote url 
     $arguments = func_get_args(); 
     // Remove the first argument 
     array_unshift($arguments, "{$packagename}.git"); 
     // Will resolve to something like '/repo.git/info/refs' 
     $path = '/' . implode('/', $arguments); 
     $service = filter_input(INPUT_GET, 'service'); 
     if ($service) { 
      $service = "?service={$service}"; 
     } 
     $res = self::proc_open("{$gitcoredir}/git-http-backend", [], $gitdir, true, [ 
      'PATH' => filter_input(INPUT_SERVER, 'PATH'), 
      'GIT_PROJECT_ROOT' => self::$basegitdir, 
      'GIT_HTTP_EXPORT_ALL' => 1, 
      // PATH_INFO <MUST> evaluate to something along the lines of '/repo.git/info/refs'. Note, basegitdir has been 
      // dropped from the beginning of the path. This is because Git joins GIT_PROJECT_ROOT and PATH_INFO. Also note the lack of a trailing slash after refs 
      'PATH_INFO' => $path, 
      'REMOTE_ADDR' => filter_input(INPUT_SERVER, 'REMOTE_ADDR'), 
      // QUERY_STRING MUST evaluate to something along the lines of '/repo.git/info/refs/?service=service', note the trailing slash after refs 
      'QUERY_STRING' => "{$path}/{$service}", 
      'REQUEST_METHOD' => filter_input(INPUT_SERVER, 'REQUEST_METHOD'), 
     ]); 
    } catch (\Exception $ex) { 
     // Log "Exception: {$ex->getMessage()}"; 
     exit; 
    } 
    $resbits = explode("\n", $res); 
    foreach ($resbits as $index => $header) { 
     if ($header && strpos($header, ':') !== false) { 
      // Headers 
      header($header); 
      unset($resbits[$index]); 
     } else { 
      // First blank line is the space between the headers and the start of the response from Git 
      break; 
     } 
    } 
    echo ltrim(implode("\n", $resbits)); 
    exit; 
} 

/** 
* A wrapper for the proc_open functions 
* @param string $command The command to execute 
* @param array $arguments Responses to bash 
* @param string $cwd The current working directory 
* @param boolean $blocking False to make requests asynchronous 
* @param array $env Environment variables 
* @return string The output 
* @throws \Exception 
*/ 
static function proc_open($command, array $arguments = [], $cwd = rootdir, $blocking = true, array $env = null) { 
    $pipes = []; 
    $descriptorspec = array(
     array('pipe', 'r'), // STDIN 
     array('pipe', 'w'), // STDOUT 
     array('pipe', 'w'), // STDERR 
    ); 
    $process = proc_open(trim($command), $descriptorspec, $pipes, $cwd, $env); 
    stream_set_blocking($pipes[1], (int) $blocking); 
    stream_set_blocking($pipes[2], (int) 1); 
    foreach ($arguments as $arg) { 
     // Write each of the supplied arguments to STDIN and ensure that it finishes with one trailing 
     fwrite($pipes[0], (preg_match("/\n(:?\s+)?$/", $arg) ? $arg : "{$arg}\n")); 
    } 
    $response = stream_get_contents($pipes[1]); 
    $error = stream_get_contents($pipes[2]); 
    if ($error) { 
     throw new \Exception($error); 
    } 
    // Make sure that each pipe is closed to prevent a lockout 
    foreach ($pipes as $pipe) { 
     fclose($pipe); 
    } 
    proc_close($process); 
    return $response; 
} 
関連する問題