8 use Girocco
::ConfigUtil
;
9 use Girocco
::TimedToken
;
10 use Girocco
::ValidUtil
;
12 use Scalar
::Util
qw(looks_like_number);
16 use base
qw(Exporter);
17 our @EXPORT = qw(get_git scrypt jailed_file sendmail_pipe mailer_pipe
18 lock_file unlock_file valid_tag rand_adjust
19 filedb_atomic_append filedb_atomic_edit filedb_grep
20 filedb_atomic_grep valid_email valid_email_multi
21 valid_repo_url valid_web_url url_base url_path url_server
22 projects_html_list parse_rfc2822_date parse_any_date
23 extract_url_hostname is_dns_hostname is_our_hostname
24 get_cmd online_cpus sys_pagesize sys_memsize
25 calc_windowmemory to_utf8 capture_command human_size
26 calc_bigfilethreshold has_reserved_suffix human_duration
27 noFatalsToBrowser calc_redeltathreshold
28 clean_email_multi read_HEAD_symref read_config_file
29 read_config_file_hash is_git_dir git_bool util_path
30 is_shellish read_HEAD_ref git_add_config to_json
31 json_bool from_json ref_indicator get_token_key
32 get_timed_token get_token_field check_timed_token
33 valid_branch_name get_project_from_dir
37 BEGIN {require "Girocco/extra/capture_command.pl"}
39 # Return the entire output sent to stdout from running a command
40 # Any output the command sends to stderr is discarded
41 # Returns undef if there was an error running the command (see $!)
43 my ($status, $result) = capture_command
(1, undef, @_);
44 return defined($status) && $status == 0 ?
$result : undef;
47 # Same as get_cmd except configured git binary is automatically provided
48 # as the first argument to get_cmd
50 return get_cmd
($Girocco::Config
::git_bin
, @_);
53 # Same as get_git except that the result (if not undef) is chomp'd before
56 my $ans = get_git
(@_);
57 defined($ans) and chomp $ans;
63 crypt($pwd||'', join ('', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]));
69 $Girocco::Config
::chroot."/$filename";
78 use Fcntl
qw(O_WRONLY O_CREAT O_EXCL);
80 my $handle = new IO
::Handle
;
82 unless (sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
84 while (not sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
85 ($! == EEXIST
) or die "$path open failed: $!";
86 ($cnt++ < 16) or die "$path open failed: cannot open lockfile";
90 # XXX: filedb-specific
91 chmod 0664, $path or die "$path g+w failed: $!";
97 return defined($_[0]) && $_[0] eq jailed_file
('/etc/passwd');
100 sub _run_update_pwd_db
{
101 my ($path, $updatearg) = @_;
102 my @cmd = ($Girocco::Config
::basedir
.'/bin/update-pwd-db', "$path");
103 push(@cmd, $updatearg) if $updatearg;
104 system(@cmd) == 0 or die "update-pwd-db failed: $?";
108 my ($path, $noreplace, $updatearg) = @_;
111 _run_update_pwd_db
("$path.lock", $updatearg)
112 if $Girocco::Config
::update_pwd_db
&& _is_passwd_file
($path);
113 rename "$path.lock", $path or die "$path unlock failed: $!";
115 unlink "$path.lock" or die "$path unlock failed: $!";
119 sub filedb_atomic_append
{
120 my ($file, $line, $updatearg) = @_;
123 open my $src, '<', $file or die "$file open for reading failed: $!";
124 my $dst = lock_file
($file);
127 my $aid = (split /:/)[2];
128 $id = $aid + 1 if ($aid >= $id);
130 print $dst $_ or die "$file(l) write failed: $!";
133 $line =~ s/\\i/$id/g;
134 print $dst "$line\n" or die "$file(l) write failed: $!";
136 close $dst or die "$file(l) close failed: $!";
139 unlock_file
($file, 0, $updatearg);
144 sub filedb_atomic_edit
{
145 my ($file, $fn, $updatearg) = @_;
147 open my $src, '<', $file or die "$file open for reading failed: $!";
148 my $dst = lock_file
($file);
151 print $dst $fn->($_) or die "$file(l) write failed: $!";
154 close $dst or die "$file(l) close failed: $!";
157 unlock_file
($file, 0, $updatearg);
160 sub filedb_atomic_grep
{
161 my ($file, $fn) = @_;
164 open my $src, '<', $file or die "$file open for reading failed: $!";
165 my $dst = lock_file
($file);
168 my $result = $fn->($_);
169 push(@results, $result) if $result;
172 close $dst or die "$file(l) close failed: $!";
175 unlock_file
($file, 1);
180 my ($file, $fn) = @_;
183 open my $src, '<', $file or die "$file open for reading failed: $!";
186 my $result = $fn->($_);
187 push(@results, $result) if $result;
197 defined($email) or $email = '';
198 return $email =~ /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9.-]+$/;
201 sub clean_email_multi
{
203 defined($input) or $input = '';
204 $input =~ s/^\s+//; $input =~ s/\s+$//;
207 foreach (split(/\s*,\s*/, $input)) {
209 $seen{lc($_)} = 1, push(@newlist, $_) unless $seen{lc($_)};
211 return join(",", @newlist);
214 sub valid_email_multi
{
215 # each email address must be a valid_email but we silently
216 # ignore extra spaces at the beginning/end and around any comma(s)
217 foreach (split(/,/, clean_email_multi
(shift))) {
218 return 0 unless valid_email
($_);
225 defined($url) or $url = '';
227 /^https?:\/\
/[a-zA-Z0-9.:-]+(\/[_\
%a-zA
-Z0
-9.\
/~:?&=;-]*)?(#[a-zA-Z0-9._-]+)?$/;
231 my $url = shift || '';
232 # Currently neither username nor password is allowed in the URL (except for svn)
233 # and IPv6 literal addresses are not accepted either.
234 $Girocco::Config
::mirror_svn
&&
235 $url =~ /^svn(\+https?)?:\/\
/([^\@\/\s
]+\@
)?
[a
-zA
-Z0
-9.:-]+(\
/[_\%a-zA-Z0-9.\/+~-]*)?
$/os
237 $Girocco::Config
::mirror_darcs
&&
238 $url =~ /^darcs(?:\+https?)?:\/\
/[a-zA-Z0-9.:-]+(\/[_\
%a-zA
-Z0
-9.\
/+~-]*)?$/os
240 $Girocco::Config
::mirror_bzr
&&
241 $url =~ /^bzr:\/\
/[a-zA-Z0-9.:-]+(\/[_\
%a-zA
-Z0
-9.\
/+~-]*)?$/os
243 $Girocco::Config
::mirror_hg
&&
244 $url =~ /^hg\+https?:\/\
/[a-zA-Z0-9.:-]+(\/[_\
%a-zA
-Z0
-9.\
/+~-]*)?$/os
246 return $url =~ /^(https?|git):\/\
/[a-zA-Z0-9.:-]+(\/[_\
%a-zA
-Z0
-9.\
/+~-]*)?$/;
249 sub extract_url_hostname
{
250 my $url = shift || '';
251 if ($url =~ m
,^bzr
://,) {
253 return 'launchpad.net' if $url =~ /^lp:/;
255 return undef unless $url =~ m
,^[A
-Za
-z0
-9+.-]+://[^/],;
256 $url =~ s
,^[A
-Za
-z0
-9+.-]+://,,;
257 $url =~ s
,^([^/]+).*$,$1,;
258 $url =~ s/:[0-9]*$//;
259 $url =~ s/^[^\@]*[\@]//;
260 return $url ?
$url : undef;
264 # RFC 1034 section 3.5
265 # RFC 1123 section 2.1
266 # RFC 1738 section 3.1
267 # RFC 2606 sections 2 & 3
268 # RFC 3986 section 3.2.2
269 sub is_dns_hostname
{
271 defined($host) or $host = '';
272 return 0 if $host eq '' || $host =~ /\s/;
273 # first remove a trailing '.'
275 return 0 if length($host) > 255;
276 my $octet = '(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])';
277 return 0 if $host =~ /^$octet\.$octet\.$octet\.$octet$/o;
278 my @labels = split(/[.]/, $host, -1);
279 return 0 unless @labels && @labels >= $Girocco::Config
::min_dns_labels
;
280 # now check each label
281 foreach my $label (@labels) {
282 return 0 unless length($label) > 0 && length($label) <= 63;
283 return 0 unless $label =~ /^[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?$/;
285 # disallow RFC 2606 names provided at least two labels are present
287 my $tld = lc($labels[-1]);
293 my $sld = lc($labels[-2]);
294 return 0 if $sld eq 'example' &&
295 ($tld eq 'com' || $tld eq 'net' || $tld eq 'org');
300 sub is_our_hostname
{
301 my $test = shift || '';
305 $Girocco::Config
::gitweburl
,
306 $Girocco::Config
::gitwebfiles
,
307 $Girocco::Config
::webadmurl
,
308 $Girocco::Config
::bundlesurl
,
309 $Girocco::Config
::htmlurl
,
310 $Girocco::Config
::httppullurl
,
311 $Girocco::Config
::httpbundleurl
,
312 $Girocco::Config
::httpspushurl
,
313 $Girocco::Config
::gitpullurl
,
314 $Girocco::Config
::pushurl
316 foreach my $url (@urls) {
318 my $host = extract_url_hostname
($url);
319 if (defined($host)) {
321 $names{lc($host)} = 1;
325 return $names{lc($test)} ?
1 : 0;
328 my (%_oktags, %_badtags, %_canontags, $_canontagscreated, @_whitetags);
330 # These are always okay (a "whitelist") even if they would
331 # otherwise not be allowed
333 .net 2d 3d 6502 68000 68008 68010 68020 68030 68040 68060
334 8086 80286 80386 80486 80586 c cc make www x
336 map({$_oktags{lc($_)}=1} @_whitetags, @Girocco::Config
::allowed_tags
);
337 # entries MUST be all lowercase to be effective
339 # These are "nonsense" or pointless tags
340 about
=>1, after
=>1, all
=>1, also
=>1, an
=>1, and=>1, another
=>1, any
=>1,
341 are
=>1, as
=>1, at
=>1, be
=>1, because
=>1, been
=>1, before
=>1, being
=>1,
342 between
=>1, both
=>1, but
=>1, by
=>1, came
=>1, can
=>1, come
=>1, could
=>1,
343 did
=>1, do=>1, each=>1, for=>1, from
=>1, get
=>1, got
=>1, had
=>1, has
=>1,
344 have
=>1, he
=>1, her
=>1, here
=>1, him
=>1, himself
=>1, his
=>1, how
=>1,
345 if=>1, in=>1, into
=>1, is
=>1, it
=>1, like
=>1, make
=>1, many
=>1, me
=>1,
346 might
=>1, more
=>1, most
=>1, much
=>1, must
=>1, my=>1, never
=>1, now
=>1,
347 of
=>1, oh
=>1, on
=>1, only
=>1, or=>1, other
=>1, our=>1, out
=>1, over
=>1,
348 said
=>1, same
=>1, see
=>1, should
=>1, since
=>1, some
=>1, still
=>1,
349 such
=>1, take
=>1, than
=>1, that
=>1, the
=>1, their
=>1, them
=>1, then
=>1,
350 there
=>1, these
=>1, they
=>1, this
=>1, those
=>1, through
=>1, to
=>1,
351 too
=>1, under
=>1, up
=>1, very
=>1, was
=>1, way
=>1, we
=>1, well
=>1,
352 were
=>1, what
=>1, where
=>1, which
=>1, while=>1, who
=>1, with
=>1,
353 would
=>1, yea
=>1, yeah
=>1, you
=>1, your
=>1, yup
=>1
355 # These are "offensive" tags with at least one letter escaped to
356 # avoid having this file trigger various safe-scan robots
357 $_badtags{"a\x73\x73"} = 1;
358 $_badtags{"a\x73\x73hole"} = 1;
359 $_badtags{"b\x30\x30b"} = 1;
360 $_badtags{"b\x30\x30bs"} = 1;
361 $_badtags{"b\x6f\x6fb"} = 1;
362 $_badtags{"b\x6f\x6fbs"} = 1;
363 $_badtags{"b\x75tt"} = 1;
364 $_badtags{"b\x75ttd\x69\x63k"} = 1;
365 $_badtags{"c\x6f\x63k"} = 1;
366 $_badtags{"c\x75\x6e\x74"} = 1;
367 $_badtags{"d\x69\x63k"} = 1;
368 $_badtags{"d\x69\x63kb\x75tt"} = 1;
369 $_badtags{"f\x75\x63k"} = 1;
370 $_badtags{"in\x63\x65st"} = 1;
371 $_badtags{"ph\x75\x63k"} = 1;
372 $_badtags{"p\x6f\x72n"} = 1;
373 $_badtags{"p\x6f\x72no"} = 1;
374 $_badtags{"p\x6f\x72nographic"} = 1;
375 $_badtags{"p\x72\x30n"} = 1;
376 $_badtags{"p\x72\x6fn"} = 1;
377 $_badtags{"r\x61\x70e"} = 1;
378 $_badtags{"s\x65\x78"} = 1;
379 map({$_badtags{lc($_)}=1} @Girocco::Config
::blocked_tags
);
382 # A valid tag must only have [a-zA-Z0-9:.+#_-] characters, must start with a
383 # letter, must not be a noise word, must be more than one character long,
384 # must not be a repeated letter and must be no more than 32 characters long.
385 # However, anything in %_oktags is explicitly allowed even if it otherwise
386 # would violate the rules (except that none of [,\s\\\/] are allowed in tags).
387 # Returns the canonical name for the tag if the tag is valid otherwise undef.
388 # Tags consisting of only underscore characters are never valid unless in %_oktags.
391 return undef unless defined($_) && $_ ne "" && !/[,\s\/\\]/;
392 my $fold = $Girocco::Config
::foldtags
;
393 if ($fold && !$_canontagscreated) {
396 $_canontags{lc($_)} = $_ foreach sort({$b cmp $a} @_whitetags, @Girocco::Config
::allowed_tags
);
397 $_canontagscreated = 1;
399 return $_canontags{lc($_)} if $fold && exists($_canontags{lc($_)});
400 return ($fold ?
lc($_) : $_) if $_oktags{lc($_)};
401 return undef unless /^[a-zA-Z][a-zA-Z0-9:.+#_-]+$/;
404 return undef if $cu eq "" || $_badtags{lc($_)} || $_badtags{lc($cu)};
405 return undef if /^(.)\1+$/ || $cu =~ /^(.)\1+$/;
406 return length($_) <= 32 ?
($fold ?
lc($_) : $_) : undef;
409 # If the passed in argument looks like a URL, return only the stuff up through
410 # the host:port part otherwise return the entire argument.
412 my $url = shift || '';
414 $url = $1.$2.$3.$4 if $url =~ m
,^( [A
-Za
-z
][A
-Za
-z0
-9+.-]*: ) # scheme
415 ( // ) # // separator
416 ((?
:[^\@
]+\@
)?
) # optional userinfo
417 ( [^/?
#]+ ) # host and port
418 (?
:[/?#].*)?$,x; # path and optional query string and/or anchor
422 # If the passed in argument looks like a URL, return only the stuff following
423 # the host:port part otherwise return the entire argument.
424 # If the optional second argument is true, the returned value will have '/'
425 # appended if it does not already end in '/'.
427 my $url = shift || '';
428 my $add_slash = shift || 0;
430 $url = $1 if $url =~ m
,^(?
: [A
-Za
-z
][A
-Za
-z0
-9+.-]*: ) # scheme
431 (?
: // ) # // separator
432 (?
: [^\@
]+\@
)?
# optional userinfo
433 (?
: [^/?
#]+ ) # host and port
434 ((?
:[/?#].*)?)$,x; # path and optional query string and/or anchor
435 $url .= '/' if $add_slash && $url !~ m
|/$|;
439 # If both SERVER_NAME and SERVER_PORT are set pass the argument through url_path
440 # and then prefix it with the appropriate scheme (HTTPS=?on), host and port and
441 # return it. If a something that doesn't look like it could be the start of a
442 # URL path comes back from url_path or SERVER_NAME is a link-local IPv6 address
443 # then just return the argument unchanged.
445 my $url = shift || '';
446 my $path = url_path
($url);
447 return $url unless $path eq '' || $path =~ m
|^[/?
#]|;
448 return $url unless $ENV{'SERVER_NAME'} && $ENV{'SERVER_PORT'} &&
449 $ENV{'SERVER_PORT'} =~ /^[1-9][0-9]{0,4}$/;
450 return $url if $ENV{'SERVER_NAME'} =~ /^[[]?fe80:/i;
451 my $server = $ENV{'SERVER_NAME'};
452 # Deal with Apache bug where IPv6 literal server names do not include
453 # the required surrounding '[' and ']' characters
454 $server = '[' . $server . ']' if $server =~ /:/ && $server !~ /^[[]/;
455 my $ishttps = $ENV{'HTTPS'} && $ENV{'HTTPS'} =~ /^on$/i;
456 my $portnum = 0 + $ENV{'SERVER_PORT'};
458 if (($ishttps && $portnum != 443) || (!$ishttps && $portnum != 80)) {
459 $port = ':' . $portnum;
461 return 'http' . ($ishttps ?
's' : '') . '://' . $server . $port . $path;
464 # Returns the number rounded to the nearest tenths. The ".d" part will be
465 # excluded if it's ".0" unless the optional second argument is true
472 return '' . int($v/10) unless $v % 10 || $use0;
473 return '' . int($v/10) . '.' . ($v%10);
476 # Returns a human-readable size string (e.g. '1.5 MiB') for the value
477 # (in bytes) passed in. Returns '0' for undefined or 0 or not all digits.
478 # Otherwise returns '1 KiB' for < 1024, or else a number rounded to the
479 # nearest tenths of a KiB, MiB or GiB.
482 return "0" unless $v && $v =~ /^\d+$/;
483 return "1 KiB" unless $v > 1024;
485 return _tenths
($v) . " KiB" if $v < 1024;
487 return _tenths
($v) . " MiB" if $v < 1024;
489 return _tenths
($v) . " GiB";
492 # Returns a human duration string (e.g. 1h10m5s for the value (in secs)
493 # passed in. Returns the value unchanged if it's not defined or <= 0.
496 return $secs unless defined($secs) && $secs >= 0;
498 my $ans = ($secs % 60) . 's';
499 return $ans if $secs < 60;
500 $secs = int($secs / 60);
501 $ans = ($secs % 60) . 'm' . $ans;
502 return $ans if $secs < 60;
503 $secs = int($secs / 60);
504 $ans = ($secs % 24) . 'h' . $ans;
505 return $ans if $secs < 24;
506 $secs = int($secs / 24);
507 return $secs . 'd' . $ans;
512 $str =~ s/\&/\&/gs;
513 $str =~ s/\</\</gs;
514 $str =~ s/\>/\>/gs;
515 $str =~ s/\"/\"/gs; #"
519 # create relative time string from passed in age in seconds
524 if ($age > 60*60*24*365*2) {
525 $age_str = (int $age/60/60/24/365);
526 $age_str .= " years ago";
527 } elsif ($age > 60*60*24*(365/12)*2) {
528 $age_str = int $age/60/60/24/(365/12);
529 $age_str .= " months ago";
530 } elsif ($age > 60*60*24*7*2) {
531 $age_str = int $age/60/60/24/7;
532 $age_str .= " weeks ago";
533 } elsif ($age > 60*60*24*2) {
534 $age_str = int $age/60/60/24;
535 $age_str .= " days ago";
536 } elsif ($age > 60*60*2) {
537 $age_str = int $age/60/60;
538 $age_str .= " hours ago";
539 } elsif ($age > 60*2) {
540 $age_str = int $age/60;
541 $age_str .= " mins ago";
544 $age_str .= " secs ago";
545 } elsif ($age >= 0) {
546 $age_str = "right now";
548 $age_str = "future time";
553 # create relative time string from passed in idle in seconds
555 my $idle_str = _rel_age
(shift);
556 $idle_str =~ s/ ago//;
557 $idle_str = "not at all" if $idle_str eq "right now";
562 use POSIX
qw(strftime);
563 my ($fmt, $secs, $zonesecs) = @_;
564 my ($S,$M,$H,$d,$m,$y) = gmtime($secs + $zonesecs);
565 $zonesecs = int($zonesecs / 60);
567 my $ans = strftime
($fmt, $S, $M, $H, $d, $m, $y, -1, -1, -1);
571 $zonesecs = -$zonesecs;
575 $z .= sprintf("%02d%02d", int($zonesecs/60), $zonesecs % 60);
580 # Take a list of project names and produce a nicely formated table that
581 # includes owner links and descriptions. If the list is empty returns ''.
582 # The first argument may be a hash ref that contains options. The following
583 # options are available:
584 # target -- sets the target value of the owner link
585 # emptyok -- if true returns an empty table rather than ''
586 # sizecol -- if true include a human-readable size column
587 # typecol -- if true include type column with hover info
588 # changed -- if true include a changed and idle column
589 sub projects_html_list
{
591 if (defined($_[0]) && ref($_[0]) eq 'HASH') {
594 return '' unless @_ || (defined($options->{emptyok
}) && $options->{emptyok
});
595 require Girocco
::Project
;
598 $target = " target=\""._escapeHTML
($options->{target
})."\""
599 if defined($options->{target
});
600 my $withsize = defined($options->{sizecol
}) && $options->{sizecol
};
601 my $withtype = defined($options->{typecol
}) && $options->{typecol
};
602 my $withchanged = defined($options->{changed
}) && $options->{changed
};
604 $sizehead = substr(<<EOT, 0, -1) if $withsize;
605 <th class="sizecol"><span class="hover">Size<span><span class="head" _data="Size"></span
606 /><span class="none" /><br />(</span>Fork size excludes objects borrowed from the parent.<span class="none">)</span></span></span></th
610 $typehead = '<th>Type</th>' if $withtype;
612 $chghead = substr(<<EOT, 0, -1) if $withchanged;
613 <th><span class="hover">Changed<span><span class="head" _data="Changed"></span
614 /><span class="none" /><br />(</span>The last time a ref change was received by this site.<span class="none">)</span></span></span></th
615 ><th><span class="hover">Idle<span><span class="head" _data="Idle"></span
616 /><span class="none" /><br />(</span>The most recent committer time in <i>refs/heads</i>.<span class="none">)</span></span></span></th
620 <table class='projectlist'><tr valign="top" align="left"><th>Project</th>$sizehead$typehead$chghead<th class="desc">Description</th></tr>
622 my $trclass = ' class="odd"';
623 foreach (sort({lc($a) cmp lc($b)} @_)) {
624 if (Girocco
::Project
::does_exist
($_, 1)) {
625 my $proj = Girocco
::Project
->load($_);
626 my $projname = $proj->{name
}.".git";
627 my $projdesc = $proj->{desc
}||'';
628 utf8
::decode
($projdesc) if utf8
::valid
($projdesc);
631 my $psize = $proj->{reposizek
};
632 $psize = undef unless defined($psize) && $psize =~ /^\d+$/;
633 $psize = 0 if !defined($psize) && $proj->is_empty;
634 if (!defined($psize)) {
639 $psize = human_size
($psize * 1024);
640 $psize =~ s/ /\ /g;
642 $sizecol = '<td class="sizecol">'.$psize.'</td>';
646 if ($proj->{mirror
}) {
647 my $url = _escapeHTML
($proj->{url
});
648 $typecol = substr(<<EOT, 0, -1);
649 <td class="type"><span class="hover">mirror<span class="nowrap"><span class="before" _data="$url"><span class="none"> <a href="$url" rel="nofollow">(URL)</a></span></span></span></span></td>
652 my $users = @
{$proj->{users
}};
654 $users .= 's' unless @
{$proj->{users
}} == 1;
655 my $userlist = join(', ', sort({lc($a) cmp lc($b)} @
{$proj->{users
}}));
656 my $spncls = length($userlist) > 25 ?
'' : ' class="nowrap"';
657 $typecol = $userlist ?
substr(<<EOT, 0, -1) : substr(<<EOT, 0, -1);
658 <td
class="type"><span
class="hover">$users<span
$spncls><br
class="none" />$userlist</span
></span></td
>
660 <td
class="type">$users</td
>
667 my $changetime = $proj->{lastchange
};
670 $ts = parse_rfc2822_date
($changetime, \
$tz);
671 my $ct = _strftime
("%Y-%m-%d %T %z", $ts, $tz);
672 $rel = "<span class=\"hover\">" .
673 _rel_age
(time - $ts) .
674 "<span class=\"nowrap\"><span class=\"before\" _data=\"$changetime\"></span><span class=\"none\"><br />$ct</span></span></span>";
678 $changecol = substr(<<EOT, 0, -1);
679 <td class="change">$rel</td>
681 my $idletime = $proj->{lastactivity
};
683 $idlesecs = parse_any_date
($idletime, \
$tz) if $idletime;
685 my $idle2822 = _strftime
("%a, %d %b %Y %T %z", $idlesecs, $tz);
686 my $ct = _strftime
("%Y-%m-%d %T %z", $idlesecs, $tz);
687 $rel = "<span class=\"hover\">" .
688 _rel_idle
(time - $idlesecs) .
689 "<span class=\"nowrap\"><span class=\"before\" _data=\"$idle2822\"></span><span class=\"none\"><br />$ct</span></span></span>";
693 $changecol .= substr(<<EOT, 0, -1);
694 <td class="idle">$rel</td>
698 <tr valign="top"$trclass><td><a href="@{[url_path($Girocco::Config::gitweburl)]}/$projname"$target
699 >@{[_escapeHTML($projname)]}</td>$sizecol$typecol$changecol<td>@{[_escapeHTML($projdesc)]}</td></tr>
701 $trclass = $trclass ?
'' : ' class="odd"';
708 return ($count || (defined($options->{emptyok
}) && $options->{emptyok
})) ?
$html : '';
714 jan
=> 0, feb
=> 1, mar
=> 2, apr
=> 3, may
=> 4, jun
=> 5,
715 jul
=> 6, aug
=> 7, sep
=> 8, oct => 9, nov
=> 10, dec
=> 11
719 # Should be in "date '+%a, %d %b %Y %T %z'" format as saved to lastgc, lastrefresh and lastchange
720 # The leading "%a, " is optional, returns undef if unrecognized date. This is also known as
721 # RFC 2822 date format and git's '%cD', '%aD' and --date=rfc2822 format.
722 # If the second argument is a SCALAR ref, its value will be set to the TZ offset in seconds
723 sub parse_rfc2822_date
{
724 my $dstr = shift || '';
725 my $tzoff = shift || '';
726 $dstr = $1 if $dstr =~/^[^\s]+,\s*(.*)$/;
727 return undef unless $dstr =~
728 /^\s*(\d{1,2})\s+([A-Za-z]{3})\s+(\d{4})\s+(\d{1,2}):(\d{2}):(\d{2})\s+([+-]\d{4})\s*$/;
729 my ($d,$b,$Y,$H,$M,$S,$z) = ($1,$2,$3,$4,$5,$6,$7);
730 my $m = $_month_names{lc($b)};
731 return undef unless defined($m);
732 my $seconds = timegm
(0+$S, 0+$M, 0+$H, 0+$d, 0+$m, 0+$Y);
733 my $offset = 60 * (60 * (0+substr($z,1,2)) + (0+substr($z,3,2)));
734 $offset = -$offset if substr($z,0,1) eq '-';
735 $$tzoff = $offset if ref($tzoff) eq 'SCALAR';
736 return $seconds - $offset;
739 # Will parse any supported date format. Actually there are three formats
740 # currently supported:
741 # 1. RFC 2822 (uses parse_rfc2822_date)
742 # 2. RFC 3339 / ISO 8601 (T may be ' ' or '_', 'Z' is optional or may be 'UTC', ':' optional in TZ)
743 # 3. Same as #2 except no colons or hyphens allowed and hours MUST be 2 digits
744 # 4. unix seconds since epoch with optional +/- trailing TZ (may not have a ':')
745 # Returns undef if unsupported date.
746 # If the second argument is a SCALAR ref, its value will be set to the TZ offset in seconds
748 my $dstr = shift || '';
749 my $tzoff = shift || '';
750 if ($dstr =~ /^\s*([-+]?\d+)(?:\s+([-+]\d{4}))?\s*$/) {
756 $off = 60 * (60 * (0+substr($z,1,2)) + (0+substr($z,3,2)));
757 $off = -$off if substr($z,0,1) eq '-';
759 $$tzoff = $off if ref($tzoff) eq 'SCALAR';
762 if ($dstr =~ /^\s*(\d{4})-(\d{2})-(\d{2})[Tt _](\d{1,2}):(\d{2}):(\d{2})(?:[ _]?([Zz]|[Uu][Tt][Cc]|(?:[-+]\d{1,2}:?\d{2})))?\s*$/ ||
763 $dstr =~ /^\s*(\d{4})(\d{2})(\d{2})[Tt _](\d{2})(\d{2})(\d{2})(?:[ _]?([Zz]|[Uu][Tt][Cc]|(?:[-+]\d{2}\d{2})))?\s*$/) {
764 my ($Y,$m,$d,$H,$M,$S,$z) = ($1,$2,$3,$4,$5,$6,$7||'');
765 my $seconds = timegm
(0+$S, 0+$M, 0+$H, 0+$d, $m-1, 0+$Y);
766 defined($z) && $z ne '' or $z = 'Z';
769 substr($z,1,0) = '0' if length($z) == 4;
771 if ($z ne 'Z' && $z ne 'UTC') {
772 $off = 60 * (60 * (0+substr($z,1,2)) + (0+substr($z,3,2)));
773 $off = -$off if substr($z,0,1) eq '-';
775 $$tzoff = $off if ref($tzoff) eq 'SCALAR';
776 return $seconds - $off;
778 return parse_rfc2822_date
($dstr, $tzoff);
781 # Input is a number such as a minute interval
782 # Return value is a random number between the input and 1.25*input
783 # This can be used to randomize the update and gc operations a bit to avoid
784 # having them all end up all clustered together
786 my $input = shift || 0;
787 return $input unless $input;
788 return $input + int(rand(0.25 * $input));
791 # Open a pipe to a new sendmail process. The '-i' option is always passed to
792 # the new process followed by any addtional arguments passed in. Note that
793 # the sendmail process is only expected to understand the '-i', '-t' and '-f'
794 # options. Using any other options via this function is not guaranteed to work.
795 # A list of recipients may follow the options. Combining a list of recipients
796 # with the '-t' option is not recommended.
798 return undef unless @_;
799 die "\$Girocco::Config::sendmail_bin is unset or not executable!\n"
800 unless $Girocco::Config
::sendmail_bin
&& -x
$Girocco::Config
::sendmail_bin
;
801 my $result = open(my $pipe, '|-', $Girocco::Config
::sendmail_bin
, '-i', @_);
802 return $result ?
$pipe : undef;
805 # Open a pipe that works similarly to a mailer such as /usr/bin/mail in that
806 # if the first argument is '-s', a subject line will be automatically added
807 # (using the second argument as the subject). Any remaining arguments are
808 # expected to be recipient addresses that will be added to an explicit To:
809 # line as well as passed on to sendmail_pipe. In addition an
810 # "Auto-Submitted: auto-generated" header is always added as well as a suitable
814 if (@_ >= 2 && $_[0] eq '-s') {
818 my $tolist = join(", ", @_);
819 unshift(@_, '-f', $Girocco::Config
::sender
) if $Girocco::Config
::sender
;
820 my $pipe = sendmail_pipe
(@_);
822 print $pipe "From: \"$Girocco::Config::name\" ",
823 "($Girocco::Config::title) ",
824 "<$Girocco::Config::admin>\n";
825 print $pipe "To: $tolist\n";
826 print $pipe "Subject: $subject\n" if defined($subject);
827 print $pipe "MIME-Version: 1.0\n";
828 print $pipe "Content-Type: text/plain; charset=utf-8; format=fixed\n";
829 print $pipe "Content-Transfer-Encoding: 8bit\n";
830 print $pipe "X-Girocco: $Girocco::Config::gitweburl\n"
831 unless $Girocco::Config
::suppress_x_girocco
;
832 print $pipe "Auto-Submitted: auto-generated\n";
840 return undef unless defined($val);
841 $val =~ s/[\r\n]+$//s;
842 return undef unless $val =~ /^\d+$/;
844 return undef unless $val >= 1;
848 # Returns the number of "online" cpus or undef if undetermined
850 my @confcpus = $^O
eq "linux" ?
851 qw(_NPROCESSORS_ONLN NPROCESSORS_ONLN) :
852 qw(NPROCESSORS_ONLN _NPROCESSORS_ONLN) ;
853 my $cpus = _goodval
(get_cmd
('getconf', $confcpus[0]));
854 return $1 if defined($cpus) && $cpus =~ /^(\d+)$/;
855 $cpus = _goodval
(get_cmd
('getconf', $confcpus[1]));
856 return $1 if defined($cpus) && $cpus =~ /^(\d+)$/;
857 if ($^O
ne "linux") {
858 my @sysctls = qw(hw.ncpu);
859 unshift(@sysctls, qw(hw.availcpu)) if $^O
eq "darwin";
860 foreach my $mib (@sysctls) {
861 $cpus = _goodval
(get_cmd
('sysctl', '-n', $mib));
862 return $1 if defined($cpus) && $cpus =~ /^(\d+)$/;
868 # Returns the system page size in bytes or undef if undetermined
869 # This should never fail on a POSIX system
871 use POSIX
":unistd_h";
872 my $pagesize = sysconf
(_SC_PAGESIZE
);
873 return undef unless defined($pagesize) && $pagesize =~ /^\d+$/;
874 $pagesize = 0 + $pagesize;
875 return undef unless $pagesize >= 256;
879 # Returns the amount of available physical memory in bytes
880 # This may differ from the actual amount of physical memory installed
881 # Returns undef if this cannot be determined
883 my $pagesize = sys_pagesize
;
884 if ($pagesize && $^O
eq "linux") {
885 my $pages = _goodval
(get_cmd
('getconf', '_PHYS_PAGES'));
886 return $pagesize * $pages if $pages;
888 if ($^O
ne "linux") {
889 my @sysctls = qw(hw.physmem64);
890 unshift(@sysctls, qw(hw.memsize)) if $^O
eq "darwin";
891 foreach my $mib (@sysctls) {
892 my $memsize = _goodval
(get_cmd
('sysctl', '-n', $mib));
893 return $memsize if $memsize;
895 my $memsize32 = _goodval
(get_cmd
('sysctl', '-n', 'hw.physmem'));
896 return $memsize32 if $memsize32 && $memsize32 <= 2147483647;
898 my $pages = _goodval
(get_cmd
('sysctl', '-n', 'hw.availpages'));
899 return $pagesize * $pages if $pages;
901 return 2147483647 + 1 if $memsize32;
906 sub _get_max_conf_suffixed_size
{
908 return undef unless defined $conf && $conf =~ /^(\d+)([kKmMgG]?)$/;
909 my ($val, $suffix) = (0+$1, lc($2));
910 $val *= 1024 if $suffix eq 'k';
911 $val *= 1024 * 1024 if $suffix eq 'm';
912 $val *= 1024 * 1024 * 1024 if $suffix eq 'g';
916 sub _make_suffixed_size
{
918 return $size if $size % 1024;
920 return "${size}k" if $size % 1024;
922 return "${size}m" if $size % 1024;
927 # Return the value to pass to --window-memory= for git repack
928 # If the system memory or number of CPUs cannot be determined, returns "1g"
929 # Otherwise returns one third the available memory divided by the number of CPUs
930 # but never more than 1 gigabyte or max_gc_window_memory_size.
931 sub calc_windowmemory
{
932 my $cpus = online_cpus
;
933 my $memsize = sys_memsize
;
934 my $max = 1024 * 1024 * 1024;
935 if ($cpus && $memsize) {
936 $max = int($memsize / 3 / $cpus);
937 $max = 1024 * 1024 * 1024 if $max >= 1024 * 1024 * 1024;
939 my $maxconf = _get_max_conf_suffixed_size
($Girocco::Config
::max_gc_window_memory_size
);
940 $max = $maxconf if defined($maxconf) && $maxconf && $max > $maxconf;
941 return _make_suffixed_size
($max);
944 # Return the value to set as core.bigFileThreshold for git repack
945 # If the system memory cannot be determined, returns "256m"
946 # Otherwise returns the available memory divided by 16
947 # but never more than 512 megabytes or max_gc_big_file_threshold_size.
948 sub calc_bigfilethreshold
{
949 my $memsize = sys_memsize
;
950 my $max = 256 * 1024 * 1024;
952 $max = int($memsize / 16);
953 $max = 512 * 1024 * 1024 if $max >= 512 * 1024 * 1024;
955 my $maxconf = _get_max_conf_suffixed_size
($Girocco::Config
::max_gc_big_file_threshold_size
);
956 $max = $maxconf if defined($maxconf) && $maxconf && $max > $maxconf;
957 return _make_suffixed_size
($max);
960 # Return the value to use when deciding whether or not to re-calculate object deltas
961 # If there are no more than this many objects then deltas will be recomputed in
962 # order to create more efficient pack files. The new_delta_threshold value
963 # is constrained to be at least 1000 * cpu cores and no more than 100000.
964 # The default is sys_memsize rounded up to the nearest multiple of 256 MB and
965 # then 5000 per 256 MB or 50000 if we cannot determine memory size but never
966 # more than 100000 or less than 1000 * cpu cores.
967 sub calc_redeltathreshold
{
968 my $cpus = online_cpus
|| 1;
969 if (defined($Girocco::Config
::new_delta_threshold
) &&
970 $Girocco::Config
::new_delta_threshold
=~ /^\d+/) {
971 my $ndt = 0 + $Girocco::Config
::new_delta_threshold
;
972 if ($ndt >= $cpus * 1000) {
973 return $ndt <= 100000 ?
$ndt : 100000;
977 my $memsize = sys_memsize
;
979 my $quantum = 256 * 1024 * 1024;
980 $calcval = 5000 * int(($memsize + ($quantum - 1)) / $quantum);
981 $calcval = 1000 * $cpus if $calcval < 1000 * $cpus;
982 $calcval = 100000 if $calcval > 100000;
987 # $1 => thing to test
988 # $2 => optional directory, if given and -e "$2/$1$3", then return false
989 # $3 => optional, defaults to ''
990 sub has_reserved_suffix
{
991 no warnings
; # avoid silly 'unsuccessful stat on filename with \n' warning
992 my ($name, $dir, $ext) = @_;
993 $ext = '' unless defined $ext;
994 return 0 unless defined $name && $name =~ /\.([^.]+)$/;
995 return 0 unless exists $Girocco::Config
::reserved_suffixes
{lc($1)};
996 return 0 if defined $dir && -e
"$dir/$name$ext";
1000 # mostly undoes effect of `use CGI::Carp qw(fatalsToBrowser);`
1001 # mostly undoes effect of `use CGI::Carp qw(warningsToBrowser);`
1002 sub noFatalsToBrowser
{
1003 delete $SIG{__DIE__
};
1004 delete $SIG{__WARN__
};
1005 undef *CORE
::GLOBAL
::die;
1006 *CORE
::GLOBAL
::die = sub {
1008 my $ec = (0+$!) || ($?
>> 8) || 255;
1009 $ec != ($ec & 0xff) and $ec = 255;
1010 $ec |= 128 if !(0+$!) && ($?
& 0xff);
1011 my (undef, $fn, $li) = caller(0);
1012 my $loc = " at " . $fn . " line " . $li . ".\n";
1014 $msg = join("", @_) if @_;
1015 $msg = "Died" if $msg eq "";
1016 $msg .= $loc unless $msg =~ /\n$/;
1018 printf STDERR
"%s", $msg;
1021 undef *CORE
::GLOBAL
::warn;
1022 *CORE
::GLOBAL
::warn = sub {
1024 my (undef, $fn, $li) = caller(0);
1025 my $loc = " at " . $fn . " line " . $li . ".\n";
1027 $msg = join("", @_) if @_;
1028 $msg = "Warning: something's wrong" if $msg eq "";
1029 $msg .= $loc unless $msg =~ /\n$/;
1030 printf STDERR
"%s", $msg;
1034 # mimics Git's symref reading but only for HEAD
1035 # returns undef on failure otherwise an string that is
1036 # either an all-hex (lowercase) value or starts with "refs/"
1038 my $headpath = $_[0] . "/HEAD";
1040 my $rl = readlink($headpath);
1041 return defined($rl) && $rl =~ m
,^refs
/[^\x00-\x1f \x7f~^:\\*?
[]+$, ?
$rl : undef;
1043 open my $fd, '<', $headpath or return undef;
1050 defined($hv) or return undef;
1052 $hv =~ m
,^ref:\s
*(refs
/[^\x00-\x1f \x7f~^:\\*?
[]+)$, and return $1;
1053 $hv =~ m/^[0-9a-fA-F]{40,}$/ and return lc($hv);
1057 # same as read_HEAD_ref but returns undef
1058 # unless the result starts with "refs/"
1059 sub read_HEAD_symref
{
1060 my $hv = read_HEAD_ref
(@_);
1061 return defined($hv) && $hv =~ m
,^refs
/., ?
$hv : undef;
1064 # similar to Git's test except that GIT_OBJECT_DIRECTORY is ignored
1067 defined($gd) && $gd ne "" && -d
$gd or return undef;
1068 -d
"$gd/objects" && -x
"$gd/objects" or return 0;
1069 -d
"$gd/refs" && -x
"$gd/refs" or return 0;
1070 if (-l
"$gd/HEAD") {
1071 my $rl = readlink("$gd/HEAD");
1072 defined($rl) && $rl =~ m
,^refs
/., or return 0;
1073 -e
"$gd/HEAD" or return 1;
1075 open my $fd, '<', "$gd/HEAD" or return 0;
1082 defined $hv or return 0;
1084 $hv =~ m
,^ref:\s
*refs
/., and return 1;
1085 return $hv =~ /^[0-9a-f]{40}/;
1088 # Returns a PATH properly prefixed which guarantees that Git is found and the
1089 # basedir/bin utilities are found as intended. $ENV{PATH} is LEFT UNCHANGED!
1090 # Caller is responsible for assigning result to $ENV{PATH} or otherwise
1091 # arranging for it to be used. If $ENV{PATH} already has the proper prefix
1092 # then it's returned as-is (making this function idempotent).
1093 # Will die if it cannot determine a suitable full PATH.
1094 # Result is cached so all calls after the first are practically free.
1095 my $var_git_exec_path;
1097 defined($Girocco::Config
::var_git_exec_path
) && $Girocco::Config
::var_git_exec_path
ne "" and
1098 $var_git_exec_path = $Girocco::Config
::var_git_exec_path
;
1099 if (!defined($var_git_exec_path) || $var_git_exec_path eq "") {
1100 defined($Girocco::Config
::basedir
) && $Girocco::Config
::basedir
ne "" &&
1101 -d
$Girocco::Config
::basedir
&& -r _
&& -x _
or
1102 die "invalid \$Girocco::Config::basedir setting: $Girocco::Config::basedir\n";
1103 my $varsfile = $Girocco::Config
::basedir
. "/shlib_vars.sh";
1104 if (-f
$varsfile && -r _
) {
1106 if (open $vars, '<', $varsfile) {
1107 # last value for var_git_exec_path wins
1110 substr($_, 0, 19) eq "var_git_exec_path=\"" or next;
1111 substr($_, -1, 1) eq "\"" or next;
1112 my $xd = substr($_, 19, -1);
1113 $var_git_exec_path = $xd if -d
$xd && -r _
&& -x _
;
1118 if (!defined($var_git_exec_path)) {
1119 my $xd = get_git
("--exec-path");
1120 $var_git_exec_path = $xd if defined($xd) &&
1121 (chomp $xd, $xd) ne "" && -d
$xd && -r _
&& -x _
;
1123 defined($var_git_exec_path) && $var_git_exec_path ne "" or
1124 die "could not determine \$(git --exec-path) value\n";
1125 $var_git_exec_path = $1 if $var_git_exec_path =~ m
|^(/.+)$|;
1127 my $prefix = "$var_git_exec_path:$Girocco::Config::basedir/bin:";
1128 if (substr($ENV{PATH
}, 0, length($prefix)) eq $prefix) {
1131 return $prefix . $ENV{PATH
};
1135 # Note that Perl performs a "shellish" test in the Perl_do_exec3 function from doio.c,
1136 # but it has slightly different semantics in that whitespace does not automatically
1137 # make something "shellish". The semantics used here more closely match Git's
1138 # semantics so that Girocco will provide an interpretation more similar to Git's.
1140 return unless defined(local $_ = shift);
1141 return 1 if m
#[][\$&*(){}'";:=\\|?<>~`\#\s]#; # contains metacharacters
1142 return 0; # probably not shellish
1145 # Works just like the shlib.sh function git_add_config
1146 # except it takes two arguments, first the variable name, second the value
1147 # For example: git_add_config("gc.auto", "0")
1148 # No extra quoting is performed!
1149 # If the name or value requires special quoting, it must be provided by the caller!
1150 # Note this function will only be effective when running Git 1.7.3 or later
1151 sub git_add_config
{
1152 my ($name, $val) = @_;
1153 defined($name) && defined($val) or return;
1154 $name ne "" or return;
1155 my $gcp = $ENV{GIT_CONFIG_PARAMETERS
};
1156 defined($gcp) or $gcp = '';
1157 $gcp eq "" or $gcp = $gcp . " ";
1158 $gcp .= "'" . $name . '=' . $val . "'";
1159 $ENV{GIT_CONFIG_PARAMETERS
} = $gcp;
1163 package Girocco
::Util
::JSON
::Boolean
;
1164 use overload
'""' => \
&strval
;
1166 my $class = shift || __PACKAGE__
;
1168 return bless \
$val, $class;
1175 # returns a reference to a suitable object that will
1176 # encode to "true" or "false" when passed to to_json
1177 # based on the value passed to this function
1178 # For example, `print to_json(json_bool(1))` prints `true`.
1180 return Girocco
::Util
::JSON
::Boolean
->new($_[0]);
1183 # returns a utf8 encoded result that strictly conforms to
1184 # the JSON standard aka RFC 8259.
1185 # first argument is a scalar or a ref to a SCALAR, ARRAY or HASH
1186 # second argument, if true, requests a "pretty" result
1188 my ($val, $prt) = @_;
1189 $prt = 1 if $prt && !looks_like_number
($prt);
1190 $prt = 0 unless $prt;
1191 return _json_value
($val, 0+$prt, "");
1195 my ($val, $prt, $ndt) = @_;
1196 defined($val) or return "null";
1197 $val = $$val if ref($val) eq 'SCALAR';
1199 $r eq 'HASH' and return _json_hash
($val, $prt, $ndt);
1200 $r eq 'ARRAY' and return _json_array
($val, $prt, $ndt);
1201 $r eq 'Girocco::Util::JSON::Boolean' and
1202 return $val ?
"true" : "false";
1203 $r ne '' and $val = "".$val;
1204 looks_like_number
($val) and return "".(0+$val);
1205 return _json_str
("".$val);
1208 my %json_esc; BEGIN {%json_esc=(
1220 Encode
::is_utf8
($val) and utf8
::encode
($val);
1221 $val =~ s/([\\\042\b\t\n\f\r])/$json_esc{$1}/go;
1222 $val =~ s/([\x00-\x1f])/sprintf("\\u%04X",ord($1))/goe;
1223 return '"'.$val.'"';
1227 my ($val, $prt, $ndt) = @_;
1228 return '[]' unless @
{$val};
1230 $ans .= "\n" if $prt;
1233 for (my $i = 0; $i <= $#{$val}; ++$i) {
1234 $ans .= $ndt if $prt;
1235 $ans .= _json_value
(${$val}[$i], $prt, $ndt);
1236 $ans .= "," if $i < $#{$val};
1237 $ans .= "\n" if $prt;
1240 $ans .= $ndt if $prt;
1246 my ($val, $prt, $ndt) = @_;
1247 return '{}' unless %{$val};
1249 $ans .= "\n" if $prt;
1252 my @keys = sort(keys(%{$val}));
1253 for (my $i = 0; $i <= $#keys; ++$i) {
1254 $ans .= $ndt if $prt;
1255 $ans .= _json_str
("".$keys[$i]).":";
1256 $ans .= " " if $prt;
1257 $ans .= _json_value
(${$val}{$keys[$i]}, $prt, $ndt);
1258 $ans .= "," if $i < $#keys;
1259 $ans .= "\n" if $prt;
1262 $ans .= $ndt if $prt;
1267 # returns undef on error and sets $@ (otherwise $@ cleared)
1268 # if the JSON string to decode is "null" then undef is returned and $@ eq ""
1269 # $_[0] -> string value to decode from JSON
1270 # $_[1] -> if true return integers instead of json_bool for true/false
1271 # $_[2] -> if true strings are utf8::encode'd (i.e. they're bytes not chars)
1272 # returns scalar which will be an ARRAY or HASH ref for JSON array or hash values
1273 # using to_json(from_json($json_value)) will somewhat "normalize" $json_value
1274 # (and optionally pretty it up) and always recombine valid surrogate pairs
1277 eval {$ans = _from_jsonx
(@_)};
1281 # will die on bad input
1283 my ($val, $nobool, $enc) = @_;
1284 defined($val) or return undef;
1285 my $l = length($val);
1287 my $atom = _from_json_value
(\
$val, $l, $nobool, $enc);
1290 die "garbage found at offset ".pos($val);
1294 sub _from_json_value
{
1295 my ($val, $l, $nobool, $enc) = @_;
1297 my $c = substr($$val, pos($$val), 1);
1298 $c eq "" and die "unexpected end of input at offset ".pos($$val);
1299 $c eq "{" and return _from_json_hash
($val, $l, $nobool, $enc);
1300 $c eq "[" and return _from_json_array
($val, $l, $nobool, $enc);
1301 $c eq '"' and return _from_json_str
($val, $enc);
1302 index("-0123456789", $c) >= 0 and do {
1303 $$val =~ /\G(-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][-+]?\d+)?)/gc and
1304 return int($1) == $1 ?
int($1) : $1;
1305 die "invalid JSON number at offset ".pos($$val);
1307 $$val =~ /\Gnull\b/gc and return undef;
1308 $$val =~ /\Gtrue\b/gc and return $nobool?
1:json_bool
(1);
1309 $$val =~ /\Gfalse\b/gc and return $nobool?
0:json_bool
(0);
1310 die "invalid JSON value at offset ".pos($$val);
1313 my %json_unesc; BEGIN {%json_unesc=(
1323 sub _from_json_str
{
1324 my ($val, $enc) = @_;
1325 my $opos = pos($$val);
1326 $$val =~ /\G\042((?:[^\\\042]|\\.)*)\042/gsc and
1327 return _from_json_strval
($1, $opos+1, $enc);
1328 die "invalid JSON string starting at offset $opos";
1331 sub _from_json_strval
{
1332 my ($val, $pos, $enc) = @_;
1333 Encode
::is_utf8
($val) || utf8
::decode
($val) or
1334 die "invalid UTF-8 string starting at offset $pos";
1335 $val =~ s
{\\([\\\042btnfr
]|u
[0-9a
-fA
-F
]{4})}{
1336 substr($1,0,1) eq "u" ?
&{sub{
1337 my $c = hex(substr($1,1,4));
1338 0xD800 <= $c && $c <= 0xDFFF ?
1340 chr(hex(substr($1,1,4)))
1341 }} : $json_unesc{$1}
1343 $val =~ s
{\\u
([Dd
][89AaBb
][0-9a
-fA
-F
]{2})\\u
([Dd
][CcDdEeFf
][0-9a
-fA
-F
]{2})}{
1344 chr(( ((hex($1)&0x03FF)<<10) | (hex($2)&0x03FF) ) + 0x10000)
1346 !Encode
::is_utf8
($val) || utf8
::encode
($val) if $enc;
1350 sub _from_json_array
{
1351 my ($val, $l, $nobool, $enc) = @_;
1353 $$val =~ /\G\[/gc or die "expected '[' at offset ".pos($$val);
1355 while (pos($$val) < $l && substr($$val, pos($$val), 1) ne "]") {
1356 $$val =~ /\G\s+/gc and next;
1357 !$wantcomma && substr($$val, pos($$val), 1) eq "," and
1358 die "unexpected comma (,) in JSON array at offset ".pos($$val);
1359 $wantcomma && !($$val =~ /\G,/gc) and
1360 die "expected comma (,) or right-bracket (]) in JSON array at offset ".pos($$val);
1361 push(@a, _from_json_value
($val, $l, $nobool, $enc));
1364 $$val =~ /\G\]/gc or die "expected ']' at offset ".pos($$val);
1368 sub _from_json_hash
{
1369 my ($val, $l, $nobool, $enc) = @_;
1371 $$val =~ /\G\{/gc or die "expected '{' at offset ".pos($$val);
1374 while (pos($$val) < $l && substr($$val, pos($$val), 1) ne "}") {
1375 $$val =~ /\G\s+/gc and next;
1376 !$wantc && index(":,", substr($$val, pos($$val), 1)) >= 0 and
1377 die "unexpected colon (:) or comma (,) in JSON hash at offset ".pos($$val);
1378 $wantc eq ":" && !($$val =~ /\G:/gc) and
1379 die "expected colon (:) in JSON hash at offset ".pos($$val);
1380 $wantc eq "," && !($$val =~ /\G,/gc) and
1381 die "expected comma (,) or right-brace (}) in JSON hash at offset ".pos($$val);
1382 $wantc and $$val =~ /\G\s+/gc;
1383 $wantc eq "," and $wantc = "";
1384 !$wantc && substr($$val, pos($$val), 1) ne '"' and
1385 die "expected double-quote (\") in JSON hash at offset ".pos($$val);
1387 $k = _from_json_str
($val, $enc);
1391 $h{$k} = _from_json_value
($val, $l, $nobool, $enc);
1394 $wantc ne ":" or die "expected ':' at offset ".pos($$val);
1395 $$val =~ /\G\}/gc or die "expected '}' at offset ".pos($$val);
1399 # $_[0] -> full absolute path to a git ".git" directory
1400 # $_[1] -> "old" ref hash value
1401 # $_[2] -> "new" ref hash value
1403 # scalar context: "..." -- if forced ref update detected (i.e. NOT a fast-forward)
1404 # ".." -- any other condition (i.e. fast-forward/creation/deletion/no change/etc.)
1405 # array context: [0] -> scalar context result
1406 # [1] -> true value if a git command had to be run
1408 return '..' unless defined($_[0]);
1409 my ($git_dir, $old, $new) = @_;
1410 return '..' unless defined($old) && defined($new) && $old !~ /^0+$/ && $new !~ /^0+$/ && $old ne $new;
1411 # In many cases `git merge-base` is slower than this even if using the
1412 # `--is-ancestor` option available since Git 1.8.0, but it's never faster
1413 my $ans = get_git
("--git-dir=$git_dir", "rev-list", "-n", "1", "^$new^0", "$old^0", "--") ?
'...' : '..';
1414 return wantarray ?
($ans, 1) : $ans;
1417 # return the token key to use for the passed in category
1418 # if there is no such token or it cannot be read or is invalid
1419 # then silently return undef
1420 # category names must currently be 32 or fewer alphanumeric
1421 # characters where the first must be an alpha char
1422 # $_[0] -> category name
1425 defined($cname) or return undef;
1426 $cname = lc($cname);
1427 $cname =~ /^([a-z][a-z0-9]{0,31})$/ or return undef;
1429 my $tf = $Girocco::Config
::certsdir
. "/tokenkeys/$cname.tky";
1430 -e
$tf && -f _
&& -r _
&& -s _
or return undef;
1432 open $fh, '<', $tf or return undef;
1435 defined($tk) or return undef;
1437 $tk =~ /^([A-Za-z0-9_-]{48})$/ or return undef;
1441 # just like create_timed_token except that
1442 # the first argument is a category name instead of
1443 # the actual HMAC "secret"
1444 # $_[0] -> category name to pass to get_token_key
1445 # $_[1] -> optional instance info to include in "text"
1446 # $_[2] -> duration of validity in seconds (5..2147483647)
1447 # $_[3] -> optional time stamp (secs since unix Epoch)
1448 # if not provided, current time is used
1449 # Returns a base64_url token (no trailing '='s) that is
1450 # valid starting at $_[3] and expires $_[2] seconds after $_[3].
1451 # Unless get_token_key fails in which case it returns undef.
1453 sub get_timed_token
{
1454 my ($catg, $extra, $duration, $start) = @_;
1455 my $tk = get_token_key
($catg);
1456 defined($tk) && $tk ne "" or return undef;
1457 return create_timed_token
($tk, $extra, $duration, $start);
1460 # return a hidden "token" <input /> field if the token ($_[0])
1461 # can be read, otherwise the empty string "".
1462 # $_[0] -> the token category (passed to get_token_key)
1463 # $_[1] -> the optional instance info (passed to create_timed_token)
1464 # $_[2] -> the duration of validity (passed to create_timed_token)
1465 # $_[3] -> optional name of field (defaults to "token")
1466 # returns a "hidden" XHTML input element or the empty string if
1467 # get_timed_token fails. The token starting time will be the
1470 sub get_token_field
{
1471 my ($catg, $extra, $duration, $name) = @_;
1472 defined($name) && $name ne "" or $name = "token";
1473 my $tt = get_timed_token
($catg, $extra, $duration);
1474 defined($tt) && $tt ne "" or return "";
1475 return "<input type=\"hidden\" name=\"$name\" value=\"$tt\" />";
1478 # just like verify_timed_token except that
1479 # the second argument is a category name instead of
1480 # the actual HMAC "secret"
1481 # $_[0] -> a create_timed_token/get_timed_token to check
1482 # $_[1] -> category name to pass to get_token_key
1483 # $_[2] -> optional instance info to include in "text"
1484 # $_[3] -> duration of validity in seconds (5..2147483647)
1485 # $_[4] -> optional time stamp (secs since unix Epoch)
1486 # if not provided, current time is used
1487 # Returns true if $_[4] falls within the token's validity range
1488 # Returns false for a bad or expired token
1489 sub check_timed_token
{
1490 my ($token, $catg, $extra, $duration, $start) = @_;
1491 my $tk = get_token_key
($catg);
1492 defined($tk) && $tk ne "" or return undef;
1493 return verify_timed_token
($token, $tk, $extra, $duration, $start);
1496 # similar to the shlib.sh function v_get_proj_from_dir but different details
1497 # attempt to convert the first argument interpreted as a full path name to
1498 # a Girocco project name. Unlike v_get_proj_from_dir, there's no magic
1499 # "_external/..." name fallback if not found.
1500 # $_[0] -> directory name to translate, relative to getcwd if not absolute
1501 # Returns name of existing Girocco project on success, undef on failure
1502 # Minor quibbles are handled (e.g. trailing '.git' omitted and shouldn't have been)
1503 # If the resolved absolute path ends with "/.git" and that's a "gitdir: " file
1504 # that will be followed too.
1505 # And finally, if we seem to have ended up in a worktree, that will be handled too
1506 sub get_project_from_dir
{
1507 use Cwd
qw(realpath);
1508 use File
::Basename
qw(dirname);
1509 require Girocco
::Project
;
1511 defined($path) && $path ne "" or $path = ".";
1512 $path =~ s{/+$}{}; $path ne "" or $path = "/";
1513 my $fallback = sub {
1514 my $fallback = $path;
1515 # if a top-level working tree directory was specified
1516 # take that to mean its associated .git dir otherwise
1517 # if a sibling directory with a ".git" extension exists
1518 # such as where forked projects are involved, try that
1519 if ($path !~ /\/\
.git
$/ && -e "$fallback/.git
") {
1520 $fallback .= "/.git
";
1522 (my $test = $path) =~ s/\.$//;
1523 if ($test !~ /\.git$/ && -d "$test.git
") {
1524 # project fork directory trees always
1525 # use bare repositories; do NOT check
1526 # for a "$test.git
/.git
" here;
1527 # but a 2nd fallback round will!
1528 $fallback = "$test.git
";
1531 $fallback ne $path && -e $fallback or return undef;
1532 return get_project_from_dir($fallback);
1534 my $rpath = realpath($path);
1535 defined($rpath) && -e $rpath or return &$fallback;
1536 if ($rpath =~ /\/\.git$/ && -f $rpath && -s _) {
1538 # see if it's a gitdir: link (allowing relative ones)
1539 # and pick up its destination
1540 # no fallbacks in here since an existing ".git
"
1541 # was found and it was a file not a dir
1542 open my $gdf, '<', $rpath or return undef;
1543 my $gdline = <$gdf>;
1545 defined($gdline) && $gdline =~ /^gitdir:\s*([^\s].*)$/
1547 (my $gitdir = $1) =~ s/\s+$//;
1548 if (substr($gitdir, 0, 1) ne "/") {
1550 $gitdir = dirname($rpath)."/".$gitdir;
1552 -e $gitdir or return undef;
1553 $gitdir = realpath($gitdir);
1554 # a gitdir: link must point to a real directory
1555 defined($gitdir) && -d $gitdir or return undef;
1558 # an existing directory is required at this point
1559 # if it's an existing not-a-directory, no fallback
1560 -d $rpath or return undef;
1561 if (!is_git_dir($rpath)) {
1563 # see if it might be a worktree
1564 if (-f "$rpath/HEAD" && -s _ && -f "$rpath/commondir
" && -s _) {
1565 open my $cdf, '<', "$rpath/commondir
" or return &$fallback;
1568 defined($cdl) && $cdl ne "" or return &$fallback;
1569 $cdl =~ s/^\s+//; $cdl =~ s/\s+$//;
1570 $cdl ne "" or return &$fallback;
1571 # for now require "../.." for safety
1572 $cdl eq "../.." or return &$fallback;
1573 $rpath = dirname(dirname($rpath));
1574 is_git_dir($rpath) or return &$fallback;
1576 return &$fallback; # yes, try a ".git
" suffix fallback
1579 # at this point $rpath is a "realpath
" to an existing directory
1580 # that appears to be a non-worktree $GIT_DIR -- no more fallbacks
1581 # try the quick check first
1582 my $rrr = realpath($Girocco::Config::reporoot);
1583 defined($rrr) && $rrr ne "" or return undef;
1584 if ($rpath =~ m{^\Q$rrr\E/(.+)$}) {
1585 (my $proj = $1) =~ s/\.git$//;
1586 return $proj ne "" && Girocco::Project::does_exist($proj, 1)
1589 # finally, attempt to look up the path in gitdir.list if all else fails
1590 my $gdlp = "$Girocco::Config
::projlist_cache_dir
/gitdir
.list
";
1591 -f $gdlp && -s _ or return undef;
1592 my $projname = undef;
1593 open my $gdlf, '<', $gdlp or return undef;
1595 /^([^\s]+)\s+([^\s].*)$/ or next;
1596 $2 eq $rpath or next;
1601 defined($projname) && $projname ne "" && Girocco::Project::does_exist($projname, 1)
1602 or $projname = undef;