chiark / gitweb /
Debugging: use typeglobs to simplify various things (nfc)
[dgit.git] / Debian / Dgit.pm
1 # -*- perl -*-
2
3 package Debian::Dgit;
4
5 use strict;
6 use warnings;
7
8 use POSIX;
9 use IO::Handle;
10
11 BEGIN {
12     use Exporter   ();
13     our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
14
15     $VERSION     = 1.00;
16     @ISA         = qw(Exporter);
17     @EXPORT      = qw(debiantag server_branch server_ref
18                       stat_exists git_for_each_ref
19                       $package_re $component_re $branchprefix
20                       initdebug enabledebug printdebug debugcmd
21                       $debugprefix *debuglevel *DEBUG
22                       shellquote printcmd);
23     %EXPORT_TAGS = ( policyflags => [qw(NOFFCHECK FRESHREPO)] );
24     @EXPORT_OK   = @{ $EXPORT_TAGS{policyflags} };
25 }
26
27 our @EXPORT_OK;
28
29 our $package_re = '[0-9a-z][-+.0-9a-z]*';
30 our $component_re = '[0-9a-zA-Z][-+.0-9a-zA-Z]*';
31 our $branchprefix = 'dgit';
32
33 # policy hook exit status bits
34 # see dgit-repos-server head comment for documentation
35 # 1 is reserved in case something fails with `exit 1'
36 sub NOFFCHECK () { return 0x2; }
37 sub FRESHREPO () { return 0x4; }
38 # 0x80 is reserved
39
40 sub debiantag ($) { 
41     my ($v) = @_;
42     $v =~ y/~:/_%/;
43     return "debian/$v";
44 }
45
46 sub server_branch ($) { return "$branchprefix/$_[0]"; }
47 sub server_ref ($) { return "refs/".server_branch($_[0]); }
48
49 sub stat_exists ($) {
50     my ($f) = @_;
51     return 1 if stat $f;
52     return 0 if $!==&ENOENT;
53     die "stat $f: $!";
54 }
55
56 sub git_for_each_ref ($$) {
57     my ($pattern,$func) = @_;
58     # calls $func->($objid,$objtype,$fullrefname,$reftail);
59     # $reftail is RHS of ref after refs/\w+/
60     # breaks if $pattern matches any ref `refs/blah' where blah has no `/'
61     my $fh = new IO::File "-|", qw(git for-each-ref), $pattern or die $!;
62     while (<$fh>) {
63         m#^(\w+)\s+(\w+)\s+(refs/\w+/(\S+))\s# or die "$_ ?";
64         $func->($1,$2,$3,$4);
65     }
66     $!=0; $?=0; close $fh or die "$pattern $? $!";
67 }
68
69 sub git_for_each_tag_referring ($$) {
70     my ($objreferring, $func) = @_;
71     # calls $func->($objid,$fullrefname,$tagname);
72     git_for_each_ref('refs/tags', sub {
73         my ($objid,$objtype,$fullrefname,$tagname) = @_;
74         next unless $objtype eq 'tag';
75         next if defined $objreferring and $objid ne $objreferring;
76         $func->($objid,$fullrefname,$tagname);
77     });
78 }
79
80 our $debugprefix;
81 our $debuglevel = 0;
82
83 sub initdebug ($) { 
84     ($debugprefix) = @_;
85     open DEBUG, ">/dev/null" or die $!;
86 }
87
88 sub enabledebug () {
89     open DEBUG, ">&STDERR" or die $!;
90     DEBUG->autoflush(1);
91     $debuglevel ||= 1;
92 }
93     
94 sub printdebug {
95     print DEBUG $debugprefix, @_ or die $! if $debuglevel>0;
96 }
97
98 sub shellquote {
99     my @out;
100     local $_;
101     foreach my $a (@_) {
102         $_ = $a;
103         if (m{[^-=_./0-9a-z]}i) {
104             s{['\\]}{'\\$&'}g;
105             push @out, "'$_'";
106         } else {
107             push @out, $_;
108         }
109     }
110     return join ' ', @out;
111 }
112
113 sub printcmd {
114     my $fh = shift @_;
115     my $intro = shift @_;
116     print $fh $intro," " or die $!;
117     print $fh shellquote @_ or die $!;
118     print $fh "\n" or die $!;
119 }
120
121 sub debugcmd {
122     my $extraprefix = shift @_;
123     printcmd(\*DEBUG,$debugprefix.$extraprefix,@_) if $debuglevel>0;
124 }
125
126 1;