PHP Classes

PHP Git Client Class: Access files in Git repositories in pure PHP

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 63%Total: 385 All time: 6,700 This week: 33Up
Version License PHP version Categories
php-git-client 1.0.9BSD License4Files and Folders, Project Management
Description 

Author

This class can access files in Git repositories in pure PHP.

It can connect to a remote Git repository and perform several types of operations. Currently it can:

- Checkout files from a given Git repository given its HTTP URL similar to git clone command
- Retrieve the log of changes of a file since it was added to the repository
- Provides an alternative class that implements the same interface but uses the git client program to work around an issue of PHP not being able to deal with certain sequences of compressed data blocks using zlib

Picture of Manuel Lemos
  Performance   Level  
Name: Manuel Lemos is available for providing paid consulting. Contact Manuel Lemos .
Classes: 45 packages by
Country: Portugal Portugal
Age: 55
All time rank: 1
Week rank: 4 Down1 in Portugal Portugal Equal

Example

<?php
/*
 * test_git_client.php
 *
 * @(#) $Id: test_git_client.php,v 1.5 2019/09/20 07:22:06 mlemos Exp $
 *
 */

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Test for Manuel Lemos' PHP Git client class</TITLE>
</HEAD>
<BODY>
<H1 align="center">Test for Manuel Lemos' PHP Git client class</H1>
<HR>
<UL>
<?php
   
require('http.php');
    require(
'git_client.php');

   
set_time_limit(0);
   
$git = new git_client_class;

   
/* Connection timeout */
   
$git->timeout = 20;

   
/* Data transfer timeout */
   
$git->data_timeout = 60;

   
/* Output debugging information about the progress of the connection */
   
$git->debug = 0;

   
/* Output debugging information about the HTTP requests */
   
$git->http_debug = 0;

   
/* Format dubug output to display with HTML pages */
   
$git->html_debug = false;

   
$repository = 'https://github.com/manuellemos/gitclient.git';
   
$module = '';
   
$log_file = 'test_git_client.php';

    echo
'<li><h2>Validating the Git repository</h2>', "\n", '<p>Repository: ', $repository, '</p>', "\n", '<p>Module: ', $module, '</p>', "\n";
   
flush();
   
$arguments = array(
       
'Repository'=>$repository,
       
'Module'=>$module,
    );
    if(
$git->Validate($arguments, $error_code))
    {
        switch(
$error_code)
        {
            case
GIT_REPOSITORY_ERROR_NO_ERROR:
                break;
            case
GIT_REPOSITORY_ERROR_INVALID_SERVER_ADDRESS:
               
$git->error = 'It was specified an invalid Git server address';
                break;
            case
GIT_REPOSITORY_ERROR_CANNOT_CONNECT:
               
$git->error = 'Could not connect to the Git server';
                break;
            case
GIT_REPOSITORY_ERROR_INVALID_AUTHENTICATION:
               
$git->error = 'It was specified an invalid user or an incorrect password';
                break;
            case
GIT_REPOSITORY_ERROR_COMMUNICATION_FAILURE:
               
$git->error = 'There was a problem communicating with the Git server';
                break;
            case
GIT_REPOSITORY_ERROR_CANNOT_CHECKOUT:
               
$git->error = 'It was not possible to checkout the specified module from the Git server';
                break;
            case
GIT_REPOSITORY_ERROR_CANNOT_FIND_HEAD:
               
$git->error = 'The repository seems to be empty.';
                break;
            default:
               
$git->error = 'it was returned an unexpected Git repository validation error: '.$git->error;
                break;
        }
    }
    if(
strlen($git->error) == 0)
    {
        echo
'<li><h2>Connecting to the Git server</h2>', "\n", '<p>Repository: ', $repository, '</p>', "\n";
       
flush();
       
$arguments = array(
           
'Repository'=>$repository
       
);
    }
    if(
strlen($git->error) == 0
   
&& $git->Connect($arguments))
    {
        echo
'<li><h2>Checking out files from the repository '.$repository.'</h2>', "\n";
       
flush();
       
$arguments = array(
           
'Module'=>$module
       
);
        if(
$git->Checkout($arguments))
        {
           
$arguments = array(
               
'GetFileData'=>false,
               
'GetFileModes'=>false
           
);
            for(
$files = 0;; ++$files)
            {
                if(!
$git->GetNextFile($arguments, $file, $no_more_files)
                ||
$no_more_files)
                    break;
                echo
'<pre>', HtmlSpecialChars(print_r($file, 1)), '</pre>';
               
flush();
            }
            echo
'<pre>Total of '.$files.' files</pre>',"\n";
        }
        echo
'<li><h2>Getting the log of changes of file '.$log_file.'</h2>', "\n";
       
flush();
       
$arguments = array(
           
'Module'=>$module,
           
'File'=>$log_file,
// 'Revision' => 'a47e98393a5740d68ff78c34d29f68e22d38b2d0',
// 'NewerThan' => '2013-11-28 15:59:46 +0000'
       
);
        if(
$git->Log($arguments))
        {
            for(;;)
            {
               
$arguments = array();
                if(!
$git->GetNextLogFile($arguments, $file, $no_more_files)
                ||
$no_more_files)
                    break;
                echo
'<pre>', HtmlSpecialChars(print_r($file, 1)), '</pre>';
            }
        }
       
$git->Disconnect();
    }
    if(
strlen($git->error))
        echo
'<H2 align="center">Error: ', HtmlSpecialChars($git->error), '</H2>', "\n";
?>
</UL>
<HR>
</BODY>
</HTML>


  Files folder image Files (4)  
File Role Description
Plain text file git_client.php Class Git client class
Plain text file git_program_client.php Class Git program client class
Accessible without login Plain text file test_git_client.php Example Git client example script
Accessible without login Plain text file test_git_program_client.php Example Test Git program client script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
Downloadphp-git-client-2020-02-14.zip 16KB
Downloadphp-git-client-2020-02-14.tar.gz
Install with ComposerInstall with Composer
Needed packages  
Class DownloadWhy it is needed Dependency
PHP HTTP protocol client Download .zip .tar.gz Access GIT repositories via HTTP Required
 Version Control Unique User Downloads Download Rankings  
 100%
Total:385
This week:0
All time:6,700
This week:33Up
 User Ratings  
 
 All time
Utility:90%StarStarStarStarStar
Consistency:100%StarStarStarStarStarStar
Documentation:-
Examples:80%StarStarStarStarStar
Tests:-
Videos:-
Overall:63%StarStarStarStar
Rank:864