Store last_*, owner, base_url and homepage in git config
[girocco.git] / Girocco / Project.pm
blob148e5e664bd7215aee835808621ab74994d0e4c9
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::ProjPerm;
11 use Girocco::Config;
12 use base ('Girocco::ProjPerm::'.$Girocco::Config::permission_control); # mwahaha
15 our $metadata_fields = {
16 homepage => ['Homepage URL', 'hp', 'text'],
17 shortdesc => ['Short description', 'desc', 'text'],
18 README => ['README (HTML, lt 8kb)', 'README', 'textarea'],
21 sub _mkdir_forkees {
22 my $self = shift;
23 my @pelems = split('/', $self->{name});
24 pop @pelems; # do not create dir for the project itself
25 my $path = $self->{base_path};
26 foreach my $pelem (@pelems) {
27 $path .= "/$pelem";
28 (-d "$path") or mkdir $path or die "mkdir $path: $!";
29 chmod 0775, $path; # ok if fails (dir may already exist and be owned by someone else)
33 our %propmap = (
34 url => ':base_url',
35 email => ':owner',
36 desc => 'description',
37 README => 'README.html',
38 hp => ':homepage',
41 sub _property_path {
42 my $self = shift;
43 my ($name) = @_;
44 $self->{path}.'/'.$name;
47 sub _property_fget {
48 my $self = shift;
49 my ($name) = @_;
50 $propmap{$name} or die "unknown property: $name";
51 if ($propmap{$name} =~ /^:/) {
52 return `git --git-dir="$self->{path}" config "gitweb.$name"`
55 open P, $self->_property_path($propmap{$name}) or return undef;
56 my @value = <P>;
57 close P;
58 my $value = join('', @value); chomp $value;
59 $value;
62 sub _property_fput {
63 my $self = shift;
64 my ($name, $value) = @_;
65 $propmap{$name} or die "unknown property: $name";
66 $value ||= '';
67 if ($propmap{$name} =~ /^:/) {
68 return `git --git-dir="$self->{path}" config "gitweb.$name" "$value"`
71 my $P = lock_file($self->_property_path($propmap{$name}));
72 $value ne '' and print $P "$value\n";
73 close $P;
74 unlock_file($self->_property_path($propmap{$name}));
77 sub _properties_load {
78 my $self = shift;
79 foreach my $prop (keys %propmap) {
80 $self->{$prop} = $self->_property_fget($prop);
84 sub _properties_save {
85 my $self = shift;
86 foreach my $prop (keys %propmap) {
87 $self->_property_fput($prop, $self->{$prop});
91 sub _nofetch_path {
92 my $self = shift;
93 $self->_property_path('.nofetch');
96 sub _nofetch {
97 my $self = shift;
98 my ($nofetch) = @_;
99 my $np = $self->_nofetch_path;
100 if ($nofetch) {
101 open X, '>'.$np or die "nofetch failed: $!";
102 close X;
103 } else {
104 unlink $np or die "yesfetch failed: $!";
108 sub _clonelog_path {
109 my $self = shift;
110 $self->_property_path('.clonelog');
113 sub _clonefail_path {
114 my $self = shift;
115 $self->_property_path('.clone_failed');
118 sub _clonep_path {
119 my $self = shift;
120 $self->_property_path('.clone_in_progress');
123 sub _clonep {
124 my $self = shift;
125 my ($nofetch) = @_;
126 my $np = $self->_clonep_path;
127 if ($nofetch) {
128 open X, '>'.$np or die "clonep failed: $!";
129 close X;
130 } else {
131 unlink $np or die "clonef failed: $!";
134 sub _alternates_setup {
135 my $self = shift;
136 return unless $self->{name} =~ m#/#;
137 my $forkee_name = get_forkee_name($self->{name});
138 my $forkee_path = get_forkee_path($self->{name});
139 return unless -d $forkee_path;
140 mkdir $self->{path}.'/refs'; chmod 0775, $self->{path}.'/refs';
141 mkdir $self->{path}.'/objects'; chmod 0775, $self->{path}.'/objects';
142 mkdir $self->{path}.'/objects/info'; chmod 0775, $self->{path}.'/objects/info';
144 # We set up both alternates and http_alternates since we cannot use
145 # relative path in alternates - that doesn't work recursively.
147 my $filename = $self->{path}.'/objects/info/alternates';
148 open X, '>'.$filename or die "alternates failed: $!";
149 print X "$forkee_path/objects\n";
150 close X;
151 chmod 0664, $filename or warn "cannot chmod $filename: $!";
153 if ($Girocco::Config::httppullurl) {
154 $filename = $self->{path}.'/objects/info/http-alternates';
155 open X, '>'.$filename or die "http-alternates failed: $!";
156 my $upfork = $forkee_name;
157 do { print X "$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s#/?.+?$## and $upfork); #
158 close X;
159 chmod 0664, $filename or warn "cannot chmod $filename: $!";
162 symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
165 sub _ctags_setup {
166 my $self = shift;
167 mkdir $self->{path}.'/ctags'; chmod 01777, $self->{path}.'/ctags';
170 sub _group_add {
171 my $self = shift;
172 my ($xtra) = @_;
173 $xtra .= join(',', @{$self->{users}});
174 filedb_atomic_append(jailed_file('/etc/group'),
175 join(':', $self->{name}, $self->{crypt}, '\i', $xtra));
178 sub _group_update {
179 my $self = shift;
180 my $xtra = join(',', @{$self->{users}});
181 filedb_atomic_edit(jailed_file('/etc/group'),
182 sub {
183 $_ = $_[0];
184 chomp;
185 if ($self->{name} eq (split /:/)[0]) {
186 # preserve readonly flag
187 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
188 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
189 } else {
190 return "$_\n";
196 sub _group_remove {
197 my $self = shift;
198 filedb_atomic_edit(jailed_file('/etc/group'),
199 sub {
200 $self->{name} ne (split /:/)[0] and return $_;
205 sub _hook_path {
206 my $self = shift;
207 my ($name) = @_;
208 $self->{path}.'/hooks/'.$name;
211 sub _hook_install {
212 my $self = shift;
213 my ($name) = @_;
214 open SRC, "$Girocco::Config::basedir/$name-hook" or die "cannot open hook $name: $!";
215 open DST, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
216 while (<SRC>) { print DST $_; }
217 close DST;
218 close SRC;
219 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
222 sub _hooks_install {
223 my $self = shift;
224 foreach my $hook ('update') {
225 $self->_hook_install($hook);
229 # private constructor, do not use
230 sub _new {
231 my $class = shift;
232 my ($name, $base_path, $path) = @_;
233 valid_name($name) or die "refusing to create project with invalid name ($name)!";
234 $path ||= "$base_path/$name.git";
235 my $proj = { name => $name, base_path => $base_path, path => $path };
237 bless $proj, $class;
240 # public constructor #0
241 # creates a virtual project not connected to disk image
242 # you can conjure() it later to disk
243 sub ghost {
244 my $class = shift;
245 my ($name, $mirror) = @_;
246 my $self = $class->_new($name, $Girocco::Config::reporoot);
247 $self->{users} = [];
248 $self->{mirror} = $mirror;
249 $self;
252 # public constructor #1
253 sub load {
254 my $class = shift;
255 my ($name) = @_;
257 open F, jailed_file("/etc/group") or die "project load failed: $!";
258 while (<F>) {
259 chomp;
260 @_ = split /:+/;
261 next unless (shift eq $name);
263 my $self = $class->_new($name, $Girocco::Config::reporoot);
264 (-d $self->{path}) or die "invalid path (".$self->{path}.") for project ".$self->{name};
266 my $ulist;
267 ($self->{crypt}, $self->{gid}, $ulist) = @_;
268 $ulist ||= '';
269 $self->{users} = [split /,/, $ulist];
270 $self->{orig_users} = [@{$self->{users}}];
271 $self->{mirror} = ! -e $self->_nofetch_path;
272 $self->{clone_in_progress} = -e $self->_clonep_path;
273 $self->{clone_logged} = -e $self->_clonelog_path;
274 $self->{clone_failed} = -e $self->_clonefail_path;
275 $self->{ccrypt} = $self->{crypt};
277 $self->_properties_load;
278 return $self;
280 close F;
281 undef;
284 # $proj may not be in sane state if this returns false!
285 sub cgi_fill {
286 my $self = shift;
287 my ($gcgi) = @_;
288 my $cgi = $gcgi->cgi;
290 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
291 $pwd ||= ''; $pwd2 ||= ''; # in case passwords are disabled
292 if ($pwd or not $self->{crypt}) {
293 $self->{crypt} = scrypt($pwd);
296 if ($pwd2 and $pwd ne $pwd2) {
297 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
300 $self->{cpwd} = $cgi->param('cpwd');
302 if ($Girocco::Config::project_owners eq 'email') {
303 $self->{email} = $gcgi->wparam('email');
304 valid_email($self->{email})
305 or $gcgi->err("Your email sure looks weird...?");
308 $self->{url} = $gcgi->wparam('url');
309 if ($self->{url}) {
310 valid_repo_url($self->{url})
311 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
314 $self->{desc} = $gcgi->wparam('desc');
315 length($self->{desc}) <= 1024
316 or $gcgi->err("<b>Short</b> description length &gt; 1kb!");
318 $self->{README} = $gcgi->wparam('README');
319 length($self->{README}) <= 8192
320 or $gcgi->err("README length &gt; 8kb!");
322 $self->{hp} = $gcgi->wparam('hp');
323 if ($self->{hp}) {
324 valid_web_url($self->{hp})
325 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
328 # FIXME: Permit only existing users
329 $self->{users} = [grep { Girocco::User::valid_name($_) } $cgi->param('user')];
331 not $gcgi->err_check;
334 sub form_defaults {
335 my $self = shift;
337 name => $self->{name},
338 email => $self->{email},
339 url => $self->{url},
340 desc => html_esc($self->{desc}),
341 README => html_esc($self->{README}),
342 hp => $self->{hp},
343 users => $self->{users},
347 sub authenticate {
348 my $self = shift;
349 my ($gcgi) = @_;
351 $self->{ccrypt} or die "Can't authenticate against a project with no password";
352 $self->{cpwd} ||= '';
353 unless ($self->{ccrypt} eq crypt($self->{cpwd}, $self->{ccrypt})) {
354 $gcgi->err("Your admin password does not match!");
355 return 0;
357 return 1;
360 sub _setup {
361 my $self = shift;
362 my ($pushers) = @_;
364 $self->_mkdir_forkees;
366 mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
367 if ($Girocco::Config::owning_group) {
368 my $gid = scalar(getgrnam($Girocco::Config::owning_group));
369 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
370 chmod(02775, $self->{path}) or die "chmod 2775 $self->{path} failed: $!";
371 } else {
372 chmod(02777, $self->{path}) or die "chmod 2777 $self->{path} failed: $!";
374 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
375 or die "git init $self->{path} failed: $?";
376 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastforwards', 'false') == 0
377 or die "disabling receive.denyNonFastforwards failed: $?";
378 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gc.auto', '0') == 0
379 or die "disabling gc.auto failed: $?";
381 $self->_properties_save;
382 $self->_alternates_setup;
383 $self->_ctags_setup;
384 $self->_group_add($pushers);
385 $self->_hooks_install;
386 $self->perm_initialize;
389 sub premirror {
390 my $self = shift;
392 $self->_setup(':');
393 $self->_clonep(1);
396 sub conjure {
397 my $self = shift;
399 $self->_setup;
400 $self->_nofetch(1);
403 sub clone {
404 my $self = shift;
406 unlink ($self->_clonefail_path()); # Ignore EEXIST error
408 use IO::Socket;
409 my $sock = IO::Socket::UNIX->new('/tmp/girocco.cloned') or die "cannot connect to girocco.cloned: $!";
410 $sock->print($self->{name}.".git\n");
411 # Just ignore reply, we are going to succeed anyway and the I/O
412 # would apparently get quite hairy.
413 $sock->flush();
414 sleep 2; # *cough*
415 $sock->close();
418 sub update {
419 my $self = shift;
421 $self->_properties_save;
422 $self->_group_update;
424 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
425 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
426 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
427 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
432 sub update_password {
433 my $self = shift;
434 my ($pwd) = @_;
436 $self->{crypt} = scrypt($pwd);
437 $self->_group_update;
440 # You can explicitly do this just on a ghost() repository too.
441 sub delete {
442 my $self = shift;
444 if (-d $self->{path}) {
445 system('rm', '-rf', $self->{path}) == 0
446 or die "rm -rf $self->{path} failed: $?";
448 $self->_group_remove;
451 sub has_forks {
452 my $self = shift;
454 return glob($Girocco::Config::reporoot.'/'.$self->{name}.'/*');
458 ### static methods
460 sub get_forkee_name {
461 $_ = $_[0];
462 (m#^(.*)/.*?$#)[0]; #
464 sub get_forkee_path {
465 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
466 -d $forkee ? $forkee : '';
469 sub valid_name {
470 $_ = $_[0];
471 (not m#/# or -d get_forkee_path($_)) # will also catch ^/
472 and (not m#\./#)
473 and (not m#/$#)
474 and m#^[a-zA-Z0-9+./_-]+$#;
477 sub does_exist {
478 my ($name) = @_;
479 valid_name($name) or die "tried to query for project with invalid name $name!";
480 available($name);
482 sub available {
483 my ($name) = @_;
484 valid_name($name) or die "tried to query for project with invalid name $name!";
485 (-d $Girocco::Config::reporoot."/$name.git");