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