PHP code example of wp-php-toolkit / git
1. Go to this page and download the library: Download wp-php-toolkit/git library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wp-php-toolkit / git example snippets
WordPress\Filesystem\InMemoryFilesystem;
use WordPress\Git\GitRepository;
$repo = new GitRepository( InMemoryFilesystem::create() );
$oid = $repo->commit( array(
'updates' => array(
'README.md' => "# My Project\n",
'src/hello-world.php' => ' echo "Hello!";',
),
) );
echo "commit: {$oid}\n";
echo "HEAD: " . $repo->get_branch_tip( 'HEAD' ) . "\n";
echo "README: " . $repo->read_object_by_path( '/README.md' )->consume_all();
WordPress\Filesystem\InMemoryFilesystem;
use WordPress\Git\GitRepository;
use WordPress\Git\Model\Commit;
$repo = new GitRepository( InMemoryFilesystem::create() );
foreach ( array( 'add intro', 'fix typo', 'expand examples' ) as $i => $msg ) {
$repo->commit( array(
'updates' => array( 'post.md' => "# Draft {$i}" ),
'commit' => array( 'message' => $msg ),
) );
}
$oid = $repo->get_branch_tip( 'HEAD' );
while ( ! Commit::is_null_hash( $oid ) ) {
$c = $repo->read_object( $oid )->as_commit();
echo substr( $c->hash, 0, 7 ) . ' ' . trim( $c->message ) . "\n";
$oid = $c->get_first_parent_hash();
if ( ! $oid || ! $repo->has_object( $oid ) ) break;
}
WordPress\Filesystem\InMemoryFilesystem;
use WordPress\Git\GitFilesystem;
use WordPress\Git\GitRepository;
$repo = new GitRepository( InMemoryFilesystem::create() );
$fs = GitFilesystem::create( $repo );
$fs->put_contents( '/posts/hello.md', "# Hello\nFirst draft." );
$fs->put_contents( '/posts/about.md', "# About\nWho we are." );
$fs->put_contents( '/posts/hello.md', "# Hello\nSecond draft." );
echo "tree:\n";
foreach ( $fs->ls( '/posts' ) as $name ) {
echo " /posts/{$name}\n";
}
echo "\nhello.md now:\n" . $fs->get_contents( '/posts/hello.md' ) . "\n";
WordPress\Filesystem\InMemoryFilesystem;
use WordPress\Git\GitRepository;
$repo = new GitRepository( InMemoryFilesystem::create() );
$base = $repo->commit( array(
'updates' => array( 'config.json' => '{"flag":false}' ),
'commit' => array( 'message' => 'baseline' ),
) );
$repo->create_branch( 'refs/heads/experiment', $base );
$repo->checkout( 'refs/heads/experiment' );
$repo->commit( array(
'updates' => array( 'config.json' => '{"flag":true}' ),
'commit' => array( 'message' => 'flip the flag' ),
) );
echo "on experiment: " . $repo->read_object_by_path( '/config.json' )->consume_all() . "\n";
$repo->checkout( 'refs/heads/trunk' );
echo "on trunk: " . $repo->read_object_by_path( '/config.json' )->consume_all() . "\n";
WordPress\Filesystem\InMemoryFilesystem;
use WordPress\Git\GitRepository;
$repo = new GitRepository( InMemoryFilesystem::create() );
$base = $repo->commit( array( 'updates' => array(
'todo.txt' => "buy milk\nwalk dog\nread book\n",
) ) );
$repo->commit( array( 'updates' => array(
'todo.txt' => "buy oat milk\nwalk dog\nread book\n",
) ) );
$repo->create_branch( 'refs/heads/feature', $base );
$repo->checkout( 'refs/heads/feature' );
$repo->commit( array( 'updates' => array(
'todo.txt' => "buy milk\nwalk dog\nread book\nwrite blog post\n",
) ) );
$repo->checkout( 'refs/heads/trunk' );
$result = $repo->merge( 'refs/heads/feature' );
echo "merge head: {$result['new_head']}\n";
echo "conflicts: " . ( $result['conflicts'] ? implode( ',', $result['conflicts'] ) : 'none' ) . "\n";
echo "result:\n" . $repo->read_object_by_path( '/todo.txt' )->consume_all();
WordPress\Filesystem\InMemoryFilesystem;
use WordPress\Git\GitRepository;
$repo = new GitRepository( InMemoryFilesystem::create() );
$snapshots = array(
array( 'blogname' => 'My Site', 'posts_per_page' => 10, 'timezone_string' => 'UTC' ),
array( 'blogname' => 'My Site', 'posts_per_page' => 20, 'timezone_string' => 'UTC' ),
array( 'blogname' => 'New Name', 'posts_per_page' => 20, 'timezone_string' => 'Europe/Warsaw' ),
);
foreach ( $snapshots as $i => $options ) {
$repo->commit( array(
'updates' => array( 'options.json' => json_encode( $options, JSON_PRETTY_PRINT ) ),
'commit' => array( 'message' => "snapshot #{$i}" ),
) );
}
$head = $repo->get_branch_tip( 'HEAD' );
$parent = $repo->read_object( $head )->as_commit()->get_first_parent_hash();
$diff = $repo->diff_commits( $head, $parent );
echo "Files changed in last snapshot:\n";
foreach ( $diff as $name => $entry ) {
echo " {$name}\n";
}