chiark / gitweb /
0fd760f8c2c95b91756507c5ad528351ea2bf1b7
[userv-utils.git] / git-daemon / read-urlmap
1 # -*- perl -*-
2
3 # uses:
4 #     $specpath    whole path from caller, minus any leading /s
5 #     $spechost    host from caller
6 #
7 # sets:
8 #
9 #  always:
10 #     $uri
11 #
12 #  if match found for this host and path:
13 #     $serve_user   username, or undef if no match (then other serve_* invalid)
14 #     $serve_dir    directory as specified in config
15 #     $serve_repo   subpath under $serve_dir _including_ leading /
16 #
17 #  for use by user's service program
18 #     $repo_regexp
19 #     $require_exportok
20
21 sub remain_path ($) {
22     # return value matches {( / [^/]+ )+}x
23     my ($vsubpath) = @_;
24     syslog 'debug', sprintf "DEBUG remain_path %s $specpath",
25                               (defined $vsubpath ? $vsubpath : '<undef>');
26     return "/$specpath" if !defined $vsubpath;
27     return "" if $vsubpath eq $specpath;
28     return substr($specpath,length($vsubpath))
29         if substr($specpath,0,length($vsubpath)+1) eq "$vsubpath/";
30     return undef;
31 }
32
33 fail "no config ??" unless @ARGV;
34 fail "no specpath ??" unless length $specpath;
35
36 our $uri = "git://$spechost/$specpath";
37 our $repo_regexp= '^/*(\\w[-+._0-9A-Za-z]*)$';  # stupid emacs ';
38 our $check_export= 0;
39
40 our ($serve_user, $serve_dir, $serve_repo);
41
42 sub fexists ($) {
43     my ($f) = @_;
44     if (stat $f) {
45         -f _ or fail "bad config $_ - not a file";
46         return 1;
47     } else {
48         $!==&ENOENT or fail "bad config $_ - could not stat: $!";
49         return 0;
50     }
51 }
52
53 @ARGV = grep { fexists($_) } @ARGV;
54
55 while (<>) {
56
57     s/^\s*//;
58     s/\s+$//;
59     next unless m/\S/;
60     next if m/^\#/;
61
62     if (m{^ single-user \s+ (\S+?) (/\S*)? \s+ (\S+) (?: \s+ (\S+) )? $ }x) {
63         my ($h,$v,$u,$d) = ($1,$2,$3,$4);
64         next unless $h eq $spechost;
65         $serve_repo= remain_path($v);
66         next unless defined $serve_repo;
67         $serve_user= $u;
68         $serve_dir= $d;
69         syslog 'debug', "DEBUG $ARGV:$. match".
70             " $serve_user $serve_dir $serve_repo";
71     } elsif (m{^ multi-user \s+ (\S+?) (/\S*)? \s+ (\S+) $ }x) {
72         my ($h,$v,$d) = ($1,$2,$3);
73         next unless $1 eq $spechost;
74         $serve_repo= remain_path($v);
75         next unless defined $serve_repo;
76         syslog 'debug', "DEBUG $ARGV:$. perhaps $serve_repo";
77         next unless $serve_repo =~ s{ ^/\~( [a-z][-+_0-9a-z]* )/ }{/}xi;
78         $serve_user= $1;
79         $serve_dir= $d;
80         syslog 'debug', "DEBUG $ARGV:$. match".
81             " $serve_user $serve_dir $serve_repo";
82     } elsif (m{^ repo-regexp \s+ (\S.*) $ }x) {
83         $repo_regexp= $1;
84     } elsif (m{^ (no-)?require-git-daemon-export-ok $ }x) {
85         $check_export= !defined $1;
86     } else {
87         fail "config syntax error at $ARGV:$.";
88     }
89 }
90
91 # end