chiark / gitweb /
Policy hook protocol: define bits in hex (nfc)
[dgit.git] / Debian / Dgit.pm
index 3fe0309304feb8b8d8dd8d47b311b4c4fd0318ec..1ab65a8f24c6fd1da07d25bd03e1916f81f16f4d 100644 (file)
@@ -1,25 +1,37 @@
-#
+# -*- perl -*-
 
 package Debian::Dgit;
 
 use strict;
 use warnings;
 
+use POSIX;
+
 BEGIN {
     use Exporter   ();
     our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
 
     $VERSION     = 1.00;
     @ISA         = qw(Exporter);
-    @EXPORT      = qw(debiantag
-                      $package_re);
-    %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
-    @EXPORT_OK   = qw();
+    @EXPORT      = qw(debiantag server_branch server_ref
+                      stat_exists git_for_each_ref
+                      $package_re $component_re $branchprefix);
+    %EXPORT_TAGS = ( policyflags => [qw(NOFFCHECK FRESHREPO)] );
+    @EXPORT_OK   = @{ $EXPORT_TAGS{policyflags} };
 }
 
 our @EXPORT_OK;
 
 our $package_re = '[0-9a-z][-+.0-9a-z]*';
+our $component_re = '[0-9a-zA-Z][-+.0-9a-zA-Z]*';
+our $branchprefix = 'dgit';
+
+# policy hook exit status bits
+# see dgit-repos-server head comment for documentation
+# 1 is reserved in case something fails with `exit 1'
+sub NOFFCHECK () { return 0x2; }
+sub FRESHREPO () { return 0x4; }
+# 0x80 is reserved
 
 sub debiantag ($) { 
     my ($v) = @_;
@@ -27,4 +39,38 @@ 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 $? $!";
+}
+
+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;