Skip to content

use resolved objects for Blob and Tree #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/bin/*
!bin/.gitkeep
/vendor
/composer.lock
/phpunit.xml
Empty file added bin/.gitkeep
Empty file.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"symfony/process": "~2.4"
},
"require-dev": {
"psr/log": "~1.0"
"psr/log": "~1.0",
"phpunit/phpunit": "~4.5"
},
"suggest": {
"psr/log": "Add some log"
Expand All @@ -38,5 +39,8 @@
"branch-alias": {
"dev-master": "1.0-dev"
}
},
"config": {
"bin-dir": "bin"
}
}
67 changes: 25 additions & 42 deletions src/Gitonomy/Git/Blob.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,89 +20,72 @@
class Blob
{
/**
* @var Repository
* @var BlobObject
*/
protected $repository;
protected $object;

/**
* @var string
*/
protected $hash;
protected $path;

/**
* @var string
*/
protected $content;

/**
* @var string
* @param Repository $repository Repository where the blob is located
* @param string $path Path of the blob
*/
protected $mimetype;
public function __construct(BlobObject $object, $path)
{
$this->object = $object;
$this->path = $path;
}

/**
* @param Repository $repository Repository where the blob is located
* @param string $hash Hash of the blob
* Returns path to the blob.
*
* @return string
*/
public function __construct(Repository $repository, $hash)
public function getPath()
{
$this->repository = $repository;
$this->hash = $hash;
return $this->path;
}

/**
* @return string
* @see Blob::getHash
*/
public function getHash()
{
return $this->hash;
return $this->object->getHash();
}

/**
* Returns content of the blob.
*
* @throws ProcessException Error occurred while getting content of blob
* @see Blob::getContent
*/
public function getContent()
{
if (null === $this->content) {
$this->content = $this->repository->run('cat-file', array('-p', $this->hash));
}

return $this->content;
return $this->object->getContent();
}

/**
* Determine the mimetype of the blob.
*
* @return string A mimetype
* @see Blob::getMimetype
*/
public function getMimetype()
{
if (null === $this->mimetype) {
$finfo = new \finfo(FILEINFO_MIME);
$this->mimetype = $finfo->buffer($this->getContent());
}

return $this->mimetype;
return $this->object->getMimetype();
}

/**
* Determines if file is binary.
*
* @return boolean
* @see Blob::isBinary
*/
public function isBinary()
{
return !$this->isText();
return $this->object->isBinary();
}

/**
* Determines if file is text.
*
* @return boolean
* @see Blob::isText
*/
public function isText()
{
return (bool) preg_match('#^text/|^application/xml#', $this->getMimetype());
return $this->object->isText();
}
}
116 changes: 116 additions & 0 deletions src/Gitonomy/Git/BlobObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

/**
* This file is part of Gitonomy.
*
* (c) Alexandre Salomé <[email protected]>
* (c) Julien DIDIER <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Gitonomy\Git;

/**
* Representation of a Blob commit.
*
* @author Alexandre Salomé <[email protected]>
*/
class BlobObject
{
/**
* @var Repository
*/
protected $repository;

/**
* @var string
*/
protected $hash;

/**
* @var string
*/
protected $content;

/**
* @var string
*/
protected $mimetype;

/**
* @param Repository $repository Repository where the blob is located
* @param string $hash Hash of the blob
*/
public function __construct(Repository $repository, $hash)
{
$this->repository = $repository;
$this->hash = $hash;
}

/**
* @return Repository
*/
public function getRepository()
{
return $this->repository;
}

/**
* @return string
*/
public function getHash()
{
return $this->hash;
}

/**
* Returns content of the blob.
*
* @throws ProcessException Error occurred while getting content of blob
*/
public function getContent()
{
if (null === $this->content) {
$this->content = $this->repository->run('cat-file', array('-p', $this->hash));
}

return $this->content;
}

/**
* Determine the mimetype of the blob.
*
* @return string A mimetype
*/
public function getMimetype()
{
if (null === $this->mimetype) {
$finfo = new \finfo(FILEINFO_MIME);
$this->mimetype = $finfo->buffer($this->getContent());
}

return $this->mimetype;
}

/**
* Determines if file is binary.
*
* @return boolean
*/
public function isBinary()
{
return !$this->isText();
}

/**
* Determines if file is text.
*
* @return boolean
*/
public function isText()
{
return (bool) preg_match('#^text/|^application/xml#', $this->getMimetype());
}
}
2 changes: 1 addition & 1 deletion src/Gitonomy/Git/Commit.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ private function getData($name)
}

if ($name === 'tree') {
$this->data['tree'] = $this->repository->getTree($this->getData('treeHash'));
$this->data['tree'] = $this->repository->getTree($this->getData('treeHash'), '');

return $this->data['tree'];
}
Expand Down
71 changes: 65 additions & 6 deletions src/Gitonomy/Git/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,37 +331,96 @@ public function getCommit($hash)
}

/**
* Instanciates a tree object or fetches one from the cache.
* Fetches a Tree from repository.
*
* @param string $hash A tree hash, with a length of 40
* @param string $hash A tree hash, with a length of 40
* @param string|null $path A path for the tree
*
* @return Tree
*/
public function getTree($hash)
public function getTree($hash, $path = null)
{
if ($path === null) {
return $this->getTreeObject($hash);
}

if (! isset($this->objects[$hash.':'.$path])) {
$this->objects[$hash.':'.$path] = new Tree($this->getTreeObject($hash), $path);
}

return $this->objects[$hash.':'.$path];
}

/**
* Instanciates a tree object or fetches one from the cache.
*
* @param string $hash A tree hash, with a length of 40
* @param string|null $path A path for the tree
*
* @return TreeObject
*/
public function getTreeObject($hash)
{
if (! isset($this->objects[$hash])) {
$this->objects[$hash] = new Tree($this, $hash);
$this->objects[$hash] = new TreeObject($this, $hash);
}

return $this->objects[$hash];
}

/**
* Fetches a Blob from repository.
*
* @param string $hash A blob hash, with a length of 40
* @param string|null $path A path for the blob
*
* @return Blob
*/
public function getBlob($hash, $path = null)
{
if ($path === null) {
return $this->getBlobObject($hash);
}

if (! isset($this->objects[$hash.':'.$path])) {
$this->objects[$hash.':'.$path] = new Blob($this->getBlobObject($hash), $path);
}

return $this->objects[$hash.':'.$path];
}

/**
* Instanciates a blob object or fetches one from the cache.
*
* @param string $hash A blob hash, with a length of 40
*
* @return Blob
*/
public function getBlob($hash)
public function getBlobObject($hash, $path = null)
{
if (! isset($this->objects[$hash])) {
$this->objects[$hash] = new Blob($this, $hash);
$this->objects[$hash] = new BlobObject($this, $hash);
}

return $this->objects[$hash];
}


public function getResolved($entry, $path)
{
if ($entry instanceof BlobObject) {
return $this->getBlob($entry->getHash(), $path);
} elseif ($entry instanceof TreeObject) {
return $this->getTree($entry->getHash(), $path);
}

throw new \RuntimeException(sprintf(
'Unable to resolve object of type "%s" (path: %s).',
get_class($entry),
$path
));
}

public function getBlame($revision, $file, $lineRange = null)
{
if (is_string($revision)) {
Expand Down
Loading