Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional index methods #100

Merged
merged 2 commits into from
May 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ Revision history for Git-Raw
+ Repository -> cherry_pick()
+ Repository -> revert()
- Update libgit2 to 530594c (GH#99)
- Added Index methods (GH#100):
+ Index -> capabilities()
+ Index -> path()
+ Index -> add_all()
+ Index -> remove_all()

0.36 2014-05-02 15:59:18SAST+0200 Africa/Johannesburg (TRIAL RELEASE)

Expand Down
45 changes: 34 additions & 11 deletions Raw.xs
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,21 @@ STATIC int git_index_matched_path_cbb(const char *path, const char *pathspec, vo
return rv;
}

STATIC void git_list_to_paths(AV *list, git_strarray *paths) {
size_t i = 0, count = 0;
SV **path;

while ((path = av_fetch(list, i++, 0))) {
if (!path || !SvOK(*path))
continue;

Renew(paths->strings, count + 1, char *);
paths->strings[count++] = SvPVbyte_nolen(*path);
}

paths->count = count;
}


STATIC void git_hv_to_checkout_opts(HV *opts, git_checkout_options *checkout_opts) {
char **paths = NULL;
Expand Down Expand Up @@ -1489,19 +1504,27 @@ features(class)
SV *class

PREINIT:
int features;
int ctx = GIMME_V;

PPCODE:
features = git_libgit2_features();

mXPUSHs(newSVpv("threads", 0));
mXPUSHs(newSViv((features & GIT_FEATURE_THREADS) ? 1 : 0));
mXPUSHs(newSVpv("https", 0));
mXPUSHs(newSViv((features & GIT_FEATURE_HTTPS) ? 1 : 0));
mXPUSHs(newSVpv("ssh", 0));
mXPUSHs(newSViv((features & GIT_FEATURE_SSH) ? 1 : 0));

XSRETURN(6);
if (ctx != G_VOID) {
if (ctx == G_ARRAY) {
int features = git_libgit2_features();

mXPUSHs(newSVpv("threads", 0));
mXPUSHs(newSViv((features & GIT_FEATURE_THREADS) ? 1 : 0));
mXPUSHs(newSVpv("https", 0));
mXPUSHs(newSViv((features & GIT_FEATURE_HTTPS) ? 1 : 0));
mXPUSHs(newSVpv("ssh", 0));
mXPUSHs(newSViv((features & GIT_FEATURE_SSH) ? 1 : 0));

XSRETURN(6);
} else {
mXPUSHs(newSViv(3));
XSRETURN(1);
}
} else
XSRETURN_EMPTY;

INCLUDE: xs/Blame.xs
INCLUDE: xs/Blame/Hunk.xs
Expand Down
54 changes: 54 additions & 0 deletions lib/Git/Raw/Index.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,58 @@ B<WARNING>: The API of this module is unstable and may change without warning
Add C<$entry> to the index. C<$entry> should either be the path of a file
or alternatively a C<Git::Raw::Index::Entry>.

=head2 add_all( \%opts )

Add or update all index entries to match the working directory. Valid fields for
the C<%opts> hash are:

=over 4

=item * "paths"

List of path patterns to add.

=item * "flags"

Valid fields for the C<%flags> member:

=over 8

=item * "force"

Forced addition of files (even if they are ignored).

=item * "disable_pathspec_match"

Disable pathspec pattern matching against entries in C<$paths>.

=item * "check_pathspec"

Enable pathspec pattern matching against entries in C<$paths> (default).

=back

=item * "notification"

The callback to be called for each added or updated item. Receives the C<$path>
and matching C<$pathspec>. This callback should return L<0> if the file should
be added to the index, L<E<gt>0> if it should be skipped or L<E<lt>0> to abort.

=back

=head2 remove( $path )

Remove C<$path> from the index.

=head2 remove_all( \%opts )

Remove all matching index entries. See C<Git::Raw::Index-E<gt>update_all()> for
valid C<%opts> values.

=head2 path( )

Retrieve the full path to the index file on disk.

=head2 clear( )

Clear the index.
Expand Down Expand Up @@ -94,6 +142,12 @@ added to the index, L<E<gt>0> if it should be skipped or L<E<lt>0> to abort.

=back

=head2 capabilities( )

Retrieve the index's capabilities. Returns a hash with members L<"ignore_case">,
L<"no_filemode"> and L<"no_symlinks">, each indicating if the C<Git::Raw::Index>
supports the capability.

=head1 AUTHOR

Alessandro Ghedini <alexbio@cpan.org>
Expand Down
74 changes: 74 additions & 0 deletions t/01-repo.t
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,80 @@ is_deeply $repo -> status, {
'untracked' => {'flags' => ['worktree_new']},
'subdir/' => {'flags' => ['ignored']}};

my $index = $repo -> index;

ok (eval { $index -> capabilities });

my $caps_count = $index -> capabilities;
is $caps_count, 3;

my %caps = $index -> capabilities;
is scalar (keys %caps), 3;

ok exists $caps{'ignore_case'};
ok exists $caps{'no_filemode'};
ok exists $caps{'no_symlinks'};

is $caps{ignore_case}, 1 if ($^O eq 'darwin' || $^O eq 'MSWin32' || $^O eq 'cygwin');

my $triggered_add = 0;
$index -> add_all({
'paths' => [ 'ign*' ],
'flags' => {
'force' => 1,
'disable_pathspec_match' => 1
},
'notification' => sub {
$triggered_add = 1;
0;
}
});
is $triggered_add, 0;

$index -> add_all({
'paths' => [ 'ign*' ],
'flags' => {
'force' => 1,
'disable_pathspec_match' => 1
},
'notification' => sub {
$triggered_add = 1;
0;
}
});
is $triggered_add, 0;

$index -> add_all({
'paths' => [ 'ign*' ],
'flags' => {
'force' => 1
},
'notification' => sub {
my ($path, $spec) = @_;

is $path, 'ignore';
is $spec, 'ign*';

$triggered_add = 1;
0;
}
});
is $triggered_add, 1;

is_deeply $repo -> status -> {'ignore'}, {'flags' => ['index_new']};

my $triggered_removed = 0;
$index -> remove_all({
'paths' => [ 'ignore' ],
'notification' => sub {
$triggered_removed = 1;
0;
}
});

is $triggered_removed, 1;
is_deeply $repo -> status -> {'ignore'}, {'flags' => ['ignored']};

$file = $repo -> workdir . 'subdir/' .'untracked';
write_file($file, 'this file should be untracked');
is_deeply $repo -> status('subdir/') -> {'subdir/'}, {'flags' => ['worktree_new']};
Expand Down
8 changes: 7 additions & 1 deletion t/02-commit.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use Test::More;
use Git::Raw;
use File::Copy;
use File::Slurp;
use File::Spec::Functions;
use Cwd qw(abs_path);
use File::Path 2.07 qw(make_path remove_tree);
use Time::Local;
Expand All @@ -19,8 +20,13 @@ write_file($file, 'this is a test');
is_deeply $repo -> status -> {'test'}, {'flags' => ['worktree_new']};

my $index = $repo -> index;
is $index -> path, catfile($path, '.git/index');
ok (!eval { $index -> add($index) });
$index -> add('test');
$index -> add_all({
'paths' => [
'test'
]
});
$index -> write;

my $tree_id = $index -> write_tree;
Expand Down
1 change: 1 addition & 0 deletions t/15-merge.t
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ my $merge_commit2 = $repo -> commit("merge commit on branch1\n", $me, $me, [$mer

my $merged_index = $merge_commit1 -> merge($merge_commit2, {});
isa_ok $merged_index, 'Git::Raw::Index';
is $merged_index -> path, undef;
is $merged_index -> has_conflicts, 0;
my @merged_index_entries = $merged_index -> entries;
ok (scalar(@merged_index_entries) > 0);
Expand Down
16 changes: 13 additions & 3 deletions t/23-diagnostics.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@ use Test::More;

use Git::Raw;

my $count;
my %features;

ok (eval { Git::Raw -> features });

ok (eval { $count = Git::Raw -> features });
is $count, 3;

ok (eval { %features = Git::Raw -> features });
is scalar(keys %features), 3;

ok exists $features{'threads'};
ok exists $features{'https'};
ok exists $features{'ssh'};

diag("Build info:");
diag("Built with threads support: $features{threads}");
diag("Built with HTTPS support: $features{https}");
diag("Built with SSH support: $features{ssh}");
diag("Built with threads support: $features{'threads'}");
diag("Built with HTTPS support: $features{'https'}");
diag("Built with SSH support: $features{'ssh'}");

done_testing;
Loading