Browse Source

php-libgit Repository/Object/Commit/Actor classes added

master
Sven Slootweg 10 years ago
parent
commit
6f9077b618
  1. 6
      public_html/libgit/base.php
  2. 25
      public_html/libgit/class.actor.php
  3. 59
      public_html/libgit/class.commit.php
  4. 12
      public_html/libgit/class.object.php
  5. 74
      public_html/libgit/class.repository.php
  6. 5
      public_html/test.php

6
public_html/libgit/base.php

@ -0,0 +1,6 @@
<?php
require(dirname(__FILE__) . "/class.repository.php");
require(dirname(__FILE__) . "/class.object.php");
require(dirname(__FILE__) . "/class.commit.php");
require(dirname(__FILE__) . "/class.actor.php");
?>

25
public_html/libgit/class.actor.php

@ -0,0 +1,25 @@
<?php
class GitActor
{
public $timestamp = 0;
public $name = "";
function __construct($stamp)
{
$parts = explode(" ", $stamp);
$parts_count = count($parts);
$name_parts = array();
for($i = 0; $i < $parts_count - 2; $i++)
{
$name_parts[] = $parts[$i];
}
$this->name = implode(" ", $name_parts);
$timestamp = $parts[$parts_count - 2] . " " . $parts[$parts_count - 1];
date_default_timezone_set("GMT");
$this->timestamp = strtotime($parts[$parts_count - 1], $parts[$parts_count - 2]);
}
}

59
public_html/libgit/class.commit.php

@ -0,0 +1,59 @@
<?php
class GitCommit extends GitObject
{
public $tree = "";
public $author = "";
public $committer = "";
public $message = "";
public $parents = array();
function __construct($headerdata, $data)
{
parent::__construct($headerdata, $data);
$lines = explode("\n", $data);
$message_parts = array();
$parsing_message = false;
foreach($lines as $line)
{
$line = trim($line);
if(!empty($line))
{
$line = trim($line);
if($parsing_message === false)
{
list($key, $value) = explode(" ", $line, 2);
switch($key)
{
case "tree":
$this->tree = $value;
break;
case "parent":
$this->parents[] = $value;
break;
case "committer":
$this->committer = new GitActor($value);
break;
case "author":
$this->author = new GitActor($value);
break;
}
}
else
{
$message_parts[] = $line;
}
}
else
{
$parsing_message = true;
}
}
$this->message = implode("\n", $message_parts);
}
}

12
public_html/libgit/class.object.php

@ -0,0 +1,12 @@
<?php
class GitObject
{
public $rawdata = "";
public $size = 0;
function __construct($headerdata, $data)
{
$this->size = (int)$headerdata;
$this->rawdata = $data;
}
}

74
public_html/libgit/class.repository.php

@ -0,0 +1,74 @@
<?php
function pretty_dump($input)
{
ob_start();
var_dump($input);
$output = ob_get_contents();
ob_end_clean();
while(preg_match("/^[ ]*[ ]/m", $output) == 1)
{
$output = preg_replace("/^([ ]*)[ ]/m", "$1&nbsp;&nbsp;&nbsp;", $output);
}
$output = nl2br($output);
echo($output);
}
class GitRepository
{
public $path = "";
function __construct($path)
{
$this->path = $path;
}
function GetObjectRaw($sha)
{
return gzuncompress(
file_get_contents(
sprintf("{$this->path}/objects/%s/%s",
substr($sha, 0, 2),
substr($sha, 2)
)));
}
function GetObject($sha)
{
list($header, $data) = explode("\0", $this->GetObjectRaw($sha), 2);
if(strpos($header, " ") !== false)
{
list($type, $headerdata) = explode(" ", $header, 2);
}
else
{
$type = $header;
$headerdata = "";
}
switch($type)
{
case "commit":
return new GitCommit($headerdata, $data);
break;
case "blob":
return new GitBlob($headerdata, $data);
break;
case "tree":
return new GitTree($headerdata, $data);
break;
case "tag":
return new GitTag($headerdata, $data);
break;
default:
return new GitObject($headerdata, $data);
break;
}
}
}

5
public_html/test.php

@ -0,0 +1,5 @@
<?php
require("libgit/base.php");
$s = new GitRepository("/home/occupy/testrepo.git");
pretty_dump($s->GetObject("54e03e490b1bee1c154c3545bf258cab0629ee02"));
Loading…
Cancel
Save