toolbox/projtool.pl: provide homepage option for create command
[girocco.git] / toolbox / projtool.pl
blobf643168117998bc2cbc9726380a1f0fdf5f9c821
1 #!/usr/bin/perl
3 # projtool.pl - command line Girocco project maintenance tool
4 # Copyright (C) 2016,2017,2020 Kyle J. McKay. All rights reserved.
5 # License GPLv2+: GNU GPL version 2 or later.
6 # www.gnu.org/licenses/gpl-2.0.html
7 # This is free software: you are free to change and redistribute it.
8 # There is NO WARRANTY, to the extent permitted by law.
10 use strict;
11 use warnings;
12 use vars qw($VERSION);
13 BEGIN {*VERSION = \'1.0.1'}
14 use File::Basename;
15 use Digest::MD5 qw(md5_hex);
16 use IO::Socket;
17 use Cwd qw(realpath);
18 use POSIX qw(strftime);
19 use lib "__BASEDIR__";
20 use Girocco::Config;
21 use Girocco::Util;
22 use Girocco::HashUtil;
23 use Girocco::CLIUtil;
24 use Girocco::Project;
25 use Girocco::User;
27 exit(&main(@ARGV)||0);
29 our $help;
30 BEGIN {$help = <<'HELP'}
31 Usage: %s [--quiet] <command> <options>
33 help [<command>]
34 show full help or just for <command> if given
36 list [--verbose] [--sort=lcname|name|owner|gid|no] [--owner] [<regexp>]
37 list all projects (default is --sort=lcname)
38 limit to project names matching <regex> if given
39 match <regex> against owner instead of project name with --owner
41 create [--force] [--no-alternates] [--orphan] [<option>...] <project>
42 create new project <project> (prompted)
43 <option> can be:
44 --no-alternates skip setup of objects/info/alternates
45 --orphan allow creation of subproject w/o a parent
46 -p use mkdir -p during --orphan creation
47 --no-password set password crypt to invalid value "unknown"
48 --no-owner leave the gitweb.owner config unset
49 --mirror=<url> create a mirror from <url>
50 --full-mirror mirror all refs
51 --push[=<list>] create a push project
52 --desc=<string> specify project description w/o prompt
53 --homepage=<url> specify project homepage URL w/o prompt
54 --defaults do no interactive prompting at all
55 Using --no-password skips the prompts for password, using
56 --no-owner skips the prompt for owner and using --mirror=<url>
57 or --push[=<list>] skips the prompts for mirror URL and
58 heads-only and push users. With --defaults if neither
59 --mirror=<url> nor --push[=<list>] is given then --push will
60 be implied. Using --desc=<string> will force a specific
61 description (including an empty string) and skip the prompt for
62 it. Otherwise a non-empty default description will always be
63 supplied in lieu of an empty or omitted description.
65 adopt [--force] [--type=mirror|push] [<option>...] <project> [<users>]
66 adopt project <project>
67 type of project is guessed if --type=<type> omitted
68 <users> is same as <newuserslist> for setusers command
69 <option> can be:
70 --dry-run do all the checks but don't perform adoption
71 --verbose show project info dump (useful with --dry-run)
72 --no-users no push users at all (<users> must be omitted)
73 --no-owner leave the gitweb.owner config totally unchanged
74 --owner=<val> set the gitweb.owner config to <val>
75 Both --no-owner and --owner=<val> may NOT be given, with neither
76 take owner from preexisting gitweb.owner else use admin setting.
77 For mirrors <users> is ignored otherwise if no <users> and no
78 --no-users option the push users list will consist of the single
79 user name matching the owner or empty if none or more than one.
80 With --dry-run <project> can be an absolute path to a git dir.
82 remove [--force] [--really-delete] [--keep-forks] <project>
83 remove project <project>
84 do not move to _recyclebin with --really-delete (just rm -rf)
85 remove projects with forks (by keeping forks) using --keep-forks
87 show <project>
88 show project <project>
90 listheads <project>
91 list all available heads for <project> and indicate current head
93 listtags [--verbose] <project>
94 list all ctags on project <project>
96 deltags <project> [-i] <tagstodel>
97 remove any ctags on project <project> present in <tagstodel>
98 <tagstodel> is space or comma separated list of tags to remove
99 with -i match against <tagstodel> without regard to letter case
101 addtags <project> <tagstoadd>
102 add ctags to project <project>
103 <tagstoadd> is space or comma separated list of tags to add
105 chpass [--force] <project> [random | unknown]
106 change project <project> password (prompted)
107 with "random" set to random password
108 with "unknown" set password hash to invalid value "unknown"
110 checkpw <project>
111 check project <project> password for a match (prompted)
113 gc [--force | --auto] [--redelta | --recompress] <project>
114 run the gc.sh script on project <project>
115 with --auto let the gc.sh script decide what to do
116 with --force cause a full gc to take place (force_gc=1)
117 with neither --auto nor --force do a mini or if needed a full gc
118 (in other words just touch .needsgc and run gc.sh)
119 with --redelta a full gc will use pack-objects --no-reuse-delta
120 with --recompress a full gc uses pack-objects --no-reuse-object
121 (--no-reuse-delta and --no-reuse-object are accepted as aliases)
122 unless the global --quiet option is given show_progress=1 is used
124 update [--force] [--quiet | --summary] <project>
125 run the update.sh script on project <project>
126 with --force cause a fetch to always take place (force_update=1)
127 with --quiet only show errors (show_progress is left unset)
128 with --summary show progress and ref summary (show_progress=1)
129 with neither --quiet nor --summary show it all (show_progress=2)
131 remirror [--force] <project>
132 initiate a remirror of project <project>
134 [set]owner [--force] <project> <newowner>
135 set project <project> owner to <newowner>
136 without "set" and only 1 arg, just show current project owner
138 [set]desc [--force] <project> <newdesc>
139 set project <project> description to <newdesc>
140 without "set" and only 1 arg, just show current project desc
142 [set]readme [--force] <project> <newsetting>
143 set project <project> readme to <newsetting>
144 <newsetting> is automatic|suppressed|-|[@]filename
145 without "set" and only 2 args, just show current readme setting
147 [set]head <project> <newhead>
148 set project <project> HEAD symbolic ref to <newhead>
149 without "set" and only 1 arg, just show current project HEAD
151 [set]bool [--force] <project> <flagname> <boolvalue>
152 set project <project> boolean <flagname> to <boolvalue>
153 <flagname> is cleanmirror|reverseorder|summaryonly|statusupdtaes
154 without "set" and only 2 args, just show current flag value
156 [set]hooks [--force] <project> local | global | <path>
157 set project <project> hookspath to local, global or <path>
158 without "set" and only 1 arg, just show current hookspath
160 [set]autogchack <project> <boolvalue> | unset
161 set project <project> autogchack to <boolvalue> or "unset" it
162 without "set" just show current autogchack setting if enabled
163 with "set" autogchack must be enabled in Config.pm for the
164 type of project and maintain-auto-gc-hack.sh is always run
166 [set]url [--force] <project> <urlname> <newurlvalue>
167 set project <project> url <urlname> to <newurlvalue>
168 <urlname> is baseurl|homepage|notifyjson
169 without "set" and only 2 args, just show current url value
171 [set]msgs [--force] <project> <msgsname> <eaddrlist>
172 set project <project> msgs <msgsname> to <addrlist>
173 <msgsname> is notifymail|notifytag
174 <eaddrlist> is space or comma separated list of email addresses
175 without "set" and only 2 args, just show current msgs value
177 [set]users [--force] <project> <newuserslist>
178 set push project <project> users list to <newuserslist>
179 <newuserslist> is space or comma separated list of user names
180 without "set" and only 1 arg, just show current users list
182 get <project> <fieldname>
183 show project <project> field <fieldname>
184 <fieldname> is owner|desc|readme|head|hooks|users
185 or <flagname>|autogchack|<urlname>|<msgsname>
187 set [--force] <project> <fieldname> <newfieldvalue>
188 set project <project> field <fieldname> to <newfieldvalue>
189 <fieldname> same as for get
190 <newfieldvalue> same as for corresponding set... command
191 HELP
193 our $quiet;
194 our $setopt;
195 sub die_usage {
196 my $sub = shift || diename;
197 if ($sub) {
198 die "Invalid arguments to $sub command -- try \"help\"\n";
199 } else {
200 die "Invalid arguments -- try \"help\"\n";
204 sub get_readme_desc {
205 my $rm = shift;
206 defined($rm) or $rm = '';
207 if (length($rm)) {
208 my $test = $rm;
209 $test =~ s/<!--(?:[^-]|(?:-(?!-)))*-->//gs;
210 $test =~ s/\s+//s;
211 return $test eq '' ? "suppressed" : "length " . length($rm);
212 } else {
213 return "automatic";
217 sub get_ctag_counts {
218 my $project = shift;
219 my $compact = shift;
220 my @ctags = ();
221 foreach ($project->get_ctag_names) {
222 my $val = 0;
223 my $ct;
224 if (open $ct, '<', $project->{path}."/ctags/$_") {
225 my $count = <$ct>;
226 close $ct;
227 defined $count or $count = '';
228 chomp $count;
229 $val = $count =~ /^[1-9]\d*$/ ? $count : 1;
231 if ($compact) {
232 if ($val == 1) {
233 push(@ctags, $_);
234 } elsif ($val > 1) {
235 push(@ctags, $_."(".$val.")");
237 } else {
238 push(@ctags, [$_, $val]) if $val;
241 @ctags;
244 sub get_clean_project {
245 my $project = get_project(@_);
246 delete $project->{loaded};
247 delete $project->{base_path};
248 delete $project->{ccrypt};
249 /^orig/i || !defined($project->{$_}) and delete $project->{$_} foreach keys %$project;
250 $project->{owner} = $project->{email}; delete $project->{email};
251 $project->{homepage} = $project->{hp}; delete $project->{hp};
252 $project->{baseurl} = $project->{url}; delete $project->{url};
253 if (defined($project->{path}) && $project->{path} ne "") {
254 my $rp = realpath($project->{path});
255 defined($rp) && $rp ne "" and $project->{realpath} = $rp;
256 if (-f "$rp/objects/info/packs") {
257 my $ipt = (stat _)[9];
258 defined($ipt) and $project->{infopackstime} =
259 strftime("%Y-%m-%d %H:%M:%S %z", localtime($ipt));
262 my $owner = $project->{owner};
263 if ($owner) {
264 $owner = lc($owner);
265 my @owner_users = map {$owner eq lc($$_[4]) ? $$_[1] : ()} get_all_users;
266 $project->{owner_users} = \@owner_users if @owner_users;
268 my $projname = $project->{name};
269 my @forks = grep {$$_[1] =~ m,^$projname/,} get_all_projects;
270 $project->{has_forks} = 1 if @forks;
271 $project->{has_alternates} = 1 if $project->has_alternates;
272 my @bundles = $project->bundles;
273 for (my $i = 0; $i < @bundles; ++$i) {
274 my $secs = $bundles[$i]->[0];
275 $bundles[$i]->[0] = strftime("%Y-%m-%d %H:%M:%S %z", localtime($secs));
276 my $sz = $bundles[$i]->[2];
277 1 while $sz =~ s/(?<=\d)(\d{3})(?:,|$)/,$1/g;
278 $bundles[$i]->[2] = $sz;
280 delete $project->{bundles};
281 $project->{bundles} = \@bundles if @bundles;
282 $project->{mirror} = 0 unless $project->{mirror};
283 $project->{is_empty} = 1 if $project->is_empty;
284 delete $project->{showpush} unless $project->{showpush};
285 delete $project->{users} if $project->{mirror};
286 delete $project->{baseurl} unless $project->{mirror};
287 delete $project->{banged} unless $project->{mirror};
288 delete $project->{lastrefresh} unless $project->{mirror};
289 delete $project->{cleanmirror} unless $project->{mirror};
290 delete $project->{statusupdates} unless $project->{mirror};
291 delete $project->{lastparentgc} unless $projname =~ m,/,;
292 unless ($project->{banged}) {
293 delete $project->{bangcount};
294 delete $project->{bangfirstfail};
295 delete $project->{bangmessagesent};
297 my $projhook = $project->_has_notifyhook;
298 if (defined($projhook) && $projhook ne "") {
299 $project->{notifyhook} = $projhook;
300 } else {
301 delete $project->{notifyhook};
303 $project->{README} = get_readme_desc($project->{README}) if exists($project->{README});
304 my @tags = get_ctag_counts($project, 1);
305 $project->{tags} = \@tags if @tags;
306 my $projconfig = read_config_file_hash($project->{path}."/config");
307 if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) {
308 my $ahp = $projconfig->{"core.hookspath"};
309 my $rahp = realpath($ahp);
310 my $lhp = $project->{path}."/hooks";
311 my $rlhp = realpath($lhp);
312 my $ghp = $Girocco::Config::reporoot."/_global/hooks";
313 my $rghp = realpath($ghp);
314 $project->{has_local_hooks} = 1 if
315 defined($rahp) && defined($rlhp) && $rahp eq $rlhp;
316 $project->{has_global_hooks} = 1 if
317 defined($rahp) && defined($rghp) && $rahp eq $rghp;
318 $project->{hookspath} = $ahp unless $ahp eq $lhp || $ahp eq $ghp;
320 $project;
323 sub clean_addrlist {
324 my %seen = ();
325 my @newlist = ();
326 foreach (split(/[,\s]+/, $_[0])) {
327 next unless $_;
328 $seen{lc($_)} = 1, push(@newlist, $_) unless $seen{lc($_)};
330 return join(($_[1]||","), @newlist);
333 sub valid_addrlist {
334 my $cleaned = clean_addrlist(join(" ", @_));
335 return 1 if $cleaned eq "";
336 valid_email_multi($cleaned) && length($cleaned) <= 512;
339 sub validate_users {
340 my ($userlist, $force, $nodie, $quiet) = @_;
341 my @newusers = ();
342 my $badlist = 0;
343 my %seenuser = ();
344 my $mobok = $Girocco::Config::mob && $Girocco::Config::mob eq "mob";
345 my %users = map({($$_[1] => $_)} get_all_users);
346 foreach (split(/[\s,]+/, $userlist)) {
347 if (exists($users{$_}) || $_ eq "everyone" || ($mobok && $_ eq "mob")) {
348 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
349 next;
351 if (Girocco::User::does_exist($_, 1)) {
352 if ($force) {
353 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
354 } else {
355 $badlist = 1;
356 warn "refusing to allow questionable user \"$_\" without --force\n" unless $nodie && $quiet;
358 next;
360 $badlist = 1;
361 warn "invalid user: \"$_\"\n" unless $nodie && $quiet
363 die if $badlist && !$nodie;
364 return @newusers;
367 sub is_default_desc {
368 # "Unnamed repository; edit this file 'description' to name the repository."
369 # "Unnamed repository; edit this file to name it for gitweb."
370 local $_ = shift;
371 return 0 unless defined($_);
372 /Unnamed\s+repository;/i && /\s+edit\s+this\s+file\s+/i && /\s+to\s+name\s+/i;
375 sub valid_desc {
376 my $test = shift;
377 chomp $test;
378 return 0 if $test =~ /[\r\n]/;
379 $test =~ s/\s\s+/ /g;
380 $test =~ s/^\s+//;
381 $test =~ s/\s+$//;
382 return $test ne '';
385 sub clean_desc {
386 my $desc = shift;
387 defined($desc) or $desc = '';
388 chomp $desc;
389 $desc = to_utf8($desc, 1);
390 $desc =~ s/\s\s+/ /g;
391 $desc =~ s/^\s+//;
392 $desc =~ s/\s+$//;
393 return $desc;
396 sub parse_options {
397 Girocco::CLIUtil::_parse_options(
398 sub {
399 warn((($_[0]eq'?')?"unrecognized":"missing argument for")." option \"$_[1]\"\n")
400 unless $quiet;
401 die_usage;
402 }, @_);
405 sub cmd_list {
406 my %sortsub = (
407 lcname => sub {lc($$a[1]) cmp lc($$b[1])},
408 name => sub {$$a[1] cmp $$b[1]},
409 gid => sub {$$a[3] <=> $$b[3]},
410 owner => sub {lc($$a[4]) cmp lc($$b[4]) || lc($$a[1]) cmp lc($$b[1])},
411 no => sub {$$a[0] <=> $$b[0]},
413 my $sortopt = 'lcname';
414 my ($verbose, $owner);
415 parse_options(":sort" => \$sortopt, verbose => \$verbose, owner => \$owner);
416 my $regex;
417 if (@ARGV) {
418 my $val = shift @ARGV;
419 $regex = qr($val) or die "bad regex \"$val\"\n";
421 !@ARGV && exists($sortsub{$sortopt}) or die_usage;
422 my $sortsub = $sortsub{$sortopt};
423 my $grepsub = defined($regex) ? ($owner ? sub {$$_[4] =~ /$regex/} : sub {$$_[1] =~ /$regex/}) : sub {1};
424 my @projects = sort($sortsub grep {&$grepsub} get_all_projects);
425 if ($verbose) {
426 print map(sprintf("%s\n", join(":", (@$_)[1..5])), @projects);
427 } else {
428 print map(sprintf("%s: %s\n", $$_[1], $$_[5] =~ /^:/ ? "<mirror>" : $$_[5]), @projects);
430 return 0;
433 sub cmd_create {
434 my ($force, $noalternates, $orphanok, $optp, $nopasswd, $noowner, $defaults, $ispush, $pushusers,
435 $ismirror, $desc, $fullmirror, $homepage);
436 parse_options(
437 force => \$force, "no-alternates" => \$noalternates, orphan => \$orphanok, p => \$optp,
438 "no-password" => \$nopasswd, "no-owner" => \$noowner, defaults => \$defaults,
439 "push" => \$ispush, ":push" => \$pushusers, ":mirror" => \$ismirror, ":desc" => \$desc,
440 ":description" => \$desc, "full-mirror" => \$fullmirror, ":homepage" => \$homepage);
441 @ARGV == 1 or die_usage;
442 !defined($pushusers) || defined($ispush) or $ispush = 1;
443 defined($ismirror) && $ismirror =~ /^\s*$/ and die "--mirror url must not be empty\n";
444 die "--mirror and --push are mutually exclusive options\n" if $ismirror && $ispush;
445 die "--full-mirror requires use of --mirror=<url> option\n" if $fullmirror && !$ismirror;
446 !$defaults || defined($ispush) || defined($ismirror) or $ispush = 1;
447 !$defaults || defined($nopasswd) or $nopasswd = 1;
448 !$defaults || defined($noowner) or $noowner = 1;
449 !defined($ispush) || defined($pushusers) or $pushusers = "";
450 my $projname = $ARGV[0];
451 $projname =~ s/\.git$//i;
452 Girocco::Project::does_exist($projname, 1) and die "Project already exists: \"$projname\"\n";
453 if (!Girocco::Project::valid_name($projname, $orphanok, $optp)) {
454 warn "Refusing to create orphan project without --orphan\n"
455 if !$quiet && !$orphanok && Girocco::Project::valid_name($projname, 1, 1);
456 warn "Required orphan parent directory does not exist (use -p): ",
457 $Girocco::Config::reporoot.'/'.Girocco::Project::get_forkee_name($projname), "\n"
458 if !$quiet && $orphanok && Girocco::Project::valid_name($projname, 1, 1);
459 die "Invalid project name: \"$projname\"\n";
461 my ($forkee, $project) = ($projname =~ m#^(.*/)?([^/]+)$#);
462 my $newtype = $forkee ? 'fork' : 'project';
463 if (length($project) > 64) {
464 die "The $newtype name is longer than 64 characters. Do you really need that much?\n"
465 unless $force;
466 warn "Allowing $newtype name longer than 64 characters with --force\n" unless $quiet;
468 unless ($Girocco::Config::push || $Girocco::Config::mirror) {
469 die "Project creation disabled (no mirrors or push projects allowed)\n" unless $force;
470 warn "Continuing with --force even though both push and mirror projects are disabled\n" unless $quiet;
472 print "Enter settings for new project \"$projname\"\n" unless $defaults;
473 my %settings = ();
474 $settings{noalternates} = $noalternates;
475 if ($nopasswd) {
476 $settings{crypt} = "unknown";
477 } else {
478 my $np1 = prompt_noecho_nl_or_die("Admin password for project $projname (echo is off)");
479 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
480 my $np2 = prompt_noecho_nl_or_die("Retype admin password for project $projname");
481 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
482 "the admin passwords you have entered do not match each other.\n";
483 $settings{crypt} = scrypt_sha1($np1);
485 my $owner = "";
486 unless ($noowner) {
487 $owner = prompt_or_die("Owner/email name for project $projname");
488 unless (valid_email($owner)) {
489 unless ($force) {
490 warn "Your email sure looks weird...?\n";
491 redo;
493 warn "Allowing invalid email with --force\n" unless $quiet;
495 if (length($owner) > 96) {
496 unless ($force) {
497 warn "Your email is longer than 96 characters. Do you really need that much?\n";
498 redo;
500 warn "Allowing email longer than 96 characters with --force\n" unless $quiet;
503 $settings{email} = $owner;
504 my $baseurl = "";
505 my $checkmirror = sub {
506 my $checkurl = shift;
507 unless (valid_repo_url($checkurl)) {
508 unless ($force) {
509 warn "Invalid mirror URL: \"$checkurl\"\n";
510 return undef;
512 warn "Allowing invalid mirror URL with --force\n" unless $quiet;
514 if ($Girocco::Config::restrict_mirror_hosts) {
515 my $mh = extract_url_hostname($checkurl);
516 unless (is_dns_hostname($mh)) {
517 unless ($force) {
518 warn "Invalid non-DNS mirror URL: \"$checkurl\"\n";
519 return undef;
521 warn "Allowing non-DNS mirror URL with --force\n" unless $quiet;
523 if (is_our_hostname($mh)) {
524 unless ($force) {
525 warn "Invalid same-host mirror URL: \"$checkurl\"\n";
526 return undef;
528 warn "Allowing same-host mirror URL with --force\n" unless $quiet;
531 return $checkurl;
533 if ($ispush || $ismirror) {
534 !$ispush || $force || $Girocco::Config::push or
535 die "Push projects are disabled, create a mirror (or use --force)\n";
536 !$ismirror || $force || $Girocco::Config::mirror or
537 die "Mirror projects are disabled, create a push project (or use --force)\n";
538 if ($ismirror) {
539 &$checkmirror($ismirror) or die "Invalid --mirror URL\n";
540 $baseurl = $ismirror;
541 $settings{url} = $baseurl;
542 $settings{cleanmirror} = $fullmirror ? 0 : 1;
543 } else {
544 my @newusers = ();
545 if ($pushusers !~ /^[\s,]*$/) {
546 eval {@newusers = validate_users($pushusers, $force); 1;} or
547 die "Invalid --push user list\n";
549 $settings{users} = \@newusers;
551 } elsif ($force || $Girocco::Config::mirror) {{
552 if ($force || $Girocco::Config::push) {
553 $baseurl = prompt_or_die("URL to mirror from (leave blank for push project)", "");
554 } else {{
555 $baseurl = prompt_or_die("URL to mirror from");
556 unless ($baseurl ne "") {
557 warn "Push projects are disabled, you must enter a mirror URL (or use --force)\n";
558 redo;
561 if ($baseurl ne "") {
562 &$checkmirror($baseurl) or redo;
563 $settings{url} = $baseurl;
564 $settings{cleanmirror} =
565 ynprompt_or_die("Mirror only heads, tags and notes (Y/n)", "Yes");
568 my $mirror = ($baseurl eq "") ? 0 : 1;
569 my $checkdesc = sub {
570 my $d = shift;
571 if (length($d) > 1024) {
572 unless ($force) {
573 warn "Short description length greater than 1024 characters!\n";
574 return undef;
576 warn "Allowing short description length greater than 1024 characters\n" unless $quiet;
578 return $d;
580 if (defined($desc)) {
581 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
582 $desc eq "" || &$checkdesc($desc) or
583 die "Invalid --desc description\n";
584 } elsif (!$defaults) {
585 $desc = prompt_or_die("Short description", "");
586 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
587 $desc eq "" || &$checkdesc($desc) or redo;
588 $desc = undef if $desc eq "";
590 defined($desc) or $desc = $mirror ? "Mirror of $baseurl" : "Push project $projname";
591 $settings{desc} = $desc;
592 my $checkhp = sub {
593 my $hpurl = shift;
594 unless (valid_web_url($hpurl)) {
595 unless ($force) {
596 warn "Invalid home page URL: \"$hpurl\"\n";
597 return undef;
599 warn "Allowing invalid home page URL with --force\n" unless $quiet;
601 return $hpurl;
603 if (defined($homepage)) {
604 $homepage =~ s/^\s+//; $homepage =~ s/\s+$//;
605 $homepage eq "" || &$checkhp($homepage) or
606 die "Invalid --homepage URL\n";
607 } elsif (!$defaults) {
608 $homepage = prompt_or_die("Home page URL", "");
609 $homepage =~ s/^\s+//; $homepage =~ s/\s+$//;
610 $homepage eq "" || &$checkhp($homepage) or redo;
611 $homepage = undef if $homepage eq "";
613 $settings{hp} = $homepage;
614 my $jsonurl = "";
615 if (!$defaults) {
616 $jsonurl = prompt_or_die("JSON notify POST URL", "");
617 if ($jsonurl ne "" && !valid_web_url($jsonurl)) {
618 unless ($force) {
619 warn "Invalid JSON notify POST URL: \$jsonurl\"\n";
620 redo;
622 warn "Allowing invalid JSON notify POST URL with --force\n" unless $quiet;
625 $settings{notifyjson} = $jsonurl;
626 my $commitaddrs = "";
627 if (!$defaults) {
628 $commitaddrs = clean_addrlist(prompt_or_die("Commit notify email addr(s)", ""));
629 if ($commitaddrs ne "" && !valid_addrlist($commitaddrs)) {
630 unless ($force) {
631 warn"invalid commit notify email address list (use --force to accept): \"$commitaddrs\"\n";
632 redo;
634 warn "using invalid commit notify email address list with --force\n" unless $quiet;
637 $settings{notifymail} = $commitaddrs;
638 $settings{reverseorder} = 1;
639 $settings{reverseorder} = ynprompt_or_die("Oldest-to-newest commit order in emails", "Yes")
640 if !$defaults && $commitaddrs ne "";
641 $settings{summaryonly} = ynprompt_or_die("Summary only (no diff) in emails", "No")
642 if !$defaults && $commitaddrs ne "";
643 my $tagaddrs = "";
644 if (!$defaults) {
645 $tagaddrs = clean_addrlist(prompt_or_die("Tag notify email addr(s)", ""));
646 if ($tagaddrs ne "" && !valid_addrlist($tagaddrs)) {
647 unless ($force) {
648 warn"invalid tag notify email address list (use --force to accept): \"$tagaddrs\"\n";
649 redo;
651 warn "using invalid tag notify email address list with --force\n" unless $quiet;
654 $settings{notifytag} = $tagaddrs;
655 if (!$mirror && !$ispush) {
656 my @newusers = ();
658 my $userlist = prompt_or_die("Push users", join(",", @newusers));
659 eval {@newusers = validate_users($userlist, $force); 1;} or redo;
661 $settings{users} = \@newusers;
663 my $newproj = Girocco::Project->ghost($projname, $mirror, $orphanok, $optp)
664 or die "Girocco::Project->ghost call failed\n";
665 my ($k, $v);
666 $newproj->{$k} = $v while ($k, $v) = each(%settings);
667 my $killowner = sub {
668 system($Girocco::Config::git_bin, '--git-dir='.$newproj->{path},
669 'config', '--unset', "gitweb.owner");
671 if ($mirror) {
672 $newproj->premirror or die "Girocco::Project->premirror failed\n";
673 !$noowner or &$killowner;
674 $newproj->clone or die "Girocco::Project->clone failed\n";
675 warn "Project $projname created and cloning successfully initiated.\n"
676 unless $quiet;
677 } else {
678 $newproj->conjure or die "Girocco::Project->conjure failed\n";
679 !$noowner or &$killowner;
680 warn "New push project fork is empty due to use of --no-alternates\n"
681 if !$quiet && $projname =~ m,/, && $noalternates;
682 warn "Project $projname successfully created.\n" unless $quiet;
684 return 0;
687 sub git_config {
688 my $gd = shift;
689 system($Girocco::Config::git_bin, "--git-dir=$gd", 'config', @_) == 0
690 or die "\"git --git-dir='$gd' config ".join(" ", @_)."\" failed.\n";
693 sub cmd_adopt {
694 my ($force, $type, $nousers, $dryrun, $noowner, $owner, $users, $verbose);
695 parse_options(force => \$force, ":type" => \$type, "no-users" => \$nousers, "dry-run" => \$dryrun,
696 "no-owner" => \$noowner,":owner" => \$owner, quiet => \$quiet, q =>\$quiet, verbose => \$verbose);
697 @ARGV or die "Please give project name on command line.\n";
698 my $projname = shift @ARGV;
699 (!$noowner || !defined($owner)) && (!$nousers || !@ARGV) or die_usage;
700 !defined($type) || $type eq "mirror" || $type eq "push" or die_usage;
701 defined($type) or $type = "";
702 my $projdir;
703 if ($dryrun && $projname =~ m,^/[^.\s/\\:], && is_git_dir(realpath($projname))) {
704 $projdir = realpath($projname);
705 $projname = $projdir;
706 $projname =~ s/\.git$//i;
707 $projname =~ s,/+$,,;
708 $projname =~ s,^.*/,,;
709 $projname ne "" or $projname = $projdir;
710 } else {
711 $projname =~ s/\.git$//i;
712 $projname ne "" or die "Invalid project name \"\".\n";
713 unless (Girocco::Project::does_exist($projname, 1)) {
714 Girocco::Project::valid_name($projname, 1, 1)
715 or die "Invalid project name \"$projname\".\n";
716 die "No such project to adopt: $projname\n";
718 defined(Girocco::Project->load($projname))
719 and die "Project already known (no need to adopt): $projname\n";
720 $projdir = $Girocco::Config::reporoot . "/" . $projname . ".git";
721 is_git_dir($projdir) or die "Not a git directory: \"$projdir\"\n";
723 my $config = read_config_file($projdir . "/config");
724 my %config = ();
725 %config = map {($$_[0], defined($$_[1])?$$_[1]:"true")} @$config if defined($config);
726 git_bool($config{"core.bare"}) or die "Not a bare git repository: \"$projdir\"\n";
727 defined(read_HEAD_symref($projdir)) or die "Project with non-symbolic HEAD ref: \"$projdir\"\n";
728 @ARGV and $users = [validate_users(join(" ", @ARGV), $force, 1, $quiet)];
729 my $desc = "";
730 if (-e "$projdir/description") {
731 open my $fd, '<', "$projdir/description" or die "Cannot open \"$projdir/description\": $!\n";
733 local $/;
734 $desc = <$fd>;
736 close $fd;
737 defined $desc or $desc = "";
738 chomp $desc;
739 $desc = to_utf8($desc, 1);
740 is_default_desc($desc) and $desc = "";
741 if ($desc ne "" && !valid_desc($desc)) {
742 die "invalid 'description' file contents (use --force to accept): \"$desc\"\n"
743 unless $force;
744 warn "using invalid 'description' file contents with --force\n" unless $quiet;
746 $desc = clean_desc($desc);
747 if (length($desc) > 1024) {
748 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
749 unless $force;
750 warn "using longer than 1024 char description with --force\n" unless $quiet;
753 my $readme = "";
754 if (-e "$projdir/README.html") {
755 open my $fd, '<', "$projdir/README.html" or die "Cannot open \"$projdir/README.html\": $!\n";
757 local $/;
758 $readme = <$fd>;
760 close $fd;
761 defined $readme or $readme = "";
762 $readme = to_utf8($readme, 1);
763 $readme =~ s/\r\n?/\n/gs;
764 $readme =~ s/^\s+//s;
765 $readme =~ s/\s+$//s;
766 $readme eq "" or $readme .= "\n";
767 if (length($readme) > 8192) {
768 die "readme greater than 8192 chars is too long (use --force to override)\n"
769 unless $force;
770 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
772 my $rd = get_readme_desc($readme);
773 if ($rd ne "automatic" && $rd ne "suppressed") {
774 my $xmllint = qx(sh -c 'command -v xmllint'); chomp $xmllint;
775 if (-f $xmllint && -x $xmllint) {
776 my $dummy = {README => $readme};
777 my ($cnt, $err) = Girocco::Project::_lint_readme($dummy, 0);
778 if ($cnt) {
779 my $msg = "xmllint: $cnt error";
780 $msg .= "s" unless $cnt == 1;
781 print STDERR "$msg\n", "-" x length($msg), "\n", $err
782 unless $force && $quiet;
783 exit(255) unless $force;
784 warn "$projname: using invalid raw HTML with --force\n" unless $quiet;
786 } else {
787 die "xmllint not available, refusing to use raw HTML without --force\n"
788 unless $force;
789 warn "xmllint not available using unchecked raw HTML with --force\n" unless $quiet;
793 # Inspect any remotes now
794 # Yes, Virginia, remote urls can be multi-valued
795 my %remotes = ();
796 foreach (@$config) {
797 my ($k,$v) = @$_;
798 next unless $k =~ /^remote\.([^\/].*?)\.([^.]+)$/; # remote name cannot start with "/"
799 my ($name, $subkey) = ($1, $2);
800 $remotes{$name}->{skip} = git_bool($v,1), next if $subkey eq "skipdefaultupdate" || $subkey eq "skipfetchall";
801 $remotes{$name}->{mirror} = git_bool($v,1), next if $subkey eq "mirror"; # we might want this
802 $remotes{$name}->{vcs} = $v, next if defined($v) && $v !~ /^\s*$/ && $subkey eq "vcs";
803 push(@{$remotes{$name}->{$subkey}}, $v), next if defined($v) && $v !~ /^\s*$/ &&
804 ($subkey eq "url" || $subkey eq "fetch" || $subkey eq "push" || $subkey eq "pushurl");
806 # remotes.default is the default remote group to fetch for "git remote update" otherwise --all
807 # the remote names in a group are separated by runs of [ \t\n] characters
808 # remote names "", ".", ".." and any name starting with "/" are invalid
809 # a remote with no url or vcs setting is not considered valid
810 my @check = ();
811 my $usingall = 0;
812 if (exists($config{"remotes.default"})) {
813 foreach (split(/[ \t\n]+/, $config{"remotes.default"})) {
814 next unless exists($remotes{$_});
815 my $rmt = $remotes{$_};
816 next if !exists($rmt->{url}) && !$rmt->{vcs};
817 push(@check, $_);
819 } else {
820 $usingall = 1;
821 my %seenrmt = ();
822 foreach (@$config) {
823 my ($k,$v) = @$_;
824 next unless $k =~ /^remote\.([^\/].*?)\.[^.]+$/;
825 next if $seenrmt{$1};
826 $seenrmt{$1} = 1;
827 next unless exists($remotes{$1});
828 my $rmt = $remotes{$1};
829 next if $rmt->{skip} || (!exists($rmt->{url}) && !$rmt->{vcs});
830 push(@check, $1);
833 my @needskip = (); # remotes that need skipDefaultUpdate set to true
834 my $foundvcs = 0;
835 my $foundfetch = 0;
836 my $foundfetchwithmirror = 0;
837 foreach (@check) {
838 my $rmt = $remotes{$_};
839 push(@needskip, $_) if $usingall && !exists($rmt->{fetch});
840 next unless exists($rmt->{fetch});
841 ++$foundfetch;
842 ++$foundfetchwithmirror if $rmt->{mirror};
843 ++$foundvcs if $rmt->{vcs} || (exists($rmt->{url}) && $rmt->{url}->[0] =~ /^[a-zA-Z0-9][a-zA-Z0-9+.-]*::/);
845 # if we have $foundvcs then we need to explicitly set fetch.prune to false
846 # if we have $foundfetch > 1 then we need to explicitly set fetch.prune to false
847 my $neednoprune = !exists($config{"fetch.prune"}) && ($foundvcs || $foundfetch > 1);
848 my $baseurl = "";
849 my $needfakeorigin = 0; # if true we need to set remote.origin.skipDefaultUpdate = true
850 # if remote "origin" exists we always pick up its first url or use ""
851 if (exists($remotes{origin})) {
852 my $rmt = $remotes{origin};
853 $baseurl = exists($rmt->{url}) ? $rmt->{url}->[0] : "";
854 $needfakeorigin = !exists($rmt->{url}) && !$rmt->{vcs} && !$rmt->{skip};
855 } else {
856 $needfakeorigin = 1;
857 # get the first url of the @check remotes
858 foreach (@check) {
859 my $rmt = $remotes{$_};
860 next unless exists($rmt->{url});
861 next unless defined($rmt->{url}->[0]) && $rmt->{url}->[0] ne "";
862 $baseurl = $rmt->{url}->[0];
863 last;
866 my $makemirror = $type eq "mirror" || ($type eq "" && $foundfetch);
868 # If we have $foundfetch we want to make a mirror but complain if
869 # we $foundfetchwithmirror as well unless we have --type=mirror.
870 # Warn if we have --type=push and $foundfetch and !$foundfetchwithmirror.
871 # Warn if we need to set fetch.prune=false when making a mirror
872 # Warn if we need to create remote.origin.skipDefaultUpdate when making a mirror
873 # Complain if @needskip AND !$usingall (warn with --force but don't set skip)
874 # Warn if $usingall and any @needskip (and set them) if making a mirror
875 # Warn if making a mirror and $baseurl eq ""
876 # Warn if we have --type=mirror and !$foundfetch
878 if ($makemirror) {
879 warn "No base URL to mirror from for adopted \"$projname\"\n" unless $quiet || $baseurl ne "";
880 warn "Adopting mirror \"$projname\" without any fetch remotes\n" unless $quiet || $foundfetch;
881 if ($foundfetchwithmirror) {
882 warn "Refusing to adopt mirror \"$projname\" with active remote.<name>.mirror=true remote(s)\n".
883 "(Use --type=mirror to override)\n"
884 unless $type eq "mirror";
885 exit(255) unless $type eq "mirror" || $dryrun;
886 warn "Adopting mirror \"$projname\" with active remote.<name>.mirror=true remotes\n"
887 unless $quiet || $type ne "mirror";
889 warn "Setting explicit fetch.prune=false for adoption of mirror \"$projname\"\n"
890 if !$quiet && $neednoprune;
891 warn "Setting remote.origin.skipDefaultUpdate=true for adoption of mirror \"$projname\"\n"
892 if !$quiet && $needfakeorigin;
893 if (!$usingall && @needskip) {
894 warn "Refusing to adopt mirror empty fetch remote(s) (override with --force)\n"
895 unless $force;
896 exit(255) unless $force || $dryrun;
897 warn "Adopting mirror with empty fetch remote(s) with --force\n"
898 unless $quiet || !$force;
900 warn "Will set skipDefaultUpdate=true on non-fetch remote(s)\n" if !$quiet && $usingall && @needskip;
901 warn "Adopting mirror with base URL \"$baseurl\"\n" unless $quiet || $baseurl eq "";
902 } else {
903 warn "Adopting push \"$projname\" but active non-mirror remotes are present\n"
904 if !$quiet && $foundfetch && !$foundfetchwithmirror;
907 if (!$noowner && !defined($owner)) {
908 # Select the owner
909 $owner = $config{"gitweb.owner"};
910 if (!defined($owner) || $owner eq "") {
911 $owner = $Girocco::Config::admin;
912 warn "Using owner \"$owner\" for adopted project\n" unless $quiet;
915 if (!$nousers && !$makemirror && !defined($users)) {
916 # select user list for push project
917 my $findowner = $owner;
918 defined($findowner) or $findowner = $config{"gitweb.owner"};
919 $findowner = lc($findowner) if defined($findowner);
920 my @owner_users = ();
921 @owner_users = map {$findowner eq lc($$_[4]) ? $$_[1] : ()} get_all_users
922 if defined($findowner) && $findowner ne "";
923 if (@owner_users <= 1) {
924 $users = \@owner_users;
925 warn "No users found that match owner \"$findowner\"\n" unless @owner_users || $quiet;
926 } else {
927 $users = [];
928 warn "Found ".scalar(@owner_users)." users for owner \"$findowner\" (" .
929 join(" ", @owner_users) . ") not setting any\n" unless $quiet;
932 defined($users) or $users = [];
934 # Warn if we preserve an existing receive.denyNonFastForwards or receive.denyDeleteCurrent setting
935 # Complain if core.logallrefupdates or logs subdir exists and contains any files (allow with --force
936 # and warn about preserving the setting)
938 warn "Preserving existing receive.denyNonFastForwards=true\n"
939 if !$quiet && git_bool($config{"receive.denynonfastforwards"});
940 warn "Preserving existing receive.denyDeleteCurrent=$config{'receive.denydeletecurrent'}\n"
941 if !$quiet && exists($config{"receive.denydeletecurrent"}) &&
942 $config{"receive.denydeletecurrent"} ne "warn";
944 my $reflogfiles = Girocco::Project::_contains_files("$projdir/logs");
945 my $reflogactive = git_bool($config{"core.logallrefupdates"});
946 if ($reflogactive || $reflogfiles) {
947 warn "Refusing to adopt \"$projname\" with active ref logs without --force\n" if $reflogfiles && !$force;
948 warn "Refusing to adopt \"$projname\" with core.logAllRefUpdates=true without --force\n" if $reflogactive && !$force;
949 exit(255) unless $force || $dryrun;
950 warn "Adopting \"$projname\" with active ref logs with --force\n" unless $quiet || ($reflogfiles && !$force);
951 warn "Adopting \"$projname\" with core.logAllRefUpdates=true with --force\n" unless $quiet || ($reflogactive && !$force);
954 return 0 if $dryrun && !$verbose;
956 my $newproj = eval {Girocco::Project->ghost($projname, $makemirror, 1, $dryrun)};
957 defined($newproj) or die "Girocco::Project::ghost failed: $@\n";
958 $newproj->{desc} = $desc;
959 $newproj->{README} = $readme;
960 $newproj->{url} = $baseurl if $makemirror || exists($config{"gitweb.baseurl"});
961 $newproj->{email} = $owner if defined($owner);
962 $newproj->{users} = $users;
963 $newproj->{crypt} = "unknown";
964 $newproj->{reverseorder} = 1 unless exists($config{"hooks.reverseorder"});
965 $newproj->{summaryonly} = 1 unless exists($config{"hooks.summaryonly"});
966 my $dummy = bless {}, "Girocco::Project";
967 $dummy->{path} = "$projdir";
968 $dummy->{configfilehash} = \%config;
969 $dummy->_properties_load;
970 delete $dummy->{origurl};
971 foreach my $k (keys(%$dummy)) {
972 $newproj->{$k} = $dummy->{$k}
973 if exists($dummy->{$k}) && !exists($newproj->{$k});
976 if ($verbose) {
977 use Data::Dumper;
978 my %info = %$newproj;
979 $info{README} = get_readme_desc($info{README}) if exists($info{README});
980 my $d = Data::Dumper->new([\%info], ['*'.$newproj->{name}]);
981 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
982 print $d->Dump([\%info], ['*'.$newproj->{name}]);
984 return 0 if $dryrun;
986 # Make any changes as needed for @needskip, $neednoprune and $needfakeorigin
987 if ($makemirror) {
988 git_config($projdir, "fetch.prune", "false") if $neednoprune;
989 git_config($projdir, "remote.origin.skipDefaultUpdate", "true") if $needfakeorigin;
990 if ($usingall && @needskip) {
991 git_config($projdir, "remote.$_.skipDefaultUpdate", "true") foreach @needskip;
995 # Perform the actual adoption
996 $newproj->adopt or die "Girocco::Project::adopt failed\n";
998 # Perhaps restore core.logAllRefUpdates, receive.denyNonFastForwards and receive.denyDeleteCurrent
999 git_config($projdir, "receive.denyNonFastForwards", "true")
1000 if git_bool($config{"receive.denynonfastforwards"});
1001 git_config($projdir, "receive.denyDeleteCurrent", $config{"receive.denydeletecurrent"})
1002 if exists($config{"receive.denydeletecurrent"}) &&
1003 $config{"receive.denydeletecurrent"} ne "warn";
1004 git_config($projdir, "core.logAllRefUpdates", "true")
1005 if $reflogactive;
1007 # Success
1008 if ($makemirror) {
1009 warn "Mirror project \"$projname\" successfully adopted.\n" unless $quiet;
1010 } else {
1011 warn "Push project \"$projname\" successfully adopted.\n" unless $quiet;
1013 return 0;
1016 sub cmd_remove {
1017 my ($force, $reallydel, $keepforks);
1018 parse_options(force => \$force, "really-delete" => \$reallydel,
1019 "keep-forks" => \$keepforks, quiet => \$quiet, q =>\$quiet);
1020 @ARGV or die "Please give project name on command line.\n";
1021 @ARGV == 1 or die_usage;
1022 my $project = get_project($ARGV[0]);
1023 my $projname = $project->{name};
1024 my $isempty = !$project->{mirror} && $project->is_empty;
1025 if (!$project->{mirror} && !$isempty && $reallydel) {
1026 die "refusing to remove and delete non-empty push project without --force: $projname\n" unless $force;
1027 warn "allowing removal and deletion of non-empty push project with --force\n" unless $quiet;
1029 my $altwarn;
1030 my $removenogc;
1031 if ($project->has_forks) {
1032 die "refusing to remove project with forks (use --keep-forks): $projname\n" unless $keepforks;
1033 warn "allowing removal of forked project while preserving its forks with --keep-forks\n" unless $quiet;
1034 # Run pseudo GC on that repository so that objects don't get lost within forks
1035 my $basedir = $Girocco::Config::basedir;
1036 my $projdir = $project->{path};
1037 warn "We have to run pseudo GC on the repo so that the forks don't lose data. Hang on...\n" unless $quiet;
1038 my $nogcrunning = sub {
1039 die "Error: GC appears to be currently running on $projname\n"
1040 if -e "$projdir/gc.pid" || -e "$projdir/.gc_in_progress";
1042 &$nogcrunning;
1043 $removenogc = ! -e "$projdir/.nogc";
1044 recreate_file("$projdir/.nogc") if $removenogc;
1045 die "unable to create \"$projdir/.nogc\"\n" unless -e "$projdir/.nogc";
1046 delete $ENV{show_progress};
1047 $ENV{'show_progress'} = 1 unless $quiet;
1048 sleep 2; # *cough*
1049 &$nogcrunning;
1050 system("$basedir/toolbox/perform-pre-gc-linking.sh", "--include-packs", $projname) == 0
1051 or die "Running pseudo GC on project $projname failed\n";
1052 $altwarn = 1;
1054 my $archived;
1055 if (!$project->{mirror} && !$isempty && !$reallydel) {
1056 $archived = $project->archive_and_delete;
1057 unlink("$archived/.nogc") if $removenogc && defined($archived) && $archived ne "";
1058 } else {
1059 $project->delete;
1061 warn "Project '$projname' removed from $Girocco::Config::name" .
1062 ($archived ? ", backup in '$archived'" : "") .".\n" unless $quiet;
1063 warn "Retained forks may now have unwanted objects/info/alternates lines\n" if $altwarn && !$quiet;
1064 return 0;
1067 sub cmd_show {
1068 use Data::Dumper;
1069 @ARGV == 1 or die_usage;
1070 my $project = get_clean_project($ARGV[0]);
1071 my %info = %$project;
1072 my $d = Data::Dumper->new([\%info], ['*'.$project->{name}]);
1073 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
1074 print $d->Dump([\%info], ['*'.$project->{name}]);
1075 return 0;
1078 sub cmd_listheads {
1079 @ARGV == 1 or die_usage;
1080 my $project = get_project($ARGV[0]);
1081 my @heads = sort({lc($a) cmp lc($b)} $project->get_heads);
1082 my $cur = $project->{HEAD};
1083 defined($cur) or $cur = '';
1084 my $curmark = '*';
1085 my $headhash = get_git("--git-dir=$project->{path}", 'rev-parse', '--quiet', '--verify', 'HEAD');
1086 defined($headhash) or $headhash = '';
1087 chomp $headhash;
1088 $headhash or $curmark = '!';
1089 foreach (@heads) {
1090 my $mark = $_ eq $cur ? $curmark : ' ';
1091 print "$mark $_\n";
1093 return 0;
1096 sub cmd_listtags {
1097 my $vcnt = 0;
1098 shift(@ARGV), $vcnt=1 if @ARGV && ($ARGV[0] eq '--verbose' || $ARGV[0] eq '-v');
1099 @ARGV == 1 or die_usage;
1100 my $project = get_project($ARGV[0]);
1101 if ($vcnt) {
1102 print map("$$_[0]\t$$_[1]\n", get_ctag_counts($project));
1103 } else {
1104 print map("$_\n", $project->get_ctag_names);
1106 return 0;
1109 sub cmd_deltags {
1110 my $ic = 0;
1111 shift(@ARGV), $ic=1 if @ARGV && $ARGV[0] =~ /^(?:--?ignore-case|-i)$/i;
1112 @ARGV >= 2 or die_usage;
1113 my $project = get_project(shift @ARGV);
1114 my %curtags;
1115 if ($ic) {
1116 push(@{$curtags{lc($_)}}, $_) foreach $project->get_ctag_names;
1117 } else {
1118 push(@{$curtags{$_}}, $_) foreach $project->get_ctag_names;
1120 my @deltags = ();
1121 my %seentag = ();
1122 my $ctags = join(" ", @ARGV);
1123 $ctags = lc($ctags) if $ic;
1124 foreach (split(/[\s,]+/, $ctags)) {
1125 next unless exists($curtags{$_});
1126 $seentag{$_}=1, push(@deltags, $_) unless $seentag{$_};
1128 if (!@deltags) {
1129 warn $project->{name}, ": skipping removal of only non-existent tags\n" unless $quiet;
1130 } else {
1131 # Avoid touching anything other than the ctags
1132 foreach my $tg (@deltags) {
1133 $project->delete_ctag($_) foreach @{$curtags{$tg}};
1135 $project->_set_changed;
1136 $project->_set_forkchange;
1137 warn $project->{name}, ": specified tags have been removed\n" unless $quiet;
1139 return 0;
1142 sub cmd_addtags {
1143 @ARGV >= 2 or die_usage;
1144 my $project = get_project(shift @ARGV);
1145 my $ctags = join(" ", @ARGV);
1146 $ctags =~ /[^, a-zA-Z0-9:.+#_-]/ and
1147 die "Content tag(s) \"$ctags\" contain(s) evil character(s).\n";
1148 my $oldmask = umask();
1149 umask($oldmask & ~0060);
1150 my $changed = 0;
1151 foreach (split(/[\s,]+/, $ctags)) {
1152 ++$changed if $project->add_ctag($_, 1);
1154 if ($changed) {
1155 $project->_set_changed;
1156 $project->_set_forkchange;
1158 umask($oldmask);
1159 my $cnt = ($changed == 1) ? "1 content tag has" : $changed . " content tags have";
1160 warn $project->{name}, ": $cnt been added/updated\n" unless $quiet;
1161 return 0;
1164 sub _get_random_val {
1165 my $p = shift;
1166 my $md5;
1168 no warnings;
1169 $md5 = md5_hex(time . $$ . rand() . join(':',%$p));
1171 $md5;
1174 sub cmd_chpass {
1175 my $force = 0;
1176 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1177 my $random = undef;
1178 pop(@ARGV), $random=lc($ARGV[1]) if @ARGV==2 && $ARGV[1] =~ /^(?:random|unknown)$/i;
1179 @ARGV == 1 or die_usage;
1180 my $project = get_project($ARGV[0]);
1181 die "refusing to change locked password of project \"$ARGV[0]\" without --force\n"
1182 if $project->is_password_locked;
1183 my ($newpw, $rmsg);
1184 if ($random) {
1185 if ($random eq "random") {
1186 die "refusing to set random password without --force\n" unless $force;
1187 $rmsg = "set to random value";
1188 $newpw = _get_random_val($project);
1189 } else {
1190 die "refusing to set password hash to '$random' without --force\n" unless $force;
1191 $rmsg = "hash set to '$random'";
1192 $newpw = $random;
1194 } else {
1195 $rmsg = "updated";
1196 if (-t STDIN) {
1197 print "Changing admin password for project $ARGV[0]\n";
1198 my $np1 = prompt_noecho_nl_or_die("New password for project $ARGV[0] (echo is off)");
1199 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1200 my $np2 = prompt_noecho_nl_or_die("Retype new password for project $ARGV[0]");
1201 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
1202 "the admin passwords you have entered do not match each other.\n";
1203 $newpw = $np1;
1204 } else {
1205 $newpw = <STDIN>;
1206 defined($newpw) or die "missing new password on STDIN\n";
1207 chomp($newpw);
1210 $newpw ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1211 my $old = $project->{crypt};
1212 $project->{crypt} = (defined($random) && $random ne "random") ? $newpw : scrypt_sha1($newpw);
1213 if (defined($old) && $old eq $project->{crypt}) {
1214 warn $project->{name}, ": skipping update of password hash to same value\n" unless $quiet;
1215 } else {
1216 # Avoid touching anything other than the password hash
1217 $project->_group_update;
1218 warn $project->{name}, ": admin password $rmsg (new hash stored)\n" unless $quiet;
1220 return 0;
1223 sub cmd_checkpw {
1224 @ARGV == 1 or die_usage;
1225 my $project = get_project($ARGV[0]);
1226 my $pwhash = $project->{crypt};
1227 defined($pwhash) or $pwhash = "";
1228 if ($pwhash eq "") {
1229 warn $project->{name}, ": no password required\n" unless $quiet;
1230 return 0;
1232 if ($project->is_password_locked) {
1233 warn $project->{name}, ": password is locked\n" unless $quiet;
1234 exit 1;
1236 my $checkpw;
1237 if (-t STDIN) {
1238 $checkpw = prompt_noecho_nl_or_die("Admin password for project $ARGV[0] (echo is off)");
1239 $checkpw ne "" or warn "checking for empty password as hash (very unlikely)\n" unless $quiet;
1240 } else {
1241 $checkpw = <STDIN>;
1242 defined($checkpw) or die "missing admin password on STDIN\n";
1243 chomp($checkpw);
1245 unless (Girocco::CLIUtil::check_passwd_match($pwhash, $checkpw)) {
1246 warn "password check failure\n" unless $quiet;
1247 exit 1;
1249 warn "admin password match\n" unless $quiet;
1250 return 0;
1253 sub cmd_gc {
1254 my ($force, $auto, $redelta, $recompress);
1255 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, auto => \$auto,
1256 redelta => \$redelta, "no-reuse-delta" => \$redelta, aggressive => \$force,
1257 recompress => \$recompress, "no-reuse-object" => $recompress);
1258 $force && $auto and die "--force and --auto are mutually exclusive options\n";
1259 @ARGV or die "Please give project name on command line.\n";
1260 @ARGV == 1 or die_usage;
1261 my $project = get_project($ARGV[0]);
1262 delete $ENV{show_progress};
1263 delete $ENV{force_gc};
1264 $quiet or $ENV{"show_progress"} = 1;
1265 $force and $ENV{"force_gc"} = 1;
1266 if (!$auto && !$force && ! -e $project->{path}."/.needsgc") {
1267 open NEEDSGC, '>', $project->{path}."/.needsgc" and close NEEDSGC;
1269 my @args = ($Girocco::Config::basedir . "/jobd/gc.sh", $project->{name});
1270 $redelta && !$recompress and push(@args, "-f");
1271 $recompress and push(@args, "-F");
1272 my $lastgc = $project->{lastgc};
1273 system({$args[0]} @args) != 0 and return 1;
1274 # Do it again Sam, but only if lastgc was set, gc.sh succeeded and now it's not set
1275 if ($lastgc) {
1276 my $newlastgc = get_git("--git-dir=$project->{path}", 'config', '--get', 'gitweb.lastgc');
1277 if (!$newlastgc) {
1278 system({$args[0]} @args) != 0 and return 1;
1281 return 0;
1284 sub cmd_update {
1285 my ($force, $summary);
1286 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, summary => \$summary);
1287 $quiet && $summary and die "--quiet and --summary are mutually exclusive options\n";
1288 @ARGV or die "Please give project name on command line.\n";
1289 @ARGV == 1 or die_usage;
1290 my $project = get_project($ARGV[0]);
1291 $project->{mirror} or die "Project \"$ARGV[0]\" is a push project, not a mirror project.\n";
1292 delete $ENV{show_progress};
1293 delete $ENV{force_update};
1294 if ($quiet) {
1295 $ENV{"show_progress"} = 0;
1296 } else {
1297 $ENV{"show_progress"} = ($summary ? 1 : 2);
1299 $force and $ENV{"force_update"} = 1;
1300 system($Girocco::Config::basedir . "/jobd/update.sh", $project->{name}) != 0 and return 1;
1301 return 0;
1304 sub cmd_remirror {
1305 my $force = 0;
1306 parse_options(force => \$force, quiet => \$quiet, q => \$quiet);
1307 @ARGV or die "Please give project name on command line.\n";
1308 @ARGV == 1 or die_usage;
1309 my $project = get_project($ARGV[0]);
1310 $project->{mirror} or die "Project \"$ARGV[0]\" is a push project, not a mirror project.\n";
1311 if ($project->{clone_in_progress} && !$project->{clone_failed}) {
1312 warn "Project \"$ARGV[0]\" already seems to have a clone underway at this moment.\n" unless $quiet && $force;
1313 exit(255) unless $force;
1314 yes_to_continue_or_die("Are you sure you want to force a remirror");
1316 unlink($project->_clonefail_path);
1317 unlink($project->_clonelog_path);
1318 recreate_file($project->_clonep_path);
1319 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or
1320 die "cannot connect to taskd.socket: $!\n";
1321 select((select($sock),$|=1)[0]);
1322 $sock->print("clone ".$project->{name}."\n");
1323 # Just ignore reply, we are going to succeed anyway and the I/O
1324 # would apparently get quite hairy.
1325 $sock->flush();
1326 sleep 2; # *cough*
1327 $sock->close();
1328 warn "Project \"$ARGV[0]\" remirror initiated.\n" unless $quiet;
1329 return 0;
1332 sub cmd_setowner {
1333 my $force = 0;
1334 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1335 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1336 my $project = get_project($ARGV[0]);
1337 if (@ARGV == 2 && !valid_email($ARGV[1])) {
1338 die "invalid owner/email (use --force to accept): \"$ARGV[1]\"\n"
1339 unless $force;
1340 warn "using invalid owner/email with --force\n" unless $quiet;
1342 if (@ARGV == 2 && length($ARGV[1]) > 96) {
1343 die "owner/email longer than 96 chars (use --force to accept): \"$ARGV[1]\"\n"
1344 unless $force;
1345 warn "using longer than 96 char owner/email with --force\n" unless $quiet;
1347 my $old = $project->{email};
1348 if (@ARGV == 1) {
1349 print "$old\n" if defined($old);
1350 return 0;
1352 if (defined($old) && $old eq $ARGV[1]) {
1353 warn $project->{name}, ": skipping update of owner/email to same value\n" unless $quiet;
1354 } else {
1355 # Avoid touching anything other than "gitweb.owner"
1356 $project->_property_fput("email", $ARGV[1]);
1357 $project->_update_index;
1358 $project->_set_changed;
1359 warn $project->{name}, ": owner/email updated to \"$ARGV[1]\"\n" unless $quiet;
1361 return 0;
1364 sub cmd_setdesc {
1365 my $force = 0;
1366 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1367 @ARGV >= 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1368 my $project = get_project(shift @ARGV);
1369 if (@ARGV && !valid_desc(join(" ", @ARGV))) {
1370 die "invalid description (use --force to accept): \"".join(" ", @ARGV)."\"\n"
1371 unless $force;
1372 warn "using invalid description with --force\n" unless $quiet;
1374 my $desc = clean_desc(join(" ", @ARGV));
1375 if (@ARGV && length($desc) > 1024) {
1376 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
1377 unless $force;
1378 warn "using longer than 1024 char description with --force\n" unless $quiet;
1380 my $old = $project->{desc};
1381 if (!@ARGV) {
1382 print "$old\n" if defined($old);
1383 return 0;
1385 if (defined($old) && $old eq $desc) {
1386 warn $project->{name}, ": skipping update of description to same value\n" unless $quiet;
1387 } else {
1388 # Avoid touching anything other than description file
1389 $project->_property_fput("desc", $desc);
1390 $project->_set_changed;
1391 warn $project->{name}, ": description updated to \"$desc\"\n" unless $quiet;
1393 return 0;
1396 sub cmd_setreadme {
1397 my $force = 0;
1398 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1399 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1400 my $project = get_project($ARGV[0]);
1401 my $old = $project->{README};
1402 if (@ARGV == 1) {
1403 chomp $old if defined($old);
1404 print "$old\n" if defined($old) && $old ne "";
1405 return 0;
1407 my ($new, $raw, $newname);
1408 $newname = '';
1409 if ($ARGV[1] eq "-") {
1410 local $/;
1411 $new = <STDIN>;
1412 $raw = 1;
1413 $newname = "contents of <STDIN>";
1414 } elsif (lc($ARGV[1]) eq "automatic" || lc($ARGV[1]) eq "auto") {
1415 $new = "";
1416 } elsif (lc($ARGV[1]) eq "suppressed" || lc($ARGV[1]) eq "suppress") {
1417 $new = "<!-- suppress -->";
1418 } else {
1419 my $fn = $ARGV[1];
1420 $fn =~ s/^\@//;
1421 die "missing filename for README\n" unless $fn ne "";
1422 die "no such file: \"$fn\"\n" unless -f $fn && -r $fn;
1423 open F, '<', $fn or die "cannot open \"$fn\" for reading: $!\n";
1424 local $/;
1425 $new = <F>;
1426 close F;
1427 $raw = 1;
1428 $newname = "contents of \"$fn\"";
1430 defined($new) or $new = '';
1431 $project->{README} = to_utf8($new, 1);
1432 $project->_cleanup_readme;
1433 if (length($project->{README}) > 8192) {
1434 die "readme greater than 8192 chars is too long (use --force to override)\n"
1435 unless $force;
1436 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
1438 if ($raw) {
1439 my $rd = get_readme_desc($project->{README});
1440 if ($rd ne "automatic" && $rd ne "suppressed") {
1441 my $xmllint = qx(command -v xmllint); chomp $xmllint;
1442 if (-f $xmllint && -x $xmllint) {
1443 my ($cnt, $err) = $project->_lint_readme(0);
1444 if ($cnt) {
1445 my $msg = "xmllint: $cnt error";
1446 $msg .= "s" unless $cnt == 1;
1447 print STDERR "$msg\n", "-" x length($msg), "\n", $err
1448 unless $force && $quiet;
1449 exit(255) unless $force;
1450 warn $project->{name} . ": using invalid raw HTML with --force\n" unless $quiet;
1452 } else {
1453 die "xmllint not available, refusing to use raw HTML without --force\n"
1454 unless $force;
1455 warn "xmllint not available using unchecked raw HTML with --force\n" unless $quiet;
1459 if (defined($old) && $old eq $project->{README}) {
1460 warn $project->{name}, ": skipping update of README to same value\n" unless $quiet;
1461 } else {
1462 # Avoid touching anything other than README.html file
1463 $project->_property_fput("README", $project->{README});
1464 $project->_set_changed;
1465 my $desc = get_readme_desc($project->{README});
1466 if ($newname) {
1467 $newname .= " ($desc)";
1468 } else {
1469 $newname = $desc;
1471 warn $project->{name}, ": README updated to $newname\n" unless $quiet;
1473 return 0;
1476 sub valid_head {
1477 my ($proj, $newhead) = @_;
1478 my %okheads = map({($_ => 1)} $proj->get_heads);
1479 exists($okheads{$newhead});
1482 sub cmd_sethead {
1483 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1484 my $project = get_project($ARGV[0]);
1485 if (@ARGV == 2 && !valid_head($project, $ARGV[1])) {
1486 die "invalid head (try \"@{[basename($0)]} listheads $ARGV[0]\"): \"$ARGV[1]\"\n";
1488 my $old = $project->{HEAD};
1489 if (@ARGV == 1) {
1490 print "$old\n" if defined($old);
1491 return 0;
1493 if (defined($old) && $old eq $ARGV[1]) {
1494 warn $project->{name}, ": skipping update of HEAD symref to same value\n" unless $quiet;
1495 } else {
1496 # Avoid touching anything other than the HEAD symref
1497 $project->set_HEAD($ARGV[1]);
1498 $project->_set_changed;
1499 warn $project->{name}, ": HEAD symref updated to \"refs/heads/$ARGV[1]\"\n" unless $quiet;
1501 return 0;
1504 sub cmd_sethooks {
1505 my $force = 0;
1506 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1507 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1508 my $project = get_project($ARGV[0]);
1509 my $projconfig = read_config_file_hash($project->{path}."/config");
1510 my $ghp = $Girocco::Config::reporoot."/_global/hooks";
1511 my $rghp = realpath($ghp);
1512 my $lhp = $project->{path}."/hooks";
1513 my $rlhp = realpath($lhp);
1514 my $ahp = "";
1515 my $rahp = undef;
1516 if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) {
1517 $ahp = $projconfig->{"core.hookspath"};
1518 $rahp = realpath($ahp);
1520 if (@ARGV == 1) {
1521 if (defined($rahp) && $rahp ne "") {
1522 if ($rahp eq $rghp) {
1523 my $nc = ($ahp eq $ghp ? "" : " non-canonical");
1524 printf "%s \t(global%s)\n", $ahp, $nc;
1525 } elsif ($rahp eq $rlhp) {
1526 my $nc = ($ahp eq $lhp ? "" : " non-canonical");
1527 printf "%s \t(local%s)\n", $ahp, $nc;
1528 } elsif ($rahp ne $ahp) {
1529 print "$ahp \t($rahp)\n";
1530 } else {
1531 print "$ahp\n";
1533 } elsif ($ahp ne "") {
1534 print "$ahp \t(non-existent)\n";
1536 return 0;
1538 my $shp = $ARGV[1];
1539 if (lc($shp) eq "global") {
1540 $shp = $ghp;
1541 } elsif (lc($shp) eq "local") {
1542 $shp = $lhp;
1543 } elsif (substr($shp, 0, 2) eq "~/") {
1544 $shp = $ENV{"HOME"}.substr($shp,1);
1545 } elsif ($shp =~ m,^~([a-zA-Z_][a-zA-Z_0-9]*)((?:/.*)?)$,) {
1546 my $sfx = $2;
1547 my $hd = (getpwnam($1))[7];
1548 $shp = $hd . $sfx if defined($hd) && $hd ne "" && $hd ne "/" && -d $hd;
1550 $shp ne "" && -d $shp or die "no such directory: $ARGV[1]\n";
1551 my $rshp = realpath($shp);
1552 defined($rshp) && $rshp ne "" or die "could not realpath: $ARGV[1]\n";
1553 $rshp =~ m,^/[^/], or die "invalid hookspath: $rshp\n";
1554 die "refusing to switch from current non-global hookspath without --force\n"
1555 if !$force && defined($rahp) && $rahp ne "" && $rahp ne $rghp && $rshp ne $rahp;
1556 if (!$force && defined($rahp) && $rahp ne "") {
1557 if ($rshp eq $rahp && ($ahp eq $ghp || $ahp eq $lhp)) {
1558 warn $project->{name}, ": skipping update of hookspath to same effective value\n" unless $quiet;
1559 return 0;
1562 $rshp = $ghp if $rshp eq $rghp;
1563 $rshp = $lhp if $rshp eq $rlhp;
1564 if ($rshp eq $ahp) {
1565 warn $project->{name}, ": skipping update of hookspath to same value\n" unless $quiet;
1566 return 0;
1568 die "refusing to set neither local nor global hookspath without --force\n"
1569 if !$force && $rshp ne $ghp && $rshp ne $lhp;
1570 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1571 'config', "core.hookspath", $rshp);
1572 my $newval = '"'.$rshp.'"';
1573 $newval = "global" if $rshp eq $ghp;
1574 $newval = "local" if $rshp eq $lhp;
1575 warn $project->{name}, ": hookspath set to $newval\n" unless $quiet;
1576 return 0;
1579 our %boolfields;
1580 BEGIN {
1581 %boolfields = (
1582 cleanmirror => 1,
1583 reverseorder => 0,
1584 summaryonly => 0,
1585 statusupdates => 1,
1589 sub cmd_setbool {
1590 my $force = 0;
1591 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1592 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1593 my $project = get_project($ARGV[0]);
1594 if (!exists($boolfields{$ARGV[1]})) {
1595 die "invalid boolean field name: \"$ARGV[1]\" -- try \"help\"\n";
1597 if (@ARGV == 3 && $boolfields{$ARGV[1]} && !$project->{mirror}) {
1598 die "invalid boolean field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
1599 unless $force;
1600 warn "using mirror field on non-mirror with --force\n" unless $quiet;
1602 if (@ARGV == 3 && !valid_bool($ARGV[2])) {
1603 die "invalid boolean value: \"$ARGV[2]\"\n";
1605 my $bool = clean_bool($ARGV[2]);
1606 my $old = $project->{$ARGV[1]};
1607 if (@ARGV == 2) {
1608 print "$old\n" if defined($old);
1609 return 0;
1611 if (defined($old) && $old eq $bool) {
1612 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
1613 } else {
1614 # Avoid touching anything other than $ARGV[1] field
1615 $project->_property_fput($ARGV[1], $bool);
1616 warn $project->{name}, ": $ARGV[1] updated to $bool\n" unless $quiet;
1618 return 0;
1621 sub cmd_setautogchack {
1622 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1623 my $project = get_project($ARGV[0]);
1624 my $aghok = $Girocco::Config::autogchack &&
1625 ($project->{mirror} || $Girocco::Config::autogchack ne "mirror");
1626 my $old = defined($project->{autogchack}) ? clean_bool($project->{autogchack}) : "unset";
1627 if (@ARGV == 1) {
1628 print "$old\n" if $aghok;
1629 return 0;
1631 my $bool;
1632 if (lc($ARGV[1]) eq "unset") {
1633 $bool = "unset";
1634 } else {
1635 valid_bool($ARGV[1]) or die "invalid boolean value: \"$ARGV[1]\"\n";
1636 $bool = clean_bool($ARGV[1]);
1638 if (!$aghok) {
1639 die "\$Girocco::Config::autogchack is false\n" unless $Girocco::Config::autogchack;
1640 die "\$Girocco::Config::autogchack is only enabled for mirrors\n";
1642 if ($old eq $bool) {
1643 warn $project->{name}, ": autogchack value unchanged\n" unless $quiet;
1644 } else {
1645 if ($bool eq "unset") {
1646 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1647 'config', '--unset', "girocco.autogchack");
1648 } else {
1649 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1650 'config', '--bool', "girocco.autogchack", $bool);
1653 return system($Girocco::Config::basedir . "/jobd/maintain-auto-gc-hack.sh", $project->{name}) == 0
1654 ? 0 : 1;
1657 sub valid_url {
1658 my ($url, $type) = @_;
1659 $type ne 'baseurl' and return valid_web_url($url);
1660 valid_repo_url($url) or return 0;
1661 if ($Girocco::Config::restrict_mirror_hosts) {
1662 my $mh = extract_url_hostname($url);
1663 is_dns_hostname($mh) or return 0;
1664 !is_our_hostname($mh) or return 0;
1666 return 1;
1669 our %urlfields;
1670 BEGIN {
1671 %urlfields = (
1672 baseurl => ["url" , 1],
1673 homepage => ["hp" , 0],
1674 notifyjson => ["notifyjson", 0],
1678 sub cmd_seturl {
1679 my $force = 0;
1680 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1681 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1682 my $project = get_project($ARGV[0]);
1683 if (!exists($urlfields{$ARGV[1]})) {
1684 die "invalid URL field name: \"$ARGV[1]\" -- try \"help\"\n";
1686 if (@ARGV == 3 && ${$urlfields{$ARGV[1]}}[1] && !$project->{mirror}) {
1687 die "invalid URL field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
1688 unless $force;
1689 warn "using mirror field on non-mirror with --force\n" unless $quiet;
1691 if (@ARGV == 3 && !valid_url($ARGV[2], $ARGV[1])) {
1692 die "invalid URL (use --force to accept): \"$ARGV[2]\"\n"
1693 unless $force;
1694 warn "using invalid URL with --force\n" unless $quiet;
1696 my $old = $project->{${$urlfields{$ARGV[1]}}[0]};
1697 if (@ARGV == 2) {
1698 print "$old\n" if defined($old);
1699 return 0;
1701 if (defined($old) && $old eq $ARGV[2]) {
1702 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
1703 } else {
1704 # Avoid touching anything other than $ARGV[1]'s field
1705 $project->_property_fput(${$urlfields{$ARGV[1]}}[0], $ARGV[2]);
1706 if ($ARGV[1] eq "baseurl") {
1707 $project->{url} = $ARGV[2];
1708 $project->_set_bangagain;
1710 $project->_set_changed unless $ARGV[1] eq "notifyjson";
1711 warn $project->{name}, ": $ARGV[1] updated to $ARGV[2]\n" unless $quiet;
1713 return 0;
1716 our %msgsfields;
1717 BEGIN {
1718 %msgsfields = (
1719 notifymail => 1,
1720 notifytag => 1,
1724 sub cmd_setmsgs {
1725 my $force = 0;
1726 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1727 @ARGV >= 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1728 my $project = get_project(shift @ARGV);
1729 my $field = shift @ARGV;
1730 if (!exists($msgsfields{$field})) {
1731 die "invalid msgs field name: \"$field\" -- try \"help\"\n";
1733 if (@ARGV && !valid_addrlist(@ARGV)) {
1734 die "invalid email address list (use --force to accept): \"".join(" ",@ARGV)."\"\n"
1735 unless $force;
1736 warn "using invalid email address list with --force\n" unless $quiet;
1738 my $old = $project->{$field};
1739 if (!@ARGV) {
1740 printf "%s\n", clean_addrlist($old, " ") if defined($old);
1741 return 0;
1743 my $newlist = clean_addrlist(join(" ",@ARGV));
1744 if (defined($old) && $old eq $newlist) {
1745 warn $project->{name}, ": skipping update of $field to same value\n" unless $quiet;
1746 } else {
1747 # Avoid touching anything other than $field's field
1748 $project->_property_fput($field, $newlist);
1749 warn $project->{name}, ": $field updated to \"$newlist\"\n" unless $quiet;
1751 return 0;
1754 sub cmd_setusers {
1755 my $force = 0;
1756 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1757 @ARGV >= 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1758 my $project = get_project(shift @ARGV);
1759 my $projname = $project->{name};
1760 !@ARGV || !$project->{mirror} or die "cannot set users list for mirror project: \"$projname\"\n";
1761 my @newusers = ();
1762 if (@ARGV) {
1763 eval {@newusers = validate_users(join(" ", @ARGV), $force); 1;} or exit 255;
1764 die "refusing to set empty users list without --force\n" unless @newusers || $force;
1766 return 0 if !@ARGV && $project->{mirror};
1767 my $oldusers = $project->{users};
1768 if ($oldusers && ref($oldusers) eq "ARRAY") {
1769 $oldusers = join("\n", @$oldusers);
1770 } else {
1771 $oldusers = "";
1773 if (!@ARGV) {
1774 print "$oldusers\n" if $oldusers ne "";
1775 return 0;
1777 if ($oldusers eq join("\n", @newusers)) {
1778 warn "$projname: skipping update of users list to same value\n" unless $quiet;
1779 } else {
1780 # Avoid touching anything other than the users list
1781 $project->{users} = \@newusers;
1782 $project->_update_users;
1783 warn "$projname: users list updated to \"@{[join(',',@newusers)]}\"\n" unless $quiet;
1785 return 0;
1788 our %fieldnames;
1789 BEGIN {
1790 %fieldnames = (
1791 owner => [\&cmd_setowner, 0],
1792 desc => [\&cmd_setdesc, 0],
1793 description => [\&cmd_setdesc, 0],
1794 readme => [\&cmd_setreadme, 0],
1795 head => [\&cmd_sethead, 0],
1796 HEAD => [\&cmd_sethead, 0],
1797 hooks => [\&cmd_sethooks, 0],
1798 hookspath => [\&cmd_sethooks, 0],
1799 cleanmirror => [\&cmd_setbool, 1],
1800 reverseorder => [\&cmd_setbool, 1],
1801 summaryonly => [\&cmd_setbool, 1],
1802 statusupdates => [\&cmd_setbool, 1],
1803 autogchack => [\&cmd_setautogchack, 0],
1804 baseurl => [\&cmd_seturl, 1],
1805 homepage => [\&cmd_seturl, 1],
1806 notifyjson => [\&cmd_seturl, 1],
1807 notifymail => [\&cmd_setmsgs, 1],
1808 notifytag => [\&cmd_setmsgs, 1],
1809 users => [\&cmd_setusers, 0],
1813 sub do_getset {
1814 $setopt = shift;
1815 my @newargs = ();
1816 push(@newargs, shift) if @_ && $_[0] eq '--force';
1817 my $field = $_[1];
1818 (($setopt && @_ >= 3) || @_ == 2) && exists($fieldnames{$field}) or die_usage;
1819 push(@newargs, shift);
1820 shift unless ${$fieldnames{$field}}[1];
1821 push(@newargs, @_);
1822 diename(($setopt ? "set " : "get ") . $field);
1823 @ARGV = @newargs;
1824 &{${$fieldnames{$field}}[0]}(@ARGV);
1827 sub cmd_get {
1828 do_getset(0, @_);
1831 sub cmd_set {
1832 do_getset(1, @_);
1835 our %commands;
1836 BEGIN {
1837 %commands = (
1838 list => \&cmd_list,
1839 create => \&cmd_create,
1840 adopt => \&cmd_adopt,
1841 remove => \&cmd_remove,
1842 trash => \&cmd_remove,
1843 delete => \&cmd_remove,
1844 show => \&cmd_show,
1845 listheads => \&cmd_listheads,
1846 listtags => \&cmd_listtags,
1847 listctags => \&cmd_listtags,
1848 deltags => \&cmd_deltags,
1849 delctags => \&cmd_deltags,
1850 addtags => \&cmd_addtags,
1851 addctags => \&cmd_addtags,
1852 chpass => \&cmd_chpass,
1853 checkpw => \&cmd_checkpw,
1854 gc => \&cmd_gc,
1855 update => \&cmd_update,
1856 remirror => \&cmd_remirror,
1857 setowner => \&cmd_setowner,
1858 setdesc => \&cmd_setdesc,
1859 setdescription => \&cmd_setdesc,
1860 setreadme => \&cmd_setreadme,
1861 sethead => \&cmd_sethead,
1862 sethooks => \&cmd_sethooks,
1863 sethookspath => \&cmd_sethooks,
1864 setbool => \&cmd_setbool,
1865 setboolean => \&cmd_setbool,
1866 setflag => \&cmd_setbool,
1867 setautogchack => \&cmd_setautogchack,
1868 seturl => \&cmd_seturl,
1869 setmsgs => \&cmd_setmsgs,
1870 setusers => \&cmd_setusers,
1871 get => \&cmd_get,
1872 set => \&cmd_set,
1876 sub dohelp {
1877 my $cmd = shift;
1878 my $bn = basename($0);
1879 printf "%s version %s\n\n", $bn, $VERSION;
1880 if (defined($cmd) && $cmd ne '') {
1881 $cmd =~ s/^set(?=[a-zA-Z])//i;
1882 my $cmdhelp = '';
1883 my ($lastmt, $incmd);
1884 foreach (split('\n', sprintf($help, $bn))) {
1885 $lastmt || $incmd or $lastmt = /^\s*$/, next;
1886 $incmd = 1 if $lastmt && /^\s*(?:\[?set\]?)?$cmd\s/;
1887 last if $incmd && /^\s*$/;
1888 $incmd and $cmdhelp .= $_ . "\n";
1889 $lastmt = /^\s*$/;
1891 print $cmdhelp and exit 0 if $cmdhelp;
1893 printf $help, $bn;
1894 exit 0;
1897 sub main {
1898 local *ARGV = \@_;
1899 shift, $quiet=1 if @ARGV && $ARGV[0] =~ /^(?:-q|--quiet)$/i;
1900 dohelp($ARGV[1]) if !@ARGV || @ARGV && $ARGV[0] =~ /^(?:-h|-?-help|help)$/i;
1901 my $command = shift;
1902 diename($command);
1903 $setopt = 1;
1904 if (!exists($commands{$command}) && exists($commands{"set".$command})) {
1905 $setopt = 0;
1906 $command = "set" . $command;
1908 exists($commands{$command}) or die "Unknown command \"$command\" -- try \"help\"\n";
1909 dohelp($command) if @ARGV && ($ARGV[0] =~ /^(?:-h|-?-help)$/i ||
1910 $ARGV[0] =~ /^help$/i && !Girocco::Project::does_exist("help",1));
1911 &{$commands{$command}}(@ARGV);