Browse Source

Testing new Git PHP library

pull/24/head
Teknikode 8 years ago
parent
commit
9126d28b18
  1. 71
      includes/PHPGit/Command.php
  2. 88
      includes/PHPGit/Configuration.php
  3. 256
      includes/PHPGit/Repository.php
  4. 16
      includes/update_profile.php

71
includes/PHPGit/Command.php

@ -0,0 +1,71 @@ @@ -0,0 +1,71 @@
<?php
class PHPGit_Command
{
/**
* @var string Real filesystem path of the repository
*/
protected $dir;
/**
* @var string Git command to run
*/
protected $commandString;
/**
* @var boolean Whether to enable debug mode or not
* When debug mode is on, commands and their output are displayed
*/
protected $debug;
/**
* Instanciate a new Git command
*
* @param string $dir real filesystem path of the repository
* @param array $options
*/
public function __construct($dir, $commandString, $debug)
{
$commandString = trim($commandString);
$this->dir = $dir;
$this->commandString = $commandString;
$this->debug = $debug;
}
public function run()
{
$commandToRun = sprintf('cd %s && %s', escapeshellarg($this->dir), $this->commandString);
if($this->debug) {
print $commandToRun."\n";
}
ob_start();
passthru($commandToRun, $returnVar);
$output = ob_get_clean();
if($this->debug) {
print $output."\n";
}
if(0 !== $returnVar) {
// Git 1.5.x returns 1 when running "git status"
if(1 === $returnVar && 0 === strncmp($this->commandString, 'git status', 10)) {
// it's ok
}
else {
throw new GitRuntimeException(sprintf(
'Command %s failed with code %s: %s',
$commandToRun,
$returnVar,
$output
), $returnVar);
}
}
return trim($output);
}
}
class GitRuntimeException extends RuntimeException {}

88
includes/PHPGit/Configuration.php

@ -0,0 +1,88 @@ @@ -0,0 +1,88 @@
<?php
/**
* Simple PHP wrapper for Git configuration
*
* @link http://github.com/ornicar/php-git-repo
* @version 1.3.0
* @author Moritz Schwoerer <moritz.schwoerer at gmail dot com>
* @license MIT License
*
* Documentation: http://github.com/ornicar/php-git-repo/blob/master/README.markdown
* Tickets: http://github.com/ornicar/php-git-repo/issues
*/
class PHPGit_Configuration
{
const USER_NAME = 'user.name';
const USER_EMAIL = 'user.email';
/**
* Holds the actual configuration
* @var array
*/
protected $configuration = array();
/**
* Holds the Git repository instance.
* @var PHPGit_Repository
*/
protected $repository;
public function __construct(PHPGit_Repository $gitRepo)
{
$this->repository = $gitRepo;
}
/**
* Get a config option
*
* @param string $configOption The config option to read
* @param mixed $fallback Value will be returned, if $configOption is not set
*
* @return string
*/
public function get($configOption, $fallback = null)
{
if (isset($this->configuration[$configOption])) {
$optionValue = $this->configuration[$configOption];
} else {
if (array_key_exists($configOption, $this->configuration)) {
$optionValue = $fallback;
}
try {
$optionValue = $this->repository->git(sprintf('config --get ' . $configOption));
$this->configuration[$configOption] = $optionValue;
} catch (GitRuntimeException $e) {
$optionValue = $fallback;
$this->configuration[$configOption] = null;
}
}
return $optionValue;
}
/**
* Set or change a *repository* config option
*
* @param string $configOption
* @param mixed $configValue
*/
public function set($configOption, $configValue)
{
$this->repository->git(sprintf('config --local %s %s', $configOption, $configValue));
unset($this->configuration[$configOption]);
}
/**
* Removes a option from local config
*
* @param string $configOption
*/
public function remove($configOption)
{
$this->repository->git(sprintf('config --local --unset %s', $configOption));
unset($this->configuration[$configOption]);
}
}

