<?php
declare(strict_types=1);
namespace Tests\git2;
use git2\git;
use git2\git_reflog;
use git2\git_reflog_entry;
final class ReflogTest extends GitTestCase
{
public function testRead(): void
{
$dir = $this->mkdir('read');
git::repository_init($repo, $dir, false);
git::repository_index($index, $repo);
git::index_write_tree($treeId, $index);
git::tree_lookup($tree, $repo, $treeId);
git::signature_new($sig, 'John Doe', 'john.doe@example.com', 1640263000, 60);
git::commit_create_v($id, $repo, 'HEAD', $sig, $sig, null, 'foo', $tree);
$this->assertOK(git::reflog_read($reflog, $repo, 'refs/heads/master'));
$this->assertInstanceOf(git_reflog::class, $reflog);
$this->assertSame(1, git::reflog_entrycount($reflog));
$entry = git::reflog_entry_byindex($reflog, 0);
$this->assertInstanceOf(git_reflog_entry::class, $entry);
$this->assertSame('commit (initial): foo', git::reflog_entry_message($entry));
$this->assertSame('fe61c43f34fcc3bd7e9bfaa4b7739bf2be5aa05b', git::oid_tostr_s(git::reflog_entry_id_new($entry)));
$this->assertTrue(git::oid_is_zero(git::reflog_entry_id_old($entry)));
$this->assertSame('john.doe@example.com', git::reflog_entry_committer($entry)->email);
}
public function testAppend(): void
{
$dir = $this->mkdir('append');
git::repository_init($repo, $dir, false);
git::repository_index($index, $repo);
git::index_write_tree($treeId, $index);
git::tree_lookup($tree, $repo, $treeId);
git::signature_new($sig, 'John Doe', 'john.doe@example.com', 1640263000, 60);
git::commit_create_v($id1, $repo, 'HEAD', $sig, $sig, null, 'foo', $tree);
git::commit_lookup($commit, $repo, $id1);
git::commit_create_v($id2, $repo, 'HEAD', $sig, $sig, null, 'bar', $tree, $commit);
git::reflog_read($reflog, $repo, 'refs/heads/master');
$this->assertOK(git::reflog_append($reflog, $id1, $sig, 'reset'));
$this->assertSame(3, git::reflog_entrycount($reflog));
$this->assertOK(git::reflog_drop($reflog, 1, true));
$this->assertSame(2, git::reflog_entrycount($reflog));
$this->assertOK(git::reflog_rename($repo, 'refs/heads/master', 'refs/heads/renamed'));
$this->assertOK(git::reflog_read($reflog, $repo, 'refs/heads/renamed'));
$this->assertOK(git::reflog_write($reflog));
$this->assertOK(git::reflog_delete($repo, 'refs/heads/renamed'));
}
}