Skip to content

Commit

Permalink
Repository.xs: WIP: Added revparse()
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquesg committed Aug 18, 2014
1 parent 5936d4d commit cdeb1b2
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/Git/Raw/Repository.pm
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,10 @@ directory. See C<Git::Raw::Repository-E<gt>merge()> for valid C<%merge_opts>
and C<%checkout_opts> values. For merge commits C<$mainline> specifies the
parent.
=head2 revparse( $spec )
TO BE COMPLETED
=head2 state( )
Determine the state of the repository. One of the following values is returned:
Expand Down
3 changes: 3 additions & 0 deletions t/21-cherrypick.t
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ my $conflict_commit2 = $repo -> commit("commit2 on cbranch\n", $me, $me, [$confl
is $index -> has_conflicts, 0;
is $repo -> status({}) -> {'cherry_file'}, undef;
isnt $branch -> target -> tree -> id, $cherry_pick_conflict_branch -> target -> tree -> id;
is $repo -> state, "cherry_pick";
$repo -> state_cleanup;
is $repo -> state, "none";

my $master = Git::Raw::Branch -> lookup($repo, 'master', 1);
$index -> read_tree($master -> target -> tree);
Expand Down
16 changes: 16 additions & 0 deletions t/22-revert.t
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,20 @@ my $revert_commit = $repo -> commit("revert commit1\n", $me, $me, [$commit2],
is_deeply $repo -> status({}) -> {'revert_file'}, undef;
is $revert_commit -> tree -> id, $commit1 -> tree -> id;

is $repo -> state, "revert";
$repo -> state_cleanup;
is $repo -> state, "none";

my $master = Git::Raw::Branch -> lookup($repo, 'master', 1);
$index -> read_tree($master -> target -> tree);
$index -> write;

$repo -> checkout($master -> target -> tree, {
'checkout_strategy' => {
'safe' => 1
}
});

$repo -> head($master);

done_testing;
45 changes: 45 additions & 0 deletions t/24-revparse.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!perl

use Test::More;

use Git::Raw;
use Cwd qw(abs_path);

my $path = abs_path('t').'/test_repo';
my $repo = Git::Raw::Repository -> open($path);

$repo -> revparse ('HEAD'); # void context
is $repo -> revparse ('HEAD'), 1;
my @revs = $repo -> revparse ('HEAD');
is scalar(@revs), 1;
my $head = shift @revs;
isa_ok $head, 'Git::Raw::Commit';

is $repo -> revparse ('HEAD~..HEAD'), 2;
@revs = $repo -> revparse ('HEAD~..HEAD');
is scalar(@revs), 2;
isa_ok $revs[0], 'Git::Raw::Commit';
isa_ok $revs[1], 'Git::Raw::Commit';
is $revs[1] -> id, $head -> id;

my $prev_head = shift @revs;

@revs = $repo -> revparse('master~..master');
is scalar(@revs), 2;
is $revs[0] -> id, $prev_head -> id;
is $revs[1] -> id, $head -> id;

my @mb = $repo -> revparse('master~...master');
isa_ok $mb[0], 'Git::Raw::Commit';
isa_ok $mb[1], 'Git::Raw::Commit';

is $revs[0] -> id, $mb[0] -> id;
is $revs[1] -> id, $mb[1] -> id;

@revs = $repo -> revparse('@{1}');
is $revs[0] -> id, $prev_head -> id;

@revs = $repo -> revparse('master@{1}');
is $revs[0] -> id, $prev_head -> id;

done_testing;
41 changes: 41 additions & 0 deletions xs/Repository.xs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,47 @@ revert(self, commit, ...)
);
git_check_error(rc);

void
revparse(self, spec)
SV *self
SV *spec

PREINIT:
int ctx;

PPCODE:
ctx = GIMME_V;

if (ctx != G_VOID) {
int rc;
git_revspec rs = {NULL, NULL, 0};
Repository repo = GIT_SV_TO_PTR(Repository, self);

rc = git_revparse(&rs, repo -> repository,
git_ensure_pv(spec, "spec"));
if (rc != GIT_OK)
git_check_error(rc);

if (ctx == G_ARRAY) {
mXPUSHs(git_obj_to_sv(rs.from, SvRV(self)));
if (rs.flags & GIT_REVPARSE_SINGLE) {
XSRETURN(1);
} else {
mXPUSHs(git_obj_to_sv(rs.to, SvRV(self)));
XSRETURN(2);
}
} else {
if (rs.flags & GIT_REVPARSE_SINGLE)
mXPUSHs(newSViv(1));
else
mXPUSHs(newSViv(2));

XSRETURN(1);
}

} else
XSRETURN_EMPTY;

SV *
state(self)
Repository self
Expand Down

0 comments on commit cdeb1b2

Please sign in to comment.