CLIUtil.pm: provide get_full_users function
[girocco.git] / Girocco / Project.pm
blob13f8c3c91383f2d1cc78f3cc37439cb3963b97da
1 package Girocco::Project;
3 use strict;
4 use warnings;
6 BEGIN {
7 use Girocco::CGI;
8 use Girocco::User;
9 use Girocco::Util;
10 use Girocco::HashUtil;
11 use Girocco::ProjPerm;
12 use Girocco::Config;
13 use base ('Girocco::ProjPerm::'.$Girocco::Config::permission_control); # mwahaha
16 BEGIN {
17 eval {
18 require Digest::SHA;
19 Digest::SHA->import(
20 qw(sha1_hex)
21 );1} ||
22 eval {
23 require Digest::SHA1;
24 Digest::SHA1->import(
25 qw(sha1_hex)
26 );1} ||
27 eval {
28 require Digest::SHA::PurePerl;
29 Digest::SHA::PurePerl->import(
30 qw(sha1_hex)
31 );1} ||
32 die "One of Digest::SHA or Digest::SHA1 or Digest::SHA::PurePerl "
33 . "must be available\n";
36 our $metadata_fields = {
37 cleanmirror => ['Mirror refs', 'cleanmirror', 'placeholder'],
38 homepage => ['Homepage URL', 'hp', 'text'],
39 shortdesc => ['Short description', 'desc', 'text'],
40 README => ['<span style="display:inline-block;vertical-align:top">'.
41 'README (HTML, &lt; 8 KiB)<br />leave blank for automatic</span>',
42 'README', 'textarea', 'Enter only &#x201c;<!-- comments -->&#x201d; '.
43 'to completely suppress any README'],
44 notifymail => ['Commit notify &#x2013; mail to', 'notifymail', 'text',
45 'comma separated address list'],
46 reverseorder => ['Show oldest first', 'reverseorder', 'checkbox',
47 'show new revisions in oldest to newest order (instead of the default newest to oldest older)'.
48 ' in &#x201c;Commit notify&#x201d; email when showing new revisions'],
49 summaryonly => ['Summaries only', 'summaryonly', 'checkbox',
50 'suppress patch/diff output in &#x201c;Commit notify&#x201d; email when showing new revisions'],
51 notifytag => ['Tag notify &#x2013; mail to', 'notifytag', 'text',
52 'comma separated address list &#x2013; if not empty, tag '.
53 'notifications are sent here INSTEAD of to '.
54 '&#x201c;Commit notify &#x2013; mail to&#x201d; address(es)'],
55 notifyjson => ['Commit notify &#x2013; '.
56 '<a title="'.html_esc('single field name is &#x201c;payload&#x201d;', 1).'" href="'.
57 'https://developer.github.com/v3/activity/events/types/#pushevent'.
58 '">POST JSON</a> at', 'notifyjson', 'text'],
59 notifycia => ['Commit notify &#x2013; <a href="http://cia.vc/doc/">CIA project</a> name',
60 'notifycia', 'text', 'CIA is defunct &#x2013; this value is ignored'],
63 sub _mkdir_forkees {
64 my $self = shift;
65 my @pelems = split('/', $self->{name});
66 pop @pelems; # do not create dir for the project itself
67 my $path = $self->{base_path};
68 foreach my $pelem (@pelems) {
69 $path .= "/$pelem";
70 (-d "$path") or mkdir $path or die "mkdir $path: $!";
71 chmod 02775, $path; # ok if fails (dir may already exist and be owned by someone else)
75 # With a leading ':' get from project local config replacing ':' with 'gitweb.'
76 # With a leading '%' get from project local config after removing '%'
77 # With a leading '!' get boolean from project local config after removing '!' (prefixed with 'gitweb.' if no '.')
78 # With a leading [:%!] a trailing ':defval' may be added to select the default value to use if unset
79 # Otherwise it's a project file name to be loaded
80 # %propmapro entries are loaded but never written
81 # %propmapromirror entries are loaded only for mirrors but never written
83 our %propmap = (
84 url => ':baseurl',
85 email => ':owner',
86 desc => 'description',
87 README => 'README.html',
88 hp => ':homepage',
89 notifymail => '%hooks.mailinglist',
90 notifytag => '%hooks.announcelist',
91 notifyjson => '%hooks.jsonurl',
92 notifycia => '%hooks.cianame',
93 cleanmirror => '!girocco.cleanmirror',
94 statusupdates => '!statusupdates:1',
95 reverseorder => '!hooks.reverseorder',
96 summaryonly => '!hooks.summaryonly',
99 our %propmapro = (
100 lastchange => ':lastchange',
101 lastactivity => 'info/lastactivity',
102 lastgc => ':lastgc',
103 lastreceive => ':lastreceive',
104 lastparentgc => ':lastparentgc',
105 lastrefresh => ':lastrefresh',
106 creationtime => '%girocco.creationtime',
107 reposizek => '%girocco.reposizek',
108 origurl => ':baseurl',
111 our %propmapromirror = (
112 bangcount => '%girocco.bang.count',
113 bangfirstfail => '%girocco.bang.firstfail',
114 bangmessagesent => '!girocco.bang.messagesent',
115 showpush => '!showpush',
118 # Projects with any of these names will be disallowed to avoid possible
119 # collisions with cgi script paths or chroot paths
120 # NOTE: names are checked after using lc on them, so all entries MUST be lowercase
121 our %reservedprojectnames = (
122 admin => 1, # /admin/ links
123 alternates => 1, # .git/objects/info/alternates
124 b => 1, # /b/ -> bundle.cgi
125 blog => 1, # /blog/ links
126 c => 1, # /c/ -> cgit
127 'git-receive-pack' => 1, # smart HTTP
128 'git-upload-archive' => 1, # smart HTTP
129 'git-upload-pack' => 1, # smart HTTP
130 h => 1, # /h/ -> html.cgi
131 head => 1, # .git/HEAD
132 'http-alternates' => 1, # .git/objects/info/http-alternates
133 info => 1, # .git/info
134 objects => 1, # .git/objects
135 packs => 1, # .git/objects/info/packs
136 r => 1, # /r/ -> git http
137 refs => 1, # .git/refs
138 w => 1, # /w/ -> gitweb
139 wiki => 1, # /wiki/ links
140 srv => 1, # /srv/git/ -> chroot ssh git repositories
143 sub _update_index {
144 my $self = shift;
145 system("$Girocco::Config::basedir/gitweb/genindex.sh", $self->{name});
148 sub _readlocalconfigfile {
149 my $self = shift;
150 my $undefonerr = shift || 0;
151 delete $self->{configfilehash};
152 my $confighash = read_config_file_hash($self->{path} . "/config");
153 my $result = 1;
154 defined($confighash) || $undefonerr or $result = 0, $confighash = {};
155 return undef unless defined($confighash);
156 $self->{configfilehash} = $confighash;
157 return $result;
160 # @_[0]: argument to convert to boolean result (0 or 1)
161 # @_[1]: value to use if argument is undef (default is 0)
162 # Returns 0 or 1
163 sub _boolval {
164 my ($val, $def) = @_;
165 defined($def) or $def = 0;
166 defined($val) or $val = $def;
167 $val =~ s/\s+//gs;
168 $val = lc($val);
169 return 0 if $val eq '' || $val eq 'false' || $val eq 'off' || $val eq 'no' || $val =~ /^0+$/;
170 return 1;
173 sub _property_path {
174 my $self = shift;
175 my ($name) = @_;
176 $self->{path}.'/'.$name;
179 sub _property_fget {
180 my $self = shift;
181 my ($name, $nodef) = @_;
182 my $pname = $propmap{$name};
183 $pname = $propmapro{$name} unless $pname;
184 $pname = $propmapromirror{$name} unless $pname;
185 $pname or die "unknown property: $name";
186 if ($pname =~ /^([:%!])([^:]+)(:.*)?$/) {
187 my ($where, $pname, $defval) = ($1, lc($2), substr(($3||":"),1));
188 $defval = '' if $nodef;
189 $self->_readlocalconfigfile
190 unless ref($self->{configfilehash}) eq 'HASH';
191 $pname = "gitweb." . $pname if $where eq ':' or $where eq '!' && $pname !~ /[.]/;
192 my $val = $self->{configfilehash}->{$pname};
193 defined($val) or $val = $defval;
194 chomp $val;
195 $val = _boolval($val, $defval) if $where eq '!';
196 return $nodef && !exists($self->{configfilehash}->{$pname}) ? undef : $val;
199 open my $p, '<', $self->_property_path($pname) or return undef;
200 my @value = <$p>;
201 close $p;
202 my $value = join('', @value); chomp $value;
203 $value;
206 sub _prop_is_same {
207 my $self = shift;
208 my ($name, $value) = @_;
209 my $existing = $self->_property_fget($name, 1);
210 defined($value) or $value = '';
211 return defined($existing) && $existing eq $value;
214 sub _property_fput {
215 my $self = shift;
216 my ($name, $value, $nosetsame) = @_;
217 my $pname = $propmap{$name};
218 $pname or die "unknown property: $name";
219 my $defval = '';
220 ($pname, $defval) = ($1, substr(($2||":"),1)) if $pname =~ /^([:%!][^:]+)(:.*)?$/;
221 defined($value) or $value = $defval;
222 if ($pname =~ s/^://) {
223 return if $nosetsame && $self->_prop_is_same($name, $value);
224 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', "gitweb.$pname", $value);
225 return;
226 } elsif ($pname =~ s/^%//) {
227 return if $nosetsame && $self->_prop_is_same($name, $value);
228 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', $pname, $value);
229 return;
230 } elsif ($pname =~ s/^!//) {
231 $pname = "gitweb." . $pname unless $pname =~ /[.]/;
232 $value = _boolval($value, $defval);
233 return if $nosetsame && $self->_prop_is_same($name, $value);
234 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--bool', $pname, $value);
235 return;
238 my $P = lock_file($self->_property_path($pname));
239 chomp $value;
240 $value ne '' and print $P "$value\n";
241 close $P;
242 unlock_file($self->_property_path($pname));
245 sub _cleanup_readme {
246 my $self = shift;
247 defined($self->{README}) or $self->{README} = '';
248 $self->{README} =~ s/\r\n?/\n/gs;
249 $self->{README} =~ s/^\s+//s;
250 $self->{README} =~ s/\s+$//s;
251 $self->{README} eq '' or $self->{README} .= "\n";
254 sub _lint_readme {
255 my $self = shift;
256 my $htmlfrag = shift;
257 defined($htmlfrag) or $htmlfrag = 1;
258 return 0 unless defined($self->{README}) && $self->{README} ne '';
259 my $test = '<html xmlns="http://www.w3.org/1999/xhtml"><body><div>';
260 $test .= $self->{README};
261 $test .= '</div></body></html>';
262 my ($code, $errors) = capture_command(2, $test, 'xmllint', '--nonet',
263 '--noout', '--nowarning', '-');
264 return 0 unless $code;
265 my $cnt = 0;
266 my @errs = ();
267 for my $line (split(/\n+/, $errors)) {
268 $line = html_esc($line) if $htmlfrag;
269 $line =~ s/ /\&#160;/gs if $htmlfrag;
270 ++$cnt, $line = 'README'.$1 if $line =~ /^-(:\d+:.*)$/;
271 if ($htmlfrag) {
272 push @errs, '<tt>' . $line . '</tt>';
273 } else {
274 push @errs, $line . "\n";
277 if ($htmlfrag) {
278 return ($cnt, join("<br />\n", @errs));
279 } else {
280 return ($cnt, join("", @errs));
284 sub _properties_load {
285 my $self = shift;
286 foreach my $prop (keys %propmap) {
287 $self->{$prop} = $self->_property_fget($prop);
289 foreach my $prop (keys %propmapro) {
290 $self->{$prop} = $self->_property_fget($prop);
292 if ($self->{mirror}) {
293 foreach my $prop (keys %propmapromirror) {
294 $self->{$prop} = $self->_property_fget($prop);
297 $self->_readlocalconfigfile
298 unless ref($self->{configfilehash}) eq 'HASH';
299 delete $self->{auth};
300 my $val = $self->{configfilehash}->{'gitweb.repoauth'};
301 defined($val) or $val = '';
302 chomp $val;
303 if ($val =~ /^# ([A-Z]+)AUTH ([0-9a-f]+) (\d+)/) {
304 my $expire = $3;
305 if (time < $expire) {
306 $self->{authtype} = $1;
307 $self->{auth} = $2;
310 $self->_cleanup_readme;
311 delete $self->{configfilehash};
314 sub _set_bangagain {
315 my $self = shift;
316 my $fd;
317 if ($self->{mirror} && defined($self->{origurl}) && $self->{url} &&
318 $self->{origurl} ne $self->{url} && -e $self->_banged_path) {
319 if (open($fd, '>', $self->_bangagain_path)) {
320 close $fd;
321 chmod(0664, $self->_bangagain_path);
326 sub _properties_save {
327 my $self = shift;
328 delete $self->{configfilehash};
329 foreach my $prop (keys %propmap) {
330 $self->_property_fput($prop, $self->{$prop}, 1);
332 $self->_set_bangagain;
335 sub _nofetch_path {
336 my $self = shift;
337 $self->_property_path('.nofetch');
340 sub _nofetch {
341 my $self = shift;
342 my ($nofetch) = @_;
343 my $nf = $self->_nofetch_path;
344 if ($nofetch) {
345 open my $x, '>', $nf or die "nofetch failed: $!";
346 close $x;
347 } else {
348 unlink $nf or die "yesfetch failed: $!";
352 sub _banged_path {
353 my $self = shift;
354 $self->_property_path('.banged');
357 sub _bangagain_path {
358 my $self = shift;
359 $self->_property_path('.bangagain');
362 sub _gcp_path {
363 my $self = shift;
364 $self->_property_path('.gc_in_progress');
367 sub _clonelog_path {
368 my $self = shift;
369 $self->_property_path('.clonelog');
372 sub _clonefail_path {
373 my $self = shift;
374 $self->_property_path('.clone_failed');
377 sub _clonep_path {
378 my $self = shift;
379 $self->_property_path('.clone_in_progress');
382 sub _clonep {
383 my $self = shift;
384 my ($nofetch) = @_;
385 my $np = $self->_clonep_path;
386 if ($nofetch) {
387 open my $x, '>', $np or die "clonep failed: $!";
388 close $x;
389 } else {
390 unlink $np or die "clonef failed: $!";
393 sub _alternates_setup {
394 my $self = shift;
395 return unless $self->{name} =~ m#/#;
396 my $forkee_name = get_forkee_name($self->{name});
397 my $forkee_path = get_forkee_path($self->{name});
398 return unless -d $forkee_path;
399 mkdir $self->{path}.'/refs'; chmod 02775, $self->{path}.'/refs';
400 mkdir $self->{path}.'/objects'; chmod 02775, $self->{path}.'/objects';
401 mkdir $self->{path}.'/objects/info'; chmod 02775, $self->{path}.'/objects/info';
403 # We set up both alternates and http_alternates since we cannot use
404 # relative path in alternates - that doesn't work recursively.
406 my $filename = $self->{path}.'/objects/info/alternates';
407 open my $x, '>', $filename or die "alternates failed: $!";
408 print $x "$forkee_path/objects\n";
409 close $x;
410 chmod 0664, $filename or warn "cannot chmod $filename: $!";
412 if ($Girocco::Config::httppullurl) {
413 $filename = $self->{path}.'/objects/info/http-alternates';
414 open my $x, '>', $filename or die "http-alternates failed: $!";
415 my $upfork = $forkee_name;
416 do { print $x "$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s#/?.+?$## and $upfork); #
417 close $x;
418 chmod 0664, $filename or warn "cannot chmod $filename: $!";
421 # copy lastactivity from the parent project
422 if (open $x, '<', $forkee_path.'/info/lastactivity') {{
423 my $activity = <$x>;
424 close $x;
425 last unless $activity;
426 open $x, '>', $self->{path}.'/info/lastactivity' or last;
427 print $x $activity;
428 close $x;
429 chomp $activity;
430 $self->{'lastactivity'} = $activity;
433 # copy refs from parent project
434 local *SAVEOUT;
435 open(SAVEOUT, ">&STDOUT") || die "couldn't dup STDOUT: $!";
436 my $result = open(STDOUT, '>', "$self->{path}/packed-refs");
437 my $resultstr = $!;
438 if (!$result) {
439 open(STDOUT, ">&SAVEOUT") || die "couldn't dup SAVEOUT to STDOUT: $!";
440 close(SAVEOUT) or die "couldn't close SAVEOUT: $!";
441 die "could not write fork's packed-refs file: $resultstr";
443 system($Girocco::Config::git_bin, "--git-dir=$forkee_path", 'for-each-ref', '--format=%(objectname) %(refname)');
444 $result = close(STDOUT);
445 $resultstr = $!;
446 open(STDOUT, ">&SAVEOUT") || die "couldn't dup SAVEOUT to STDOUT: $!";
447 close(SAVEOUT) or die "couldn't close SAVEOUT: $!";
448 $result or die "could not close fork's packed-refs file: $resultstr";
449 unlink("$self->{path}/.delaygc") if -s "$self->{path}/packed-refs";
451 # initialize HEAD
452 my $HEAD = get_git("--git-dir=$forkee_path", 'symbolic-ref', 'HEAD');
453 defined($HEAD) && $HEAD =~ m,^refs/heads/., or $HEAD = 'refs/heads/master';
454 chomp $HEAD;
455 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', $HEAD);
456 chmod 0664, "$self->{path}/packed-refs", "$self->{path}/HEAD";
459 sub _set_changed {
460 my $self = shift;
461 my $fd;
462 open $fd, '>', "$self->{path}/htmlcache/changed" and close $fd;
465 sub _set_forkchange {
466 my $self = shift;
467 my $changedtoo = shift;
468 return unless $self->{name} =~ m#/#;
469 my $forkee_path = get_forkee_path($self->{name});
470 return unless -d $forkee_path;
471 # mark forkee as changed
472 my $fd;
473 open $fd, '>', $forkee_path.'/htmlcache/changed' and close $fd if $changedtoo;
474 open $fd, '>', $forkee_path.'/htmlcache/forkchange' and close $fd;
475 return if -e $forkee_path.'/htmlcache/summary.forkchange';
476 open $fd, '>', $forkee_path.'/htmlcache/summary.forkchange' and close $fd;
479 sub _ctags_setup {
480 my $self = shift;
481 my $perms = $Girocco::Config::permission_control eq 'Hooks' ? 02777 : 02775;
482 mkdir $self->{path}.'/ctags'; chmod $perms, $self->{path}.'/ctags';
485 sub _group_add {
486 my $self = shift;
487 my ($xtra) = @_;
488 $xtra .= join(',', @{$self->{users}});
489 my $crypt = $self->{crypt};
490 defined($crypt) or $crypt = 'unknown';
491 filedb_atomic_append(jailed_file('/etc/group'),
492 join(':', $self->{name}, $crypt, '\i', $xtra));
495 sub _group_update {
496 my $self = shift;
497 my $xtra = join(',', @{$self->{users}});
498 filedb_atomic_edit(jailed_file('/etc/group'),
499 sub {
500 $_ = $_[0];
501 chomp;
502 if ($self->{name} eq (split /:/)[0]) {
503 # preserve readonly flag
504 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
505 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
506 } else {
507 return "$_\n";
513 sub _group_remove {
514 my $self = shift;
515 filedb_atomic_edit(jailed_file('/etc/group'),
516 sub {
517 $self->{name} ne (split /:/)[0] and return $_;
522 sub _hook_path {
523 my $self = shift;
524 my ($name) = @_;
525 $self->{path}.'/hooks/'.$name;
528 sub _hook_install {
529 my $self = shift;
530 my ($name) = @_;
531 my $hooksdir = $self->{path}.'/hooks';
532 my $oldmask = umask();
533 umask($oldmask & ~0070);
534 -d $hooksdir or mkdir $hooksdir or
535 die "hooks directory does not exist and unable to create it for project " . $self->{name} . ": $!";
536 umask($oldmask);
537 open my $src, '<', "$Girocco::Config::basedir/hooks/$name" or die "cannot open hook $name: $!";
538 open my $dst, '>', $self->_hook_path($name) or die "cannot open hook $name for writing: $!";
539 while (<$src>) { print $dst $_; }
540 close $dst;
541 close $src;
542 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
545 sub _hooks_install {
546 my $self = shift;
547 foreach my $hook ('pre-receive', 'post-receive', 'update') {
548 $self->_hook_install($hook);
552 # private constructor, do not use
553 sub _new {
554 my $class = shift;
555 my ($name, $base_path, $path, $orphan, $optp) = @_;
556 does_exist($name,1) || valid_name($name, $orphan, $optp) or die "refusing to create project with invalid name ($name)!";
557 $path ||= "$base_path/$name.git";
558 my $proj = { name => $name, base_path => $base_path, path => $path };
560 bless $proj, $class;
563 # public constructor #0
564 # creates a virtual project not connected to disk image
565 # you can conjure() it later to disk
566 sub ghost {
567 my $class = shift;
568 my ($name, $mirror, $orphan, $optp) = @_;
569 my $self = $class->_new($name, $Girocco::Config::reporoot, undef, $orphan, $optp);
570 $self->{users} = [];
571 $self->{mirror} = $mirror;
572 $self->{email} = $self->{orig_email} = '';
573 $self;
576 # public constructor #1
577 sub load {
578 my $class = shift;
579 my $name = shift || '';
581 open my $fd, '<', jailed_file("/etc/group") or die "project load failed: $!";
582 my $r = qr/^\Q$name\E:/;
583 foreach (grep /$r/, <$fd>) {
584 chomp;
586 my $self = $class->_new($name, $Girocco::Config::reporoot);
587 (-d $self->{path} && $self->_readlocalconfigfile(1))
588 or die "invalid path (".$self->{path}.") for project ".$self->{name};
590 my $ulist;
591 (undef, $self->{crypt}, $self->{gid}, $ulist) = split /:/;
592 $ulist ||= '';
593 $self->{users} = [split /,/, $ulist];
594 $self->{HEAD} = $self->get_HEAD;
595 $self->{orig_HEAD} = $self->{HEAD};
596 $self->{orig_users} = [@{$self->{users}}];
597 $self->{mirror} = ! -e $self->_nofetch_path;
598 $self->{banged} = -e $self->_banged_path if $self->{mirror};
599 $self->{gc_in_progress} = -e $self->_gcp_path;
600 $self->{clone_in_progress} = -e $self->_clonep_path;
601 $self->{clone_logged} = -e $self->_clonelog_path;
602 $self->{clone_failed} = -e $self->_clonefail_path;
603 $self->{ccrypt} = $self->{crypt};
605 $self->_properties_load;
606 $self->{orig_email} = $self->{email};
607 $self->{loaded} = 1; # indicates self was loaded from etc/group file
608 close $fd;
609 return $self;
611 close $fd;
612 undef;
615 # $proj may not be in sane state if this returns false!
616 # fields listed in %$metadata_fields that are NOT also
617 # in @Girocco::Config::project_fields are totally ignored!
618 sub cgi_fill {
619 my $self = shift;
620 my ($gcgi) = @_;
621 my $cgi = $gcgi->cgi;
622 my %allowedfields = map({$_ => 1} @Girocco::Config::project_fields);
623 my $field_enabled = sub {
624 !exists($metadata_fields->{$_[0]}) || exists($allowedfields{$_[0]})};
626 my $pwd = $cgi->param('pwd');
627 my $pwd2 = $cgi->param('pwd2');
628 # in case passwords are disabled
629 defined($pwd) or $pwd = ''; defined($pwd2) or $pwd2 = '';
630 if ($Girocco::Config::project_passwords and not $self->{crypt} and $pwd eq '' and $pwd2 eq '') {
631 $gcgi->err("Empty passwords are not permitted.");
633 if ($pwd ne '' or not $self->{crypt}) {
634 $self->{crypt} = scrypt_sha1($pwd);
636 if (($pwd ne '' || $pwd2 ne '') and $pwd ne $pwd2) {
637 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
640 $self->{cpwd} = $cgi->param('cpwd');
642 my ($forkee,$project) = ($self->{name} =~ m#^(.*/)?([^/]+)$#);
643 my $newtype = $forkee ? 'fork' : 'project';
644 length($project) <= 64
645 or $gcgi->err("The $newtype name is longer than 64 characters. Do you really need that much?");
647 if ($Girocco::Config::project_owners eq 'email') {
648 $self->{email} = $gcgi->wparam('email');
649 valid_email($self->{email})
650 or $gcgi->err("Your email sure looks weird...?");
651 length($self->{email}) <= 96
652 or $gcgi->err("Your email is longer than 96 characters. Do you really need that much?");
655 # No setting the url unless we're either new or an existing mirror!
656 unless ($self->{loaded} && !$self->{mirror}) {
657 $self->{url} = $gcgi->wparam('url') ;
658 if ($field_enabled->('cleanmirror')) {
659 $self->{cleanmirror} = $gcgi->wparam('cleanmirror') || 0;
662 # Always validate the url if we're an existing mirror
663 if ((defined($self->{url}) && $self->{url} ne '') || ($self->{loaded} && $self->{mirror})) {{
664 # Always allow the url to be left unchanged without validation when editing
665 last if $self->{loaded} && defined($self->{origurl}) && defined($self->{url}) && $self->{origurl} eq $self->{url};
666 valid_repo_url($self->{url})
667 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocols are supported. If the URL contains funny characters, contact me.");
668 if ($Girocco::Config::restrict_mirror_hosts) {
669 my $mh = extract_url_hostname($self->{url});
670 is_dns_hostname($mh)
671 or $gcgi->err("Invalid URL. Note that only DNS names are allowed, not IP addresses.");
672 !is_our_hostname($mh)
673 or $gcgi->err("Invalid URL. Mirrors from this host are not allowed, please create a fork instead.");
677 if ($field_enabled->('desc')) {
678 $self->{desc} = to_utf8($gcgi->wparam('desc'), 1);
679 length($self->{desc}) <= 1024
680 or $gcgi->err("<b>Short</b> description length &gt; 1kb!");
683 if ($field_enabled->('README')) {
684 $self->{README} = to_utf8($gcgi->wparam('README'), 1);
685 $self->_cleanup_readme;
686 length($self->{README}) <= 8192
687 or $gcgi->err("README length &gt; 8kb!");
688 my ($cnt, $err) = (0);
689 ($cnt, $err) = $self->_lint_readme if $gcgi->ok && $Girocco::Config::xmllint_readme;
690 $gcgi->err($err), $gcgi->{err} += $cnt-1 if $cnt;
693 if ($field_enabled->('hp')) {
694 $self->{hp} = $gcgi->wparam('hp');
695 if ($self->{hp}) {
696 valid_web_url($self->{hp})
697 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
701 # No mucking about with users unless we're a push project
702 if (($self->{loaded} && !$self->{mirror}) ||
703 (!$self->{loaded} && (!defined($self->{url}) || $self->{url} eq ''))) {
704 my %users = ();
705 my @users = ();
706 foreach my $user ($cgi->multi_param('user')) {
707 if (!exists($users{$user})) {
708 $users{$user} = 1;
709 push(@users, $user) if Girocco::User::does_exist($user, 1);
712 $self->{users} = \@users;
715 # HEAD can only be set with editproj, NOT regproj (regproj sets it automatically)
716 # It may ALWAYS be set to what it was (even if there's no such refs/heads/...)
717 my $newhead;
718 if (defined($self->{orig_HEAD}) && $self->{orig_HEAD} ne '' &&
719 defined($newhead = $cgi->param('HEAD')) && $newhead ne '') {
720 if ($newhead eq $self->{orig_HEAD} ||
721 get_git("--git-dir=$self->{path}", 'rev-parse', '--verify', '--quiet', 'refs/heads/'.$newhead)) {
722 $self->{HEAD} = $newhead;
723 } else {
724 $gcgi->err("Invalid default branch (no such ref)");
728 # schedule deletion of tags (will be committed by update() after auth)
729 $self->{tags_to_delete} = [$cgi->multi_param('tags')];
731 if ($field_enabled->('notifymail')) {
732 my $newaddrs = clean_email_multi($gcgi->wparam('notifymail'));
733 if ($newaddrs eq "" or (valid_email_multi($newaddrs) and length($newaddrs) <= 512)) {
734 $self->{notifymail} = $newaddrs;
735 } else {
736 $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
738 if ($field_enabled->('reverseorder')) {
739 $self->{reverseorder} = $gcgi->wparam('reverseorder') || 0;
741 if ($field_enabled->('summaryonly')) {
742 $self->{summaryonly} = $gcgi->wparam('summaryonly') || 0;
746 if ($field_enabled->('notifytag')) {
747 my $newaddrs = clean_email_multi($gcgi->wparam('notifytag'));
748 if ($newaddrs eq "" or (valid_email_multi($newaddrs) and length($newaddrs) <= 512)) {
749 $self->{notifytag} = $newaddrs;
750 } else {
751 $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
755 if ($field_enabled->('notifyjson')) {
756 $self->{notifyjson} = $gcgi->wparam('notifyjson');
757 if ($self->{notifyjson}) {
758 valid_web_url($self->{notifyjson})
759 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
763 if ($field_enabled->('notifycia')) {
764 $self->{notifycia} = $gcgi->wparam('notifycia');
765 if ($self->{notifycia}) {
766 $self->{notifycia} =~ /^[a-zA-Z0-9._-]+$/
767 or $gcgi->err("Overly suspicious CIA notify project name. If it's actually valid, don't contact me, CIA is defunct.");
771 if ($cgi->param('setstatusupdates')) {
772 my $val = $gcgi->wparam('statusupdates') || '0';
773 $self->{statusupdates} = $val ? 1 : 0;
776 not $gcgi->err_check;
779 sub form_defaults {
780 my $self = shift;
782 name => $self->{name},
783 email => $self->{email},
784 url => $self->{url},
785 cleanmirror => $self->{cleanmirror},
786 desc => html_esc($self->{desc}),
787 README => html_esc($self->{README}),
788 hp => $self->{hp},
789 users => $self->{users},
790 notifymail => html_esc($self->{notifymail}),
791 reverseorder => $self->{reverseorder},
792 summaryonly => $self->{summaryonly},
793 notifytag => html_esc($self->{notifytag}),
794 notifyjson => html_esc($self->{notifyjson}),
795 notifycia => html_esc($self->{notifycia}),
799 # return true if $enc_passwd is a match for $plain_passwd
800 my $_check_passwd_match = sub {
801 my $enc_passwd = shift;
802 my $plain_passwd = shift;
803 defined($enc_passwd) or $enc_passwd = '';
804 defined($plain_passwd) or $plain_passwd = '';
805 # $enc_passwd may be crypt or crypt_sha1
806 if ($enc_passwd =~ m(^\$sha1\$(\d+)\$([./0-9A-Za-z]{1,64})\$[./0-9A-Za-z]{28}$)) {
807 # It's using sha1-crypt
808 return $enc_passwd eq crypt_sha1($plain_passwd, $2, -(0+$1));
809 } else {
810 # It's using crypt
811 return $enc_passwd eq crypt($plain_passwd, $enc_passwd);
815 sub authenticate {
816 my $self = shift;
817 my ($gcgi) = @_;
819 $self->{ccrypt} or die "Can't authenticate against a project with no password";
820 defined($self->{cpwd}) or $self->{cpwd} = '';
821 unless ($_check_passwd_match->($self->{ccrypt}, $self->{cpwd})) {
822 $gcgi->err("Your admin password does not match!");
823 return 0;
825 return 1;
828 # return true if the password from the file is empty or consists of all the same
829 # character. However, if the project was NOT loaded from the group file
830 # (!self->{loaded}) then the password is never locked.
831 # This function does NOT check $Girocco::Config::project_passwords, the caller
832 # is responsible for doing so if desired. Same for $self->{email}.
833 sub is_password_locked {
834 my $self = shift;
836 $self->{loaded} or return 0;
837 my $testcrypt = $self->{ccrypt}; # The value from the group file
838 defined($testcrypt) or $testcrypt = '';
839 $testcrypt ne '' or return 1; # No password at all
840 $testcrypt =~ /^(.)\1*$/ and return 1; # Bogus crypt value
841 return 0; # Not locked
844 sub _setup {
845 use POSIX qw(strftime);
846 my $self = shift;
847 my ($pushers) = @_;
848 my $fd;
850 defined($self->{path}) && $self->{path} ne "" or die "invalid setup call";
851 $self->_mkdir_forkees unless $self->{adopt};
853 my $gid;
854 $self->{adopt} || mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
855 -d $self->{path} or die "unable to setup nonexistent $self->{path}";
856 if ($Girocco::Config::owning_group) {
857 $gid = scalar(getgrnam($Girocco::Config::owning_group));
858 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
859 chmod(02775, $self->{path}) or die "chmod 02775 $self->{path} failed: $!";
860 } else {
861 chmod(02777, $self->{path}) or die "chmod 02777 $self->{path} failed: $!";
863 delete $ENV{GIT_OBJECT_DIRECTORY};
864 $ENV{'GIT_TEMPLATE_DIR'} = $Girocco::Config::chroot.'/var/empty';
865 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--quiet', '--bare', '--shared='.$self->shared_mode()) == 0
866 or die "git init $self->{path} failed: $?";
867 # we don't need these two, remove them (they will normally be created empty) if they exist
868 rmdir $self->{path}."/branches";
869 rmdir $self->{path}."/remotes";
870 -d $self->{path}."/info" or mkdir $self->{path}."/info"
871 or die "info directory does not exist and unable to create it: $!";
872 -d $self->{path}."/hooks" or mkdir $self->{path}."/hooks"
873 or die "hooks directory does not exist and unable to create it: $!";
874 # clean out any kruft that may have come in from the initial template directory
875 foreach my $cleandir (qw(hooks info)) {
876 if (opendir my $hooksdir, $self->{path}.'/'.$cleandir) {
877 unlink map "$self->{path}/$_", grep { $_ ne '.' && $_ ne '..' } readdir $hooksdir;
878 closedir $hooksdir;
881 if ($Girocco::Config::owning_group) {
882 chown(-1, $gid, $self->{path}."/hooks") or die "chgrp $gid $self->{path}/hooks failed: $!";
884 # hooks never world writable
885 chmod 02775, $self->{path}."/hooks" or die "chmod 02775 $self->{path}/hooks failed: $!";
886 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'core.compression', '5') == 0
887 or die "setting core.compression failed: $?";
888 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'core.logAllRefUpdates', 'false') == 0
889 or die "disabling core.logAllRefUpdates failed: $?";
890 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'core.ignoreCase', 'false') == 0
891 or die "disabling core.ignoreCase failed: $?";
892 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'transfer.fsckObjects', 'true') == 0
893 or die "enabling transfer.fsckObjects failed: $?";
894 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastForwards', 'false') == 0
895 or die "disabling receive.denyNonFastForwards failed: $?";
896 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyDeleteCurrent', 'warn') == 0
897 or die "disabling receive.denyDeleteCurrent failed: $?";
898 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gc.auto', '0') == 0
899 or die "disabling gc.auto failed: $?";
900 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.autogc', '0') == 0
901 or die "disabling receive.autogc failed: $?";
902 my ($S,$M,$H,$d,$m,$y) = gmtime(time());
903 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.updateServerInfo', 'true') == 0
904 or die "enabling receive.updateServerInfo failed: $?";
905 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'repack.writeBitmaps', 'true') == 0
906 or die "enabling repack.writeBitmaps failed: $?";
907 $self->{creationtime} = strftime("%Y-%m-%dT%H:%M:%SZ", $S, $M, $H, $d, $m, $y, -1, -1, -1);
908 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'girocco.creationtime', $self->{creationtime}) == 0
909 or die "setting girocco.creationtime failed: $?";
910 -d $self->{path}."/htmlcache" or mkdir $self->{path}."/htmlcache"
911 or die "htmlcache directory does not exist and unable to create it: $!";
912 -d $self->{path}."/bundles" or mkdir $self->{path}."/bundles"
913 or die "bundles directory does not exist and unable to create it: $!";
914 -d $self->{path}."/reflogs" or mkdir $self->{path}."/reflogs"
915 or die "reflogs directory does not exist and unable to create it: $!";
916 foreach my $file (qw(info/lastactivity .delaygc)) {
917 if (open $fd, '>', $self->{path}."/".$file) {
918 close $fd;
919 chmod 0664, $self->{path}."/".$file;
923 # /info must have right permissions,
924 # and git init didn't do it for some reason.
925 # config must have correct permissions.
926 # and Git 2.1.0 - 2.2.1 incorrectly add +x for some reason.
927 # also make sure /refs, /objects and /htmlcache are correct too.
928 my ($dmode, $dmodestr, $fmode, $fmodestr);
929 if ($Girocco::Config::owning_group) {
930 ($dmode, $dmodestr) = (02775, '02775');
931 ($fmode, $fmodestr) = (0664, '0664');
932 } else {
933 ($dmode, $dmodestr) = (02777, '02777');
934 ($fmode, $fmodestr) = (0666, '0666');
936 foreach my $dir (qw(info refs objects htmlcache bundles reflogs)) {
937 chmod($dmode, $self->{path}."/$dir") or die "chmod $dmodestr $self->{path}/$dir failed: $!";
939 foreach my $file (qw(config)) {
940 chmod($fmode, $self->{path}."/$file") or die "chmod $fmodestr $self->{path}/$file failed: $!";
942 # these ones are probably not strictly required but are nice to have
943 foreach my $dir (qw(refs/heads refs/tags objects/info objects/pack)) {
944 -d $self->{path}."/$dir" or mkdir $self->{path}."/$dir";
945 chmod($dmode, $self->{path}."/$dir");
948 $self->_properties_save;
949 $self->_alternates_setup unless $self->{noalternates} || $self->{adopt};
950 $self->_ctags_setup;
951 $self->_group_remove;
952 $self->_group_add($pushers);
953 $self->_hooks_install;
954 $self->_update_index;
955 $self->_set_changed;
956 $self->_set_forkchange(1);
959 sub premirror {
960 my $self = shift;
962 delete $self->{adopt};
963 $self->_setup(':');
964 $self->_clonep(1);
965 $self->perm_initialize;
968 sub conjure {
969 my $self = shift;
971 delete $self->{adopt};
972 $self->_setup;
973 $self->_nofetch(1);
974 if ($Girocco::Config::mob && $Girocco::Config::mob eq "mob") {
975 system("$Girocco::Config::basedir/bin/create-personal-mob-area", $self->{name}) == 0
976 or die "create-personal-mob-area $self->{name} failed";
978 $self->perm_initialize;
981 sub clone {
982 my $self = shift;
984 unlink ($self->_clonefail_path()); # Ignore EEXIST error
985 unlink ($self->_clonelog_path()); # Ignore EEXIST error
987 use IO::Socket;
988 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
989 select((select($sock),$|=1)[0]);
990 $sock->print("clone ".$self->{name}."\n");
991 # Just ignore reply, we are going to succeed anyway and the I/O
992 # would apparently get quite hairy.
993 $sock->flush();
994 sleep 2; # *cough*
995 $sock->close();
998 # call this after ghost instead of conjure or premirror+clone to adopt a pre-existing Git dir
999 # ghost must be called with the proper value of $mirror for the to-be-adopted project
1000 # for mirrors the $proj->{url} needs to be set and for push projects the $proj->{users} array ref
1001 sub adopt {
1002 my $self = shift;
1003 my $name = $self->{name};
1005 # Sanity check first
1006 defined($name) && $name ne "" && !$self->{loaded} or return undef;
1007 does_exist($name, 1) or return undef;
1008 defined($self->{path}) && $self->{path} eq $Girocco::Config::reporoot."/$name.git" or return undef;
1009 defined($self->{base_path}) && $self->{base_path} eq $Girocco::Config::reporoot or return undef;
1010 defined($self->{email}) && defined($self->{orig_email}) && defined($self->{mirror}) && ref($self->{users}) eq 'ARRAY'
1011 or return undef;
1012 !defined(Girocco::Project->load($name)) or return undef;
1013 is_git_dir($self->{path}) or return undef;
1014 my $config = read_config_file_hash($self->{path}."/config");
1015 defined($config) && _boolval($config->{"core.bare"}) or die "refusing to adopt non-bare repository";
1016 defined(read_HEAD_symref($self->{path})) or die "refusing to adopt non-symref HEAD repository";
1018 # Adopt the project by creating a new $chroot/etc/group entry and setting up anything that's missing
1019 $self->_nofetch(!$self->{mirror});
1020 $self->{adopt} = 1;
1021 $self->_setup($self->{mirror} ? ":" : "");
1022 delete $self->{adopt};
1023 if (!$self->{mirror} && $Girocco::Config::mob && $Girocco::Config::mob eq "mob") {
1024 system("$Girocco::Config::basedir/bin/create-personal-mob-area", $self->{name}) == 0
1025 or die "create-personal-mob-area $self->{name} failed";
1027 my $result = $self->perm_initialize;
1028 unlink("$self->{path}/.delaygc") unless $self->is_empty;
1029 # Pick up any pre-existing settings
1030 my $p = Girocco::Project->load($name);
1031 %$self = %$p if defined($p) && $p->{loaded};
1032 $result;
1035 sub _update_users {
1036 my $self = shift;
1038 $self->_group_update;
1039 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
1040 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
1041 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
1042 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
1045 sub update {
1046 my $self = shift;
1048 $self->_properties_save;
1049 $self->_update_users;
1051 if (exists($self->{tags_to_delete})) {
1052 $self->delete_ctag($_) foreach(@{$self->{tags_to_delete}});
1055 $self->set_HEAD($self->{HEAD}) unless $self->{orig_HEAD} eq $self->{HEAD};
1057 $self->_update_index if $self->{email} ne $self->{orig_email};
1058 $self->{orig_email} = $self->{email};
1059 $self->_set_changed;
1064 sub update_password {
1065 my $self = shift;
1066 my ($pwd) = @_;
1068 $self->{crypt} = scrypt_sha1($pwd);
1069 $self->_group_update;
1072 # You can explicitly do this just on a ghost() repository too.
1073 sub delete {
1074 my $self = shift;
1076 if (-d $self->{path}) {
1077 system('rm', '-rf', $self->{path}) == 0
1078 or die "rm -rf $self->{path} failed: $?";
1080 # attempt to clean up any empty fork directories by removing them
1081 my @pelems = split('/', $self->{name});
1082 while (@pelems > 1) {
1083 pop @pelems;
1084 # okay to fail
1085 rmdir join('/', $Girocco::Config::reporoot, @pelems) or last;
1087 $self->_group_remove;
1088 $self->_update_index;
1089 $self->_set_forkchange(1);
1094 # If the project's directory actually exists archive it before deleting it
1095 # Return full path to archived project ("" if none)
1096 sub archive_and_delete {
1097 my $self = shift;
1099 unless (-d $self->{path}) {
1100 $self->delete;
1101 return "";
1104 # archive the project before deletion
1105 use POSIX qw(strftime);
1106 my $destdir = $self->{base_path};
1107 $destdir =~ s,(?<=[^/])/+$,,;
1108 $destdir .= "/_recyclebin/";
1109 $destdir .= $1 if $self->{name} =~ m,^(.*/)[^/]+$,;
1110 my $destbase = $self->{name};
1111 $destbase = $1 if $destbase =~ m,^.*/([^/]+)$,;
1112 my $oldmask = umask();
1113 umask($oldmask & ~0070);
1114 system('mkdir', '-p', $destdir) == 0 && -d $destdir
1115 or die "mkdir -p \"$destdir\" failed: $?";
1116 umask($oldmask);
1117 my $suffix = '';
1118 if (-e "$destdir$destbase.git") {
1119 $suffix = 1;
1120 while (-e "$destdir$destbase~$suffix.git") {
1121 ++$suffix;
1122 last if $suffix >= 10000; # don't get too carried away
1124 $suffix = '~'.$suffix;
1126 not -e "$destdir$destbase$suffix.git"
1127 or die "Unable to compute suitable archive path";
1128 system('mv', $self->{path}, "$destdir$destbase$suffix.git") == 0
1129 or die "mv \"$self->{path}\" \"$destdir$destbase$suffix.git\" failed: $?";
1130 if (!$self->{mirror} && @{$self->{users}}) {
1131 # Remember the user list at recycle time
1132 system($Girocco::Config::git_bin, '--git-dir='.$destdir.$destbase.$suffix.".git",
1133 'config', 'girocco.recycleusers', join(",", @{$self->{users}}));
1135 my ($S,$M,$H,$d,$m,$y) = gmtime(time());
1136 my $recycletime = strftime("%Y-%m-%dT%H:%M:%SZ", $S, $M, $H, $d, $m, $y, -1, -1, -1);
1137 # We ought to do something if this fails, but the project has already been moved
1138 # so there's really nothing to be done at this point.
1139 system($Girocco::Config::git_bin, '--git-dir='.$destdir.$destbase.$suffix.".git",
1140 'config', 'girocco.recycletime', $recycletime);
1142 $self->delete;
1143 return $destdir.$destbase.$suffix.".git";
1146 sub _contains_files {
1147 my $dir = shift;
1148 (-d $dir) or return 0;
1149 opendir(my $dh, $dir) or die "opendir $dir failed: $!";
1150 while (my $entry = readdir($dh)) {
1151 next if $entry eq '' || $entry eq '.' || $entry eq '..';
1152 closedir($dh), return 1
1153 if -f "$dir/$entry" ||
1154 -d "$dir/$entry" && _contains_files("$dir/$entry");
1156 closedir($dh);
1157 return 0;
1160 sub has_forks {
1161 my $self = shift;
1163 return _contains_files($Girocco::Config::reporoot.'/'.$self->{name});
1166 # Returns an array of 0 or more array refs, one for each bundle:
1167 # ->[0]: bundle creation time (seconds since epoch)
1168 # ->[1]: bundle name (e.g. foo-xxxxxxxx.bundle)
1169 # ->[2]: bundle size in bytes
1170 sub bundles {
1171 my $self = shift;
1172 use Time::Local;
1174 return @{$self->{bundles}} if ref($self->{bundles}) eq 'ARRAY';
1175 my @blist = ();
1176 if (-d $self->{path}.'/bundles') {{
1177 my $prefix = $self->{name};
1178 $prefix =~ s|^.*[^/]/([^/]+)$|$1|;
1179 opendir(my $dh, $self->{path}.'/bundles') or last;
1180 while (my $bfile = readdir($dh)) {
1181 next unless $bfile =~ /^\d{8}_\d{6}-[0-9a-f]{8}$/;
1182 my $ctime = eval {timegm(
1183 0+substr($bfile,13,2), 0+substr($bfile,11,2), 0+substr($bfile,9,2),
1184 0+substr($bfile,6,2), 0+substr($bfile,4,2)-1, 0+substr($bfile,0,4)-1900)};
1185 next unless $ctime;
1186 open(my $bh, '<', $self->{path}.'/bundles/'.$bfile) or next;
1187 my $f1 = <$bh>;
1188 my $f2 = <$bh>;
1189 $f1 = $self->{path}.'/objects/pack/'.$f1 if $f1 && $f1 !~ m|^/|;
1190 $f2 = $self->{path}.'/objects/pack/'.$f2 if $f2 && $f2 !~ m|^/|;
1191 close($bh);
1192 next unless $f1 && $f2 && $f1 ne $f2;
1193 chomp $f1;
1194 chomp $f2;
1195 next unless -e $f1 && -e $f2;
1196 my $s1 = -s $f1 || 0;
1197 my $s2 = -s $f2 || 0;
1198 next unless $s1 || $s2;
1199 push(@blist, [$ctime, "$prefix-".substr($bfile,16,8).".bundle", $s1+$s2]);
1201 closedir($dh);
1203 @blist = sort({$b->[0] <=> $a->[0]} @blist);
1204 my %seen = ();
1205 my @result = ();
1206 foreach my $bndl (@blist) {
1207 next if $seen{$bndl->[1]};
1208 $seen{$bndl->[1]} = 1;
1209 push(@result, $bndl);
1211 $self->{bundles} = \@result;
1212 return @result;
1215 sub has_alternates {
1216 my $self = shift;
1217 return -s $self->{path}.'/objects/info/alternates' ? 1 : 0;
1220 sub has_bundle {
1221 my $self = shift;
1223 return scalar($self->bundles);
1226 # returns true if any of the notify fields are non-empty
1227 sub has_notify {
1228 my $self = shift;
1229 # We do not ckeck notifycia since it's defunct
1230 return $self->{'notifymail'} || $self->{'notifytag'} || $self->{'notifyjson'};
1233 sub is_empty {
1234 # A project is considered empty if the git repository does not
1235 # have any refs. This means packed-refs does not exist or is
1236 # empty or only has lines starting with '#' AND there are no
1237 # files in the refs subdirectory hierarchy (no matter how deep).
1239 my $self = shift;
1241 (-d $self->{path}) or return 0;
1242 if (-e $self->{path}.'/packed-refs') {
1243 open(my $pr, '<', $self->{path}.'/packed-refs')
1244 or die "open $self->{path}./packed-refs failed: $!";
1245 my $foundref = 0;
1246 while (my $ref = <$pr>) {
1247 next if $ref =~ /^#/;
1248 $foundref = 1;
1249 last;
1251 close($pr);
1252 return 0 if $foundref;
1254 (-d $self->{path}.'/refs') or return 1;
1255 return !_contains_files($self->{path}.'/refs');
1258 # returns array:
1259 # [0]: earliest possible scheduled next gc, undef if lastgc not set
1260 # [1]: approx. latest possible scheduled next gc, undef if lastgc not set
1261 # Both values (if not undef) are seconds since epoch
1262 # Result only considers lastgc and min_gc_interval nothing else
1263 sub next_gc {
1264 my $self = shift;
1265 my $lastgcepoch = parse_any_date($self->{lastgc});
1266 return (undef, undef) unless defined $lastgcepoch;
1267 return ($lastgcepoch + $Girocco::Config::min_gc_interval,
1268 int($lastgcepoch + 1.25 * $Girocco::Config::min_gc_interval +
1269 $Girocco::Config::min_mirror_interval));
1272 # returns boolean (0 or 1)
1273 # Attempts to determine whether or not a gc (and corresponding build
1274 # of a .bitmap/.bndl file) will actually take place at the next_gc
1275 # time (as returned by next_gc). Whether or not a .bitmap and .bndl
1276 # end up being built depends on whether or not the local object graph
1277 # is complete. In general if has_alternates is true then a .bitmap/.bndl
1278 # is not possible because the object graph will be incomplete,
1279 # but it *is* possible that even though the repository has_alternates,
1280 # it does not actually borrow any objects so a .bitmap/.bndl will build
1281 # in spite of the presence of alternates -- but this is expected to be rare.
1282 # The result is a best guess and false return value is not an absolute
1283 # guarantee that gc will not take place at the next interval, but it probably
1284 # will not if nothing changes in the meantime.
1285 sub needs_gc {
1286 my $self = shift;
1287 my $lgc = parse_any_date($self->{lastgc});
1288 my $lrecv = parse_any_date($self->{lastreceive});
1289 my $lpgc = parse_any_date($self->{lastparentgc});
1290 return 1 unless defined($lgc) && defined($lrecv);
1291 if ($self->has_alternates) {
1292 return 1 unless defined($lpgc);
1293 return 1 unless $lpgc < $lgc;
1295 return 1 unless $lrecv < $lgc;
1296 # We don't try running any "is_dirty" check, so if somehow the
1297 # repository became dirty without updating lastreceive we might
1298 # incorrectly return false instead of true.
1299 return 0;
1302 sub delete_ctag {
1303 my $self = shift;
1304 my ($ctag) = @_;
1306 # sanity check, disallow filenames starting with . .. or /
1307 unlink($self->{path}.'/ctags/'.$ctag)
1308 unless !defined($ctag) || $ctag =~ m|^/| || $ctag =~ m{(?:^|/)(?:\.\.?)(?:/|$)};
1311 # returns new tag count value on success (will always be >= 1) otherwise undef
1312 sub add_ctag {
1313 my $self = shift;
1314 my $ctag = valid_tag(shift);
1315 my $nochanged = shift;
1317 # sanity check, disallow filenames starting with . .. or /
1318 return undef if !defined($ctag) || $ctag =~ m|^/| || $ctag =~ m{(?:^|/)(?:\.\.?)(?:/|$)};
1320 my $val = 0;
1321 my $ct;
1322 if (open $ct, '<', $self->{path}."/ctags/$ctag") {
1323 my $count = <$ct>;
1324 close $ct;
1325 defined $count or $count = '';
1326 chomp $count;
1327 $val = $count =~ /^[1-9]\d*$/ ? $count : 1;
1329 ++$val;
1330 my $oldmask = umask();
1331 umask($oldmask & ~0060);
1332 open $ct, '>', $self->{path}."/ctags/$ctag" and print $ct $val."\n" and close $ct;
1333 $self->_set_changed unless $nochanged;
1334 $self->_set_forkchange unless $nochanged;
1335 umask($oldmask);
1336 return $val;
1339 sub get_ctag_names {
1340 my $self = shift;
1341 my @ctags = ();
1342 opendir(my $dh, $self->{path}.'/ctags')
1343 or return @ctags;
1344 @ctags = grep { -f "$self->{path}/ctags/$_" } readdir($dh);
1345 closedir($dh);
1346 return sort({lc($a) cmp lc($b)} @ctags);
1349 sub get_heads {
1350 my $self = shift;
1351 my $fh;
1352 my $heads = get_git("--git-dir=$self->{path}", 'for-each-ref', '--format=%(objectname) %(refname)', 'refs/heads');
1353 defined($heads) or $heads = '';
1354 chomp $heads;
1355 my @res = ();
1356 foreach (split(/\n/, $heads)) {
1357 chomp;
1358 next if !m#^[0-9a-f]{40}\s+refs/heads/(.+)$ #x;
1359 push @res, $1;
1361 push(@res, $self->{orig_HEAD}) if !@res && defined($self->{orig_HEAD}) && $self->{orig_HEAD} ne '';
1362 @res;
1365 sub get_HEAD {
1366 my $self = shift;
1367 my $HEAD = read_HEAD_symref($self->{path});
1368 defined($HEAD) or $HEAD = '';
1369 die "could not get HEAD" if ($HEAD !~ m{^refs/heads/(.+)$});
1370 return $1;
1373 sub set_HEAD {
1374 my $self = shift;
1375 my $newHEAD = shift;
1376 # Cursory checks only -- if you want to break your HEAD, be my guest
1377 if ($newHEAD =~ /^\/|['<>]|\.\.|\/$/) {
1378 die "grossly invalid new HEAD: $newHEAD";
1380 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', "refs/heads/$newHEAD");
1381 die "could not set HEAD" if ($? >> 8);
1382 ! -d "$self->{path}/mob" || $Girocco::Config::mob ne 'mob'
1383 or system('cp', '-p', '-f', "$self->{path}/HEAD", "$self->{path}/mob/HEAD") == 0;
1386 sub gen_auth {
1387 my $self = shift;
1388 my ($type) = @_;
1389 $type = 'REPO' unless $type && $type =~ /^[A-Z]+$/;
1391 $self->{authtype} = $type;
1393 no warnings;
1394 $self->{auth} = sha1_hex(time . $$ . rand() . join(':',%$self));
1396 my $expire = time + 24 * 3600;
1397 my $propval = "# ${type}AUTH $self->{auth} $expire";
1398 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gitweb.repoauth', $propval);
1399 $self->{auth};
1402 sub del_auth {
1403 my $self = shift;
1405 delete $self->{auth};
1406 delete $self->{authtype};
1407 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--unset', 'gitweb.repoauth');
1410 sub remove_user {
1411 my $self = shift;
1412 my ($username) = @_;
1414 my $before_count = @{$self->{users}};
1415 $self->{users} = [grep { $_ ne $username } @{$self->{users}}];
1416 return @{$self->{users}} != $before_count;
1419 ### static methods
1421 sub get_forkee_name {
1422 local $_ = $_[0];
1423 (m#^(.*)/.*?$#)[0]; #
1426 sub get_forkee_path {
1427 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1428 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
1429 -d $forkee ? $forkee : '';
1432 # Ultimately the full project/fork name could end up being part of a Git ref name
1433 # when a project's forks are combined into one giant repository for efficiency.
1434 # That means that the project/fork name must satisfy the Git ref name requirements:
1436 # 1. Characters with an ASCII value less than or equal to 32 are not allowed
1437 # 2. The character with an ASCII value of 0x7F is not allowed
1438 # 3. The characters '~', '^', ':', '\', '*', '?', and '[' are not allowed
1439 # 4. The character '/' is a separator and is not allowed within a name
1440 # 5. The name may not start with '.' or end with '.'
1441 # 6. The name may not end with '.lock'
1442 # 7. The name may not contain the '..' sequence
1443 # 8. The name may not contain the '@{' sequence
1444 # 9. If multiple components are used (separated by '/'), no empty '' components
1446 # We also prohibit a trailing '.git' on any path component and futher restrict
1447 # the allowed characters to alphanumeric and [+._-] where names must start with
1448 # an alphanumeric.
1450 sub _valid_name_characters {
1451 local $_ = $_[0];
1452 (not m#^[/+._-]#)
1453 and (not m#//#)
1454 and (not m#\.\.#)
1455 and (not m#/[+._-]#)
1456 and (not m#\./#)
1457 and (not m#\.$#)
1458 and (not m#\.git/#i)
1459 and (not m#\.git$#i)
1460 and (not m#\.idx/#i)
1461 and (not m#\.idx$#i)
1462 and (not m#\.lock/#i)
1463 and (not m#\.lock$#i)
1464 and (not m#\.pack/#i)
1465 and (not m#\.pack$#i)
1466 and (not m#\.bundle/#i)
1467 and (not m#\.bundle$#i)
1468 and (not m#/$#)
1469 and (not m/^[a-fA-F0-9]{38}$/)
1470 and m#^[a-zA-Z0-9/+._-]+$#
1471 and !has_reserved_suffix($_, $_[1], $_[2]);
1474 # $_[0] => prospective project name (WITHOUT trailing .git)
1475 # $_[1] => true to allow orphans (i.e. two-or-more-level-deep projects without a parent)
1476 # (the directory in which the orphan will be created must, however, already exist)
1477 # $_[2] => true to allow orphans w/o needed directory if $_[1] also true (like mkdir -p)
1478 sub valid_name {
1479 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1480 local $_ = $_[0];
1481 _valid_name_characters($_) and not exists($reservedprojectnames{lc($_)})
1482 and @{[m#/#g]} <= 5 # maximum fork depth is 5
1483 and ((not m#/#) or -d get_forkee_path($_) or ($_[1] and ($_[2] or -d $Girocco::Config::reporoot.'/'.get_forkee_name($_))))
1484 and (! -f $Girocco::Config::reporoot."/$_.git");
1487 # It's possible that some forks have been kept but the forkee is gone.
1488 # In this case the standard valid_name check is too strict.
1489 sub does_exist {
1490 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
1491 my ($name, $nodie) = @_;
1492 my $okay = (
1493 _valid_name_characters($name, $Girocco::Config::reporoot, ".git")
1494 and ((not $name =~ m#/#)
1495 or -d get_forkee_path($name)
1496 or -d $Girocco::Config::reporoot.'/'.get_forkee_name($name)));
1497 (!$okay && $nodie) and return undef;
1498 !$okay and die "tried to query for project with invalid name $name!";
1499 (-d $Girocco::Config::reporoot."/$name.git");
1502 sub get_full_list {
1503 open my $fd, '<', jailed_file("/etc/group") or die "getting project list failed: $!";
1504 my @projects = map {/^([^:_\s#][^:\s#]*):[^:]*:\d{5,}:/ ? $1 : ()} <$fd>;
1505 close $fd;
1506 @projects;