chiark / gitweb /
Introduce git_for_each_ref
[dgit.git] / Debian / Dgit.pm
index b93477402c72dc3a3662c7a99cab81d6735b66b2..4d9b81aa83af274469dbb2f302f090a82a9e46b8 100644 (file)
@@ -11,8 +11,9 @@ BEGIN {
 
     $VERSION     = 1.00;
     @ISA         = qw(Exporter);
-    @EXPORT      = qw(debiantag
-                      $package_re);
+    @EXPORT      = qw(debiantag server_branch server_ref
+                      stat_exists git_for_each_ref
+                      $package_re $branchprefix);
     %EXPORT_TAGS = ( policyflags => qw() );
     @EXPORT_OK   = qw();
 }
@@ -20,6 +21,7 @@ BEGIN {
 our @EXPORT_OK;
 
 our $package_re = '[0-9a-z][-+.0-9a-z]*';
+our $branchprefix = 'dgit';
 
 
 # policy hook exit status bits
@@ -39,4 +41,27 @@ sub debiantag ($) {
     return "debian/$v";
 }
 
+sub server_branch ($) { return "$branchprefix/$_[0]"; }
+sub server_ref ($) { return "refs/".server_branch($_[0]); }
+
+sub stat_exists ($) {
+    my ($f) = @_;
+    return 1 if stat $f;
+    return 0 if $!==&ENOENT;
+    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 $? $!";
+}
+
 1;