1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
#!perl
use Test::More;
use Git::Raw;
use File::Spec::Unix;
use File::Spec::Functions qw(catfile rel2abs);
my $native_path = rel2abs(catfile('t', 'test_repo'));
my $path = File::Spec::Unix -> rel2abs(File::Spec::Unix -> catfile('t', 'test_repo'));
my $repo = Git::Raw::Repository -> open($native_path);
my $head = $repo -> head;
isa_ok $head, 'Git::Raw::Reference';
is $head -> type, 'direct';
is $head -> name, 'refs/heads/main';
ok $head -> is_branch;
my $ref = Git::Raw::Reference -> lookup('HEAD', $repo);
is $ref -> type, 'symbolic';
isa_ok $ref -> target, 'Git::Raw::Reference';
$ref = Git::Raw::Reference -> lookup('refs/heads/main', $repo);
isa_ok $ref, 'Git::Raw::Reference';
my $non_existent_ref = Git::Raw::Reference -> lookup('refs/heads/non-existent', $repo);
is $non_existent_ref, undef;
is $ref -> type, 'direct';
is $ref -> name, 'refs/heads/main';
ok $ref -> is_branch;
ok !$ref -> is_remote;
ok !$ref -> is_note;
ok !$ref -> is_tag;
$head = $ref -> target;
isa_ok $head, 'Git::Raw::Commit';
is $head -> summary, "third commit";
ok(!eval { $ref -> peel('any') });
my $peeled_commit = $ref -> peel('commit');
isa_ok $peeled_commit, 'Git::Raw::Commit';
is $peeled_commit -> id, $head -> id;
my $peeled_tree = $ref -> peel('tree');
isa_ok $peeled_tree, 'Git::Raw::Tree';
is $peeled_tree -> id, $head -> tree -> id;
$ref = $repo -> branch('foobar06', $head);
$ref -> delete;
my $repo2 = $ref -> owner;
isa_ok $repo2, 'Git::Raw::Repository';
is $repo2 -> path, "$path/.git/";
$ref = Git::Raw::Reference -> create('refs/test-ref', $repo, $head);
isa_ok $ref, 'Git::Raw::Reference';
is $ref -> type, 'direct';
is $ref -> target -> id, $head -> id;
is $ref -> name, 'refs/test-ref';
is $ref -> shorthand, 'test-ref';
ok !$ref -> is_branch;
ok !$ref -> is_remote;
ok !$ref -> is_note;
ok !$ref -> is_tag;
eval {
Git::Raw::Reference -> create('refs/test-ref', $repo, $head);
fail q{Should've raised an error!};
} or do {
like $@, qr/Failed to write reference/i;
};
# shouldn't die with force argument
Git::Raw::Reference -> create('refs/test-ref', $repo, $head, 1);
my $empty_blob = $repo -> blob('');
$ref = Git::Raw::Reference -> create('refs/test-ref', $repo, $empty_blob, 1);
is $ref -> type, 'direct';
is $ref -> target -> id, $empty_blob -> id;
is $ref -> name, 'refs/test-ref';
ok !$ref -> is_branch;
ok !$ref -> is_remote;
ok !$ref -> is_note;
ok !$ref -> is_tag;
my $tree = $head -> tree();
$ref = Git::Raw::Reference -> create('refs/test-ref', $repo, $tree, 1);
is $ref -> type, 'direct';
is $ref -> target -> id, $tree -> id;
is $ref -> name, 'refs/test-ref';
ok !$ref -> is_branch;
ok !$ref -> is_remote;
ok !$ref -> is_note;
ok !$ref -> is_tag;
$ref -> delete;
my @refs = $repo -> refs();
is scalar(grep { !$_ -> isa('Git::Raw::Reference') } @refs), 0, 'Everything returned by $repo->refs() should be a Git::Raw::Reference';
my @ref_names = sort map { $_ -> name() } @refs;
is_deeply \@ref_names, [ 'refs/commit-test-ref', 'refs/heads/main' ];
my $symbolic = 1;
my $symbolic_ref = Git::Raw::Reference -> create('refs/test-symbolic-ref', $repo, 'refs/heads/main', 0, $symbolic);
my $main_ref = Git::Raw::Reference->lookup('refs/heads/main', $repo);
is $symbolic_ref -> type, 'symbolic';
is $symbolic_ref -> name, 'refs/test-symbolic-ref';
is $symbolic_ref -> target -> name, $main_ref -> name;
ok !$symbolic_ref -> is_branch;
ok !$symbolic_ref -> is_remote;
ok !$symbolic_ref -> is_note;
ok !$symbolic_ref -> is_tag;
# should fail to overwrite existing ref is overwrite is falsy
eval {
Git::Raw::Reference -> create('refs/test-symbolic-ref', $repo, 'refs/heads/main', 0, $symbolic);
fail q{Should've raised an error!};
} or do {
like $@, qr/Failed to write reference/i;
};
# shouldn't die with force argument
$symbolic_ref = Git::Raw::Reference -> create('refs/test-symbolic-ref', $repo, 'refs/heads/main', 1, $symbolic);
# should still reject Git::Raw::Reference as object when creating direct ref
eval {
Git::Raw::Reference -> create('refs/test-ref', $repo, $main_ref, not $symbolic);
fail q{Should've raised an error!};
} or do {
like $@, qr/Argument is not of type/;
};
# should reject Git::Raw::Blob as target for symbolic ref
eval {
Git::Raw::Reference -> create('refs/test-symbolic-ref', $repo, $empty_blob, 1, $symbolic);
fail q{Should've raised an error!};
} or do {
like $@, qr/Argument is not of type Git::Raw::Reference/;
};
# should reject Git::Raw::Commit as target for symbolic ref
eval {
Git::Raw::Reference -> create('refs/test-symbolic-ref', $repo, $main_ref -> target, 1, $symbolic);
fail q{Should've raised an error!};
} or do {
like $@, qr/Argument is not of type Git::Raw::Reference/;
};
# should reject Git::Raw::Tree as target for symbolic ref
eval {
Git::Raw::Reference -> create('refs/test-symbolic-ref', $repo, $tree, 1, $symbolic);
fail q{Should've raised an error!};
} or do {
like $@, qr/Argument is not of type Git::Raw::Reference/;
};
# and we can also use an existing Git::Raw::Reference object as the target
$symbolic_ref -> delete;
$symbolic_ref = Git::Raw::Reference -> create('refs/test-symbolic-ref', $repo, $main_ref, 0, $symbolic);
is $symbolic_ref -> type, 'symbolic';
is $symbolic_ref -> name, 'refs/test-symbolic-ref';
is $symbolic_ref -> target -> name, $main_ref -> name;
ok !$symbolic_ref -> is_branch;
ok !$symbolic_ref -> is_remote;
ok !$symbolic_ref -> is_note;
ok !$symbolic_ref -> is_tag;
$symbolic_ref -> delete;
done_testing;
|