chiark / gitweb /
Introduce git_for_each_tag_referring
[dgit.git] / Debian / Dgit.pm
index 8b29ba2746be30267b9cef7cf73ff60b3d95e25e..e2f8edd05848c76f74e13c65d6561e39df1d1e48 100644 (file)
@@ -12,7 +12,7 @@ BEGIN {
     $VERSION     = 1.00;
     @ISA         = qw(Exporter);
     @EXPORT      = qw(debiantag server_branch server_ref
-                      stat_exists
+                      stat_exists git_for_each_ref
                       $package_re $branchprefix);
     %EXPORT_TAGS = ( policyflags => qw() );
     @EXPORT_OK   = qw();
@@ -51,4 +51,28 @@ sub stat_exists ($) {
     die "stat $f: $!";
 }
 
+sub git_for_each_ref ($$) {
+    my ($pattern,$func) = @_;
+    # calls $func->($objid,$objtype,$fullrefname,$reftail);
+    # $reftail is RHS of ref after refs/\w+/
+    # breaks if $pattern matches any ref `refs/blah' where blah has no `/'
+    my $fh = new IO::File, "-|", qw(git for-each-ref), $pattern or die $!;
+    while (<$fh>) {
+       m#^(\w+)\s+(\w+)\s+(refs/\w+/(\S+))\s# or die "$_ ?";
+       $func->($1,$2,$3,$4);
+    }
+    $!=0; $?=0; close $fh or die "$pattern $? $!";
+}
+
+sub git_for_each_tag_referring ($$) {
+    my ($objreferring, $func) = @_;
+    # calls $func->($objid,$fullrefname,$tagname);
+    git_for_each_ref('refs/tags', sub {
+       my ($objid,$objtype,$fullrefname,$tagname) = @_;
+       next unless $objtype eq 'tag';
+       next if defined $objreferring and $objid ne $objreferring;
+       $func->($objid,$fullrefname,$tagname);
+    });
+}
+
 1;