Git.pm: Add remote_refs() git-ls-remote frontend
This patch also converts the good ole' git-remote.perl to use it. It is otherwise used in the repo.or.cz machinery and I guess other scripts might find it useful too. Unfortunately, git-ls-remote --heads . is subtly different from git-ls-remote . refs/heads/ (since the second matches anywhere in the string, not just at the beginning) so we have to provide interface for both. Signed-off-by: Petr Baudis <pasky@suse.cz> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Petr Baudis committed
Jul 8, 2008 at 19:48 UTC
31a92f6aa47732e93f6685b8d1fd1ac09c1fec44
2 files changed
+56
-5
contrib/examples/git-remote.perl
+1
-4
@@ -129,10 +129,7 @@ sub update_ls_remote {
129
return if (($harder == 0) ||
130
(($harder == 1) && exists $info->{'LS_REMOTE'}));
131
132
- my @ref = map {
133
- s|^[0-9a-f]{40}\s+refs/heads/||;
134
- $_;
135
- } $git->command(qw(ls-remote --heads), $info->{'URL'});
132
+ my @ref = map { s|refs/heads/||; $_; } keys %{$git->remote_refs($info->{'URL'}, [ 'heads' ])};
133
$info->{'LS_REMOTE'} = \@ref;
134
}
135
perl/Git.pm
+55
-1
@@ -56,7 +56,8 @@ require Exporter;
56
@EXPORT_OK = qw(command command_oneline command_noisy
57
command_output_pipe command_input_pipe command_close_pipe
58
command_bidi_pipe command_close_bidi_pipe
59
- version exec_path hash_object git_cmd_try);
59
+ version exec_path hash_object git_cmd_try
60
+ remote_refs);
61
62
63
=head1 DESCRIPTION
@@ -668,6 +669,59 @@ sub get_color {
669
return $color;
670
}
671
672
+=item remote_refs ( REPOSITORY [, GROUPS [, REFGLOBS ] ] )
673
+
674
+This function returns a hashref of refs stored in a given remote repository.
675
+The hash is in the format C<refname =\> hash>. For tags, the C<refname> entry
676
+contains the tag object while a C<refname^{}> entry gives the tagged objects.
677
+
678
+C<REPOSITORY> has the same meaning as the appropriate C<git-ls-remote>
679
+argument; either an URL or a remote name (if called on a repository instance).
680
+C<GROUPS> is an optional arrayref that can contain 'tags' to return all the
681
+tags and/or 'heads' to return all the heads. C<REFGLOB> is an optional array
682
+of strings containing a shell-like glob to further limit the refs returned in
683
+the hash; the meaning is again the same as the appropriate C<git-ls-remote>
684
+argument.
685
+
686
+This function may or may not be called on a repository instance. In the former
687
+case, remote names as defined in the repository are recognized as repository
688
+specifiers.
689
+
690
+=cut
691
+
692
+sub remote_refs {
693
+ my ($self, $repo, $groups, $refglobs) = _maybe_self(@_);
694
+ my @args;
695
+ if (ref $groups eq 'ARRAY') {
696
+ foreach (@$groups) {
697
+ if ($_ eq 'heads') {
698
+ push (@args, '--heads');
699
+ } elsif ($_ eq 'tags') {
700
+ push (@args, '--tags');
701
+ } else {
702
+ # Ignore unknown groups for future
703
+ # compatibility
704
+ }
705
+ }
706
+ }
707
+ push (@args, $repo);
708
+ if (ref $refglobs eq 'ARRAY') {
709
+ push (@args, @$refglobs);
710
+ }
711
+
712
+ my @self = $self ? ($self) : (); # Ultra trickery
713
+ my ($fh, $ctx) = Git::command_output_pipe(@self, 'ls-remote', @args);
714
+ my %refs;
715
+ while (<$fh>) {
716
+ chomp;
717
+ my ($hash, $ref) = split(/\t/, $_, 2);
718
+ $refs{$ref} = $hash;
719
+ }
720
+ Git::command_close_pipe(@self, $fh, $ctx);
721
+ return \%refs;
722
+}
723
+
724
+
725
=item ident ( TYPE | IDENTSTR )
726
727
=item ident_person ( TYPE | IDENTSTR | IDENTARRAY )