Girocco/Project.pm: add optional silent flag to cgi_fill
[girocco.git] / Girocco / Project.pm
blob20b8ac46ec047a2a26ba117e4cc251c854a43686
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 # NOTE: Each value may be either an ARRAY ref of a single element
37 # OR an ARRAY ref containing 1 or more ARRAY refs thus allowing
38 # a set of fields to be associated with a name (e.g. 'notifyjson')
39 our $metadata_fields = {
40 cleanmirror => ['Mirror refs', 'cleanmirror', 'placeholder'],
41 homepage => ['Homepage URL', 'hp', 'text'],
42 shortdesc => ['Short description', 'desc', 'text'],
43 README => ['<span style="display:inline-block;vertical-align:top">'.
44 'README (HTML, &lt; 8 KiB)<br />leave blank for automatic</span>',
45 'README', 'textarea', 'Enter only &#x201c;<!-- comments -->&#x201d; '.
46 'to completely suppress any README'],
47 notifymail => ['Commit notify &#x2013; mail to', 'notifymail', 'text',
48 'comma separated address list'],
49 reverseorder => ['Show oldest first', 'reverseorder', 'checkbox',
50 'show new revisions in oldest to newest order (instead of the default newest to oldest older)'.
51 ' in &#x201c;Commit notify&#x201d; email when showing new revisions'],
52 summaryonly => ['Summaries only', 'summaryonly', 'checkbox',
53 'suppress patch/diff output in &#x201c;Commit notify&#x201d; email when showing new revisions'],
54 notifytag => ['Tag notify &#x2013; mail to', 'notifytag', 'text',
55 'comma separated address list &#x2013; if not empty, tag '.
56 'notifications are sent here INSTEAD of to '.
57 '&#x201c;Commit notify &#x2013; mail to&#x201d; address(es)'],
58 notifyjson => [
59 ['Commit notify &#x2013; '.
60 '<a title="'.html_esc('single field name is &#x201c;payload&#x201d;', 1).'" href="'.
61 'https://docs.github.com/developers/webhooks-and-events/webhook-events-and-payloads#push'.
62 '">POST JSON</a> at', 'notifyjson', 'text'],
63 ['JSON Content-Type', 'jsontype', 'select', 'JSON POST Content-Type', \&_json_choices]],
64 notifycia => ['Commit notify &#x2013; <a href="http://cia.vc/doc/">CIA project</a> name',
65 'notifycia', 'text', 'CIA is defunct &#x2013; this value is ignored'],
68 sub _json_choices {
69 return ('application/x-www-form-urlencoded', 'application/json');
72 sub _mkdir_forkees {
73 my $self = shift;
74 my @pelems = split('/', $self->{name});
75 pop @pelems; # do not create dir for the project itself
76 my $path = $self->{base_path};
77 foreach my $pelem (@pelems) {
78 $path .= "/$pelem";
79 (-d "$path") or mkdir $path or die "mkdir $path: $!";
80 chmod 02775, $path; # ok if fails (dir may already exist and be owned by someone else)
84 # With a leading ':' get from project local config replacing ':' with 'gitweb.'
85 # With a leading '%' get from project local config after removing '%'
86 # With a leading '!' get boolean from project local config after removing '!' (prefixed with 'gitweb.' if no '.')
87 # With a leading [:%!] a trailing ':defval' may be added to select the default value to use if unset
88 # Otherwise it's a project file name to be loaded
89 # %propmapro entries are loaded but never written
90 # %propmapromirror entries are loaded only for mirrors but never written
92 our %propmap = (
93 url => ':baseurl',
94 email => ':owner',
95 desc => 'description',
96 README => 'README.html',
97 hp => ':homepage',
98 notifymail => '%hooks.mailinglist',
99 notifytag => '%hooks.announcelist',
100 notifyjson => '%hooks.jsonurl',
101 jsontype => '%hooks.jsontype',
102 jsonsecret => '%hooks.jsonsecret',
103 notifycia => '%hooks.cianame',
104 cleanmirror => '!girocco.cleanmirror',
105 statusupdates => '!statusupdates:1',
106 reverseorder => '!hooks.reverseorder',
107 summaryonly => '!hooks.summaryonly',
110 our %propmapro = (
111 lastchange => ':lastchange',
112 lastactivity => 'info/lastactivity',
113 lastgc => ':lastgc',
114 lastreceive => ':lastreceive',
115 lastparentgc => ':lastparentgc',
116 lastrefresh => ':lastrefresh',
117 creationtime => '%girocco.creationtime',
118 reposizek => '%girocco.reposizek',
119 notifyhook => '%girocco.notifyhook:undef',
120 origurl => ':baseurl',
123 our %propmapromirror = (
124 bangcount => '%girocco.bang.count',
125 bangfirstfail => '%girocco.bang.firstfail',
126 bangmessagesent => '!girocco.bang.messagesent',
127 showpush => '!showpush',
130 # Projects with any of these names will be disallowed to avoid possible
131 # collisions with cgi script paths or chroot paths
132 # NOTE: names are checked after using lc on them, so all entries MUST be lowercase
133 our %reservedprojectnames = (
134 admin => 1, # /admin/ links
135 alternates => 1, # .git/objects/info/alternates
136 b => 1, # /b/ -> bundle.cgi
137 blog => 1, # /blog/ links
138 c => 1, # /c/ -> cgit
139 'git-receive-pack' => 1, # smart HTTP
140 'git-upload-archive' => 1, # smart HTTP
141 'git-upload-pack' => 1, # smart HTTP
142 h => 1, # /h/ -> html.cgi
143 head => 1, # .git/HEAD
144 'http-alternates' => 1, # .git/objects/info/http-alternates
145 info => 1, # .git/info
146 objects => 1, # .git/objects
147 packs => 1, # .git/objects/info/packs
148 r => 1, # /r/ -> git http
149 refs => 1, # .git/refs
150 w => 1, # /w/ -> gitweb
151 wiki => 1, # /wiki/ links
152 srv => 1, # /srv/git/ -> chroot ssh git repositories
155 sub _update_index {
156 my $self = shift;
157 system("$Girocco::Config::basedir/gitweb/genindex.sh", $self->{name});
160 sub _readlocalconfigfile {
161 my $self = shift;
162 my $undefonerr = shift || 0;
163 delete $self->{configfilehash};
164 my $confighash = read_config_file_hash($self->{path} . "/config");
165 my $result = 1;
166 defined($confighash) || $undefonerr or $result = 0, $confighash = {};
167 return undef unless defined($confighash);
168 $self->{configfilehash} = $confighash;
169 return $result;
172 # @_[0]: argument to convert to boolean result (0 or 1)
173 # @_[1]: value to use if argument is undef (default is 0)
174 # Returns 0 or 1
175 sub _boolval {
176 my ($val, $def) = @_;
177 defined($def) or $def = 0;
178 defined($val) or $val = $def;
179 $val =~ s/\s+//gs;
180 $val = lc($val);
181 return 0 if $val eq '' || $val eq 'false' || $val eq 'off' || $val eq 'no' || $val =~ /^[-+]?0+$/;
182 return 1;
185 sub _property_path {
186 my $self = shift;
187 my ($name) = @_;
188 $self->{path}.'/'.$name;
191 sub _property_fget {
192 my $self = shift;
193 my ($name, $nodef) = @_;
194 my $pname = $propmap{$name};
195 $pname = $propmapro{$name} unless $pname;
196 $pname = $propmapromirror{$name} unless $pname;
197 $pname or die "unknown property: $name";
198 if ($pname =~ /^([:%!])([^:]+)(:.*)?$/) {
199 my ($where, $pname, $defval) = ($1, lc($2), substr(($3||":"),1));
200 $defval = undef if $defval eq "undef";
201 $defval = '' if $nodef;
202 $self->_readlocalconfigfile
203 unless ref($self->{configfilehash}) eq 'HASH';
204 $pname = "gitweb." . $pname if $where eq ':' or $where eq '!' && $pname !~ /[.]/;
205 my $val = $self->{configfilehash}->{$pname};
206 defined($val) or $val = $defval;
207 chomp $val if defined($val);
208 $val = _boolval($val, $defval) if $where eq '!';
209 return $nodef && !exists($self->{configfilehash}->{$pname}) ? undef : $val;
212 open my $p, '<', $self->_property_path($pname) or return undef;
213 my @value = <$p>;
214 close $p;
215 my $value = join('', @value); chomp $value;
216 $value;
219 sub _prop_is_same {
220 my $self = shift;
221 my ($name, $value) = @_;
222 my $existing = $self->_property_fget($name, 1);
223 defined($value) or $value = '';
224 return defined($existing) && $existing eq $value;
227 sub _property_fput {
228 my $self = shift;
229 my ($name, $value, $nosetsame) = @_;
230 my $pname = $propmap{$name};
231 $pname or die "unknown property: $name";
232 my $defval = '';
233 ($pname, $defval) = ($1, substr(($2||":"),1)) if $pname =~ /^([:%!][^:]+)(:.*)?$/;
234 defined($value) or $value = $defval;
235 if ($pname =~ s/^://) {
236 return if $nosetsame && $self->_prop_is_same($name, $value);
237 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', "gitweb.$pname", $value);
238 return;
239 } elsif ($pname =~ s/^%//) {
240 return if $nosetsame && $self->_prop_is_same($name, $value);
241 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', $pname, $value);
242 return;
243 } elsif ($pname =~ s/^!//) {
244 $pname = "gitweb." . $pname unless $pname =~ /[.]/;
245 $value = _boolval($value, $defval);
246 return if $nosetsame && $self->_prop_is_same($name, $value);
247 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--bool', $pname, $value);
248 return;
251 my $P = lock_file($self->_property_path($pname));
252 chomp $value;
253 $value ne '' and print $P "$value\n";
254 close $P;
255 unlock_file($self->_property_path($pname));
258 sub _cleanup_datetime {
259 my $self = shift;
260 my $k = shift;
261 defined($k) && $k ne "" or return;
262 local $_ = $self->{$k}; defined($_) or $_ = '';
263 {use bytes; s/[\x00-\x08\x0e-\x1f\x7f]+//gs;}
264 s/\s+/ /gs;
265 s/^\s+//s;
266 s/\s+$//s;
267 my $ts = parse_any_date($_);
268 defined($ts) or $_ = '';
269 /^(.*)$/ and $self->{$k} = $1;
272 sub _cleanup_description {
273 my $self = shift;
274 local $_ = $self->{desc}; defined($_) or $_ = '';
275 {use bytes; s/[\x00-\x08\x0e-\x1f\x7f]+//gs;}
276 s/\r\n?/\n/gs;
277 s/^\s+//s;
278 s/\s+$//s;
279 /\A(.*)$/m and $self->{desc} = $1;
282 sub _cleanup_readme {
283 my $self = shift;
284 local $_ = $self->{README}; defined($_) or $_ = '';
285 {use bytes; s/[\x00-\x08\x0e-\x1f\x7f]+//gs;}
286 s/\r\n?/\n/gs;
287 s/^\s+//s;
288 s/\s+$//s;
289 $_ eq '' or $_ .= "\n";
290 /^(.*)$/s and $self->{README} = $1;
293 sub _lint_readme {
294 my $self = shift;
295 my $htmlfrag = shift;
296 defined($htmlfrag) or $htmlfrag = 1;
297 return 0 unless defined($self->{README}) && $self->{README} ne '';
298 my $test = '<html xmlns="http://www.w3.org/1999/xhtml"><body><div>';
299 $test .= $self->{README};
300 $test .= '</div></body></html>';
301 my ($code, $errors) = capture_command(2, $test, 'xmllint', '--nonet',
302 '--noout', '--nowarning', '-');
303 return 0 unless $code;
304 my $cnt = 0;
305 my @errs = ();
306 for my $line (split(/\n+/, $errors)) {
307 $line = html_esc($line) if $htmlfrag;
308 $line =~ s/ /\&#160;/gs if $htmlfrag;
309 ++$cnt, $line = 'README'.$1 if $line =~ /^-(:\d+:.*)$/;
310 if ($htmlfrag) {
311 push @errs, '<tt>' . $line . '</tt>';
312 } else {
313 push @errs, $line . "\n";
316 if ($htmlfrag) {
317 return ($cnt, join("<br />\n", @errs));
318 } else {
319 return ($cnt, join("", @errs));
323 sub _properties_load {
324 my $self = shift;
325 my $setprop = sub {
326 my $propval = $self->_property_fget($_);
327 defined($propval) and $self->{$_} = $propval;
329 &$setprop foreach keys %propmap;
330 &$setprop foreach keys %propmapro;
331 do {&$setprop foreach keys %propmapromirror} if $self->{mirror};
332 $self->_readlocalconfigfile
333 unless ref($self->{configfilehash}) eq 'HASH';
334 delete $self->{auth};
335 my $val = $self->{configfilehash}->{'gitweb.repoauth'};
336 defined($val) or $val = '';
337 chomp $val;
338 if ($val =~ /^# ([A-Z]+)AUTH ([0-9a-f]+) (\d+)/) {
339 my $expire = $3;
340 if (time < $expire) {
341 $self->{authtype} = $1;
342 $self->{auth} = $2;
345 if ($Girocco::Config::autogchack && ($self->{mirror} || $Girocco::Config::autogchack ne "mirror")) {
346 if (defined($self->{configfilehash}->{'girocco.autogchack'})) {
347 $self->{autogchack} = _boolval($self->{configfilehash}->{'girocco.autogchack'});
350 defined($self->{jsontype}) or $self->{jsontype} = "";
351 $self->{jsontype} = lc($self->{jsontype});
352 $self->{jsontype} eq 'application/x-www-form-urlencoded' || $self->{jsontype} eq 'application/json' or
353 $self->{jsontype} = 'application/x-www-form-urlencoded';
354 defined($self->{jsonsecret}) or $self->{jsonsecret} = "";
355 $self->_cleanup_datetime('lastactivity');
356 $self->_cleanup_description;
357 $self->_cleanup_readme;
358 delete $self->{configfilehash};
361 sub _set_bangagain {
362 my $self = shift;
363 my $fd;
364 if ($self->{mirror} && defined($self->{origurl}) && $self->{url} &&
365 $self->{origurl} ne $self->{url} && -e $self->_banged_path) {
366 if (open($fd, '>', $self->_bangagain_path)) {
367 close $fd;
368 chmod(0664, $self->_bangagain_path);
373 sub _properties_save {
374 my $self = shift;
375 delete $self->{configfilehash};
376 foreach my $prop (keys %propmap) {
377 $self->_property_fput($prop, $self->{$prop}, 1);
379 $self->_set_bangagain;
382 sub _nofetch_path {
383 my $self = shift;
384 $self->_property_path('.nofetch');
387 sub _nofetch {
388 my $self = shift;
389 my ($nofetch) = @_;
390 my $nf = $self->_nofetch_path;
391 if ($nofetch) {
392 open my $x, '>', $nf or die "nofetch failed: $!";
393 close $x;
394 } else {
395 ! -e $nf or unlink $nf or die "yesfetch failed: $!";
399 sub _banged_path {
400 my $self = shift;
401 $self->_property_path('.banged');
404 sub _bangagain_path {
405 my $self = shift;
406 $self->_property_path('.bangagain');
409 sub _gcp_path {
410 my $self = shift;
411 $self->_property_path('.gc_in_progress');
414 sub _clonelog_path {
415 my $self = shift;
416 $self->_property_path('.clonelog');
419 sub _clonefail_path {
420 my $self = shift;
421 $self->_property_path('.clone_failed');
424 sub _clonep_path {
425 my $self = shift;
426 $self->_property_path('.clone_in_progress');
429 sub _clonep {
430 my $self = shift;
431 my ($nofetch) = @_;
432 my $np = $self->_clonep_path;
433 if ($nofetch) {
434 open my $x, '>', $np or die "clonep failed: $!";
435 close $x;
436 } else {
437 unlink $np or die "clonef failed: $!";
440 sub _alternates_setup {
441 use POSIX qw(:fcntl_h);
442 my $self = shift;
443 return unless $self->{name} =~ m#/#;
444 my $forkee_name = get_forkee_name($self->{name});
445 my $forkee_path = get_forkee_path($self->{name});
446 return unless -d $forkee_path;
447 mkdir $self->{path}.'/refs'; chmod 02775, $self->{path}.'/refs';
448 mkdir $self->{path}.'/objects'; chmod 02775, $self->{path}.'/objects';
449 mkdir $self->{path}.'/objects/info'; chmod 02775, $self->{path}.'/objects/info';
450 mkdir $self->{path}.'/objects/pack'; chmod 02775, $self->{path}.'/objects/pack';
452 # If somehow either our objects/pack or the prospective alternate's pack
453 # directory does not exist decline to set up any alternates
454 my $altpath = "$forkee_path/objects";
455 -d $self->{path}.'/objects/pack' && -d $altpath.'/pack' or return;
457 # If our objects/pack and the prospective alternate's pack directory
458 # do not share the same device then decline to set up any alternates
459 my ($selfdev) = stat($self->{path}.'/objects/pack');
460 my ($altdev) = stat($altpath.'/pack');
461 defined($selfdev) && defined($altdev) && $selfdev ne "" && $altdev ne "" && $selfdev == $altdev or return;
463 # We set up both alternates and http_alternates since we cannot use
464 # relative path in alternates - that doesn't work recursively.
466 my $filename = $self->{path}.'/objects/info/alternates';
467 open my $x, '>', $filename or die "alternates failed: $!";
468 print $x "$altpath\n";
469 close $x;
470 chmod 0664, $filename or warn "cannot chmod $filename: $!";
472 if ($Girocco::Config::httppullurl) {
473 $filename = $self->{path}.'/objects/info/http-alternates';
474 open my $x, '>', $filename or die "http-alternates failed: $!";
475 my $upfork = $forkee_name;
476 do { print $x "$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s#/?.+?$## and $upfork); #
477 close $x;
478 chmod 0664, $filename or warn "cannot chmod $filename: $!";
481 # copy lastactivity from the parent project
482 if (open $x, '<', $forkee_path.'/info/lastactivity') {{
483 my $activity = <$x>;
484 close $x;
485 last unless $activity;
486 open $x, '>', $self->{path}.'/info/lastactivity' or last;
487 print $x $activity;
488 close $x;
489 chomp $activity;
490 $self->{'lastactivity'} = $activity;
493 # copy refs from parent project
494 my $dupout;
495 open $dupout, '>&1' or
496 die "could not dup STDOUT_FILENO: $!";
497 my $packedrefsfd = POSIX::open("$self->{path}/packed-refs", O_WRONLY|O_TRUNC|O_CREAT, 0664);
498 defined($packedrefsfd) && $packedrefsfd >= 0 or die "could not open fork's packed-refs file for writing: $!";
499 POSIX::dup2($packedrefsfd, 1) or
500 die "could not dup2 STDOUT_FILENO: $!";
501 POSIX::close($packedrefsfd);
502 my $result = system($Girocco::Config::git_bin, "--git-dir=$forkee_path", 'for-each-ref', '--format=%(objectname) %(refname)');
503 my $resultstr = $!;
504 POSIX::dup2(fileno($dupout), 1);
505 close($dupout);
506 $result == 0 or die "could not create fork's packed-refs file data: $resultstr";
507 unlink("$self->{path}/.delaygc") if -s "$self->{path}/packed-refs";
509 # initialize HEAD
510 my $HEAD = get_git("--git-dir=$forkee_path", 'symbolic-ref', 'HEAD');
511 defined($HEAD) && $HEAD =~ m,^refs/heads/., or $HEAD = 'refs/heads/master';
512 chomp $HEAD;
513 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', $HEAD);
514 chmod 0664, "$self->{path}/packed-refs", "$self->{path}/HEAD";
517 sub _set_changed {
518 my $self = shift;
519 my $fd;
520 open $fd, '>', "$self->{path}/htmlcache/changed" and close $fd;
523 sub _set_forkchange {
524 my $self = shift;
525 my $changedtoo = shift;
526 return unless $self->{name} =~ m#/#;
527 my $forkee_path = get_forkee_path($self->{name});
528 return unless -d $forkee_path;
529 # mark forkee as changed
530 my $fd;
531 open $fd, '>', $forkee_path.'/htmlcache/changed' and close $fd if $changedtoo;
532 open $fd, '>', $forkee_path.'/htmlcache/forkchange' and close $fd;
533 return if -e $forkee_path.'/htmlcache/summary.forkchange';
534 open $fd, '>', $forkee_path.'/htmlcache/summary.forkchange' and close $fd;
537 sub _ctags_setup {
538 my $self = shift;
539 my $perms = $Girocco::Config::permission_control eq 'Hooks' ? 02777 : 02775;
540 mkdir $self->{path}.'/ctags'; chmod $perms, $self->{path}.'/ctags';
543 sub _group_add {
544 my $self = shift;
545 my ($xtra) = @_;
546 $xtra .= join(',', @{$self->{users}});
547 my $crypt = $self->{crypt};
548 defined($crypt) or $crypt = 'unknown';
549 filedb_atomic_append(jailed_file('/etc/group'),
550 join(':', $self->{name}, $crypt, '\i', $xtra));
553 sub _group_update {
554 my $self = shift;
555 my $xtra = join(',', @{$self->{users}});
556 filedb_atomic_edit(jailed_file('/etc/group'),
557 sub {
558 $_ = $_[0];
559 chomp;
560 if ($self->{name} eq (split /:/)[0]) {
561 # preserve readonly flag
562 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
563 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
564 } else {
565 return "$_\n";
571 sub _group_remove {
572 my $self = shift;
573 filedb_atomic_edit(jailed_file('/etc/group'),
574 sub {
575 $self->{name} ne (split /:/)[0] and return $_;
580 sub _hook_path {
581 my $self = shift;
582 my ($name) = @_;
583 $self->{path}.'/hooks/'.$name;
586 sub _hook_install {
587 my $self = shift;
588 my ($name) = @_;
589 my $hooksdir = $self->{path}.'/hooks';
590 my $oldmask = umask();
591 umask($oldmask & ~0070);
592 -d $hooksdir or mkdir $hooksdir or
593 die "hooks directory does not exist and unable to create it for project " . $self->{name} . ": $!";
594 umask($oldmask);
595 my $globalhooks = $Girocco::Config::reporoot . "/_global/hooks";
596 -f "$globalhooks/$name" && -r _ or die "cannot find hook $name: $!";
597 ! -e $self->_hook_path($name) || unlink($self->_hook_path($name)) && ! -e $self->_hook_path($name) or
598 die "hooks directory contains unremovable pre-existing hook $name: $!";
599 symlink("$globalhooks/$name", $self->_hook_path($name)) or
600 die "cannot create hook $name symlink: $!";
603 sub _hooks_install {
604 my $self = shift;
605 foreach my $hook ('pre-auto-gc', 'pre-receive', 'post-commit', 'post-receive', 'update') {
606 $self->_hook_install($hook);
610 # private constructor, do not use
611 sub _new {
612 my $class = shift;
613 my ($name, $base_path, $path, $orphan, $optp) = @_;
614 does_exist(\$name,1) || valid_name(\$name, $orphan, $optp) or die "refusing to create project with invalid name ($name)!";
615 $path ||= "$base_path/$name.git";
616 my $proj = { name => $name, base_path => $base_path, path => $path };
618 bless $proj, $class;
621 # public constructor #0
622 # creates a virtual project not connected to disk image
623 # you can conjure() it later to disk
624 sub ghost {
625 my $class = shift;
626 my ($name, $mirror, $orphan, $optp) = @_;
627 my $self = $class->_new($name, $Girocco::Config::reporoot, undef, $orphan, $optp);
628 $self->{users} = [];
629 $self->{mirror} = $mirror;
630 $self->{email} = $self->{orig_email} = '';
631 $self;
634 # public constructor #1
635 sub load {
636 my $class = shift;
637 my $name = shift || '';
639 open my $fd, '<', jailed_file("/etc/group") or die "project load failed: $!";
640 my $r = qr/^\Q$name\E:/;
641 foreach (grep /$r/, <$fd>) {
642 chomp;
644 my $self = $class->_new($name, $Girocco::Config::reporoot);
645 (-d $self->{path} && $self->_readlocalconfigfile(1))
646 or die "invalid path (".$self->{path}.") for project ".$self->{name};
648 my (undef, $crypt, $gid, $ulist) = split /:/;
649 $gid =~ /^(\d+)$/ or next; $self->{gid} = $1;
650 { use bytes;
651 $crypt =~ /^([^:\x00-\x1F\x7F-\xFF]*)$/ or next; $self->{crypt} = $1; }
652 defined($ulist) or $ulist = '';
653 $ulist =~ /^((?:\w+(?:,\w+)*)?)$/ or next;
654 $self->{users} = [split /,/, $1];
655 $self->{HEAD} = $self->get_HEAD;
656 $self->{orig_HEAD} = $self->{HEAD};
657 $self->{orig_users} = [@{$self->{users}}];
658 $self->{mirror} = ! -e $self->_nofetch_path;
659 $self->{banged} = -e $self->_banged_path if $self->{mirror};
660 $self->{gc_in_progress} = -e $self->_gcp_path;
661 $self->{clone_in_progress} = -e $self->_clonep_path;
662 $self->{clone_logged} = -e $self->_clonelog_path;
663 $self->{clone_failed} = -e $self->_clonefail_path;
664 $self->{ccrypt} = $self->{crypt};
666 $self->_properties_load;
667 $self->{orig_email} = $self->{email};
668 $self->{loaded} = 1; # indicates self was loaded from etc/group file
669 close $fd;
670 return $self;
672 close $fd;
673 undef;
676 # $proj may not be in sane state if this returns false!
677 # fields listed in %$metadata_fields that are NOT also
678 # in @Girocco::Config::project_fields are totally ignored!
679 sub cgi_fill {
680 my $self = shift;
681 my ($gcgi, $silent) = @_;
682 my $cgi = $gcgi->cgi;
683 my %allowedfields = map({$_ => 1} @Girocco::Config::project_fields);
684 my $field_enabled = sub {
685 defined($cgi->param($_[0])) &&
686 (!exists($metadata_fields->{$_[0]}) || exists($allowedfields{$_[0]}))};
688 my $pwd = $cgi->param('pwd');
689 my $pwd2 = $cgi->param('pwd2');
690 # in case passwords are disabled
691 defined($pwd) or $pwd = ''; defined($pwd2) or $pwd2 = '';
692 if ($Girocco::Config::project_passwords and not $self->{crypt} and $pwd eq '' and $pwd2 eq '') {
693 $gcgi->err("Empty passwords are not permitted.");
695 if ($pwd ne '' or not $self->{crypt}) {
696 $self->{crypt} = scrypt_sha1($pwd);
698 if (($pwd ne '' || $pwd2 ne '') and $pwd ne $pwd2) {
699 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
702 $self->{cpwd} = $cgi->param('cpwd');
704 my ($forkee,$project) = ($self->{name} =~ m#^(.*/)?([^/]+)$#);
705 my $newtype = $forkee ? 'fork' : 'project';
706 length($project) <= 64
707 or $gcgi->err("The $newtype name is longer than 64 characters. Do you really need that much?");
709 if ($Girocco::Config::project_owners eq 'email') {
710 $self->{email} = $gcgi->wparam('email');
711 valid_email($self->{email})
712 or $gcgi->err("Your email sure looks weird...?");
713 length($self->{email}) <= 96
714 or $gcgi->err("Your email is longer than 96 characters. Do you really need that much?");
717 # No setting the url unless we're either new or an existing mirror!
718 unless ($self->{loaded} && !$self->{mirror}) {
719 $self->{url} = $gcgi->wparam('url') ;
720 if ($field_enabled->('cleanmirror')) {
721 $self->{cleanmirror} = $gcgi->wparam('cleanmirror') || 0;
724 # Always validate the url if we're an existing mirror
725 if ((defined($self->{url}) && $self->{url} ne '') || ($self->{loaded} && $self->{mirror})) {{
726 # Always allow the url to be left unchanged without validation when editing
727 last if $self->{loaded} && defined($self->{origurl}) && defined($self->{url}) && $self->{origurl} eq $self->{url};
728 valid_repo_url($self->{url})
729 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocols are supported. If the URL contains funny characters, contact me.");
730 if ($Girocco::Config::restrict_mirror_hosts) {
731 my $mh = extract_url_hostname($self->{url});
732 is_dns_hostname($mh)
733 or $gcgi->err("Invalid URL. Note that only DNS names are allowed, not IP addresses.");
734 !is_our_hostname($mh)
735 or $gcgi->err("Invalid URL. Mirrors from this host are not allowed, please create a fork instead.");
739 if ($field_enabled->('desc')) {
740 $self->{desc} = to_utf8($gcgi->wparam('desc'), 1);
741 length($self->{desc}) <= 1024
742 or $gcgi->err("<b>Short</b> description length &gt; 1kb!");
745 if ($field_enabled->('README')) {
746 $self->{README} = to_utf8($gcgi->wparam('README'), 1);
747 $self->_cleanup_readme;
748 length($self->{README}) <= 8192
749 or $gcgi->err("README length &gt; 8kb!");
750 my ($cnt, $err) = (0);
751 ($cnt, $err) = $self->_lint_readme if $gcgi->ok && $Girocco::Config::xmllint_readme;
752 $gcgi->err($err), $gcgi->{err} += $cnt-1 if $cnt;
755 if ($field_enabled->('hp')) {
756 $self->{hp} = $gcgi->wparam('hp');
757 if ($self->{hp}) {
758 valid_web_url($self->{hp})
759 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
763 # No mucking about with users unless we're a push project
764 if (($self->{loaded} && !$self->{mirror}) ||
765 (!$self->{loaded} && (!defined($self->{url}) || $self->{url} eq ''))) {
766 my %users = ();
767 my @users = ();
768 foreach my $user ($cgi->multi_param('user')) {
769 if (!exists($users{$user})) {
770 $users{$user} = 1;
771 push(@users, $user) if Girocco::User::does_exist($user, 1);
774 $self->{users} = \@users;
777 # HEAD can only be set with editproj, NOT regproj (regproj sets it automatically)
778 # It may ALWAYS be set to what it was (even if there's no such refs/heads/...)
779 my $newhead;
780 if (defined($self->{orig_HEAD}) && $self->{orig_HEAD} ne '' &&
781 defined($newhead = $cgi->param('HEAD')) && $newhead ne '') {
782 if ($newhead eq $self->{orig_HEAD} ||
783 get_git("--git-dir=$self->{path}", 'rev-parse', '--verify', '--quiet', 'refs/heads/'.$newhead)) {
784 $self->{HEAD} = $newhead;
785 } else {
786 $gcgi->err("Invalid default branch (no such ref)");
790 # schedule deletion of tags (will be committed by update() after auth)
791 $self->{tags_to_delete} = [$cgi->multi_param('tags')];
793 if ($field_enabled->('notifymail')) {
794 my $newaddrs = clean_email_multi($gcgi->wparam('notifymail'));
795 if ($newaddrs eq "" or (valid_email_multi($newaddrs) and length($newaddrs) <= 512)) {
796 $self->{notifymail} = $newaddrs;
797 } else {
798 $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
800 if ($field_enabled->('reverseorder')) {
801 $self->{reverseorder} = $gcgi->wparam('reverseorder') || 0;
803 if ($field_enabled->('summaryonly')) {
804 $self->{summaryonly} = $gcgi->wparam('summaryonly') || 0;
808 if ($field_enabled->('notifytag')) {
809 my $newaddrs = clean_email_multi($gcgi->wparam('notifytag'));
810 if ($newaddrs eq "" or (valid_email_multi($newaddrs) and length($newaddrs) <= 512)) {
811 $self->{notifytag} = $newaddrs;
812 } else {
813 $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
817 if ($field_enabled->('notifyjson')) {
818 $self->{notifyjson} = $gcgi->wparam('notifyjson');
819 if ($self->{notifyjson}) {
820 valid_web_url($self->{notifyjson})
821 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
823 $self->{jsontype} = $gcgi->wparam('jsontype');
824 defined($self->{jsontype}) && $self->{jsontype} ne "" or
825 $self->{jsontype} = 'application/x-www-form-urlencoded';
826 $self->{jsontype} = lc($self->{jsontype});
827 unless ($self->{jsontype} eq 'application/x-www-form-urlencoded' ||
828 $self->{jsontype} eq 'application/json') {
829 if ($self->{notifyjson}) {
830 $gcgi->err("Invalid JSON Content-Type. Must be 'application/x-www-form-urlencoded' or 'application/json'.");
831 } else {
832 $self->{jsontype} = 'application/x-www-form-urlencoded';
837 if ($field_enabled->('notifycia')) {
838 $self->{notifycia} = $gcgi->wparam('notifycia');
839 if ($self->{notifycia}) {
840 $self->{notifycia} =~ /^[a-zA-Z0-9._-]+$/
841 or $gcgi->err("Overly suspicious CIA notify project name. If it's actually valid, don't contact me, CIA is defunct.");
845 if ($cgi->param('setstatusupdates')) {
846 my $val = $gcgi->wparam('statusupdates') || '0';
847 $self->{statusupdates} = $val ? 1 : 0;
850 $silent ? $gcgi->ok : !$gcgi->err_check;
853 sub form_defaults {
854 my $self = shift;
856 name => $self->{name},
857 email => $self->{email},
858 url => $self->{url},
859 cleanmirror => $self->{cleanmirror},
860 desc => html_esc($self->{desc}),
861 README => html_esc($self->{README}),
862 hp => $self->{hp},
863 users => $self->{users},
864 notifymail => html_esc($self->{notifymail}),
865 reverseorder => $self->{reverseorder},
866 summaryonly => $self->{summaryonly},
867 notifytag => html_esc($self->{notifytag}),
868 notifyjson => html_esc($self->{notifyjson}),
869 jsontype => html_esc($self->{jsontype}),
870 notifycia => html_esc($self->{notifycia}),
872 __project__ => $self
876 # return true if $enc_passwd is a match for $plain_passwd
877 my $_check_passwd_match = sub {
878 my $enc_passwd = shift;
879 my $plain_passwd = shift;
880 defined($enc_passwd) or $enc_passwd = '';
881 defined($plain_passwd) or $plain_passwd = '';
882 # $enc_passwd may be crypt or crypt_sha1
883 if ($enc_passwd =~ m(^\$sha1\$(\d+)\$([./0-9A-Za-z]{1,64})\$[./0-9A-Za-z]{28}$)) {
884 # It's using sha1-crypt
885 return $enc_passwd eq crypt_sha1($plain_passwd, $2, -(0+$1));
886 } else {
887 # It's using crypt
888 return $enc_passwd eq crypt($plain_passwd, $enc_passwd);
892 sub authenticate {
893 my $self = shift;
894 my ($gcgi) = @_;
896 $self->{ccrypt} or die "Can't authenticate against a project with no password";
897 defined($self->{cpwd}) or $self->{cpwd} = '';
898 unless ($_check_passwd_match->($self->{ccrypt}, $self->{cpwd})) {
899 $gcgi->err("Your admin password does not match!");
900 return 0;
902 return 1;
905 # return true if the password from the file is empty or consists of all the same
906 # character. However, if the project was NOT loaded from the group file
907 # (!self->{loaded}) then the password is never locked.
908 # This function does NOT check $Girocco::Config::project_passwords, the caller
909 # is responsible for doing so if desired. Same for $self->{email}.
910 sub is_password_locked {
911 my $self = shift;
913 $self->{loaded} or return 0;
914 my $testcrypt = $self->{ccrypt}; # The value from the group file
915 defined($testcrypt) or $testcrypt = '';
916 $testcrypt ne '' or return 1; # No password at all
917 $testcrypt =~ /^(.)\1*$/ and return 1; # Bogus crypt value
918 return 0; # Not locked
921 sub _setup {
922 use POSIX qw(strftime);
923 my $self = shift;
924 my ($pushers) = @_;
925 my $fd;
927 defined($self->{path}) && $self->{path} ne "" or die "invalid setup call";
928 $self->_mkdir_forkees unless $self->{adopt};
930 my $gid;
931 $self->{adopt} || mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
932 -d $self->{path} or die "unable to setup nonexistent $self->{path}";
933 if ($Girocco::Config::owning_group) {
934 $gid = scalar(getgrnam($Girocco::Config::owning_group));
935 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
936 chmod(02775, $self->{path}) or die "chmod 02775 $self->{path} failed: $!";
937 } else {
938 chmod(02777, $self->{path}) or die "chmod 02777 $self->{path} failed: $!";
940 delete $ENV{GIT_OBJECT_DIRECTORY};
941 $ENV{'GIT_TEMPLATE_DIR'} = $Girocco::Config::chroot.'/var/empty';
942 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--quiet', '--bare', '--shared='.$self->shared_mode()) == 0
943 or die "git init $self->{path} failed: $?";
944 # we don't need these two, remove them (they will normally be created empty) if they exist
945 rmdir $self->{path}."/branches";
946 rmdir $self->{path}."/remotes";
947 -d $self->{path}."/info" or mkdir $self->{path}."/info"
948 or die "info directory does not exist and unable to create it: $!";
949 -d $self->{path}."/hooks" or mkdir $self->{path}."/hooks"
950 or die "hooks directory does not exist and unable to create it: $!";
951 # clean out any kruft that may have come in from the initial template directory
952 foreach my $cleandir (qw(hooks info)) {
953 if (opendir my $hooksdir, $self->{path}.'/'.$cleandir) {
954 unlink map "$self->{path}/$_", grep { $_ ne '.' && $_ ne '..' } readdir $hooksdir;
955 closedir $hooksdir;
958 if ($Girocco::Config::owning_group) {
959 chown(-1, $gid, $self->{path}."/hooks") or die "chgrp $gid $self->{path}/hooks failed: $!";
961 # hooks never world writable
962 chmod 02775, $self->{path}."/hooks" or die "chmod 02775 $self->{path}/hooks failed: $!";
963 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'core.compression', '5') == 0
964 or die "setting core.compression failed: $?";
965 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'core.logAllRefUpdates', 'false') == 0
966 or die "disabling core.logAllRefUpdates failed: $?";
967 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'core.ignoreCase', 'false') == 0
968 or die "disabling core.ignoreCase failed: $?";
969 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'core.hooksPath',
970 ($Girocco::Config::localhooks ? $self->{path}."/hooks" : $Girocco::Config::reporoot . "/_global/hooks")) == 0
971 or die "setting core.hooksPath failed: $?";
972 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'extensions.preciousObjects', 'true') == 0
973 or die "setting extensions.preciousObjects failed: $?";
974 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'transfer.fsckObjects', 'true') == 0
975 or die "enabling transfer.fsckObjects failed: $?";
976 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'transfer.unpackLimit', '1') == 0
977 or die "setting transfer.unpackLimit failed: $?";
978 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.fsckObjects', 'true') == 0
979 or die "enabling receive.fsckObjects failed: $?";
980 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastForwards', 'false') == 0
981 or die "disabling receive.denyNonFastForwards failed: $?";
982 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyDeleteCurrent', 'warn') == 0
983 or die "disabling receive.denyDeleteCurrent failed: $?";
984 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.autogc', '0') == 0
985 or die "disabling receive.autogc failed: $?";
986 my ($S,$M,$H,$d,$m,$y) = gmtime(time());
987 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.updateServerInfo', 'true') == 0
988 or die "enabling receive.updateServerInfo failed: $?";
989 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'repack.writeBitmaps', 'true') == 0
990 or die "enabling repack.writeBitmaps failed: $?";
991 $self->{creationtime} = strftime("%Y-%m-%dT%H:%M:%SZ", $S, $M, $H, $d, $m, $y, -1, -1, -1);
992 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'girocco.creationtime', $self->{creationtime}) == 0
993 or die "setting girocco.creationtime failed: $?";
994 -d $self->{path}."/htmlcache" or mkdir $self->{path}."/htmlcache"
995 or die "htmlcache directory does not exist and unable to create it: $!";
996 -d $self->{path}."/bundles" or mkdir $self->{path}."/bundles"
997 or die "bundles directory does not exist and unable to create it: $!";
998 -d $self->{path}."/reflogs" or mkdir $self->{path}."/reflogs"
999 or die "reflogs directory does not exist and unable to create it: $!";
1000 foreach my $file (qw(info/lastactivity .delaygc)) {
1001 if (open $fd, '>', $self->{path}."/".$file) {
1002 close $fd;
1003 chmod 0664, $self->{path}."/".$file;
1007 # /info must have right permissions,
1008 # and git init didn't do it for some reason.
1009 # config must have correct permissions.
1010 # and Git 2.1.0 - 2.2.1 incorrectly add +x for some reason.
1011 # also make sure /refs, /objects and /htmlcache are correct too.
1012 my ($dmode, $dmodestr, $fmode, $fmodestr);
1013 if ($Girocco::Config::owning_group) {
1014 ($dmode, $dmodestr) = (02775, '02775');
1015 ($fmode, $fmodestr) = (0664, '0664');
1016 } else {
1017 ($dmode, $dmodestr) = (02777, '02777');
1018 ($fmode, $fmodestr) = (0666, '0666');
1020 foreach my $dir (qw(info refs objects htmlcache bundles reflogs)) {
1021 chmod($dmode, $self->{path}."/$dir") or die "chmod $dmodestr $self->{path}/$dir failed: $!";
1023 foreach my $file (qw(config)) {
1024 chmod($fmode, $self->{path}."/$file") or die "chmod $fmodestr $self->{path}/$file failed: $!";
1026 # these ones are probably not strictly required but are nice to have
1027 foreach my $dir (qw(refs/heads refs/tags objects/info objects/pack)) {
1028 -d $self->{path}."/$dir" or mkdir $self->{path}."/$dir";
1029 chmod($dmode, $self->{path}."/$dir");
1031 if ($Girocco::Config::owning_group && defined($gid) && $Girocco::Config::htmlcache_owning_group) {
1032 my $htmlgid = scalar(getgrnam($Girocco::Config::htmlcache_owning_group));
1033 if (defined($htmlgid) && $htmlgid ne $gid) {
1034 chown(-1, $htmlgid, $self->{path}."/htmlcache") or die "chgrp $htmlgid $self->{path}/htmlcache failed: $!";
1035 chmod($dmode, $self->{path}."/htmlcache") or die "chmod $dmodestr $self->{path}/htmlcache failed: $!";
1038 if ($Girocco::Config::owning_group && defined($gid) && $Girocco::Config::ctags_owning_group) {
1039 my $ctagsgid = scalar(getgrnam($Girocco::Config::ctags_owning_group));
1040 if (defined($ctagsgid) && $ctagsgid ne $gid) {
1041 chown(-1, $ctagsgid, $self->{path}."/ctags") or die "chgrp $ctagsgid $self->{path}/ctags failed: $!";
1042 chmod($dmode, $self->{path}."/ctags") or die "chmod $dmodestr $self->{path}/ctags failed: $!";
1046 $self->_properties_save;
1047 $self->_alternates_setup unless $self->{noalternates} || $self->{adopt};
1048 $self->_ctags_setup;
1049 $self->_group_remove;
1050 $self->_group_add($pushers);
1051 $self->_hooks_install;
1052 $self->_update_index;
1053 $self->_set_changed;
1054 $self->_set_forkchange(1);
1057 sub premirror {
1058 my $self = shift;
1060 delete $self->{adopt};
1061 $self->_setup(':');
1062 $self->_clonep(1);
1063 if ($Girocco::Config::autogchack) {
1064 system("$Girocco::Config::basedir/jobd/maintain-auto-gc-hack.sh", $self->{name}) == 0
1065 or die "maintain-auto-gc-hack.sh $self->{name} failed";
1067 $self->perm_initialize;
1070 sub conjure {
1071 my $self = shift;
1073 delete $self->{adopt};
1074 $self->_setup;
1075 $self->_nofetch(1);
1076 if ($Girocco::Config::autogchack && $Girocco::Config::autogchack ne "mirror") {
1077 system("$Girocco::Config::basedir/jobd/maintain-auto-gc-hack.sh", $self->{name}) == 0
1078 or die "maintain-auto-gc-hack.sh $self->{name} failed";
1080 if ($Girocco::Config::mob && $Girocco::Config::mob eq "mob") {
1081 system("$Girocco::Config::basedir/bin/create-personal-mob-area", $self->{name}) == 0
1082 or die "create-personal-mob-area $self->{name} failed";
1084 $self->perm_initialize;
1087 sub clone {
1088 my $self = shift;
1090 unlink ($self->_clonefail_path()); # Ignore EEXIST error
1091 unlink ($self->_clonelog_path()); # Ignore EEXIST error
1093 use IO::Socket;
1094 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
1095 select((select($sock),$|=1)[0]);
1096 $sock->print("clone ".$self->{name}."\n");
1097 # Just ignore reply, we are going to succeed anyway and the I/O
1098 # would apparently get quite hairy.
1099 $sock->flush();
1100 sleep 2; # *cough*
1101 $sock->close();
1104 # call this after ghost instead of conjure or premirror+clone to adopt a pre-existing Git dir
1105 # ghost must be called with the proper value of $mirror for the to-be-adopted project
1106 # for mirrors the $proj->{url} needs to be set and for push projects the $proj->{users} array ref
1107 sub adopt {
1108 my $self = shift;
1109 my $name = $self->{name};
1111 # Sanity check first
1112 defined($name) && $name ne "" && !$self->{loaded} or return undef;
1113 does_exist($name, 1) or return undef;
1114 defined($self->{path}) && $self->{path} eq $Girocco::Config::reporoot."/$name.git" or return undef;
1115 defined($self->{base_path}) && $self->{base_path} eq $Girocco::Config::reporoot or return undef;
1116 defined($self->{email}) && defined($self->{orig_email}) && defined($self->{mirror}) && ref($self->{users}) eq 'ARRAY'
1117 or return undef;
1118 !defined(Girocco::Project->load($name)) or return undef;
1119 is_git_dir($self->{path}) or return undef;
1120 my $config = read_config_file_hash($self->{path}."/config");
1121 defined($config) && _boolval($config->{"core.bare"}) or die "refusing to adopt non-bare repository";
1122 defined(read_HEAD_symref($self->{path})) or die "refusing to adopt non-symref HEAD repository";
1124 # Adopt the project by creating a new $chroot/etc/group entry and setting up anything that's missing
1125 $self->_nofetch(!$self->{mirror});
1126 $self->{adopt} = 1;
1127 $self->_setup($self->{mirror} ? ":" : "");
1128 delete $self->{adopt};
1129 if ($Girocco::Config::autogchack && ($self->{mirror} || $Girocco::Config::autogchack ne "mirror")) {
1130 system("$Girocco::Config::basedir/jobd/maintain-auto-gc-hack.sh", $self->{name}) == 0
1131 or die "maintain-auto-gc-hack.sh $self->{name} failed";
1133 if (!$self->{mirror} && $Girocco::Config::mob && $Girocco::Config::mob eq "mob") {
1134 system("$Girocco::Config::basedir/bin/create-personal-mob-area", $self->{name}) == 0
1135 or die "create-personal-mob-area $self->{name} failed";
1137 my $result = $self->perm_initialize;
1138 unlink("$self->{path}/.delaygc") unless $self->is_empty;
1139 # Pick up any pre-existing settings
1140 my $p = Girocco::Project->load($name);
1141 %$self = %$p if defined($p) && $p->{loaded};
1142 $result;
1145 sub _update_users {
1146 my $self = shift;
1148 $self->_group_update;
1149 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
1150 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
1151 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
1152 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
1155 sub update {
1156 my $self = shift;
1158 $self->_properties_save;
1159 $self->_update_users;
1161 if (exists($self->{tags_to_delete})) {
1162 $self->delete_ctag($_) foreach(@{$self->{tags_to_delete}});
1165 $self->set_HEAD($self->{HEAD}) unless $self->{orig_HEAD} eq $self->{HEAD};
1167 $self->_update_index if $self->{email} ne $self->{orig_email};
1168 $self->{orig_email} = $self->{email};
1169 $self->_set_changed;
1174 sub update_password {
1175 my $self = shift;
1176 my ($pwd) = @_;
1178 $self->{crypt} = scrypt_sha1($pwd);
1179 $self->_group_update;
1182 # You can explicitly do this just on a ghost() repository too.
1183 sub delete {
1184 my $self = shift;
1186 if (-d $self->{path}) {
1187 system('rm', '-rf', $self->{path}) == 0
1188 or die "rm -rf $self->{path} failed: $?";
1190 # attempt to clean up any empty fork directories by removing them
1191 my @pelems = split('/', $self->{name});
1192 while (@pelems > 1) {
1193 pop @pelems;
1194 # okay to fail
1195 rmdir join('/', $Girocco::Config::reporoot, @pelems) or last;
1197 $self->_group_remove;
1198 $self->_update_index;
1199 $self->_set_forkchange(1);
1204 # If the project's directory actually exists archive it before deleting it
1205 # Return full path to archived project ("" if none)
1206 sub archive_and_delete {
1207 my $self = shift;
1209 unless (-d $self->{path}) {
1210 $self->delete;
1211 return "";
1214 # archive the project before deletion
1215 use POSIX qw(strftime);
1216 my $destdir = $self->{base_path};
1217 $destdir =~ s,(?<=[^/])/+$,,;
1218 $destdir .= "/_recyclebin/";
1219 $destdir .= $1 if $self->{name} =~ m,^(.*/)[^/]+$,;
1220 my $destbase = $self->{name};
1221 $destbase = $1 if $destbase =~ m,^.*/([^/]+)$,;
1222 my $oldmask = umask();
1223 umask($oldmask & ~0070);
1224 system('mkdir', '-p', $destdir) == 0 && -d $destdir
1225 or die "mkdir -p \"$destdir\" failed: $?";
1226 umask($oldmask);
1227 my $suffix = '';
1228 if (-e "$destdir$destbase.git") {
1229 $suffix = 1;
1230 while (-e "$destdir$destbase~$suffix.git") {
1231 ++$suffix;
1232 last if $suffix >= 10000; # don't get too carried away
1234 $suffix = '~'.$suffix;
1236 not -e "$destdir$destbase$suffix.git"
1237 or die "Unable to compute suitable archive path";
1238 system('mv', $self->{path}, "$destdir$destbase$suffix.git") == 0
1239 or die "mv \"$self->{path}\" \"$destdir$destbase$suffix.git\" failed: $?";
1240 if (!$self->{mirror} && @{$self->{users}}) {
1241 # Remember the user list at recycle time
1242 system($Girocco::Config::git_bin, '--git-dir='.$destdir.$destbase.$suffix.".git",
1243 'config', 'girocco.recycleusers', join(",", @{$self->{users}}));
1245 my ($S,$M,$H,$d,$m,$y) = gmtime(time());
1246 my $recycletime = strftime("%Y-%m-%dT%H:%M:%SZ", $S, $M, $H, $d, $m, $y, -1, -1, -1);
1247 # We ought to do something if this fails, but the project has already been moved
1248 # so there's really nothing to be done at this point.
1249 system($Girocco::Config::git_bin, '--git-dir='.$destdir.$destbase.$suffix.".git",
1250 'config', 'girocco.recycletime', $recycletime);
1252 $self->delete;
1253 return $destdir.$destbase.$suffix.".git";
1256 sub _contains_files {
1257 my $dir = shift;
1258 (-d $dir) or return 0;
1259 opendir(my $dh, $dir) or die "opendir $dir failed: $!";
1260 while (my $entry = readdir($dh)) {
1261 next if $entry eq '' || $entry eq '.' || $entry eq '..';
1262 closedir($dh), return 1
1263 if -f "$dir/$entry" ||
1264 -d "$dir/$entry" && _contains_files("$dir/$entry");
1266 closedir($dh);
1267 return 0;
1270 sub has_forks {
1271 my $self = shift;
1273 return _contains_files($Girocco::Config::reporoot.'/'.$self->{name});
1276 # Returns an array of 0 or more array refs, one for each bundle:
1277 # ->[0]: bundle creation time (seconds since epoch)
1278 # ->[1]: bundle name (e.g. foo-xxxxxxxx.bundle)
1279 # ->[2]: bundle size in bytes
1280 sub bundles {
1281 my $self = shift;
1282 use Time::Local;
1284 return @{$self->{bundles}} if ref($self->{bundles}) eq 'ARRAY';
1285 my @blist = ();
1286 if (-d $self->{path}.'/bundles') {{
1287 my $prefix = $self->{name};
1288 $prefix =~ s|^.*[^/]/([^/]+)$|$1|;
1289 opendir(my $dh, $self->{path}.'/bundles') or last;
1290 while (my $bfile = readdir($dh)) {
1291 next unless $bfile =~ /^\d{8}_\d{6}-[0-9a-f]{8}$/;
1292 my $ctime = eval {timegm(
1293 0+substr($bfile,13,2), 0+substr($bfile,11,2), 0+substr($bfile,9,2),
1294 0+substr($bfile,6,2), 0+substr($bfile,4,2)-1, 0+substr($bfile,0,4))};
1295 next unless $ctime;
1296 open(my $bh, '<', $self->{path}.'/bundles/'.$bfile) or next;
1297 my $f1 = <$bh>;
1298 my $f2 = <$bh>;
1299 $f1 = $self->{path}.'/objects/pack/'.$f1 if $f1 && $f1 !~ m|^/|;
1300 $f2 = $self->{path}.'/objects/pack/'.$f2 if $f2 && $f2 !~ m|^/|;
1301 close($bh);
1302 next unless $f1 && $f2 && $f1 ne $f2;
1303 chomp $f1;
1304 chomp $f2;
1305 next unless -e $f1 && -e $f2;
1306 my $s1 = -s $f1 || 0;
1307 my $s2 = -s $f2 || 0;
1308 next unless $s1 || $s2;
1309 push(@blist, [$ctime, "$prefix-".substr($bfile,16,8).".bundle", $s1+$s2]);
1311 closedir($dh);
1313 @blist = sort({$b->[0] <=> $a->[0]} @blist);
1314 my %seen = ();
1315 my @result = ();
1316 foreach my $bndl (@blist) {
1317 next if $seen{$bndl->[1]};
1318 $seen{$bndl->[1]} = 1;
1319 push(@result, $bndl);
1321 $self->{bundles} = \@result;
1322 return @result;
1325 sub has_alternates {
1326 my $self = shift;
1327 my $af = $self->{path}.'/objects/info/alternates';
1328 -f $af && -s _ or return 0;
1329 my $nb = 0;
1330 open my $fh, '<', $af or return 1;
1331 while (my $line = <$fh>) {
1332 next if $line =~ /^$/ || $line =~ /^#/;
1333 $nb = 1;
1334 last;
1336 close $fh;
1337 return $nb;
1340 sub has_bundle {
1341 my $self = shift;
1343 return scalar($self->bundles);
1346 sub _has_notifyhook {
1347 my $self = shift;
1348 my $val = $Girocco::Config::default_notifyhook;
1349 defined($self->{'notifyhook'}) and $val = $self->{'notifyhook'};
1350 return (defined($val) && $val ne "") ? $val : undef;
1353 # returns true if any of the notify fields are non-empty
1354 sub has_notify {
1355 my $self = shift;
1356 # We do not ckeck notifycia since it's defunct
1357 return
1358 $self->{'notifymail'} || $self->{'notifytag'} ||
1359 $self->{'notifyjson'} ||
1360 !!$self->_has_notifyhook;
1363 sub is_empty {
1364 # A project is considered empty if the git repository does not
1365 # have any refs. This means packed-refs does not exist or is
1366 # empty or only has lines starting with '#' AND there are no
1367 # files in the refs subdirectory hierarchy (no matter how deep).
1369 my $self = shift;
1371 (-d $self->{path}) or return 0;
1372 if (-e $self->{path}.'/packed-refs') {
1373 open(my $pr, '<', $self->{path}.'/packed-refs')
1374 or die "open $self->{path}./packed-refs failed: $!";
1375 my $foundref = 0;
1376 while (my $ref = <$pr>) {
1377 next if $ref =~ /^#/;
1378 $foundref = 1;
1379 last;
1381 close($pr);
1382 return 0 if $foundref;
1384 (-d $self->{path}.'/refs') or return 1;
1385 return !_contains_files($self->{path}.'/refs');
1388 # returns array:
1389 # [0]: earliest possible scheduled next gc, undef if lastgc not set
1390 # [1]: approx. latest possible scheduled next gc, undef if lastgc not set
1391 # Both values (if not undef) are seconds since epoch
1392 # Result only considers lastgc and min_gc_interval nothing else
1393 sub next_gc {
1394 my $self = shift;
1395 my $lastgcepoch = parse_any_date($self->{lastgc});
1396 return (undef, undef) unless defined $lastgcepoch;
1397 return ($lastgcepoch + $Girocco::Config::min_gc_interval,
1398 int($lastgcepoch + 1.25 * $Girocco::Config::min_gc_interval +
1399 $Girocco::Config::min_mirror_interval));
1402 # returns boolean (0 or 1)
1403 # Attempts to determine whether or not a gc (and corresponding build
1404 # of a .bitmap/.bndl file) will actually take place at the next_gc
1405 # time (as returned by next_gc). Whether or not a .bitmap and .bndl
1406 # end up being built depends on whether or not the local object graph
1407 # is complete. In general if has_alternates is true then a .bitmap/.bndl
1408 # is not possible because the object graph will be incomplete,
1409 # but it *is* possible that even though the repository has_alternates,
1410 # it does not actually borrow any objects so a .bitmap/.bndl will build
1411 # in spite of the presence of alternates -- but this is expected to be rare.
1412 # The result is a best guess and false return value is not an absolute
1413 # guarantee that gc will not take place at the next interval, but it probably
1414 # will not if nothing changes in the meantime.
1415 sub needs_gc {
1416 my $self = shift;
1417 my $lgc = parse_any_date($self->{lastgc});
1418 my $lrecv = parse_any_date($self->{lastreceive});
1419 my $lpgc = parse_any_date($self->{lastparentgc});
1420 return 1 unless defined($lgc) && defined($lrecv);
1421 if ($self->has_alternates) {
1422 return 1 unless defined($lpgc);
1423 return 1 unless $lpgc < $lgc;
1425 return 1 unless $lrecv < $lgc;
1426 # We don't try running any "is_dirty" check, so if somehow the
1427 # repository became dirty without updating lastreceive we might
1428 # incorrectly return false instead of true.
1429 return 0;
1432 sub delete_ctag {
1433 my $self = shift;
1434 my ($ctag) = @_;
1436 # sanity check, disallow filenames starting with . .. or /
1437 unlink($self->{path}.'/ctags/'.$ctag)
1438 unless !defined($ctag) || $ctag =~ m|^/| || $ctag =~ m{(?:^|/)(?:\.\.?)(?:/|$)};
1441 # returns new tag count value on success (will always be >= 1) otherwise undef
1442 sub add_ctag {
1443 my $self = shift;
1444 my $ctag = valid_tag(shift);
1445 my $nochanged = shift;
1447 # sanity check, disallow filenames starting with . .. or /
1448 return undef if !defined($ctag) || $ctag =~ m|^/| || $ctag =~ m{(?:^|/)(?:\.\.?)(?:/|$)};
1450 my $val = 0;
1451 my $ct;
1452 if (open $ct, '<', $self->{path}."/ctags/$ctag") {
1453 my $count = <$ct>;
1454 close $ct;
1455 defined $count or $count = '';
1456 chomp $count;
1457 $val = $count =~ /^[1-9]\d*$/ ? $count : 1;
1459 ++$val;
1460 my $oldmask = umask();
1461 umask($oldmask & ~0060);
1462 open $ct, '>', $self->{path}."/ctags/$ctag" and print $ct $val."\n" and close $ct;
1463 $self->_set_changed unless $nochanged;
1464 $self->_set_forkchange unless $nochanged;
1465 umask($oldmask);
1466 return $val;
1469 sub get_ctag_names {
1470 my $self = shift;
1471 my @ctags = ();
1472 opendir(my $dh, $self->{path}.'/ctags')
1473 or return @ctags;
1474 @ctags = grep { -f "$self->{path}/ctags/$_" } readdir($dh);
1475 closedir($dh);
1476 return sort({lc($a) cmp lc($b)} @ctags);
1479 sub get_heads {
1480 my $self = shift;
1481 my $fh;
1482 my $heads = get_git("--git-dir=$self->{path}", 'for-each-ref', '--format=%(objectname) %(refname)', 'refs/heads');
1483 defined($heads) or $heads = '';
1484 chomp $heads;
1485 my @res = ();
1486 foreach (split(/\n/, $heads)) {
1487 chomp;
1488 next if !m#^[0-9a-f]{40}\s+refs/heads/(.+)$ #x;
1489 push @res, $1;
1491 push(@res, $self->{orig_HEAD}) if !@res && defined($self->{orig_HEAD}) && $self->{orig_HEAD} ne '';
1492 @res;
1495 sub get_HEAD {
1496 my $self = shift;
1497 my $HEAD = read_HEAD_ref($self->{path});
1498 defined($HEAD) && $HEAD ne "" or die "could not get HEAD";
1499 return $1 if $HEAD =~ m{^refs/heads/(.+)$};
1500 return "[other]" if $HEAD =~ m{^refs/};
1501 return "[detached]" if $HEAD =~ m{^[0-9a-fA-F]{4,}$};
1502 return "[invalid]";
1505 sub set_HEAD {
1506 my $self = shift;
1507 my $newHEAD = shift;
1508 # Cursory checks only -- if you want to break your HEAD, be my guest
1509 if ($newHEAD =~ /^\/|^\.|[\x00-\x1f \x7f\[~^'<>*?\\:]|\@\{|\.\.|\.lock$|\.$|\/$/) {
1510 die "grossly invalid new HEAD: $newHEAD";
1512 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', "refs/heads/$newHEAD");
1513 die "could not set HEAD" if ($? >> 8);
1514 ! -d "$self->{path}/mob" || $Girocco::Config::mob ne 'mob'
1515 or system('cp', '-p', '-f', "$self->{path}/HEAD", "$self->{path}/mob/HEAD") == 0;
1518 sub gen_auth {
1519 my $self = shift;
1520 my ($type) = @_;
1521 $type = 'REPO' unless $type && $type =~ /^[A-Z]+$/;
1523 $self->{authtype} = $type;
1525 no warnings;
1526 $self->{auth} = sha1_hex(time . $$ . rand() . join(':',%$self));
1528 my $expire = time + 24 * 3600;
1529 my $propval = "# ${type}AUTH $self->{auth} $expire";
1530 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gitweb.repoauth', $propval);
1531 $self->{auth};
1534 sub del_auth {
1535 my $self = shift;
1537 delete $self->{auth};
1538 delete $self->{authtype};
1539 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--unset', 'gitweb.repoauth');
1542 sub remove_user {
1543 my $self = shift;
1544 my ($username) = @_;
1546 my $before_count = @{$self->{users}};
1547 $self->{users} = [grep { $_ ne $username } @{$self->{users}}];
1548 return @{$self->{users}} != $before_count;
1551 ### static methods
1553 sub get_forkee_name {
1554 local $_ = $_[0];
1555 (m#^(.*)/.*?$#)[0]; #
1558 sub get_forkee_path {
1559 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1560 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
1561 -d $forkee ? $forkee : '';
1564 # Ultimately the full project/fork name could end up being part of a Git ref name
1565 # when a project's forks are combined into one giant repository for efficiency.
1566 # That means that the project/fork name must satisfy the Git ref name requirements:
1568 # 1. Characters with an ASCII value less than or equal to 32 are not allowed
1569 # 2. The character with an ASCII value of 0x7F is not allowed
1570 # 3. The characters '~', '^', ':', '\', '*', '?', and '[' are not allowed
1571 # 4. The character '/' is a separator and is not allowed within a name
1572 # 5. The name may not start with '.' or end with '.'
1573 # 6. The name may not end with '.lock'
1574 # 7. The name may not contain the '..' sequence
1575 # 8. The name may not contain the '@{' sequence
1576 # 9. If multiple components are used (separated by '/'), no empty '' components
1578 # We also prohibit a trailing '.git' on any path component and futher restrict
1579 # the allowed characters to alphanumeric and [+._-] where names must start with
1580 # an alphanumeric.
1582 sub _valid_name_characters {
1583 local $_ = $_[0];
1584 (not m#^[/+._-]#)
1585 and (not m#//#)
1586 and (not m#\.\.#)
1587 and (not m#/[+._-]#)
1588 and (not m#\./#)
1589 and (not m#\.$#)
1590 and (not m#\.git/#i)
1591 and (not m#\.git$#i)
1592 and (not m#\.idx/#i)
1593 and (not m#\.idx$#i)
1594 and (not m#\.lock/#i)
1595 and (not m#\.lock$#i)
1596 and (not m#\.pack/#i)
1597 and (not m#\.pack$#i)
1598 and (not m#\.bundle/#i)
1599 and (not m#\.bundle$#i)
1600 and (not m#/$#)
1601 and (not m/^[a-fA-F0-9]{38}$/)
1602 and m#^[a-zA-Z0-9/+._-]+$#
1603 and !has_reserved_suffix($_, $_[1], $_[2]);
1606 # $_[0] => prospective project name (WITHOUT trailing .git)
1607 # $_[1] => true to allow orphans (i.e. two-or-more-level-deep projects without a parent)
1608 # (the directory in which the orphan will be created must, however, already exist)
1609 # $_[2] => true to allow orphans w/o needed directory if $_[1] also true (like mkdir -p)
1610 # If $_[0] is a SCALAR ref, ${$_[0]} contains the name and will be untainted on success.
1611 sub valid_name {
1612 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1613 local $_ = $_[0];
1614 my $mayberef_name = $_;
1615 ref($_) eq 'SCALAR' and $_ = $$_;
1616 my $rv = (
1617 _valid_name_characters($_) and not exists($reservedprojectnames{lc($_)})
1618 and @{[m#/#g]} <= 5 # maximum fork depth is 5
1619 and ((not m#/#) or -d get_forkee_path($_) or ($_[1] and ($_[2] or -d $Girocco::Config::reporoot.'/'.get_forkee_name($_))))
1620 and (! -f $Girocco::Config::reporoot."/$_.git")
1622 $rv && ref($mayberef_name) eq 'SCALAR' && m|^(.+)$| and $$mayberef_name = $1;
1623 return $rv;
1626 # It's possible that some forks have been kept but the forkee is gone.
1627 # In this case the standard valid_name check is too strict.
1628 # If $_[0] is a SCALAR ref, ${$_[0]} contains the name and will be untainted on success.
1629 sub does_exist {
1630 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1631 my ($mayberef_name, $nodie) = @_;
1632 my $name = ref($mayberef_name) eq 'SCALAR' ? $$mayberef_name : $mayberef_name;
1633 my $okay = (
1634 _valid_name_characters($name, $Girocco::Config::reporoot, ".git")
1635 and ((not $name =~ m#/#)
1636 or -d get_forkee_path($name)
1637 or -d $Girocco::Config::reporoot.'/'.get_forkee_name($name)));
1638 (!$okay && $nodie) and return undef;
1639 !$okay and die "tried to query for project with invalid name $name!";
1640 -d $Girocco::Config::reporoot."/$name.git" or return undef;
1641 ref($mayberef_name) eq 'SCALAR' && $name =~ m|^(.+)$| and $$mayberef_name = $1;
1642 return 1;
1645 sub get_full_list {
1646 open my $fd, '<', jailed_file("/etc/group") or die "getting project list failed: $!";
1647 my @projects = map {/^([^:_\s#][^:\s#]*):[^:]*:\d{5,}:/ ? $1 : ()} <$fd>;
1648 close $fd;
1649 @projects;