Browse Source

SHA1 hash variables, fixes, and pack finding

master
Sven Slootweg 10 years ago
parent
commit
6480a4b0bb
  1. 4
      public_html/libgit/class.blob.php
  2. 4
      public_html/libgit/class.commit.php
  3. 4
      public_html/libgit/class.object.php
  4. 9
      public_html/libgit/class.pack.php
  5. 64
      public_html/libgit/class.repository.php
  6. 4
      public_html/libgit/class.tag.php
  7. 4
      public_html/libgit/class.tree.php
  8. 21
      public_html/test.php

4
public_html/libgit/class.blob.php

@ -1,8 +1,8 @@
<?php
class GitBlob extends GitObject
{
function __construct($repo, $headerdata, $data)
function __construct($repo, $headerdata, $data, $sha)
{
parent::__construct($repo, $headerdata, $data);
parent::__construct($repo, $headerdata, $data, $sha);
}
}

4
public_html/libgit/class.commit.php

@ -7,9 +7,9 @@ class GitCommit extends GitObject
public $message = "";
public $parents = array();
function __construct($repo, $headerdata, $data)
function __construct($repo, $headerdata, $data, $sha)
{
parent::__construct($repo, $headerdata, $data);
parent::__construct($repo, $headerdata, $data, $sha);
$lines = explode("\n", $data);
$message_parts = array();

4
public_html/libgit/class.object.php

@ -4,11 +4,13 @@ class GitObject
public $rawdata = "";
public $size = 0;
public $repo = null;
public $sha = "";
function __construct($repo, $headerdata, $data)
function __construct($repo, $headerdata, $data, $sha)
{
$this->repo = $repo;
$this->size = (int)$headerdata;
$this->rawdata = $data;
$this->sha = $sha;
}
}

9
public_html/libgit/class.pack.php

@ -46,7 +46,14 @@ class GitPack
case OBJ_TAG:
// this is a compressed object
$data = fread($file, $size);
return $this->repo->CreateObject(gzuncompress($data), $type, $size);
$uncompressed = gzuncompress($data);
if($uncompressed === false)
{
$uncompressed = $data;
}
return $this->repo->CreateObject($uncompressed, $type, $size);
break;
case OBJ_OFS_DELTA:
case OBJ_REF_DELTA:

64
public_html/libgit/class.repository.php

@ -10,20 +10,25 @@ class GitRepository
function GetObjectRaw($sha)
{
return gzuncompress(
file_get_contents(
sprintf("{$this->path}/objects/%s/%s",
substr($sha, 0, 2),
substr($sha, 2)
)));
$filename = sprintf("{$this->path}/objects/%s/%s", substr($sha, 0, 2), substr($sha, 2));
return gzuncompress(file_get_contents($filename));
}
function GetObject($sha)
{
return $this->CreateObject($this->GetObjectRaw($sha));
$filename = sprintf("{$this->path}/objects/%s/%s", substr($sha, 0, 2), substr($sha, 2));
if(file_exists($filename))
{
return $this->CreateObject($this->GetObjectRaw($sha), null, null, $sha);
}
else
{
return $this->FindPackedObject($sha);
}
}
function CreateObject($data, $type = null, $size = null)
function CreateObject($data, $type = null, $size = null, $sha)
{
if($type == null && $size == null)
{
@ -64,19 +69,19 @@ class GitRepository
switch($type)
{
case "commit":
return new GitCommit($this, $headerdata, $data);
return new GitCommit($this, $headerdata, $data, $sha);
break;
case "blob":
return new GitBlob($this, $headerdata, $data);
return new GitBlob($this, $headerdata, $data, $sha);
break;
case "tree":
return new GitTree($this, $headerdata, $data);
return new GitTree($this, $headerdata, $data, $sha);
break;
case "tag":
return new GitTag($this, $headerdata, $data);
return new GitTag($this, $headerdata, $data, $sha);
break;
default:
return new GitObject($this, $headerdata, $data);
return new GitObject($this, $headerdata, $data, $sha);
break;
}
}
@ -141,6 +146,22 @@ class GitRepository
return $tags;
}
function GetPacks()
{
$packs = array();
foreach(scandir("{$this->path}/objects/pack/") as $pack)
{
if($pack != "." && $pack != ".." && substr($pack, strlen($pack) - 5) != ".pack")
{
$pack_name = substr($pack, 0, strlen($pack) - 4);
$packs[] = $pack_name;
}
}
return $packs;
}
function GetObjectForPath($origin, $path)
{
$path_parts = explode("/", $path);
@ -188,4 +209,21 @@ class GitRepository
throw new GitInvalidOriginException("You can only use a GitTree hash as origin.");
}
}
function FindPackedObject($sha)
{
foreach($this->GetPacks() as $item)
{
$pack = new GitPack($this, $item);
try
{
return $pack->UnpackObject($sha);
}
catch (GitObjectNotFoundException $e)
{
// Silently ignore.
}
}
}
}

4
public_html/libgit/class.tag.php

@ -7,9 +7,9 @@ class GitTag extends GitObject
public $tag = "";
public $message = "";
function __construct($repo, $headerdata, $data)
function __construct($repo, $headerdata, $data, $sha)
{
parent::__construct($repo, $headerdata, $data);
parent::__construct($repo, $headerdata, $data, $sha);
$lines = explode("\n", $data);
$message_parts = array();

4
public_html/libgit/class.tree.php

@ -3,9 +3,9 @@ class GitTree extends GitObject
{
public $elements = array();
function __construct($repo, $headerdata, $data)
function __construct($repo, $headerdata, $data, $sha)
{
parent::__construct($repo, $headerdata, $data);
parent::__construct($repo, $headerdata, $data, $sha);
$parsing_sha = false;
$sha_bytecount = 0;

21
public_html/test.php

@ -2,7 +2,26 @@
require("libgit/base.php");
$repo = new GitRepository("/home/occupy/testrepo.git");
pretty_dump($repo->GetTags());
//pretty_dump($repo->GetTags());
if(!empty($_GET['sha']))
{
$sha = $_GET['sha'];
}
elseif(!empty($_GET['branch']))
{
$sha = $repo->GetBranch($_GET['branch'])->sha;
}
elseif(!empty($_GET['tag']))
{
$sha = $repo->GetTag($_GET['tag'])->sha;
}
else
{
die();
}
pretty_dump($repo->GetObject($sha));
/*$pack = new GitPack($repo, "pack-8503a2b8cf6e60831dd012afd4d486eb1eddfef8");
pretty_dump($pack->UnpackObject("a6269a2ffd269289d7d026818511fab88718feff"));*/

Loading…
Cancel
Save