256
includes/PHPGit/Repository.php

@ -0,0 +1,256 @@ @@ -0,0 +1,256 @@
<?php
/**
* Include the command class
*/
require_once(dirname(__FILE__).'/Command.php');
require_once(dirname(__FILE__).'/Configuration.php');
/**
* Simple PHP wrapper for Git repository
*
* @link http://github.com/ornicar/php-git-repo
* @version 1.3.0
* @author Thibault Duplessis <thibault.duplessis at gmail dot com>
* @license MIT License
*
* Documentation: http://github.com/ornicar/php-git-repo/blob/master/README.markdown
* Tickets: http://github.com/ornicar/php-git-repo/issues
*/
class PHPGit_Repository
{
/**
* @var string local repository directory
*/
protected $dir;
protected $dateFormat = 'iso';
protected $logFormat = '"%H|%T|%an|%ae|%ad|%cn|%ce|%cd|%s"';
/**
* @var boolean Whether to enable debug mode or not
* When debug mode is on, commands and their output are displayed
*/
protected $debug;
/**
* @var array of options
*/
protected $options;
protected static $defaultOptions = array(
'command_class' => 'PHPGit_Command', // class used to create a command
'git_executable' => '/usr/bin/git' // path of the executable on the server
);
/**
* Instanciate a new Git repository wrapper
*
* @param string $dir real filesystem path of the repository
* @param boolean $debug
* @param array $options
*/
public function __construct($dir, $debug = false, array $options = array())
{
$this->dir = $dir;
$this->debug = $debug;
$this->options = array_merge(self::$defaultOptions, $options);
$this->checkIsValidGitRepo();
}
/**
* Helper method to get a list of commits which exist in $sourceBranch that do not yet exist in $targetBranch.
*
* @param string $targetBranch
* @param string $sourceBranch
* @return array Formatted list of commits.
*/
public function getDifferenceBetweenBranches($targetBranch, $sourceBranch)
{
$output = $this->git(sprintf('log %s..%s --date=%s --format=format:%s', $targetBranch, $sourceBranch, $this->dateFormat, $this->logFormat));
return $this->parseLogsIntoArray($output);
}
/**
* Create a new Git repository in filesystem, running "git init"
* Returns the git repository wrapper
*
* @param string $dir real filesystem path of the repository
* @param boolean $debug
* @param array $options
* @return PHPGit_Repository
**/
public static function create($dir, $debug = false, array $options = array())
{
$options = array_merge(self::$defaultOptions, $options);
$commandString = $options['git_executable'].' init';
$command = new $options['command_class']($dir, $commandString, $debug);
$command->run();
$repo = new self($dir, $debug, $options);
return $repo;
}
/**
* Clone a new Git repository in filesystem, running "git clone"
* Returns the git repository wrapper
*
* @param string $url of the repository
* @param string $dir real filesystem path of the repository
* @param boolean $debug
* @param array $options
* @return PHPGit_Repository
**/
public static function cloneUrl($url, $dir, $debug = false, array $options = array())
{
$options = array_merge(self::$defaultOptions, $options);
$commandString = $options['git_executable'].' clone '.escapeshellarg($url).' '.escapeshellarg($dir);
$command = new $options['command_class'](getcwd(), $commandString, $debug);
$command->run();
$repo = new self($dir, $debug, $options);
return $repo;
}
/**
* Get the configuration for current
* @return PHPGit_Configuration
*/
public function getConfiguration()
{
return new PHPGit_Configuration($this);
}
/**
* Get branches list
*
* @return array list of branches names
*/
public function getBranches($flags='')
{
return array_filter(preg_replace('/[\s\*]/', '', explode("\n", $this->git('branch '.$flags))));
}
/**
* Get current branch
*
* @return string the current branch name
*/
public function getCurrentBranch()
{
$output = $this->git('branch');
foreach(explode("\n", $this->git('branch')) as $branchLine) {
if('*' === $branchLine{0}) {
return substr($branchLine, 2);
}
}
}
/**
* Tell if a branch exists
*
* @return boolean true if the branch exists, false otherwise
*/
public function hasBranch($branchName)
{
return in_array($branchName, $this->getBranches());
}
/**
* Get tags list
*
* @return array list of tag names
*/
public function getTags()
{
$output = $this->git('tag');
return $output ? array_filter(explode("\n", $output)) : array();
}
/**
* Return the result of `git log` formatted in a PHP array
*
* @return array list of commits and their properties
**/
public function getCommits($nbCommits = 10)
{
$output = $this->git(sprintf('log -n %d --date=%s --format=format:%s', $nbCommits, $this->dateFormat, $this->logFormat));
return $this->parseLogsIntoArray($output);
}
/**
* Convert a formatted log string into an array
* @param string $logOutput The output from a `git log` command formated using $this->logFormat
*/
private function parseLogsIntoArray($logOutput)
{
$commits = array();
foreach(explode("\n", $logOutput) as $line) {
$infos = explode('|', $line);
$commits[] = array(
'id' => $infos[0],
'tree' => $infos[1],
'author' => array(
'name' => $infos[2],
'email' => $infos[3]
),
'authored_date' => $infos[4],
'commiter' => array(
'name' => $infos[5],
'email' => $infos[6]
),
'committed_date' => $infos[7],
'message' => $infos[8]
);
}
return $commits;
}
/**
* Check if a directory is a valid Git repository
*/
public function checkIsValidGitRepo()
{
if(!file_exists($this->dir.'/.git/HEAD')) {
throw new InvalidGitRepositoryDirectoryException($this->dir.' is not a valid Git repository');
}
}
/**
* Run any git command, like "status" or "checkout -b mybranch origin/mybranch"
*
* @throws RuntimeException
* @param string $commandString
* @return string $output
*/
public function git($commandString)
{
// clean commands that begin with "git "
$commandString = preg_replace('/^git\s/', '', $commandString);
$commandString = $this->options['git_executable'].' '.$commandString;
$command = new $this->options['command_class']($this->dir, $commandString, $this->debug);
return $command->run();
}
/**
* Get the repository directory
*
* @return string the repository directory
*/
public function getDir()
{
return $this->dir;
}
}
class InvalidGitRepositoryDirectoryException extends InvalidArgumentException
{
}

