Skip to content

Commit

Permalink
Add WP_Git_Cached_Index that stores objects in a local directory
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Dec 29, 2024
1 parent 8b764c1 commit fdf9393
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"step": "activatePlugin",
"pluginPath": "z-data-liberation-static-files-editor/plugin.php"
},
{
"step": "activatePlugin",
"pluginPath": "gutenberg/gutenberg.php"
},
{
"step": "runPHP",
"code": "<?php require_once '/wordpress/wp-load.php'; wp_insert_post(array('post_title' => 'My Notes', 'post_status' => 'publish', 'post_type' => 'page'));"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ bun --inspect ../cli/src/cli.ts \
--mount=../data-liberation-static-files-editor:/wordpress/wp-content/plugins/z-data-liberation-static-files-editor \
--mount=../data-liberation-markdown:/wordpress/wp-content/plugins/z-data-liberation-markdown \
--mount=../data-liberation:/wordpress/wp-content/plugins/data-liberation \
--mount=../../../../gutenberg:/wordpress/wp-content/plugins/gutenberg \
--mount=./my-notes/workdir:/wordpress/wp-content/uploads/static-pages \
--blueprint=./blueprint.json
1 change: 1 addition & 0 deletions packages/playground/data-liberation/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
require_once __DIR__ . '/src/git/WP_Git_Client.php';
require_once __DIR__ . '/src/git/WP_Git_Pack_Processor.php';
require_once __DIR__ . '/src/git/WP_Git_Pack_Index.php';
require_once __DIR__ . '/src/git/WP_Git_Cached_Index.php';
require_once __DIR__ . '/src/git/WP_Git_Filesystem.php';

require_once __DIR__ . '/src/WP_Data_Liberation_HTML_Processor.php';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

use WordPress\Filesystem\WP_Abstract_Filesystem;

class WP_Git_Cached_Index {

private $fs;

public function __construct(
WP_Abstract_Filesystem $fs
) {
$this->fs = $fs;
if(!$this->fs->is_dir('objects')) {
$this->fs->mkdir('objects');
}
if(!$this->fs->is_dir('refs')) {
$this->fs->mkdir('refs');
}
if(!$this->fs->is_dir('refs/heads')) {
$this->fs->mkdir('refs/heads');
}
}

/**
* @TODO: Streaming read
*/
public function get_object($oid) {
$contents = $this->fs->read_file($this->get_object_path($oid));
return WP_Git_Pack_Processor::inflate($contents);
}

public function set_head($head, $oid) {
if($head !== 'HEAD' && !str_starts_with($head, 'refs/heads/')) {
_doing_it_wrong(__METHOD__, 'Invalid head: ' . $head);
return false;
}
return $this->fs->put_contents($head, $oid);
}

public function get_head($head='HEAD') {
if($head === 'HEAD') {
$head_contents = $this->fs->read_file('HEAD');
if(strpos($head_contents, 'ref: ') !== 0) {
return null;
}
$head = trim(substr($head_contents, 5));
} else if(!str_starts_with($head, 'refs/heads/')) {
_doing_it_wrong(__METHOD__, 'Invalid head: ' . $head);
return false;
}
return trim($this->fs->read_file($head));
}

public function add_object($type, $content) {
$oid = sha1(self::wrap_git_object($type, $content));
$oid_path = $this->get_object_path($oid);
$oid_dir = dirname($oid_path);
if(!$this->fs->is_dir($oid_dir)) {
$this->fs->mkdir($oid_dir, true);
}
$success = $this->fs->put_contents(
$this->get_object_path($oid),
WP_Git_Pack_Processor::deflate($content)
);
if(!$success) {
return false;
}
return $oid;
}

private function get_object_path($oid) {
return 'objects/' . $oid[0] . $oid[1] . '/' . substr($oid, 2);
}

static private function wrap_git_object($type, $object) {
$length = strlen($object);
$type_name = WP_Git_Pack_Processor::OBJECT_NAMES[$type];
return "$type_name $length\x00" . $object;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,16 @@ static public function encode(array $objects): string {
return $pack . $packSha;
}

static private function deflate(string $content): string {
static public function deflate(string $content): string {
$context = deflate_init(ZLIB_ENCODING_DEFLATE, ['level' => 9]);
return deflate_add($context, $content, ZLIB_FINISH);
}

static public function inflate(string $content): string {
$context = inflate_init(ZLIB_ENCODING_DEFLATE);
return inflate_add($context, $content, ZLIB_FINISH);
}

static private function object_header(int $type, int $size): string {
// First byte: type in bits 4-6, size bits 0-3
$firstByte = $size & 0b1111;
Expand Down Expand Up @@ -101,7 +106,7 @@ static private function object_header(int $type, int $size): string {
* }
* }
*/
static private function encode_tree_bytes($tree) {
static public function encode_tree_bytes($tree) {
$tree_bytes = '';
foreach ($tree as $value) {
$tree_bytes .= $value['mode'] . " " . $value['name'] . "\0" . hex2bin($value['sha1']);
Expand Down

0 comments on commit fdf9393

Please sign in to comment.