chiark / gitweb /
c39ff72e6e2c9e388f3c0a473244f42b02500e38
[dgit.git] / Debian / Dgit.pm
1 # -*- perl -*-
2
3 package Debian::Dgit;
4
5 use strict;
6 use warnings;
7
8 use POSIX;
9
10 BEGIN {
11     use Exporter   ();
12     our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
13
14     $VERSION     = 1.00;
15     @ISA         = qw(Exporter);
16     @EXPORT      = qw(debiantag server_branch server_ref
17                       stat_exists git_for_each_ref
18                       $package_re $component_re $branchprefix);
19     %EXPORT_TAGS = ( policyflags => [qw(NOFFCHECK FRESHREPO)] );
20     @EXPORT_OK   = @{ $EXPORT_TAGS{policyflags} };
21 }
22
23 our @EXPORT_OK;
24
25 our $package_re = '[0-9a-z][-+.0-9a-z]*';
26 our $component_re = '[0-9a-zA-Z][-+.0-9a-zA-Z]*';
27 our $branchprefix = 'dgit';
28
29 # policy hook exit status bits
30 # see dgit-repos-server head comment for documentation
31 # 1 is reserved in case something fails with `exit 1'
32 sub NOFFCHECK () { return 2; }
33 sub FRESHREPO () { return 4; }
34 # 128 is reserved
35
36 sub debiantag ($) { 
37     my ($v) = @_;
38     $v =~ y/~:/_%/;
39     return "debian/$v";
40 }
41
42 sub server_branch ($) { return "$branchprefix/$_[0]"; }
43 sub server_ref ($) { return "refs/".server_branch($_[0]); }
44
45 sub stat_exists ($) {
46     my ($f) = @_;
47     return 1 if stat $f;
48     return 0 if $!==&ENOENT;
49     die "stat $f: $!";
50 }
51
52 sub git_for_each_ref ($$) {
53     my ($pattern,$func) = @_;
54     # calls $func->($objid,$objtype,$fullrefname,$reftail);
55     # $reftail is RHS of ref after refs/\w+/
56     # breaks if $pattern matches any ref `refs/blah' where blah has no `/'
57     my $fh = new IO::File "-|", qw(git for-each-ref), $pattern or die $!;
58     while (<$fh>) {
59         m#^(\w+)\s+(\w+)\s+(refs/\w+/(\S+))\s# or die "$_ ?";
60         $func->($1,$2,$3,$4);
61     }
62     $!=0; $?=0; close $fh or die "$pattern $? $!";
63 }
64
65 sub git_for_each_tag_referring ($$) {
66     my ($objreferring, $func) = @_;
67     # calls $func->($objid,$fullrefname,$tagname);
68     git_for_each_ref('refs/tags', sub {
69         my ($objid,$objtype,$fullrefname,$tagname) = @_;
70         next unless $objtype eq 'tag';
71         next if defined $objreferring and $objid ne $objreferring;
72         $func->($objid,$fullrefname,$tagname);
73     });
74 }
75
76 1;