16
includes/update_profile.php

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
<?php
require_once('config.php');
require_once('Git.php');
require_once('PHPGit/Repository.php');
//initialize php variables used in the form
$current_password = "";
@ -129,14 +129,12 @@ if(isset($_POST)) @@ -129,14 +129,12 @@ if(isset($_POST))
}
// Add the user's keys to his git account
/*
if ($public_key != $user->public_key)
{
$Git = new Git();
$Git->windows_mode();
$repo = $Git->open($CONF['git_repo_path'][0].'gitolite-admin\\');
$repo->setenv("HOME", "/home/git");
$repo->pull('origin', 'master');
$repo = new PHPGit_Repository($CONF['git_repo_path'][0].'gitolite-admin\\', true, array('git_executable' => $CONF['git_path']));
$repo->git('pull origin master');
if (is_dir($CONF['git_repo_path'][0].'gitolite-admin\\keydir\\u\\'.$user->username))
{
@ -163,11 +161,11 @@ if(isset($_POST)) @@ -163,11 +161,11 @@ if(isset($_POST))
fclose($fileHandle);
$index++;
}
$repo->add('.');
$repo->commit('Modified keys for '.$user->username);
$repo->push('origin', 'master');
$repo->git('add .');
$repo->git('commit -m "Modified keys for '.$user->username.'"');
$repo->git('push origin master');
}
*/
/*
if ($minecraft != $user->minecraft_user)
{

Loading…
Cancel
Save