Project.pm: validate new HEAD setting
[girocco/readme.git] / Girocco / Project.pm
blob8aeef99d2022351c9f19fef184fa7276d152a8d6
1 package Girocco::Project;
3 use strict;
4 use warnings;
6 BEGIN {
7 use Girocco::CGI;
8 use Girocco::User;
9 use Girocco::Util;
10 use Girocco::HashUtil;
11 use Girocco::ProjPerm;
12 use Girocco::Config;
13 use base ('Girocco::ProjPerm::'.$Girocco::Config::permission_control); # mwahaha
16 BEGIN {
17 eval {
18 require Digest::SHA;
19 Digest::SHA->import(
20 qw(sha1_hex)
21 );1} ||
22 eval {
23 require Digest::SHA1;
24 Digest::SHA1->import(
25 qw(sha1_hex)
26 );1} ||
27 eval {
28 require Digest::SHA::PurePerl;
29 Digest::SHA::PurePerl->import(
30 qw(sha1_hex)
31 );1} ||
32 die "One of Digest::SHA or Digest::SHA1 or Digest::SHA::PurePerl "
33 . "must be available\n";
36 our $metadata_fields = {
37 homepage => ['Homepage URL', 'hp', 'text'],
38 shortdesc => ['Short description', 'desc', 'text'],
39 README => ['<span style="display:inline-block;vertical-align:top">'.
40 'README (HTML, &lt; 8 KiB)<br />leave blank for automatic</span>',
41 'README', 'textarea', 'Enter only &#x201c;<!-- comments -->&#x201d; '.
42 'to completely suppress any README'],
43 notifymail => ['Commit notify &#x2013; mail to', 'notifymail', 'text',
44 'comma separated address list'],
45 notifytag => ['Tag notify &#x2013; mail to', 'notifytag', 'text',
46 'comma separated address list &#x2013; if not empty, tag '.
47 'notifications are sent here INSTEAD of to '.
48 '&#x201c;Commit notify &#x2013; mail to&#x201d; address(es)'],
49 notifyjson => ['Commit notify &#x2013; '.
50 '<a title="'.html_esc('single field name is &#x201c;payload&#x201d;', 1).'" href="'.
51 'https://developer.github.com/v3/activity/events/types/#pushevent'.
52 '">POST JSON</a> at', 'notifyjson', 'text'],
53 notifycia => ['Commit notify &#x2013; <a href="http://cia.vc/doc/">CIA project</a> name',
54 'notifycia', 'text', 'CIA is defunct &#x2013; this value is ignored'],
57 sub _mkdir_forkees {
58 my $self = shift;
59 my @pelems = split('/', $self->{name});
60 pop @pelems; # do not create dir for the project itself
61 my $path = $self->{base_path};
62 foreach my $pelem (@pelems) {
63 $path .= "/$pelem";
64 (-d "$path") or mkdir $path or die "mkdir $path: $!";
65 chmod 02775, $path; # ok if fails (dir may already exist and be owned by someone else)
69 # With a leading ':' get from project local config replacing ':' with 'gitweb.'
70 # With a leading '%' get from project local config after removing '%'
71 # Otherwise it's a project file name to be loaded
72 # %propmapro entries are loaded but never written
74 our %propmap = (
75 url => ':baseurl',
76 email => ':owner',
77 desc => 'description',
78 README => 'README.html',
79 hp => ':homepage',
80 notifymail => '%hooks.mailinglist',
81 notifytag => '%hooks.announcelist',
82 notifyjson => '%hooks.jsonurl',
83 notifycia => '%hooks.cianame',
86 our %propmapro = (
87 lastchange => ':lastchange',
88 lastactivity => 'info/lastactivity',
89 lastgc => ':lastgc',
90 lastreceive => ':lastreceive',
91 lastparentgc => ':lastparentgc',
92 lastrefresh => ':lastrefresh',
93 creationtime => '%girocco.creationtime',
94 reposizek => '%girocco.reposizek',
95 origurl => ':baseurl',
98 # Projects with any of these names will be disallowed to avoid possible
99 # collisions with cgi script paths or chroot paths
100 our %reservedprojectnames = (
101 admin => 1, # /admin/ links
102 b => 1, # /b/ -> bundle.cgi
103 blog => 1, # /blog/ links
104 c => 1, # /c/ -> cgit
105 h => 1, # /h/ -> html.cgi
106 r => 1, # /r/ -> git http
107 w => 1, # /w/ -> gitweb
108 wiki => 1, # /wiki/ links
109 srv => 1, # /srv/git/ -> chroot ssh git repositories
112 sub _update_index {
113 my $self = shift;
114 system("$Girocco::Config::basedir/gitweb/genindex.sh", $self->{name});
117 sub _readlocalconfigfile {
118 my $self = shift;
119 my $undefonerr = shift || 0;
120 delete $self->{configfilehash};
121 my $configfile = get_git("--git-dir=$self->{path}",
122 'config', '--list', '--local', '--null');
123 my $result = 1;
124 defined($configfile) || $undefonerr or $result = 0, $configfile = '';
125 return undef unless defined($configfile);
126 my %config = map({split(/\n/, $_=~/\n/?$_:"$_\ntrue", 2)} split(/\x00/, $configfile));
127 $self->{configfilehash} = \%config;
128 return $result;
131 # @_[0]: argument to convert to boolean result (0 or 1)
132 # @_[1]: value to use if argument is undef (default is 0)
133 # Returns 0 or 1
134 sub _boolval {
135 my ($val, $def) = @_;
136 defined($def) or $def = 0;
137 defined($val) or $val = $def;
138 $val =~ s/\s+//gs;
139 $val = lc($val);
140 return 0 if $val eq '' || $val eq 'false' || $val eq 'off' || $val =~ /^0+$/;
141 return 1;
144 sub _property_path {
145 my $self = shift;
146 my ($name) = @_;
147 $self->{path}.'/'.$name;
150 sub _property_fget {
151 my $self = shift;
152 my ($name) = @_;
153 my $pname = $propmap{$name};
154 $pname = $propmapro{$name} unless $pname;
155 $pname or die "unknown property: $name";
156 if ($pname =~ /^([:%])(.*)$/) {
157 my ($where, $pname) = ($1,lc($2));
158 $self->_readlocalconfigfile
159 unless ref($self->{configfilehash}) eq 'HASH';
160 my $val = $where eq ':' ?
161 $self->{configfilehash}->{"gitweb.$pname"} :
162 $self->{configfilehash}->{$pname};
163 defined($val) or $val = '';
164 chomp $val;
165 return $val;
168 open my $p, '<', $self->_property_path($pname) or return undef;
169 my @value = <$p>;
170 close $p;
171 my $value = join('', @value); chomp $value;
172 $value;
175 sub _property_fput {
176 my $self = shift;
177 my ($name, $value) = @_;
178 my $pname = $propmap{$name};
179 $pname or die "unknown property: $name";
180 $value ||= '';
181 if ($pname =~ s/^://) {
182 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', "gitweb.$pname", $value);
183 return;
184 } elsif ($pname =~ s/^%//) {
185 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', $pname, $value);
186 return;
189 my $P = lock_file($self->_property_path($pname));
190 chomp $value;
191 $value ne '' and print $P "$value\n";
192 close $P;
193 unlock_file($self->_property_path($pname));
196 sub _cleanup_readme {
197 my $self = shift;
198 defined($self->{README}) or $self->{README} = '';
199 $self->{README} =~ s/\r\n?/\n/gs;
200 $self->{README} =~ s/^\s+//s;
201 $self->{README} =~ s/\s+$//s;
202 $self->{README} eq '' or $self->{README} .= "\n";
205 sub _lint_readme {
206 my $self = shift;
207 return 0 unless defined($self->{README}) && $self->{README} ne '';
208 my $test = '<html xmlns="http://www.w3.org/1999/xhtml"><body><div>';
209 $test .= $self->{README};
210 $test .= '</div></body></html>';
211 my ($code, $errors) = capture_command(2, $test, 'xmllint', '--nonet',
212 '--noout', '--nowarning', '-');
213 return 0 unless $code;
214 my $cnt = 0;
215 my @errs = ();
216 for my $line (split(/\n+/, $errors)) {
217 $line = html_esc($line);
218 $line =~ s/ /\&#160;/gs;
219 ++$cnt, $line = 'README'.$1 if $line =~ /^-(:\d+:.*)$/;
220 push @errs, '<tt>' . $line . '</tt>';
222 return ($cnt, join("<br />\n", @errs));
225 sub _properties_load {
226 my $self = shift;
227 foreach my $prop (keys %propmap) {
228 $self->{$prop} = $self->_property_fget($prop);
230 foreach my $prop (keys %propmapro) {
231 $self->{$prop} = $self->_property_fget($prop);
233 $self->_readlocalconfigfile
234 unless ref($self->{configfilehash}) eq 'HASH';
235 $self->{statusupdates} = _boolval($self->{configfilehash}->{'gitweb.statusupdates'}, 1);
236 delete $self->{auth};
237 my $val = $self->{configfilehash}->{'gitweb.repoauth'};
238 defined($val) or $val = '';
239 chomp $val;
240 if ($val =~ /^# ([A-Z]+)AUTH ([0-9a-f]+) (\d+)/) {
241 my $expire = $3;
242 if (time < $expire) {
243 $self->{authtype} = $1;
244 $self->{auth} = $2;
247 $self->_cleanup_readme;
248 delete $self->{configfilehash};
251 sub _properties_save {
252 my $self = shift;
253 my $fd;
254 foreach my $prop (keys %propmap) {
255 $self->_property_fput($prop, $self->{$prop});
257 $self->{statusupdates} = 1
258 unless defined($self->{statusupdates}) && $self->{statusupdates} =~ /^\d+$/;
259 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--bool',
260 "gitweb.statusupdates", $self->{statusupdates});
261 if (defined($self->{origurl}) && defined($self->{url}) &&
262 $self->{origurl} ne $self->{url} && -e $self->_property_path(".banged")) {
263 if (open($fd, '>', $self->_property_path(".bangagain"))) {
264 close $fd;
265 chmod(0664, $self->_property_path(".bangagain"));
270 sub _nofetch_path {
271 my $self = shift;
272 $self->_property_path('.nofetch');
275 sub _nofetch {
276 my $self = shift;
277 my ($nofetch) = @_;
278 my $nf = $self->_nofetch_path;
279 if ($nofetch) {
280 open my $x, '>', $nf or die "nofetch failed: $!";
281 close $x;
282 } else {
283 unlink $nf or die "yesfetch failed: $!";
287 sub _gcp_path {
288 my $self = shift;
289 $self->_property_path('.gc_in_progress');
292 sub _clonelog_path {
293 my $self = shift;
294 $self->_property_path('.clonelog');
297 sub _clonefail_path {
298 my $self = shift;
299 $self->_property_path('.clone_failed');
302 sub _clonep_path {
303 my $self = shift;
304 $self->_property_path('.clone_in_progress');
307 sub _clonep {
308 my $self = shift;
309 my ($nofetch) = @_;
310 my $np = $self->_clonep_path;
311 if ($nofetch) {
312 open my $x, '>', $np or die "clonep failed: $!";
313 close $x;
314 } else {
315 unlink $np or die "clonef failed: $!";
318 sub _alternates_setup {
319 my $self = shift;
320 return unless $self->{name} =~ m#/#;
321 my $forkee_name = get_forkee_name($self->{name});
322 my $forkee_path = get_forkee_path($self->{name});
323 return unless -d $forkee_path;
324 mkdir $self->{path}.'/refs'; chmod 02775, $self->{path}.'/refs';
325 mkdir $self->{path}.'/objects'; chmod 02775, $self->{path}.'/objects';
326 mkdir $self->{path}.'/objects/info'; chmod 02775, $self->{path}.'/objects/info';
328 # We set up both alternates and http_alternates since we cannot use
329 # relative path in alternates - that doesn't work recursively.
331 my $filename = $self->{path}.'/objects/info/alternates';
332 open my $x, '>', $filename or die "alternates failed: $!";
333 print $x "$forkee_path/objects\n";
334 close $x;
335 chmod 0664, $filename or warn "cannot chmod $filename: $!";
337 if ($Girocco::Config::httppullurl) {
338 $filename = $self->{path}.'/objects/info/http-alternates';
339 open my $x, '>', $filename or die "http-alternates failed: $!";
340 my $upfork = $forkee_name;
341 do { print $x "$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s#/?.+?$## and $upfork); #
342 close $x;
343 chmod 0664, $filename or warn "cannot chmod $filename: $!";
346 # copy lastactivity from the parent project
347 if (open $x, '<', $forkee_path.'/info/lastactivity') {{
348 my $activity = <$x>;
349 close $x;
350 last unless $activity;
351 open $x, '>', $self->{path}.'/info/lastactivity' or last;
352 print $x $activity;
353 close $x;
354 chomp $activity;
355 $self->{'lastactivity'} = $activity;
358 # copy refs from parent project
359 local *SAVEOUT;
360 open(SAVEOUT, ">&STDOUT") || die "couldn't dup STDOUT: $!";
361 my $result = open(STDOUT, '>', "$self->{path}/packed-refs");
362 my $resultstr = $!;
363 if (!$result) {
364 open(STDOUT, ">&SAVEOUT") || die "couldn't dup SAVEOUT to STDOUT: $!";
365 close(SAVEOUT) or die "couldn't close SAVEOUT: $!";
366 die "could not write fork's packed-refs file: $resultstr";
368 system($Girocco::Config::git_bin, "--git-dir=$forkee_path", 'for-each-ref', '--format=%(objectname) %(refname)');
369 $result = close(STDOUT);
370 $resultstr = $!;
371 open(STDOUT, ">&SAVEOUT") || die "couldn't dup SAVEOUT to STDOUT: $!";
372 close(SAVEOUT) or die "couldn't close SAVEOUT: $!";
373 $result or die "could not close fork's packed-refs file: $resultstr";
374 unlink("$self->{path}/.delaygc") if -s "$self->{path}/packed-refs";
376 # initialize HEAD
377 my $HEAD = get_git("--git-dir=$forkee_path", 'symbolic-ref', 'HEAD');
378 defined($HEAD) && $HEAD =~ m,^refs/heads/., or $HEAD = 'refs/heads/master';
379 chomp $HEAD;
380 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', $HEAD);
381 chmod 0664, "$self->{path}/packed-refs", "$self->{path}/HEAD";
384 sub _set_changed {
385 my $self = shift;
386 my $fd;
387 open $fd, '>', "$self->{path}/htmlcache/changed" and close $fd;
390 sub _set_forkchange {
391 my $self = shift;
392 my $changedtoo = shift;
393 return unless $self->{name} =~ m#/#;
394 my $forkee_path = get_forkee_path($self->{name});
395 return unless -d $forkee_path;
396 # mark forkee as changed
397 my $fd;
398 open $fd, '>', $forkee_path.'/htmlcache/changed' and close $fd if $changedtoo;
399 open $fd, '>', $forkee_path.'/htmlcache/forkchange' and close $fd;
400 return if -e $forkee_path.'/htmlcache/summary.forkchange';
401 open $fd, '>', $forkee_path.'/htmlcache/summary.forkchange' and close $fd;
404 sub _ctags_setup {
405 my $self = shift;
406 my $perms = $Girocco::Config::permission_control eq 'Hooks' ? 02777 : 02775;
407 mkdir $self->{path}.'/ctags'; chmod $perms, $self->{path}.'/ctags';
410 sub _group_add {
411 my $self = shift;
412 my ($xtra) = @_;
413 $xtra .= join(',', @{$self->{users}});
414 filedb_atomic_append(jailed_file('/etc/group'),
415 join(':', $self->{name}, $self->{crypt}, '\i', $xtra));
418 sub _group_update {
419 my $self = shift;
420 my $xtra = join(',', @{$self->{users}});
421 filedb_atomic_edit(jailed_file('/etc/group'),
422 sub {
423 $_ = $_[0];
424 chomp;
425 if ($self->{name} eq (split /:/)[0]) {
426 # preserve readonly flag
427 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
428 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
429 } else {
430 return "$_\n";
436 sub _group_remove {
437 my $self = shift;
438 filedb_atomic_edit(jailed_file('/etc/group'),
439 sub {
440 $self->{name} ne (split /:/)[0] and return $_;
445 sub _hook_path {
446 my $self = shift;
447 my ($name) = @_;
448 $self->{path}.'/hooks/'.$name;
451 sub _hook_install {
452 my $self = shift;
453 my ($name) = @_;
454 my $hooksdir = $self->{path}.'/hooks';
455 my $oldmask = umask();
456 umask($oldmask & ~0070);
457 -d $hooksdir or mkdir $hooksdir or
458 die "hooks directory does not exist and unable to create it for project " . $self->{name} . ": $!";
459 umask($oldmask);
460 open my $src, '<', "$Girocco::Config::basedir/hooks/$name" or die "cannot open hook $name: $!";
461 open my $dst, '>', $self->_hook_path($name) or die "cannot open hook $name for writing: $!";
462 while (<$src>) { print $dst $_; }
463 close $dst;
464 close $src;
465 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
468 sub _hooks_install {
469 my $self = shift;
470 foreach my $hook ('pre-receive', 'post-receive', 'update') {
471 $self->_hook_install($hook);
475 # private constructor, do not use
476 sub _new {
477 my $class = shift;
478 my ($name, $base_path, $path) = @_;
479 does_exist($name,1) || valid_name($name) or die "refusing to create project with invalid name ($name)!";
480 $path ||= "$base_path/$name.git";
481 my $proj = { name => $name, base_path => $base_path, path => $path };
483 bless $proj, $class;
486 # public constructor #0
487 # creates a virtual project not connected to disk image
488 # you can conjure() it later to disk
489 sub ghost {
490 my $class = shift;
491 my ($name, $mirror) = @_;
492 my $self = $class->_new($name, $Girocco::Config::reporoot);
493 $self->{users} = [];
494 $self->{mirror} = $mirror;
495 $self->{email} = $self->{orig_email} = '';
496 $self;
499 # public constructor #1
500 sub load {
501 my $class = shift;
502 my $name = shift || '';
504 open my $fd, '<', jailed_file("/etc/group") or die "project load failed: $!";
505 my $r = qr/^\Q$name\E:/;
506 foreach (grep /$r/, <$fd>) {
507 chomp;
509 my $self = $class->_new($name, $Girocco::Config::reporoot);
510 (-d $self->{path} && $self->_readlocalconfigfile(1))
511 or die "invalid path (".$self->{path}.") for project ".$self->{name};
513 my $ulist;
514 (undef, $self->{crypt}, $self->{gid}, $ulist) = split /:/;
515 $ulist ||= '';
516 $self->{users} = [split /,/, $ulist];
517 $self->{HEAD} = $self->get_HEAD;
518 $self->{orig_HEAD} = $self->{HEAD};
519 $self->{orig_users} = [@{$self->{users}}];
520 $self->{mirror} = ! -e $self->_nofetch_path;
521 $self->{gc_in_progress} = -e $self->_gcp_path;
522 $self->{clone_in_progress} = -e $self->_clonep_path;
523 $self->{clone_logged} = -e $self->_clonelog_path;
524 $self->{clone_failed} = -e $self->_clonefail_path;
525 $self->{ccrypt} = $self->{crypt};
527 $self->_properties_load;
528 $self->{orig_email} = $self->{email};
529 $self->{loaded} = 1; # indicates self was loaded from etc/group file
530 close $fd;
531 return $self;
533 close $fd;
534 undef;
537 # $proj may not be in sane state if this returns false!
538 # fields listed in %$metadata_fields that are NOT also
539 # in @Girocco::Config::project_fields are totally ignored!
540 sub cgi_fill {
541 my $self = shift;
542 my ($gcgi) = @_;
543 my $cgi = $gcgi->cgi;
544 my %allowedfields = map({$_ => 1} @Girocco::Config::project_fields);
545 my $field_enabled = sub {
546 !exists($metadata_fields->{$_[0]}) || exists($allowedfields{$_[0]})};
548 my $pwd = $cgi->param('pwd');
549 my $pwd2 = $cgi->param('pwd2');
550 # in case passwords are disabled
551 defined($pwd) or $pwd = ''; defined($pwd2) or $pwd = '';
552 if ($Girocco::Config::project_passwords and not $self->{crypt} and $pwd eq '' and $pwd2 eq '') {
553 $gcgi->err("Empty passwords are not permitted.");
555 if ($pwd ne '' or not $self->{crypt}) {
556 $self->{crypt} = scrypt_sha1($pwd);
558 if (($pwd ne '' || $pwd2 ne '') and $pwd ne $pwd2) {
559 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
562 $self->{cpwd} = $cgi->param('cpwd');
564 my ($forkee,$project) = ($self->{name} =~ m#^(.*/)?([^/]+)$#);
565 my $newtype = $forkee ? 'fork' : 'project';
566 length($project) <= 64
567 or $gcgi->err("The $newtype name is longer than 64 characters. Do you really need that much?");
569 if ($Girocco::Config::project_owners eq 'email') {
570 $self->{email} = $gcgi->wparam('email');
571 valid_email($self->{email})
572 or $gcgi->err("Your email sure looks weird...?");
573 length($self->{email}) <= 96
574 or $gcgi->err("Your email is longer than 96 characters. Do you really need that much?");
577 # No setting the url unless we're either new or an existing mirror!
578 $self->{url} = $gcgi->wparam('url') unless $self->{loaded} && !$self->{mirror};
579 # Always validate the url if we're an existing mirror
580 if ((defined($self->{url}) && $self->{url} ne '') || ($self->{loaded} && $self->{mirror})) {
581 valid_repo_url($self->{url})
582 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocols are supported. If the URL contains funny characters, contact me.");
583 if ($Girocco::Config::restrict_mirror_hosts) {
584 my $mh = extract_url_hostname($self->{url});
585 is_dns_hostname($mh)
586 or $gcgi->err("Invalid URL. Note that only DNS names are allowed, not IP addresses.");
587 !is_our_hostname($mh)
588 or $gcgi->err("Invalid URL. Mirrors from this host are not allowed, please create a fork instead.");
592 if ($field_enabled->('desc')) {
593 $self->{desc} = to_utf8($gcgi->wparam('desc'), 1);
594 length($self->{desc}) <= 1024
595 or $gcgi->err("<b>Short</b> description length &gt; 1kb!");
598 if ($field_enabled->('README')) {
599 $self->{README} = to_utf8($gcgi->wparam('README'), 1);
600 $self->_cleanup_readme;
601 length($self->{README}) <= 8192
602 or $gcgi->err("README length &gt; 8kb!");
603 my ($cnt, $err) = (0);
604 ($cnt, $err) = $self->_lint_readme if $gcgi->ok && $Girocco::Config::xmllint_readme;
605 $gcgi->err($err), $gcgi->{err} += $cnt-1 if $cnt;
608 if ($field_enabled->('hp')) {
609 $self->{hp} = $gcgi->wparam('hp');
610 if ($self->{hp}) {
611 valid_web_url($self->{hp})
612 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
616 # No mucking about with users unless we're a push project
617 if (($self->{loaded} && !$self->{mirror}) ||
618 (!$self->{loaded} && (!defined($self->{url}) || $self->{url} eq ''))) {
619 my %users = ();
620 my @users = ();
621 foreach my $user ($cgi->multi_param('user')) {
622 if (!exists($users{$user})) {
623 $users{$user} = 1;
624 push(@users, $user) if Girocco::User::does_exist($user, 1);
627 $self->{users} = \@users;
630 # HEAD can only be set with editproj, NOT regproj (regproj sets it automatically)
631 # It may ALWAYS be set to what it was (even if there's no such refs/heads/...)
632 my $newhead;
633 if (defined($self->{orig_HEAD}) && $self->{orig_HEAD} ne '' &&
634 defined($newhead = $cgi->param('HEAD')) && $newhead ne '') {
635 if ($newhead eq $self->{orig_HEAD} ||
636 get_git("--git-dir=$self->{path}", 'rev-parse', '--verify', '--quiet', 'refs/heads/'.$newhead)) {
637 $self->{HEAD} = $newhead;
638 } else {
639 $gcgi->err("Invalid default branch (no such ref)");
643 # schedule deletion of tags (will be committed by update() after auth)
644 $self->{tags_to_delete} = [$cgi->multi_param('tags')];
646 if ($field_enabled->('notifymail')) {
647 $self->{notifymail} = $gcgi->wparam('notifymail');
648 if ($self->{notifymail}) {
649 (valid_email_multi($self->{notifymail}) and length($self->{notifymail}) <= 512)
650 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
654 if ($field_enabled->('notifytag')) {
655 $self->{notifytag} = $gcgi->wparam('notifytag');
656 if ($self->{notifytag}) {
657 (valid_email_multi($self->{notifytag}) and length($self->{notifytag}) <= 512)
658 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
662 if ($field_enabled->('notifyjson')) {
663 $self->{notifyjson} = $gcgi->wparam('notifyjson');
664 if ($self->{notifyjson}) {
665 valid_web_url($self->{notifyjson})
666 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
670 if ($field_enabled->('notifycia')) {
671 $self->{notifycia} = $gcgi->wparam('notifycia');
672 if ($self->{notifycia}) {
673 $self->{notifycia} =~ /^[a-zA-Z0-9._-]+$/
674 or $gcgi->err("Overly suspicious CIA notify project name. If it's actually valid, don't contact me, CIA is defunct.");
678 if ($cgi->param('setstatusupdates')) {
679 my $val = $gcgi->wparam('statusupdates') || '0';
680 $self->{statusupdates} = $val ? 1 : 0;
683 not $gcgi->err_check;
686 sub form_defaults {
687 my $self = shift;
689 name => $self->{name},
690 email => $self->{email},
691 url => $self->{url},
692 desc => html_esc($self->{desc}),
693 README => html_esc($self->{README}),
694 hp => $self->{hp},
695 users => $self->{users},
696 notifymail => html_esc($self->{notifymail}),
697 notifytag => html_esc($self->{notifytag}),
698 notifyjson => html_esc($self->{notifyjson}),
699 notifycia => html_esc($self->{notifycia}),
703 # return true if $enc_passwd is a match for $plain_passwd
704 my $_check_passwd_match = sub {
705 my $enc_passwd = shift;
706 my $plain_passwd = shift;
707 defined($enc_passwd) or $enc_passwd = '';
708 defined($plain_passwd) or $plain_passwd = '';
709 # $enc_passwd may be crypt or crypt_sha1
710 if ($enc_passwd =~ m(^\$sha1\$(\d+)\$([./0-9A-Za-z]{1,64})\$[./0-9A-Za-z]{28}$)) {
711 # It's using sha1-crypt
712 return $enc_passwd eq crypt_sha1($plain_passwd, $2, -(0+$1));
713 } else {
714 # It's using crypt
715 return $enc_passwd eq crypt($plain_passwd, $enc_passwd);
719 sub authenticate {
720 my $self = shift;
721 my ($gcgi) = @_;
723 $self->{ccrypt} or die "Can't authenticate against a project with no password";
724 defined($self->{cpwd}) or $self->{cpwd} = '';
725 unless ($_check_passwd_match->($self->{ccrypt}, $self->{cpwd})) {
726 $gcgi->err("Your admin password does not match!");
727 return 0;
729 return 1;
732 # return true if the password from the file is empty or consists of all the same
733 # character. However, if the project was NOT loaded from the group file
734 # (!self->{loaded}) then the password is never locked.
735 # This function does NOT check $Girocco::Config::project_passwords, the caller
736 # is responsible for doing so if desired. Same for $self->{email}.
737 sub is_password_locked {
738 my $self = shift;
740 $self->{loaded} or return 0;
741 my $testcrypt = $self->{ccrypt}; # The value from the group file
742 defined($testcrypt) or $testcrypt = '';
743 $testcrypt ne '' or return 1; # No password at all
744 $testcrypt =~ /^(.)\1*$/ and return 1; # Bogus crypt value
745 return 0; # Not locked
748 sub _setup {
749 use POSIX qw(strftime);
750 my $self = shift;
751 my ($pushers) = @_;
752 my $fd;
754 $self->_mkdir_forkees;
756 my $gid;
757 mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
758 if ($Girocco::Config::owning_group) {
759 $gid = scalar(getgrnam($Girocco::Config::owning_group));
760 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
761 chmod(02775, $self->{path}) or die "chmod 02775 $self->{path} failed: $!";
762 } else {
763 chmod(02777, $self->{path}) or die "chmod 02777 $self->{path} failed: $!";
765 $ENV{'GIT_TEMPLATE_DIR'} = $Girocco::Config::chroot.'/var/empty';
766 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--quiet', '--bare', '--shared='.$self->shared_mode()) == 0
767 or die "git init $self->{path} failed: $?";
768 # we don't need these two, remove them (they will normally be created empty) if they exist
769 rmdir $self->{path}."/branches";
770 rmdir $self->{path}."/remotes";
771 -d $self->{path}."/info" or mkdir $self->{path}."/info"
772 or die "info directory does not exist and unable to create it: $!";
773 -d $self->{path}."/hooks" or mkdir $self->{path}."/hooks"
774 or die "hooks directory does not exist and unable to create it: $!";
775 # clean out any kruft that may have come in from the initial template directory
776 foreach my $cleandir (qw(hooks info)) {
777 if (opendir my $hooksdir, $self->{path}.'/'.$cleandir) {
778 unlink map "$self->{path}/$_", grep { $_ ne '.' && $_ ne '..' } readdir $hooksdir;
779 closedir $hooksdir;
782 if ($Girocco::Config::owning_group) {
783 chown(-1, $gid, $self->{path}."/hooks") or die "chgrp $gid $self->{path}/hooks failed: $!";
785 # hooks never world writable
786 chmod 02775, $self->{path}."/hooks" or die "chmod 02775 $self->{path}/hooks failed: $!";
787 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'core.logallrefupdates', 'false') == 0
788 or die "disabling core.logallrefupdates failed: $?";
789 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastforwards', 'false') == 0
790 or die "disabling receive.denyNonFastforwards failed: $?";
791 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gc.auto', '0') == 0
792 or die "disabling gc.auto failed: $?";
793 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.autogc', '0') == 0
794 or die "disabling receive.autogc failed: $?";
795 my ($S,$M,$H,$d,$m,$y) = gmtime(time());
796 $self->{creationtime} = strftime("%Y-%m-%dT%H:%M:%SZ", $S, $M, $H, $d, $m, $y, -1, -1, -1);
797 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'girocco.creationtime', $self->{creationtime}) == 0
798 or die "setting girocco.creationtime failed: $?";
799 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.updateserverinfo', 'true') == 0
800 or die "enabling receive.updateserverinfo failed: $?";
801 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'repack.writebitmaps', 'true') == 0
802 or die "enabling repack.writebitmaps failed: $?";
803 -d $self->{path}."/htmlcache" or mkdir $self->{path}."/htmlcache"
804 or die "htmlcache directory does not exist and unable to create it: $!";
805 -d $self->{path}."/bundles" or mkdir $self->{path}."/bundles"
806 or die "bundles directory does not exist and unable to create it: $!";
807 foreach my $file (qw(info/lastactivity .delaygc)) {
808 if (open $fd, '>', $self->{path}."/".$file) {
809 close $fd;
810 chmod 0664, $self->{path}."/".$file;
814 # /info must have right permissions,
815 # and git init didn't do it for some reason.
816 # config must have correct permissions.
817 # and Git 2.1.0 - 2.2.1 incorrectly add +x for some reason.
818 # also make sure /refs, /objects and /htmlcache are correct too.
819 my ($dmode, $dmodestr, $fmode, $fmodestr);
820 if ($Girocco::Config::owning_group) {
821 ($dmode, $dmodestr) = (02775, '02775');
822 ($fmode, $fmodestr) = (0664, '0664');
823 } else {
824 ($dmode, $dmodestr) = (02777, '02777');
825 ($fmode, $fmodestr) = (0666, '0666');
827 foreach my $dir (qw(info refs objects htmlcache bundles)) {
828 chmod($dmode, $self->{path}."/$dir") or die "chmod $dmodestr $self->{path}/$dir failed: $!";
830 foreach my $file (qw(config)) {
831 chmod($fmode, $self->{path}."/$file") or die "chmod $fmodestr $self->{path}/$file failed: $!";
833 # these ones are probably not strictly required but are nice to have
834 foreach my $dir (qw(refs/heads refs/tags objects/info objects/pack)) {
835 -d $self->{path}."/$dir" or mkdir $self->{path}."/$dir";
836 chmod($dmode, $self->{path}."/$dir");
839 $self->_properties_save;
840 $self->_alternates_setup;
841 $self->_ctags_setup;
842 $self->_group_remove;
843 $self->_group_add($pushers);
844 $self->_hooks_install;
845 $self->_update_index;
846 $self->_set_changed;
847 $self->_set_forkchange(1);
850 sub premirror {
851 my $self = shift;
853 $self->_setup(':');
854 $self->_clonep(1);
855 $self->perm_initialize;
858 sub conjure {
859 my $self = shift;
861 $self->_setup;
862 $self->_nofetch(1);
863 if ($Girocco::Config::mob && $Girocco::Config::mob eq "mob") {
864 system("$Girocco::Config::basedir/bin/create-personal-mob-area", $self->{name}) == 0
865 or die "create-personal-mob-area $self->{name} failed";
867 $self->perm_initialize;
870 sub clone {
871 my $self = shift;
873 unlink ($self->_clonefail_path()); # Ignore EEXIST error
874 unlink ($self->_clonelog_path()); # Ignore EEXIST error
876 use IO::Socket;
877 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
878 select((select($sock),$|=1)[0]);
879 $sock->print("clone ".$self->{name}."\n");
880 # Just ignore reply, we are going to succeed anyway and the I/O
881 # would apparently get quite hairy.
882 $sock->flush();
883 sleep 2; # *cough*
884 $sock->close();
887 sub update {
888 my $self = shift;
890 $self->_properties_save;
891 $self->_group_update;
893 if (exists($self->{tags_to_delete})) {
894 $self->delete_ctag($_) foreach(@{$self->{tags_to_delete}});
897 $self->set_HEAD($self->{HEAD}) unless $self->{orig_HEAD} eq $self->{HEAD};
899 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
900 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
901 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
902 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
904 $self->_update_index if $self->{email} ne $self->{orig_email};
905 $self->{orig_email} = $self->{email};
906 $self->_set_changed;
911 sub update_password {
912 my $self = shift;
913 my ($pwd) = @_;
915 $self->{crypt} = scrypt_sha1($pwd);
916 $self->_group_update;
919 # You can explicitly do this just on a ghost() repository too.
920 sub delete {
921 my $self = shift;
923 if (-d $self->{path}) {
924 system('rm', '-rf', $self->{path}) == 0
925 or die "rm -rf $self->{path} failed: $?";
927 # attempt to clean up any empty fork directories by removing them
928 my @pelems = split('/', $self->{name});
929 while (@pelems > 1) {
930 pop @pelems;
931 # okay to fail
932 rmdir join('/', $Girocco::Config::reporoot, @pelems) or last;
934 $self->_group_remove;
935 $self->_update_index;
936 $self->_set_forkchange(1);
941 sub _contains_files {
942 my $dir = shift;
943 (-d $dir) or return 0;
944 opendir(my $dh, $dir) or die "opendir $dir failed: $!";
945 while (my $entry = readdir($dh)) {
946 next if $entry eq '' || $entry eq '.' || $entry eq '..';
947 closedir($dh), return 1
948 if -f "$dir/$entry" ||
949 -d "$dir/$entry" && _contains_files("$dir/$entry");
951 closedir($dh);
952 return 0;
955 sub has_forks {
956 my $self = shift;
958 return _contains_files($Girocco::Config::reporoot.'/'.$self->{name});
961 # Returns an array of 0 or more array refs, one for each bundle:
962 # ->[0]: bundle creation time (seconds since epoch)
963 # ->[1]: bundle name (e.g. foo-xxxxxxxx.bundle)
964 # ->[2]: bundle size in bytes
965 sub bundles {
966 my $self = shift;
967 use Time::Local;
969 return @{$self->{bundles}} if ref($self->{bundles}) eq 'ARRAY';
970 my @blist = ();
971 if (-d $self->{path}.'/bundles') {{
972 my $prefix = $self->{name};
973 $prefix =~ s|^.*[^/]/([^/]+)$|$1|;
974 opendir(my $dh, $self->{path}.'/bundles') or last;
975 while (my $bfile = readdir($dh)) {
976 next unless $bfile =~ /^\d{8}_\d{6}-[0-9a-f]{8}$/;
977 my $ctime = eval {timegm(
978 0+substr($bfile,13,2), 0+substr($bfile,11,2), 0+substr($bfile,9,2),
979 0+substr($bfile,6,2), 0+substr($bfile,4,2)-1, 0+substr($bfile,0,4)-1900)};
980 next unless $ctime;
981 open(my $bh, '<', $self->{path}.'/bundles/'.$bfile) or next;
982 my $f1 = <$bh>;
983 my $f2 = <$bh>;
984 $f1 = $self->{path}.'/objects/pack/'.$f1 if $f1 && $f1 !~ m|^/|;
985 $f2 = $self->{path}.'/objects/pack/'.$f2 if $f2 && $f2 !~ m|^/|;
986 close($bh);
987 next unless $f1 && $f2 && $f1 ne $f2;
988 chomp $f1;
989 chomp $f2;
990 next unless -e $f1 && -e $f2;
991 my $s1 = -s $f1 || 0;
992 my $s2 = -s $f2 || 0;
993 next unless $s1 || $s2;
994 push(@blist, [$ctime, "$prefix-".substr($bfile,16,8).".bundle", $s1+$s2]);
996 closedir($dh);
998 @blist = sort({$b->[0] <=> $a->[0]} @blist);
999 my %seen = ();
1000 my @result = ();
1001 foreach my $bndl (@blist) {
1002 next if $seen{$bndl->[1]};
1003 $seen{$bndl->[1]} = 1;
1004 push(@result, $bndl);
1006 $self->{bundles} = \@result;
1007 return @result;
1010 sub has_alternates {
1011 my $self = shift;
1012 return -s $self->{path}.'/objects/info/alternates' ? 1 : 0;
1015 sub has_bundle {
1016 my $self = shift;
1018 return scalar($self->bundles);
1021 # returns true if any of the notify fields are non-empty
1022 sub has_notify {
1023 my $self = shift;
1024 # We do not ckeck notifycia since it's defunct
1025 return $self->{'notifymail'} || $self->{'notifytag'} || $self->{'notifyjson'};
1028 sub is_empty {
1029 # A project is considered empty if the git repository does not
1030 # have any refs. This means packed-refs does not exist or is
1031 # empty or only has lines starting with '#' AND there are no
1032 # files in the refs subdirectory hierarchy (no matter how deep).
1034 my $self = shift;
1036 (-d $self->{path}) or return 0;
1037 if (-e $self->{path}.'/packed-refs') {
1038 open(my $pr, '<', $self->{path}.'/packed-refs')
1039 or die "open $self->{path}./packed-refs failed: $!";
1040 my $foundref = 0;
1041 while (my $ref = <$pr>) {
1042 next if $ref =~ /^#/;
1043 $foundref = 1;
1044 last;
1046 close($pr);
1047 return 0 if $foundref;
1049 (-d $self->{path}.'/refs') or return 1;
1050 return !_contains_files($self->{path}.'/refs');
1053 # returns array:
1054 # [0]: earliest possible scheduled next gc, undef if lastgc not set
1055 # [1]: approx. latest possible scheduled next gc, undef if lastgc not set
1056 # Both values (if not undef) are seconds since epoch
1057 # Result only considers lastgc and min_gc_interval nothing else
1058 sub next_gc {
1059 my $self = shift;
1060 my $lastgcepoch = parse_any_date($self->{lastgc});
1061 return (undef, undef) unless defined $lastgcepoch;
1062 return ($lastgcepoch + $Girocco::Config::min_gc_interval,
1063 int($lastgcepoch + 1.25 * $Girocco::Config::min_gc_interval +
1064 $Girocco::Config::min_mirror_interval));
1067 # returns boolean (0 or 1)
1068 # Attempts to determine whether or not a gc (and corresponding build
1069 # of a .bitmap/.bndl file) will actually take place at the next_gc
1070 # time (as returned by next_gc). Whether or not a .bitmap and .bndl
1071 # end up being built depends on whether or not the local object graph
1072 # is complete. In general if has_alternates is true then a .bitmap/.bndl
1073 # is not possible because the object graph will be incomplete,
1074 # but it *is* possible that even though the repository has_alternates,
1075 # it does not actually borrow any objects so a .bitmap/.bndl will build
1076 # in spite of the presence of alternates -- but this is expected to be rare.
1077 # The result is a best guess and false return value is not an absolute
1078 # guarantee that gc will not take place at the next interval, but it probably
1079 # will not if nothing changes in the meantime.
1080 sub needs_gc {
1081 my $self = shift;
1082 my $lgc = parse_any_date($self->{lastgc});
1083 my $lrecv = parse_any_date($self->{lastreceive});
1084 my $lpgc = parse_any_date($self->{lastparentgc});
1085 return 1 unless defined($lgc) && defined($lrecv);
1086 if ($self->has_alternates) {
1087 return 1 unless defined($lpgc);
1088 return 1 unless $lpgc < $lgc;
1090 return 1 unless $lrecv < $lgc;
1091 # We don't try running any "is_dirty" check, so if somehow the
1092 # repository became dirty without updating lastreceive we might
1093 # incorrectly return false instead of true.
1094 return 0;
1097 sub delete_ctag {
1098 my $self = shift;
1099 my ($ctag) = @_;
1101 # sanity check, disallow filenames starting with . .. or /
1102 unlink($self->{path}.'/ctags/'.$ctag) if($ctag !~ m|^(\.\.?)/|);
1105 sub get_ctag_names {
1106 my $self = shift;
1107 my @ctags = ();
1108 opendir(my $dh, $self->{path}.'/ctags')
1109 or return @ctags;
1110 @ctags = grep { -f "$self->{path}/ctags/$_" } readdir($dh);
1111 closedir($dh);
1112 return sort({lc($a) cmp lc($b)} @ctags);
1115 sub get_heads {
1116 my $self = shift;
1117 my $fh;
1118 my $heads = get_git("--git-dir=$self->{path}", 'for-each-ref', '--format=%(objectname) %(refname)', 'refs/heads');
1119 defined($heads) or $heads = '';
1120 chomp $heads;
1121 my @res = ();
1122 foreach (split(/\n/, $heads)) {
1123 chomp;
1124 next if !m#^[0-9a-f]{40}\s+refs/heads/(.+)$ #x;
1125 push @res, $1;
1127 push(@res, $self->{orig_HEAD}) if !@res && defined($self->{orig_HEAD}) && $self->{orig_HEAD} ne '';
1128 @res;
1131 sub get_HEAD {
1132 my $self = shift;
1133 my $HEAD = get_git("--git-dir=$self->{path}", 'symbolic-ref', 'HEAD');
1134 defined($HEAD) or $HEAD = '';
1135 chomp $HEAD;
1136 die "could not get HEAD" if ($HEAD !~ m{^refs/heads/(.+)$});
1137 return $1;
1140 sub set_HEAD {
1141 my $self = shift;
1142 my $newHEAD = shift;
1143 # Cursory checks only -- if you want to break your HEAD, be my guest
1144 if ($newHEAD =~ /^\/|['<>]|\.\.|\/$/) {
1145 die "grossly invalid new HEAD: $newHEAD";
1147 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', "refs/heads/$newHEAD");
1148 die "could not set HEAD" if ($? >> 8);
1149 ! -d "$self->{path}/mob" || $Girocco::Config::mob ne 'mob'
1150 or system('cp', '-p', '-f', "$self->{path}/HEAD", "$self->{path}/mob/HEAD") == 0;
1153 sub gen_auth {
1154 my $self = shift;
1155 my ($type) = @_;
1156 $type = 'REPO' unless $type && $type =~ /^[A-Z]+$/;
1158 $self->{authtype} = $type;
1160 no warnings;
1161 $self->{auth} = sha1_hex(time . $$ . rand() . join(':',%$self));
1163 my $expire = time + 24 * 3600;
1164 my $propval = "# ${type}AUTH $self->{auth} $expire";
1165 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gitweb.repoauth', $propval);
1166 $self->{auth};
1169 sub del_auth {
1170 my $self = shift;
1172 delete $self->{auth};
1173 delete $self->{authtype};
1174 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--unset', 'gitweb.repoauth');
1177 sub remove_user {
1178 my $self = shift;
1179 my ($username) = @_;
1181 my $before_count = @{$self->{users}};
1182 $self->{users} = [grep { $_ ne $username } @{$self->{users}}];
1183 return @{$self->{users}} != $before_count;
1186 ### static methods
1188 sub get_forkee_name {
1189 local $_ = $_[0];
1190 (m#^(.*)/.*?$#)[0]; #
1193 sub get_forkee_path {
1194 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1195 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
1196 -d $forkee ? $forkee : '';
1199 # Ultimately the full project/fork name could end up being part of a Git ref name
1200 # when a project's forks are combined into one giant repository for efficiency.
1201 # That means that the project/fork name must satisfy the Git ref name requirements:
1203 # 1. Characters with an ASCII value less than or equal to 32 are not allowed
1204 # 2. The character with an ASCII value of 0x7F is not allowed
1205 # 3. The characters '~', '^', ':', '\', '*', '?', and '[' are not allowed
1206 # 4. The character '/' is a separator and is not allowed within a name
1207 # 5. The name may not start with '.' or end with '.'
1208 # 6. The name may not end with '.lock'
1209 # 7. The name may not contain the '..' sequence
1210 # 8. The name may not contain the '@{' sequence
1211 # 9. If multiple components are used (separated by '/'), no empty '' components
1213 # We also prohibit a trailing '.git' on any path component and futher restrict
1214 # the allowed characters to alphanumeric and [+._-] where names must start with
1215 # an alphanumeric.
1217 sub _valid_name_characters {
1218 local $_ = $_[0];
1219 (not m#^[/+._-]#)
1220 and (not m#//#)
1221 and (not m#\.\.#)
1222 and (not m#/[+._-]#)
1223 and (not m#\./#)
1224 and (not m#\.$#)
1225 and (not m#\.git/#i)
1226 and (not m#\.git$#i)
1227 and (not m#\.lock/#i)
1228 and (not m#\.lock$#i)
1229 and (not m#\.bundle/#i)
1230 and (not m#\.bundle$#i)
1231 and (not m#/$#)
1232 and m#^[a-zA-Z0-9/+._-]+$#
1233 and !has_reserved_suffix($_, $_[1], $_[2]);
1236 sub valid_name {
1237 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1238 local $_ = $_[0];
1239 _valid_name_characters($_) and not exists($reservedprojectnames{lc($_)})
1240 and @{[m#/#g]} <= 5 # maximum fork depth is 5
1241 and ((not m#/#) or -d get_forkee_path($_)); # will also catch ^/
1244 # It's possible that some forks have been kept but the forkee is gone.
1245 # In this case the standard valid_name check is too strict.
1246 sub does_exist {
1247 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1248 my ($name, $nodie) = @_;
1249 my $okay = (
1250 _valid_name_characters($name, $Girocco::Config::reporoot, ".git")
1251 and ((not $name =~ m#/#)
1252 or -d get_forkee_path($name)
1253 or -d $Girocco::Config::reporoot.'/'.get_forkee_name($name)));
1254 (!$okay && $nodie) and return undef;
1255 !$okay and die "tried to query for project with invalid name $name!";
1256 (-d $Girocco::Config::reporoot."/$name.git");
1259 sub get_full_list {
1260 open my $fd, '<', jailed_file("/etc/group") or die "getting project list failed: $!";
1261 my @projects = map {/^([^:_\s#][^:\s#]*):[^:]*:\d{5,}:/ ? $1 : ()} <$fd>;
1262 close $fd;
1263 @projects;