2 use lib
(split(/:/, $ENV{GITPERLLIB
}));
7 use Test
::More
qw(no_plan);
12 BEGIN { use_ok
('Git::Repo') }
15 my ($coderef, $descr) = @_;
16 eval { $coderef->(); };
21 my ($coderef, $descr) = @_;
22 eval { $coderef->(); };
28 open our $old_stderr, ">&", STDERR
or die "cannot save STDERR";
32 open STDERR
, ">&", $old_stderr or die "cannot restore STDERR";
36 our $abs_wc_dir = Cwd
->cwd;
37 ok
(our $r = Git
::Repo
->new(repo_dir
=> "./.git"), 'open repository');
40 chomp(my $sha1 = `git rev-parse $name 2> /dev/null`);
44 my @revisions = split /\s/, `git-rev-list --first-parent HEAD`;
45 my $head = $revisions[0];
48 is
($r->get_sha1('HEAD'), $head, 'get_sha1: scalar');
49 is
($r->get_sha1('HEAD'), $head, 'get_sha1: scalar, repeated');
50 my($sha1, $type, $head_size) = $r->get_sha1('HEAD');
51 is
($sha1, $head, 'get_sha1: array (SHA1)');
52 is
($type, 'commit', 'get_sha1: array (commit)');
53 ok
($head_size > 0, 'get_sha1: array (size)');
56 is_deeply
([$r->get_object($r->get_sha1("$revisions[-1]:file1"))], ['blob', "test file 1\n"], 'get_object: blob');
57 is_deeply
([$r->get_object($r->get_sha1("$revisions[-1]:file1"))], ['blob', "test file 1\n"], 'get_object: blob, repeated');
58 dies_ok
{ $r->get_object('0' x
40) } 'get_object: non-existent sha1';
61 isa_ok
($r->get_commit($revisions[-1]), 'Git::Commit',
62 'get_commit: returns Git::Commit object');
65 isa_ok
($r->get_tag($r->get_sha1('tag-object-1')), 'Git::Tag',
66 'get_tag: returns Git::Tag object');
69 is
($r->name_rev($revisions[-2]), 'branch-2', 'name_rev: branch');
70 is
($r->name_rev($head, 1), undef, 'name_rev: branch, tags only');
71 is
($r->name_rev($revisions[-1]), 'tags/tag-object-1^0', 'name_rev: tag object');
72 is
($r->name_rev($revisions[-1], 1), 'tag-object-1^0', 'name_rev: tag object, tags only');
77 print "# Git::Commit:\n";
79 BEGIN { use_ok
('Git::Commit') }
81 my $invalid_commit = Git
::Commit
->new($r, '0' x
40);
82 is
($invalid_commit->sha1, '0' x
40, 'new, sha1: accept invalid SHA1');
83 dies_ok
{ $invalid_commit->tree } 'die on accessing properties of invalid SHA1s';
85 $invalid_commit = Git
::Commit
->new($r, $r->get_sha1('HEAD:')); # tree, not commit
86 dies_ok
{ $invalid_commit->tree } 'die on accessing properties of non-commit objects';
88 my $c = Git
::Commit
->new($r, $revisions[-2]);
89 is
($c->repo, $r, 'repo: basic');
90 is
($c->sha1, $revisions[-2], 'sha1: basic');
91 is
($c->{parents
}, undef, 'lazy loading: not loaded after reading SHA1');
92 is
($c->tree, $r->get_sha1("$revisions[-2]:"), 'tree: basic');
93 ok
($c->{parents
}, 'lazy loading: loaded after reading tree');
94 is_deeply
([$c->parents], [$revisions[-1]], 'parents: basic');
95 like
($c->author, qr/A U Thor <author\@example.com> [0-9]+ \+0000/, 'author: basic');
96 like
($c->committer, qr/C O Mitter <committer\@example.com> [0-9]+ \+0000/, 'committer: basic');
97 is
($c->encoding, undef, 'encoding: undef');
98 is
($c->message, "second commit\n", 'message: basic');
99 is
($c, $c->sha1, 'stringify: basic');
102 dies_ok
{ Git
::Commit
->new($r, $r->get_sha1('tag-object-3'))->_load }
103 'new: pass tag SHA1 (dies)';
104 dies_ok
{ Git
::Commit
->new($r, '0' x
40)->_load }
105 'new: pass invalid SHA1 (dies)';
109 print "# Git::Tag:\n";
111 BEGIN { use_ok
('Git::Tag') }
113 # We don't test functionality inherited from Git::Object that we
114 # already tested in the Git::Commit tests.
116 my $t = Git
::Tag
->new($r, $r->get_sha1('tag-object-1'));
117 is
($t->tag, 'tag-object-1', 'tag: basic');
118 is
($t->object, $revisions[-1], 'object: basic');
119 is
($t->type, 'commit', 'tag: type');
120 like
($t->tagger, qr/C O Mitter <committer\@example.com> [0-9]+ \+0000/, 'tagger: basic');
121 is
($t->encoding, undef, 'encoding: undef');
122 is
($t->message, "tag message 1\n", 'message: basic');
125 dies_ok
{ Git
::Tag
->new($r, $head)->_load } 'new: pass commit SHA1 (dies)';
126 dies_ok
{ Git
::Tag
->new($r, '0' x
40)->_load } 'new: pass invalid SHA1 (dies)';
130 print "# Git::RepoRoot:\n";
132 BEGIN { use_ok
('Git::RepoRoot'); }
134 my $reporoot = Git
::RepoRoot
->new(root_dir
=> $abs_wc_dir);
135 is
($reporoot->repo(repo_dir
=> '.git')->get_sha1('HEAD'), $head,