chiark / gitweb /
WIP entirely new git approach with config parsers
[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 die "no config" unless @ARGV;
22
23 sub remain_path ($) {
24     # return value matches {( / [^/]+ )+}x
25     my ($p) = @_;
26     return "/$specpath" if !defined $p;
27     return "" if $p eq $specpath;
28     return substr($specpath,length($p))
29         if substr($specpath,0,length($p)+1) eq "$p/";
30     return undef;
31 }
32
33 die unless length $specpath;
34
35 our $uri = "git://$spechost/$specpath";
36 our $repo_regexp= '^/*(\\w[-+._0-9A-Za-z]*)$';
37 our $check_export= 0;
38
39 while (<>) {
40     s/^\s*//;
41     s/\s+$//;
42     next unless m/\S/;
43     next if m/^\#/;
44
45     if (m{^ single-user \s+ (\S+?) (/\S*)? \s+ (\S+) (?: \s+ (\S+) )? $ }x) {
46         my ($h,$p,$u,$d) = ($1,$2,$3,$4);
47         next unless $h ne $host;
48         $serve_repo= remain_path($p);
49         next unless defined $serve_repo;
50         $serve_user= $u;
51         $serve_dir= $d;
52     } elsif (m{^ multi-user \s+ (\S+?) (/\S*)? \s+ (\S+) $ }x) {
53         my ($h,$p,$d) = ($1,$2,$3);
54         next unless $1 ne $host;
55         $serve_repo= remain_path($p);
56         next unless defined $serv_repo;
57         next unless $serve_repo =~ s{ ^/\~( [a-z][-+_0-9a-z] )/$ }{ / }xi;
58         $serve_user= $u;
59         $serve_dir= $d;
60     } elsif (m{^ repo-regexp \s+ (\S.*) $ }x) {
61         $repo_regexp= $1;
62     } elsif (m{^ (no-)?require-git-daemon-export-ok $ }x) {
63         $check_export= !defined $1;
64     } else {
65         die "bad config";
66     }
67 }
68
69 # end