Skip to content

Commit

Permalink
Diff.xs: Detect patches() calling context
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquesg committed Aug 18, 2014
1 parent 6e91fb0 commit 052f7eb
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
1 change: 1 addition & 0 deletions t/02-commit.t
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ is $head -> committer -> offset, $off;
is $head -> time, $time;
is $head -> offset, $off;

$head -> parents; # void context
my $parent_count = $head -> parents;
is $parent_count, 1;
my @parents = $head -> parents;
Expand Down
1 change: 1 addition & 0 deletions t/05-tree.t
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ isa_ok $lookup_tree, 'Git::Raw::Tree';
$lookup_tree = Git::Raw::Tree -> lookup($repo, substr($tree -> id, 0, 7));
isa_ok $lookup_tree, 'Git::Raw::Tree';

$tree -> entries; # void context
my $entry_count = $tree -> entries;
is $entry_count, 3;
@entries = $tree -> entries;
Expand Down
3 changes: 3 additions & 0 deletions t/09-diff.t
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ $output = capture_stdout { $diff -> print("patch_header", $printer) };
is $output, $expected;

is $diff -> delta_count, 2;
$diff -> patches; # void context
my $patch_count = $diff -> patches;
is $patch_count, 2;
my @patches = $diff -> patches;
is scalar(@patches), 2;

Expand Down
1 change: 0 additions & 1 deletion xs/Commit.xs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ parents(self)
mXPUSHs(tmp);
}

mXPUSHs(newSViv((int) count));
XSRETURN((int) count);
} else {
mXPUSHs(newSViv((int) count));
Expand Down
47 changes: 27 additions & 20 deletions xs/Diff.xs
Original file line number Diff line number Diff line change
Expand Up @@ -116,35 +116,42 @@ patches(self)
SV *self

PREINIT:
int rc;
int ctx;

Diff diff_ptr;
PPCODE:
ctx = GIMME_V;

size_t i, count, num_patches = 0;
if (ctx != G_VOID) {
int rc;
size_t count;
Diff diff_ptr;
diff_ptr = GIT_SV_TO_PTR(Diff, self);

PPCODE:
diff_ptr = GIT_SV_TO_PTR(Diff, self);
count = git_diff_num_deltas(diff_ptr);
if (ctx == G_ARRAY) {
size_t i;

count = git_diff_num_deltas(diff_ptr);
for (i = 0; i < count; ++i) {
Patch patch;
for (i = 0; i < count; ++i) {
SV *tmp;
Patch patch;

rc = git_patch_from_diff(&patch, diff_ptr, i);
git_check_error(rc);
rc = git_patch_from_diff(&patch, diff_ptr, i);
git_check_error(rc);

if (patch) {
SV *p;
GIT_NEW_OBJ_WITH_MAGIC(
tmp, "Git::Raw::Patch", patch, SvRV(self)
);

GIT_NEW_OBJ_WITH_MAGIC(
p, "Git::Raw::Patch", patch, SvRV(self)
);
mXPUSHs(tmp);
}

mXPUSHs(p);
++num_patches;
XSRETURN((int) count);
} else {
mXPUSHs(newSViv((int) count));
XSRETURN(1);
}
}

XSRETURN(num_patches);
} else
XSRETURN_EMPTY;

void
DESTROY(self)
Expand Down
1 change: 0 additions & 1 deletion xs/Tree.xs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ entries(self)
mXPUSHs(tmp);
}

mXPUSHs(newSViv((int) count));
XSRETURN((int) count);
} else {
mXPUSHs(newSViv((int) count));
Expand Down

0 comments on commit 052f7eb

Please sign in to comment.