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