Permalink
Browse files

Git compatibility fixes

* Fixed issue where trees written by git would be unreadable (due to the mode string)
* Fixed issue where git and Gittern would sort trees differently
* Fixed issue where Gittern writes mode strings differently than git
* Partially fixed issue #4, Gittern no longer attempts to write objects that already exist as loose objects in the repo
  • Loading branch information...
magnusnordlander committed May 9, 2012
1 parent 25d7f92 commit 53c3200e2b583fd9af37f9fcb125ab779ce987fc
@@ -23,7 +23,7 @@ public function getIntegerMode()
public function getOctalModeString()
{
- return str_pad(decoct($this->mode), 6, "0", STR_PAD_LEFT);
+ return decoct($this->mode);
}
public function setName($name)
@@ -38,7 +38,7 @@ public function getSha()
public function addNode(Node\BaseNode $node)
{
$this->nodes[$node->getName()] = $node;
- ksort($this->nodes);
+ uksort($this->nodes, 'strcmp');
}
/**
@@ -35,8 +35,7 @@ public function hydrate(RawObject $raw_object)
while ($reader->available())
{
- $mode = intval($reader->readString8(6), 8);
- assert($reader->readString8(1) == ' ');
+ $mode = intval($this->readModeString($reader), 8);
$name = $this->readName($reader);
$sha = $reader->readHHex(20);
$is_tree = (bool)($mode & 040000);
@@ -61,6 +60,22 @@ public function hydrate(RawObject $raw_object)
return $tree;
}
+ protected function readModeString(StringReader $reader)
+ {
+ $mode_string = '';
+ do {
+ $char = $reader->read(1);
+ if ($char != " ")
+ {
+ assert(is_numeric($char));

This comment has been minimized.

Show comment
Hide comment
@morticue

morticue May 9, 2012

I think something other than an assert() check should be used here.

@morticue

morticue May 9, 2012

I think something other than an assert() check should be used here.

+ $mode_string .= $char;
+ }
+
+ } while($char != " ");
+
+ return $mode_string;
+ }
+
protected function readName(StringReader $reader)
{
$name = '';
@@ -180,7 +180,10 @@ public function putRawObject(RawObject $raw_object)
$data = gzcompress($raw_object->getType().' '.$raw_object->getLength()."\0".$raw_object->getData(), 4);
- $this->writeFileRelative('objects/'.$first.'/'.$last, $data);
+ if (!$this->isFileRelative('objects/'.$first.'/'.$last))
+ {
+ $this->writeFileRelative('objects/'.$first.'/'.$last, $data);
+ }
}
protected function isFileRelative($relative_path)
@@ -22,10 +22,10 @@ public function testCanGetAndSetMode()
$this->assertEquals("100644", $this->node_mock->getOctalModeString());
}
- public function testCanPadModeToSixChars()
+ public function testWontPadMode()
{
$this->node_mock->setIntegerMode(040000);
- $this->assertEquals("040000", $this->node_mock->getOctalModeString());
+ $this->assertSame("40000", $this->node_mock->getOctalModeString());
}
public function testCanGetAndSetName()
@@ -67,4 +67,11 @@ public function testSortsNodes()
$this->assertEquals(array($first_node, $new_node, $this->node_mock), $this->tree->getNodes());
}
+
+ public function testSortsNodesWithStrcmp()
+ {
+ $new_node = M::mock('Gittern\Entity\GitObject\Node\BaseNode', array('getName' => '1337'));
+ $this->tree->addNode($new_node);
+ $this->assertEquals(array($new_node, $this->node_mock), $this->tree->getNodes());
+ }
}
@@ -14,7 +14,7 @@ public function testTreeHydratorHappyPath()
{
$sha = "deadbeefcafebabefacebadc0ffeebadf00dcafe";
$blob_line = sprintf("%s %s\0%s", "100644", 'testblob.md', pack("H*", "deadbeefcafebabefacebadc0ffeebadf00dface"));
- $tree_line = sprintf("%s %s\0%s", "040000", 'testtree', pack("H*", "deadbeefcafebabefacebadc0ffeebadf00dbeef"));
+ $tree_line = sprintf("%s %s\0%s", "40000", 'testtree', pack("H*", "deadbeefcafebabefacebadc0ffeebadf00dbeef"));
$raw_object = M::mock('Gittern\Transport\RawObject', array('getSha' => $sha, 'getData' => $blob_line.$tree_line));
@@ -25,12 +25,12 @@ public function testTreeHydratorHappyPath()
$this->assertEquals($sha, $tree->getSha());
$nodes = $tree->getNodes();
$this->assertEquals('testblob.md', $nodes[0]->getName());
- $this->assertEquals('100644', $nodes[0]->getOctalModeString());
+ $this->assertSame('100644', $nodes[0]->getOctalModeString());
$this->assertInstanceOf('Gittern\Entity\GitObject\Node\BlobNode', $nodes[0]);
$this->assertEquals("deadbeefcafebabefacebadc0ffeebadf00dface", $nodes[0]->getRelatedObject()->getSha());
$this->assertEquals('testtree', $nodes[1]->getName());
- $this->assertEquals('040000', $nodes[1]->getOctalModeString());
+ $this->assertSame('40000', $nodes[1]->getOctalModeString());
$this->assertInstanceOf('Gittern\Entity\GitObject\Node\TreeNode', $nodes[1]);
$this->assertEquals("deadbeefcafebabefacebadc0ffeebadf00dbeef", $nodes[1]->getRelatedObject()->getSha());
}

0 comments on commit 53c3200

Please sign in to comment.