{proj,user}tool.pl: add missing parentheses around test
[girocco.git] / toolbox / projtool.pl
blob2d13aefb80fa38e36c71f47953ea7d200ef61174
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 --defaults do no interactive prompting at all
54 Using --no-password skips the prompts for password, using
55 --no-owner skips the prompt for owner and using --mirror=<url>
56 or --push[=<list>] skips the prompts for mirror URL and
57 heads-only and push users. With --defaults if neither
58 --mirror=<url> nor --push[=<list>] is given then --push will
59 be implied. Using --desc=<string> will force a specific
60 description (including an empty string) and skip the prompt for
61 it. Otherwise a non-empty default description will always be
62 supplied in lieu of an empty or omitted description.
64 adopt [--force] [--type=mirror|push] [<option>...] <project> [<users>]
65 adopt project <project>
66 type of project is guessed if --type=<type> omitted
67 <users> is same as <newuserslist> for setusers command
68 <option> can be:
69 --dry-run do all the checks but don't perform adoption
70 --verbose show project info dump (useful with --dry-run)
71 --no-users no push users at all (<users> must be omitted)
72 --no-owner leave the gitweb.owner config totally unchanged
73 --owner=<val> set the gitweb.owner config to <val>
74 Both --no-owner and --owner=<val> may NOT be given, with neither
75 take owner from preexisting gitweb.owner else use admin setting.
76 For mirrors <users> is ignored otherwise if no <users> and no
77 --no-users option the push users list will consist of the single
78 user name matching the owner or empty if none or more than one.
79 With --dry-run <project> can be an absolute path to a git dir.
81 remove [--force] [--really-delete] [--keep-forks] <project>
82 remove project <project>
83 do not move to _recyclebin with --really-delete (just rm -rf)
84 remove projects with forks (by keeping forks) using --keep-forks
86 show <project>
87 show project <project>
89 listheads <project>
90 list all available heads for <project> and indicate current head
92 listtags [--verbose] <project>
93 list all ctags on project <project>
95 deltags <project> [-i] <tagstodel>
96 remove any ctags on project <project> present in <tagstodel>
97 <tagstodel> is space or comma separated list of tags to remove
98 with -i match against <tagstodel> without regard to letter case
100 addtags <project> <tagstoadd>
101 add ctags to project <project>
102 <tagstoadd> is space or comma separated list of tags to add
104 chpass [--force] <project> [random | unknown]
105 change project <project> password (prompted)
106 with "random" set to random password
107 with "unknown" set password hash to invalid value "unknown"
109 checkpw <project>
110 check project <project> password for a match (prompted)
112 gc [--force | --auto] [--redelta | --recompress] <project>
113 run the gc.sh script on project <project>
114 with --auto let the gc.sh script decide what to do
115 with --force cause a full gc to take place (force_gc=1)
116 with neither --auto nor --force do a mini or if needed a full gc
117 (in other words just touch .needsgc and run gc.sh)
118 with --redelta a full gc will use pack-objects --no-reuse-delta
119 with --recompress a full gc uses pack-objects --no-reuse-object
120 (--no-reuse-delta and --no-reuse-object are accepted as aliases)
121 unless the global --quiet option is given show_progress=1 is used
123 update [--force] [--quiet | --summary] <project>
124 run the update.sh script on project <project>
125 with --force cause a fetch to always take place (force_update=1)
126 with --quiet only show errors (show_progress is left unset)
127 with --summary show progress and ref summary (show_progress=1)
128 with neither --quiet nor --summary show it all (show_progress=2)
130 remirror [--force] <project>
131 initiate a remirror of project <project>
133 [set]owner [--force] <project> <newowner>
134 set project <project> owner to <newowner>
135 without "set" and only 1 arg, just show current project owner
137 [set]desc [--force] <project> <newdesc>
138 set project <project> description to <newdesc>
139 without "set" and only 1 arg, just show current project desc
141 [set]readme [--force] <project> <newsetting>
142 set project <project> readme to <newsetting>
143 <newsetting> is automatic|suppressed|-|[@]filename
144 without "set" and only 2 args, just show current readme setting
146 [set]head <project> <newhead>
147 set project <project> HEAD symbolic ref to <newhead>
148 without "set" and only 1 arg, just show current project HEAD
150 [set]bool [--force] <project> <flagname> <boolvalue>
151 set project <project> boolean <flagname> to <boolvalue>
152 <flagname> is cleanmirror|reverseorder|summaryonly|statusupdtaes
153 without "set" and only 2 args, just show current flag value
155 [set]hooks [--force] <project> local | global | <path>
156 set project <project> hookspath to local, global or <path>
157 without "set" and only 1 arg, just show current hookspath
159 [set]autogchack <project> <boolvalue> | unset
160 set project <project> autogchack to <boolvalue> or "unset" it
161 without "set" just show current autogchack setting if enabled
162 with "set" autogchack must be enabled in Config.pm for the
163 type of project and maintain-auto-gc-hack.sh is always run
165 [set]url [--force] <project> <urlname> <newurlvalue>
166 set project <project> url <urlname> to <newurlvalue>
167 <urlname> is baseurl|homepage|notifyjson
168 without "set" and only 2 args, just show current url value
170 [set]msgs [--force] <project> <msgsname> <eaddrlist>
171 set project <project> msgs <msgsname> to <addrlist>
172 <msgsname> is notifymail|notifytag
173 <eaddrlist> is space or comma separated list of email addresses
174 without "set" and only 2 args, just show current msgs value
176 [set]users [--force] <project> <newuserslist>
177 set push project <project> users list to <newuserslist>
178 <newuserslist> is space or comma separated list of user names
179 without "set" and only 1 arg, just show current users list
181 get <project> <fieldname>
182 show project <project> field <fieldname>
183 <fieldname> is owner|desc|readme|head|hooks|users
184 or <flagname>|autogchack|<urlname>|<msgsname>
186 set [--force] <project> <fieldname> <newfieldvalue>
187 set project <project> field <fieldname> to <newfieldvalue>
188 <fieldname> same as for get
189 <newfieldvalue> same as for corresponding set... command
190 HELP
192 our $quiet;
193 our $setopt;
194 sub die_usage {
195 my $sub = shift || diename;
196 if ($sub) {
197 die "Invalid arguments to $sub command -- try \"help\"\n";
198 } else {
199 die "Invalid arguments -- try \"help\"\n";
203 sub get_readme_desc {
204 my $rm = shift;
205 defined($rm) or $rm = '';
206 if (length($rm)) {
207 my $test = $rm;
208 $test =~ s/<!--(?:[^-]|(?:-(?!-)))*-->//gs;
209 $test =~ s/\s+//s;
210 return $test eq '' ? "suppressed" : "length " . length($rm);
211 } else {
212 return "automatic";
216 sub get_ctag_counts {
217 my $project = shift;
218 my $compact = shift;
219 my @ctags = ();
220 foreach ($project->get_ctag_names) {
221 my $val = 0;
222 my $ct;
223 if (open $ct, '<', $project->{path}."/ctags/$_") {
224 my $count = <$ct>;
225 close $ct;
226 defined $count or $count = '';
227 chomp $count;
228 $val = $count =~ /^[1-9]\d*$/ ? $count : 1;
230 if ($compact) {
231 if ($val == 1) {
232 push(@ctags, $_);
233 } elsif ($val > 1) {
234 push(@ctags, $_."(".$val.")");
236 } else {
237 push(@ctags, [$_, $val]) if $val;
240 @ctags;
243 sub get_clean_project {
244 my $project = get_project(@_);
245 delete $project->{loaded};
246 delete $project->{base_path};
247 delete $project->{ccrypt};
248 /^orig/i || !defined($project->{$_}) and delete $project->{$_} foreach keys %$project;
249 $project->{owner} = $project->{email}; delete $project->{email};
250 $project->{homepage} = $project->{hp}; delete $project->{hp};
251 $project->{baseurl} = $project->{url}; delete $project->{url};
252 if (defined($project->{path}) && $project->{path} ne "") {
253 my $rp = realpath($project->{path});
254 defined($rp) && $rp ne "" and $project->{realpath} = $rp;
256 my $owner = $project->{owner};
257 if ($owner) {
258 $owner = lc($owner);
259 my @owner_users = map {$owner eq lc($$_[4]) ? $$_[1] : ()} get_all_users;
260 $project->{owner_users} = \@owner_users if @owner_users;
262 my $projname = $project->{name};
263 my @forks = grep {$$_[1] =~ m,^$projname/,} get_all_projects;
264 $project->{has_forks} = 1 if @forks;
265 $project->{has_alternates} = 1 if $project->has_alternates;
266 my @bundles = $project->bundles;
267 for (my $i = 0; $i < @bundles; ++$i) {
268 my $secs = $bundles[$i]->[0];
269 $bundles[$i]->[0] = strftime("%Y-%m-%dT%H:%M:%S%z", localtime($secs));
270 my $sz = $bundles[$i]->[2];
271 1 while $sz =~ s/(?<=\d)(\d{3})(?:,|$)/,$1/g;
272 $bundles[$i]->[2] = $sz;
274 delete $project->{bundles};
275 $project->{bundles} = \@bundles if @bundles;
276 $project->{mirror} = 0 unless $project->{mirror};
277 $project->{is_empty} = 1 if $project->is_empty;
278 delete $project->{showpush} unless $project->{showpush};
279 delete $project->{users} if $project->{mirror};
280 delete $project->{baseurl} unless $project->{mirror};
281 delete $project->{banged} unless $project->{mirror};
282 delete $project->{lastrefresh} unless $project->{mirror};
283 delete $project->{cleanmirror} unless $project->{mirror};
284 delete $project->{statusupdates} unless $project->{mirror};
285 delete $project->{lastparentgc} unless $projname =~ m,/,;
286 unless ($project->{banged}) {
287 delete $project->{bangcount};
288 delete $project->{bangfirstfail};
289 delete $project->{bangmessagesent};
291 my $projhook = $project->_has_notifyhook;
292 if (defined($projhook) && $projhook ne "") {
293 $project->{notifyhook} = $projhook;
294 } else {
295 delete $project->{notifyhook};
297 $project->{README} = get_readme_desc($project->{README}) if exists($project->{README});
298 my @tags = get_ctag_counts($project, 1);
299 $project->{tags} = \@tags if @tags;
300 my $projconfig = read_config_file_hash($project->{path}."/config");
301 if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) {
302 my $ahp = $projconfig->{"core.hookspath"};
303 my $rahp = realpath($ahp);
304 my $lhp = $project->{path}."/hooks";
305 my $rlhp = realpath($lhp);
306 my $ghp = $Girocco::Config::reporoot."/_global/hooks";
307 my $rghp = realpath($ghp);
308 $project->{has_local_hooks} = 1 if
309 defined($rahp) && defined($rlhp) && $rahp eq $rlhp;
310 $project->{has_global_hooks} = 1 if
311 defined($rahp) && defined($rghp) && $rahp eq $rghp;
312 $project->{hookspath} = $ahp unless $ahp eq $lhp || $ahp eq $ghp;
314 $project;
317 sub clean_addrlist {
318 my %seen = ();
319 my @newlist = ();
320 foreach (split(/[,\s]+/, $_[0])) {
321 next unless $_;
322 $seen{lc($_)} = 1, push(@newlist, $_) unless $seen{lc($_)};
324 return join(($_[1]||","), @newlist);
327 sub valid_addrlist {
328 my $cleaned = clean_addrlist(join(" ", @_));
329 return 1 if $cleaned eq "";
330 valid_email_multi($cleaned) && length($cleaned) <= 512;
333 sub validate_users {
334 my ($userlist, $force, $nodie, $quiet) = @_;
335 my @newusers = ();
336 my $badlist = 0;
337 my %seenuser = ();
338 my $mobok = $Girocco::Config::mob && $Girocco::Config::mob eq "mob";
339 my %users = map({($$_[1] => $_)} get_all_users);
340 foreach (split(/[\s,]+/, $userlist)) {
341 if (exists($users{$_}) || $_ eq "everyone" || ($mobok && $_ eq "mob")) {
342 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
343 next;
345 if (Girocco::User::does_exist($_, 1)) {
346 if ($force) {
347 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
348 } else {
349 $badlist = 1;
350 warn "refusing to allow questionable user \"$_\" without --force\n" unless $nodie && $quiet;
352 next;
354 $badlist = 1;
355 warn "invalid user: \"$_\"\n" unless $nodie && $quiet
357 die if $badlist && !$nodie;
358 return @newusers;
361 sub is_default_desc {
362 # "Unnamed repository; edit this file 'description' to name the repository."
363 # "Unnamed repository; edit this file to name it for gitweb."
364 local $_ = shift;
365 return 0 unless defined($_);
366 /Unnamed\s+repository;/i && /\s+edit\s+this\s+file\s+/i && /\s+to\s+name\s+/i;
369 sub valid_desc {
370 my $test = shift;
371 chomp $test;
372 return 0 if $test =~ /[\r\n]/;
373 $test =~ s/\s\s+/ /g;
374 $test =~ s/^\s+//;
375 $test =~ s/\s+$//;
376 return $test ne '';
379 sub clean_desc {
380 my $desc = shift;
381 defined($desc) or $desc = '';
382 chomp $desc;
383 $desc = to_utf8($desc, 1);
384 $desc =~ s/\s\s+/ /g;
385 $desc =~ s/^\s+//;
386 $desc =~ s/\s+$//;
387 return $desc;
390 sub parse_options {
391 Girocco::CLIUtil::_parse_options(
392 sub {
393 warn((($_[0]eq'?')?"unrecognized":"missing argument for")." option \"$_[1]\"\n")
394 unless $quiet;
395 die_usage;
396 }, @_);
399 sub cmd_list {
400 my %sortsub = (
401 lcname => sub {lc($$a[1]) cmp lc($$b[1])},
402 name => sub {$$a[1] cmp $$b[1]},
403 gid => sub {$$a[3] <=> $$b[3]},
404 owner => sub {lc($$a[4]) cmp lc($$b[4]) || lc($$a[1]) cmp lc($$b[1])},
405 no => sub {$$a[0] <=> $$b[0]},
407 my $sortopt = 'lcname';
408 my ($verbose, $owner);
409 parse_options(":sort" => \$sortopt, verbose => \$verbose, owner => \$owner);
410 my $regex;
411 if (@ARGV) {
412 my $val = shift @ARGV;
413 $regex = qr($val) or die "bad regex \"$val\"\n";
415 !@ARGV && exists($sortsub{$sortopt}) or die_usage;
416 my $sortsub = $sortsub{$sortopt};
417 my $grepsub = defined($regex) ? ($owner ? sub {$$_[4] =~ /$regex/} : sub {$$_[1] =~ /$regex/}) : sub {1};
418 my @projects = sort($sortsub grep {&$grepsub} get_all_projects);
419 if ($verbose) {
420 print map(sprintf("%s\n", join(":", (@$_)[1..5])), @projects);
421 } else {
422 print map(sprintf("%s: %s\n", $$_[1], $$_[5] =~ /^:/ ? "<mirror>" : $$_[5]), @projects);
424 return 0;
427 sub cmd_create {
428 my ($force, $noalternates, $orphanok, $optp, $nopasswd, $noowner, $defaults, $ispush, $pushusers,
429 $ismirror, $desc, $fullmirror);
430 parse_options(
431 force => \$force, "no-alternates" => \$noalternates, orphan => \$orphanok, p => \$optp,
432 "no-password" => \$nopasswd, "no-owner" => \$noowner, defaults => \$defaults,
433 "push" => \$ispush, ":push" => \$pushusers, ":mirror" => \$ismirror, ":desc" => \$desc,
434 ":description" => \$desc, "full-mirror" => \$fullmirror);
435 @ARGV == 1 or die_usage;
436 !defined($pushusers) || defined($ispush) or $ispush = 1;
437 defined($ismirror) && $ismirror =~ /^\s*$/ and die "--mirror url must not be empty\n";
438 die "--mirror and --push are mutually exclusive options\n" if $ismirror && $ispush;
439 die "--full-mirror requires use of --mirror=<url> option\n" if $fullmirror && !$ismirror;
440 !$defaults || defined($ispush) || defined($ismirror) or $ispush = 1;
441 !$defaults || defined($nopasswd) or $nopasswd = 1;
442 !$defaults || defined($noowner) or $noowner = 1;
443 !defined($ispush) || defined($pushusers) or $pushusers = "";
444 my $projname = $ARGV[0];
445 $projname =~ s/\.git$//i;
446 Girocco::Project::does_exist($projname, 1) and die "Project already exists: \"$projname\"\n";
447 if (!Girocco::Project::valid_name($projname, $orphanok, $optp)) {
448 warn "Refusing to create orphan project without --orphan\n"
449 if !$quiet && !$orphanok && Girocco::Project::valid_name($projname, 1, 1);
450 warn "Required orphan parent directory does not exist (use -p): ",
451 $Girocco::Config::reporoot.'/'.Girocco::Project::get_forkee_name($projname), "\n"
452 if !$quiet && $orphanok && Girocco::Project::valid_name($projname, 1, 1);
453 die "Invalid project name: \"$projname\"\n";
455 my ($forkee, $project) = ($projname =~ m#^(.*/)?([^/]+)$#);
456 my $newtype = $forkee ? 'fork' : 'project';
457 if (length($project) > 64) {
458 die "The $newtype name is longer than 64 characters. Do you really need that much?\n"
459 unless $force;
460 warn "Allowing $newtype name longer than 64 characters with --force\n" unless $quiet;
462 unless ($Girocco::Config::push || $Girocco::Config::mirror) {
463 die "Project creation disabled (no mirrors or push projects allowed)\n" unless $force;
464 warn "Continuing with --force even though both push and mirror projects are disabled\n" unless $quiet;
466 print "Enter settings for new project \"$projname\"\n" unless $defaults;
467 my %settings = ();
468 $settings{noalternates} = $noalternates;
469 if ($nopasswd) {
470 $settings{crypt} = "unknown";
471 } else {
472 my $np1 = prompt_noecho_nl_or_die("Admin password for project $projname (echo is off)");
473 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
474 my $np2 = prompt_noecho_nl_or_die("Retype admin password for project $projname");
475 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
476 "the admin passwords you have entered do not match each other.\n";
477 $settings{crypt} = scrypt_sha1($np1);
479 my $owner = "";
480 unless ($noowner) {
481 $owner = prompt_or_die("Owner/email name for project $projname");
482 unless (valid_email($owner)) {
483 unless ($force) {
484 warn "Your email sure looks weird...?\n";
485 redo;
487 warn "Allowing invalid email with --force\n" unless $quiet;
489 if (length($owner) > 96) {
490 unless ($force) {
491 warn "Your email is longer than 96 characters. Do you really need that much?\n";
492 redo;
494 warn "Allowing email longer than 96 characters with --force\n" unless $quiet;
497 $settings{email} = $owner;
498 my $baseurl = "";
499 my $checkmirror = sub {
500 my $checkurl = shift;
501 unless (valid_repo_url($checkurl)) {
502 unless ($force) {
503 warn "Invalid mirror URL: \"$checkurl\"\n";
504 return undef;
506 warn "Allowing invalid mirror URL with --force\n" unless $quiet;
508 if ($Girocco::Config::restrict_mirror_hosts) {
509 my $mh = extract_url_hostname($checkurl);
510 unless (is_dns_hostname($mh)) {
511 unless ($force) {
512 warn "Invalid non-DNS mirror URL: \"$checkurl\"\n";
513 return undef;
515 warn "Allowing non-DNS mirror URL with --force\n" unless $quiet;
517 if (is_our_hostname($mh)) {
518 unless ($force) {
519 warn "Invalid same-host mirror URL: \"$checkurl\"\n";
520 return undef;
522 warn "Allowing same-host mirror URL with --force\n" unless $quiet;
525 return $checkurl;
527 if ($ispush || $ismirror) {
528 !$ispush || $force || $Girocco::Config::push or
529 die "Push projects are disabled, create a mirror (or use --force)\n";
530 !$ismirror || $force || $Girocco::Config::mirror or
531 die "Mirror projects are disabled, create a push project (or use --force)\n";
532 if ($ismirror) {
533 &$checkmirror($ismirror) or die "Invalid --mirror URL\n";
534 $baseurl = $ismirror;
535 $settings{url} = $baseurl;
536 $settings{cleanmirror} = $fullmirror ? 0 : 1;
537 } else {
538 my @newusers = ();
539 if ($pushusers !~ /^[\s,]*$/) {
540 eval {@newusers = validate_users($pushusers, $force); 1;} or
541 die "Invalid --push user list\n";
543 $settings{users} = \@newusers;
545 } elsif ($force || $Girocco::Config::mirror) {{
546 if ($force || $Girocco::Config::push) {
547 $baseurl = prompt_or_die("URL to mirror from (leave blank for push project)", "");
548 } else {{
549 $baseurl = prompt_or_die("URL to mirror from");
550 unless ($baseurl ne "") {
551 warn "Push projects are disabled, you must enter a mirror URL (or use --force)\n";
552 redo;
555 if ($baseurl ne "") {
556 &$checkmirror($baseurl) or redo;
557 $settings{url} = $baseurl;
558 $settings{cleanmirror} =
559 ynprompt_or_die("Mirror only heads, tags and notes (Y/n)", "Yes");
562 my $mirror = ($baseurl eq "") ? 0 : 1;
563 my $checkdesc = sub {
564 my $d = shift;
565 if (length($d) > 1024) {
566 unless ($force) {
567 warn "Short description length greater than 1024 characters!\n";
568 return undef;
570 warn "Allowing short description length greater than 1024 characters\n" unless $quiet;
572 return $d;
574 if (defined($desc)) {
575 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
576 $desc eq "" || &$checkdesc($desc) or
577 die "Invalid --desc description\n";
578 } elsif (!$defaults) {
579 $desc = prompt_or_die("Short description", "");
580 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
581 $desc eq "" || &$checkdesc($desc) or redo;
582 $desc = undef if $desc eq "";
584 defined($desc) or $desc = $mirror ? "Mirror of $baseurl" : "Push project $projname";
585 $settings{desc} = $desc;
586 my $homepage = "";
587 if (!$defaults) {
588 $homepage = prompt_or_die("Home page URL", "");
589 if ($homepage ne "" && !valid_web_url($homepage)) {
590 unless ($force) {
591 warn "Invalid home page URL: \$homepage\"\n";
592 redo;
594 warn "Allowing invalid home page URL with --force\n" unless $quiet;
597 $settings{hp} = $homepage;
598 my $jsonurl = "";
599 if (!$defaults) {
600 $jsonurl = prompt_or_die("JSON notify POST URL", "");
601 if ($jsonurl ne "" && !valid_web_url($jsonurl)) {
602 unless ($force) {
603 warn "Invalid JSON notify POST URL: \$jsonurl\"\n";
604 redo;
606 warn "Allowing invalid JSON notify POST URL with --force\n" unless $quiet;
609 $settings{notifyjson} = $jsonurl;
610 my $commitaddrs = "";
611 if (!$defaults) {
612 $commitaddrs = clean_addrlist(prompt_or_die("Commit notify email addr(s)", ""));
613 if ($commitaddrs ne "" && !valid_addrlist($commitaddrs)) {
614 unless ($force) {
615 warn"invalid commit notify email address list (use --force to accept): \"$commitaddrs\"\n";
616 redo;
618 warn "using invalid commit notify email address list with --force\n" unless $quiet;
621 $settings{notifymail} = $commitaddrs;
622 $settings{reverseorder} = 1;
623 $settings{reverseorder} = ynprompt_or_die("Oldest-to-newest commit order in emails", "Yes")
624 if !$defaults && $commitaddrs ne "";
625 $settings{summaryonly} = ynprompt_or_die("Summary only (no diff) in emails", "No")
626 if !$defaults && $commitaddrs ne "";
627 my $tagaddrs = "";
628 if (!$defaults) {
629 $tagaddrs = clean_addrlist(prompt_or_die("Tag notify email addr(s)", ""));
630 if ($tagaddrs ne "" && !valid_addrlist($tagaddrs)) {
631 unless ($force) {
632 warn"invalid tag notify email address list (use --force to accept): \"$tagaddrs\"\n";
633 redo;
635 warn "using invalid tag notify email address list with --force\n" unless $quiet;
638 $settings{notifytag} = $tagaddrs;
639 if (!$mirror && !$ispush) {
640 my @newusers = ();
642 my $userlist = prompt_or_die("Push users", join(",", @newusers));
643 eval {@newusers = validate_users($userlist, $force); 1;} or redo;
645 $settings{users} = \@newusers;
647 my $newproj = Girocco::Project->ghost($projname, $mirror, $orphanok, $optp)
648 or die "Girocco::Project->ghost call failed\n";
649 my ($k, $v);
650 $newproj->{$k} = $v while ($k, $v) = each(%settings);
651 my $killowner = sub {
652 system($Girocco::Config::git_bin, '--git-dir='.$newproj->{path},
653 'config', '--unset', "gitweb.owner");
655 if ($mirror) {
656 $newproj->premirror or die "Girocco::Project->premirror failed\n";
657 !$noowner or &$killowner;
658 $newproj->clone or die "Girocco::Project->clone failed\n";
659 warn "Project $projname created and cloning successfully initiated.\n"
660 unless $quiet;
661 } else {
662 $newproj->conjure or die "Girocco::Project->conjure failed\n";
663 !$noowner or &$killowner;
664 warn "New push project fork is empty due to use of --no-alternates\n"
665 if !$quiet && $projname =~ m,/, && $noalternates;
666 warn "Project $projname successfully created.\n" unless $quiet;
668 return 0;
671 sub git_config {
672 my $gd = shift;
673 system($Girocco::Config::git_bin, "--git-dir=$gd", 'config', @_) == 0
674 or die "\"git --git-dir='$gd' config ".join(" ", @_)."\" failed.\n";
677 sub cmd_adopt {
678 my ($force, $type, $nousers, $dryrun, $noowner, $owner, $users, $verbose);
679 parse_options(force => \$force, ":type" => \$type, "no-users" => \$nousers, "dry-run" => \$dryrun,
680 "no-owner" => \$noowner,":owner" => \$owner, quiet => \$quiet, q =>\$quiet, verbose => \$verbose);
681 @ARGV or die "Please give project name on command line.\n";
682 my $projname = shift @ARGV;
683 (!$noowner || !defined($owner)) && (!$nousers || !@ARGV) or die_usage;
684 !defined($type) || $type eq "mirror" || $type eq "push" or die_usage;
685 defined($type) or $type = "";
686 my $projdir;
687 if ($dryrun && $projname =~ m,^/[^.\s/\\:], && is_git_dir(realpath($projname))) {
688 $projdir = realpath($projname);
689 $projname = $projdir;
690 $projname =~ s/\.git$//i;
691 $projname =~ s,/+$,,;
692 $projname =~ s,^.*/,,;
693 $projname ne "" or $projname = $projdir;
694 } else {
695 $projname =~ s/\.git$//i;
696 $projname ne "" or die "Invalid project name \"\".\n";
697 unless (Girocco::Project::does_exist($projname, 1)) {
698 Girocco::Project::valid_name($projname, 1, 1)
699 or die "Invalid project name \"$projname\".\n";
700 die "No such project to adopt: $projname\n";
702 defined(Girocco::Project->load($projname))
703 and die "Project already known (no need to adopt): $projname\n";
704 $projdir = $Girocco::Config::reporoot . "/" . $projname . ".git";
705 is_git_dir($projdir) or die "Not a git directory: \"$projdir\"\n";
707 my $config = read_config_file($projdir . "/config");
708 my %config = ();
709 %config = map {($$_[0], defined($$_[1])?$$_[1]:"true")} @$config if defined($config);
710 git_bool($config{"core.bare"}) or die "Not a bare git repository: \"$projdir\"\n";
711 defined(read_HEAD_symref($projdir)) or die "Project with non-symbolic HEAD ref: \"$projdir\"\n";
712 @ARGV and $users = [validate_users(join(" ", @ARGV), $force, 1, $quiet)];
713 my $desc = "";
714 if (-e "$projdir/description") {
715 open my $fd, '<', "$projdir/description" or die "Cannot open \"$projdir/description\": $!\n";
717 local $/;
718 $desc = <$fd>;
720 close $fd;
721 defined $desc or $desc = "";
722 chomp $desc;
723 $desc = to_utf8($desc, 1);
724 is_default_desc($desc) and $desc = "";
725 if ($desc ne "" && !valid_desc($desc)) {
726 die "invalid 'description' file contents (use --force to accept): \"$desc\"\n"
727 unless $force;
728 warn "using invalid 'description' file contents with --force\n" unless $quiet;
730 $desc = clean_desc($desc);
731 if (length($desc) > 1024) {
732 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
733 unless $force;
734 warn "using longer than 1024 char description with --force\n" unless $quiet;
737 my $readme = "";
738 if (-e "$projdir/README.html") {
739 open my $fd, '<', "$projdir/README.html" or die "Cannot open \"$projdir/README.html\": $!\n";
741 local $/;
742 $readme = <$fd>;
744 close $fd;
745 defined $readme or $readme = "";
746 $readme = to_utf8($readme, 1);
747 $readme =~ s/\r\n?/\n/gs;
748 $readme =~ s/^\s+//s;
749 $readme =~ s/\s+$//s;
750 $readme eq "" or $readme .= "\n";
751 if (length($readme) > 8192) {
752 die "readme greater than 8192 chars is too long (use --force to override)\n"
753 unless $force;
754 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
756 my $rd = get_readme_desc($readme);
757 if ($rd ne "automatic" && $rd ne "suppressed") {
758 my $xmllint = qx(sh -c 'command -v xmllint'); chomp $xmllint;
759 if (-f $xmllint && -x $xmllint) {
760 my $dummy = {README => $readme};
761 my ($cnt, $err) = Girocco::Project::_lint_readme($dummy, 0);
762 if ($cnt) {
763 my $msg = "xmllint: $cnt error";
764 $msg .= "s" unless $cnt == 1;
765 print STDERR "$msg\n", "-" x length($msg), "\n", $err
766 unless $force && $quiet;
767 exit(255) unless $force;
768 warn "$projname: using invalid raw HTML with --force\n" unless $quiet;
770 } else {
771 die "xmllint not available, refusing to use raw HTML without --force\n"
772 unless $force;
773 warn "xmllint not available using unchecked raw HTML with --force\n" unless $quiet;
777 # Inspect any remotes now
778 # Yes, Virginia, remote urls can be multi-valued
779 my %remotes = ();
780 foreach (@$config) {
781 my ($k,$v) = @$_;
782 next unless $k =~ /^remote\.([^\/].*?)\.([^.]+)$/; # remote name cannot start with "/"
783 my ($name, $subkey) = ($1, $2);
784 $remotes{$name}->{skip} = git_bool($v,1), next if $subkey eq "skipdefaultupdate" || $subkey eq "skipfetchall";
785 $remotes{$name}->{mirror} = git_bool($v,1), next if $subkey eq "mirror"; # we might want this
786 $remotes{$name}->{vcs} = $v, next if defined($v) && $v !~ /^\s*$/ && $subkey eq "vcs";
787 push(@{$remotes{$name}->{$subkey}}, $v), next if defined($v) && $v !~ /^\s*$/ &&
788 ($subkey eq "url" || $subkey eq "fetch" || $subkey eq "push" || $subkey eq "pushurl");
790 # remotes.default is the default remote group to fetch for "git remote update" otherwise --all
791 # the remote names in a group are separated by runs of [ \t\n] characters
792 # remote names "", ".", ".." and any name starting with "/" are invalid
793 # a remote with no url or vcs setting is not considered valid
794 my @check = ();
795 my $usingall = 0;
796 if (exists($config{"remotes.default"})) {
797 foreach (split(/[ \t\n]+/, $config{"remotes.default"})) {
798 next unless exists($remotes{$_});
799 my $rmt = $remotes{$_};
800 next if !exists($rmt->{url}) && !$rmt->{vcs};
801 push(@check, $_);
803 } else {
804 $usingall = 1;
805 my %seenrmt = ();
806 foreach (@$config) {
807 my ($k,$v) = @$_;
808 next unless $k =~ /^remote\.([^\/].*?)\.[^.]+$/;
809 next if $seenrmt{$1};
810 $seenrmt{$1} = 1;
811 next unless exists($remotes{$1});
812 my $rmt = $remotes{$1};
813 next if $rmt->{skip} || (!exists($rmt->{url}) && !$rmt->{vcs});
814 push(@check, $1);
817 my @needskip = (); # remotes that need skipDefaultUpdate set to true
818 my $foundvcs = 0;
819 my $foundfetch = 0;
820 my $foundfetchwithmirror = 0;
821 foreach (@check) {
822 my $rmt = $remotes{$_};
823 push(@needskip, $_) if $usingall && !exists($rmt->{fetch});
824 next unless exists($rmt->{fetch});
825 ++$foundfetch;
826 ++$foundfetchwithmirror if $rmt->{mirror};
827 ++$foundvcs if $rmt->{vcs} || (exists($rmt->{url}) && $rmt->{url}->[0] =~ /^[a-zA-Z0-9][a-zA-Z0-9+.-]*::/);
829 # if we have $foundvcs then we need to explicitly set fetch.prune to false
830 # if we have $foundfetch > 1 then we need to explicitly set fetch.prune to false
831 my $neednoprune = !exists($config{"fetch.prune"}) && ($foundvcs || $foundfetch > 1);
832 my $baseurl = "";
833 my $needfakeorigin = 0; # if true we need to set remote.origin.skipDefaultUpdate = true
834 # if remote "origin" exists we always pick up its first url or use ""
835 if (exists($remotes{origin})) {
836 my $rmt = $remotes{origin};
837 $baseurl = exists($rmt->{url}) ? $rmt->{url}->[0] : "";
838 $needfakeorigin = !exists($rmt->{url}) && !$rmt->{vcs} && !$rmt->{skip};
839 } else {
840 $needfakeorigin = 1;
841 # get the first url of the @check remotes
842 foreach (@check) {
843 my $rmt = $remotes{$_};
844 next unless exists($rmt->{url});
845 next unless defined($rmt->{url}->[0]) && $rmt->{url}->[0] ne "";
846 $baseurl = $rmt->{url}->[0];
847 last;
850 my $makemirror = $type eq "mirror" || ($type eq "" && $foundfetch);
852 # If we have $foundfetch we want to make a mirror but complain if
853 # we $foundfetchwithmirror as well unless we have --type=mirror.
854 # Warn if we have --type=push and $foundfetch and !$foundfetchwithmirror.
855 # Warn if we need to set fetch.prune=false when making a mirror
856 # Warn if we need to create remote.origin.skipDefaultUpdate when making a mirror
857 # Complain if @needskip AND !$usingall (warn with --force but don't set skip)
858 # Warn if $usingall and any @needskip (and set them) if making a mirror
859 # Warn if making a mirror and $baseurl eq ""
860 # Warn if we have --type=mirror and !$foundfetch
862 if ($makemirror) {
863 warn "No base URL to mirror from for adopted \"$projname\"\n" unless $quiet || $baseurl ne "";
864 warn "Adopting mirror \"$projname\" without any fetch remotes\n" unless $quiet || $foundfetch;
865 if ($foundfetchwithmirror) {
866 warn "Refusing to adopt mirror \"$projname\" with active remote.<name>.mirror=true remote(s)\n".
867 "(Use --type=mirror to override)\n"
868 unless $type eq "mirror";
869 exit(255) unless $type eq "mirror" || $dryrun;
870 warn "Adopting mirror \"$projname\" with active remote.<name>.mirror=true remotes\n"
871 unless $quiet || $type ne "mirror";
873 warn "Setting explicit fetch.prune=false for adoption of mirror \"$projname\"\n"
874 if !$quiet && $neednoprune;
875 warn "Setting remote.origin.skipDefaultUpdate=true for adoption of mirror \"$projname\"\n"
876 if !$quiet && $needfakeorigin;
877 if (!$usingall && @needskip) {
878 warn "Refusing to adopt mirror empty fetch remote(s) (override with --force)\n"
879 unless $force;
880 exit(255) unless $force || $dryrun;
881 warn "Adopting mirror with empty fetch remote(s) with --force\n"
882 unless $quiet || !$force;
884 warn "Will set skipDefaultUpdate=true on non-fetch remote(s)\n" if !$quiet && $usingall && @needskip;
885 warn "Adopting mirror with base URL \"$baseurl\"\n" unless $quiet || $baseurl eq "";
886 } else {
887 warn "Adopting push \"$projname\" but active non-mirror remotes are present\n"
888 if !$quiet && $foundfetch && !$foundfetchwithmirror;
891 if (!$noowner && !defined($owner)) {
892 # Select the owner
893 $owner = $config{"gitweb.owner"};
894 if (!defined($owner) || $owner eq "") {
895 $owner = $Girocco::Config::admin;
896 warn "Using owner \"$owner\" for adopted project\n" unless $quiet;
899 if (!$nousers && !$makemirror && !defined($users)) {
900 # select user list for push project
901 my $findowner = $owner;
902 defined($findowner) or $findowner = $config{"gitweb.owner"};
903 $findowner = lc($findowner) if defined($findowner);
904 my @owner_users = ();
905 @owner_users = map {$findowner eq lc($$_[4]) ? $$_[1] : ()} get_all_users
906 if defined($findowner) && $findowner ne "";
907 if (@owner_users <= 1) {
908 $users = \@owner_users;
909 warn "No users found that match owner \"$findowner\"\n" unless @owner_users || $quiet;
910 } else {
911 $users = [];
912 warn "Found ".scalar(@owner_users)." users for owner \"$findowner\" (" .
913 join(" ", @owner_users) . ") not setting any\n" unless $quiet;
916 defined($users) or $users = [];
918 # Warn if we preserve an existing receive.denyNonFastForwards or receive.denyDeleteCurrent setting
919 # Complain if core.logallrefupdates or logs subdir exists and contains any files (allow with --force
920 # and warn about preserving the setting)
922 warn "Preserving existing receive.denyNonFastForwards=true\n"
923 if !$quiet && git_bool($config{"receive.denynonfastforwards"});
924 warn "Preserving existing receive.denyDeleteCurrent=$config{'receive.denydeletecurrent'}\n"
925 if !$quiet && exists($config{"receive.denydeletecurrent"}) &&
926 $config{"receive.denydeletecurrent"} ne "warn";
928 my $reflogfiles = Girocco::Project::_contains_files("$projdir/logs");
929 my $reflogactive = git_bool($config{"core.logallrefupdates"});
930 if ($reflogactive || $reflogfiles) {
931 warn "Refusing to adopt \"$projname\" with active ref logs without --force\n" if $reflogfiles && !$force;
932 warn "Refusing to adopt \"$projname\" with core.logAllRefUpdates=true without --force\n" if $reflogactive && !$force;
933 exit(255) unless $force || $dryrun;
934 warn "Adopting \"$projname\" with active ref logs with --force\n" unless $quiet || ($reflogfiles && !$force);
935 warn "Adopting \"$projname\" with core.logAllRefUpdates=true with --force\n" unless $quiet || ($reflogactive && !$force);
938 return 0 if $dryrun && !$verbose;
940 my $newproj = eval {Girocco::Project->ghost($projname, $makemirror, 1, $dryrun)};
941 defined($newproj) or die "Girocco::Project::ghost failed: $@\n";
942 $newproj->{desc} = $desc;
943 $newproj->{README} = $readme;
944 $newproj->{url} = $baseurl if $makemirror || exists($config{"gitweb.baseurl"});
945 $newproj->{email} = $owner if defined($owner);
946 $newproj->{users} = $users;
947 $newproj->{crypt} = "unknown";
948 $newproj->{reverseorder} = 1 unless exists($config{"hooks.reverseorder"});
949 $newproj->{summaryonly} = 1 unless exists($config{"hooks.summaryonly"});
950 my $dummy = bless {}, "Girocco::Project";
951 $dummy->{path} = "$projdir";
952 $dummy->{configfilehash} = \%config;
953 $dummy->_properties_load;
954 delete $dummy->{origurl};
955 foreach my $k (keys(%$dummy)) {
956 $newproj->{$k} = $dummy->{$k}
957 if exists($dummy->{$k}) && !exists($newproj->{$k});
960 if ($verbose) {
961 use Data::Dumper;
962 my %info = %$newproj;
963 $info{README} = get_readme_desc($info{README}) if exists($info{README});
964 my $d = Data::Dumper->new([\%info], ['*'.$newproj->{name}]);
965 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
966 print $d->Dump([\%info], ['*'.$newproj->{name}]);
968 return 0 if $dryrun;
970 # Make any changes as needed for @needskip, $neednoprune and $needfakeorigin
971 if ($makemirror) {
972 git_config($projdir, "fetch.prune", "false") if $neednoprune;
973 git_config($projdir, "remote.origin.skipDefaultUpdate", "true") if $needfakeorigin;
974 if ($usingall && @needskip) {
975 git_config($projdir, "remote.$_.skipDefaultUpdate", "true") foreach @needskip;
979 # Perform the actual adoption
980 $newproj->adopt or die "Girocco::Project::adopt failed\n";
982 # Perhaps restore core.logAllRefUpdates, receive.denyNonFastForwards and receive.denyDeleteCurrent
983 git_config($projdir, "receive.denyNonFastForwards", "true")
984 if git_bool($config{"receive.denynonfastforwards"});
985 git_config($projdir, "receive.denyDeleteCurrent", $config{"receive.denydeletecurrent"})
986 if exists($config{"receive.denydeletecurrent"}) &&
987 $config{"receive.denydeletecurrent"} ne "warn";
988 git_config($projdir, "core.logAllRefUpdates", "true")
989 if $reflogactive;
991 # Success
992 if ($makemirror) {
993 warn "Mirror project \"$projname\" successfully adopted.\n" unless $quiet;
994 } else {
995 warn "Push project \"$projname\" successfully adopted.\n" unless $quiet;
997 return 0;
1000 sub cmd_remove {
1001 my ($force, $reallydel, $keepforks);
1002 parse_options(force => \$force, "really-delete" => \$reallydel,
1003 "keep-forks" => \$keepforks, quiet => \$quiet, q =>\$quiet);
1004 @ARGV or die "Please give project name on command line.\n";
1005 @ARGV == 1 or die_usage;
1006 my $project = get_project($ARGV[0]);
1007 my $projname = $project->{name};
1008 my $isempty = !$project->{mirror} && $project->is_empty;
1009 if (!$project->{mirror} && !$isempty && $reallydel) {
1010 die "refusing to remove and delete non-empty push project without --force: $projname\n" unless $force;
1011 warn "allowing removal and deletion of non-empty push project with --force\n" unless $quiet;
1013 my $altwarn;
1014 my $removenogc;
1015 if ($project->has_forks) {
1016 die "refusing to remove project with forks (use --keep-forks): $projname\n" unless $keepforks;
1017 warn "allowing removal of forked project while preserving its forks with --keep-forks\n" unless $quiet;
1018 # Run pseudo GC on that repository so that objects don't get lost within forks
1019 my $basedir = $Girocco::Config::basedir;
1020 my $projdir = $project->{path};
1021 warn "We have to run pseudo GC on the repo so that the forks don't lose data. Hang on...\n" unless $quiet;
1022 my $nogcrunning = sub {
1023 die "Error: GC appears to be currently running on $projname\n"
1024 if -e "$projdir/gc.pid" || -e "$projdir/.gc_in_progress";
1026 &$nogcrunning;
1027 $removenogc = ! -e "$projdir/.nogc";
1028 recreate_file("$projdir/.nogc") if $removenogc;
1029 die "unable to create \"$projdir/.nogc\"\n" unless -e "$projdir/.nogc";
1030 delete $ENV{show_progress};
1031 $ENV{'show_progress'} = 1 unless $quiet;
1032 sleep 2; # *cough*
1033 &$nogcrunning;
1034 system("$basedir/toolbox/perform-pre-gc-linking.sh", "--include-packs", $projname) == 0
1035 or die "Running pseudo GC on project $projname failed\n";
1036 $altwarn = 1;
1038 my $archived;
1039 if (!$project->{mirror} && !$isempty && !$reallydel) {
1040 $archived = $project->archive_and_delete;
1041 unlink("$archived/.nogc") if $removenogc && defined($archived) && $archived ne "";
1042 } else {
1043 $project->delete;
1045 warn "Project '$projname' removed from $Girocco::Config::name" .
1046 ($archived ? ", backup in '$archived'" : "") .".\n" unless $quiet;
1047 warn "Retained forks may now have unwanted objects/info/alternates lines\n" if $altwarn && !$quiet;
1048 return 0;
1051 sub cmd_show {
1052 use Data::Dumper;
1053 @ARGV == 1 or die_usage;
1054 my $project = get_clean_project($ARGV[0]);
1055 my %info = %$project;
1056 my $d = Data::Dumper->new([\%info], ['*'.$project->{name}]);
1057 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
1058 print $d->Dump([\%info], ['*'.$project->{name}]);
1059 return 0;
1062 sub cmd_listheads {
1063 @ARGV == 1 or die_usage;
1064 my $project = get_project($ARGV[0]);
1065 my @heads = sort({lc($a) cmp lc($b)} $project->get_heads);
1066 my $cur = $project->{HEAD};
1067 defined($cur) or $cur = '';
1068 my $curmark = '*';
1069 my $headhash = get_git("--git-dir=$project->{path}", 'rev-parse', '--quiet', '--verify', 'HEAD');
1070 defined($headhash) or $headhash = '';
1071 chomp $headhash;
1072 $headhash or $curmark = '!';
1073 foreach (@heads) {
1074 my $mark = $_ eq $cur ? $curmark : ' ';
1075 print "$mark $_\n";
1077 return 0;
1080 sub cmd_listtags {
1081 my $vcnt = 0;
1082 shift(@ARGV), $vcnt=1 if @ARGV && ($ARGV[0] eq '--verbose' || $ARGV[0] eq '-v');
1083 @ARGV == 1 or die_usage;
1084 my $project = get_project($ARGV[0]);
1085 if ($vcnt) {
1086 print map("$$_[0]\t$$_[1]\n", get_ctag_counts($project));
1087 } else {
1088 print map("$_\n", $project->get_ctag_names);
1090 return 0;
1093 sub cmd_deltags {
1094 my $ic = 0;
1095 shift(@ARGV), $ic=1 if @ARGV && $ARGV[0] =~ /^(?:--?ignore-case|-i)$/i;
1096 @ARGV >= 2 or die_usage;
1097 my $project = get_project(shift @ARGV);
1098 my %curtags;
1099 if ($ic) {
1100 push(@{$curtags{lc($_)}}, $_) foreach $project->get_ctag_names;
1101 } else {
1102 push(@{$curtags{$_}}, $_) foreach $project->get_ctag_names;
1104 my @deltags = ();
1105 my %seentag = ();
1106 my $ctags = join(" ", @ARGV);
1107 $ctags = lc($ctags) if $ic;
1108 foreach (split(/[\s,]+/, $ctags)) {
1109 next unless exists($curtags{$_});
1110 $seentag{$_}=1, push(@deltags, $_) unless $seentag{$_};
1112 if (!@deltags) {
1113 warn $project->{name}, ": skipping removal of only non-existent tags\n" unless $quiet;
1114 } else {
1115 # Avoid touching anything other than the ctags
1116 foreach my $tg (@deltags) {
1117 $project->delete_ctag($_) foreach @{$curtags{$tg}};
1119 $project->_set_changed;
1120 $project->_set_forkchange;
1121 warn $project->{name}, ": specified tags have been removed\n" unless $quiet;
1123 return 0;
1126 sub cmd_addtags {
1127 @ARGV >= 2 or die_usage;
1128 my $project = get_project(shift @ARGV);
1129 my $ctags = join(" ", @ARGV);
1130 $ctags =~ /[^, a-zA-Z0-9:.+#_-]/ and
1131 die "Content tag(s) \"$ctags\" contain(s) evil character(s).\n";
1132 my $oldmask = umask();
1133 umask($oldmask & ~0060);
1134 my $changed = 0;
1135 foreach (split(/[\s,]+/, $ctags)) {
1136 ++$changed if $project->add_ctag($_, 1);
1138 if ($changed) {
1139 $project->_set_changed;
1140 $project->_set_forkchange;
1142 umask($oldmask);
1143 my $cnt = ($changed == 1) ? "1 content tag has" : $changed . " content tags have";
1144 warn $project->{name}, ": $cnt been added/updated\n" unless $quiet;
1145 return 0;
1148 sub _get_random_val {
1149 my $p = shift;
1150 my $md5;
1152 no warnings;
1153 $md5 = md5_hex(time . $$ . rand() . join(':',%$p));
1155 $md5;
1158 sub cmd_chpass {
1159 my $force = 0;
1160 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1161 my $random = undef;
1162 pop(@ARGV), $random=lc($ARGV[1]) if @ARGV==2 && $ARGV[1] =~ /^(?:random|unknown)$/i;
1163 @ARGV == 1 or die_usage;
1164 my $project = get_project($ARGV[0]);
1165 die "refusing to change locked password of project \"$ARGV[0]\" without --force\n"
1166 if $project->is_password_locked;
1167 my ($newpw, $rmsg);
1168 if ($random) {
1169 if ($random eq "random") {
1170 die "refusing to set random password without --force\n" unless $force;
1171 $rmsg = "set to random value";
1172 $newpw = _get_random_val($project);
1173 } else {
1174 die "refusing to set password hash to '$random' without --force\n" unless $force;
1175 $rmsg = "hash set to '$random'";
1176 $newpw = $random;
1178 } else {
1179 $rmsg = "updated";
1180 if (-t STDIN) {
1181 print "Changing admin password for project $ARGV[0]\n";
1182 my $np1 = prompt_noecho_nl_or_die("New password for project $ARGV[0] (echo is off)");
1183 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1184 my $np2 = prompt_noecho_nl_or_die("Retype new password for project $ARGV[0]");
1185 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
1186 "the admin passwords you have entered do not match each other.\n";
1187 $newpw = $np1;
1188 } else {
1189 $newpw = <STDIN>;
1190 defined($newpw) or die "missing new password on STDIN\n";
1191 chomp($newpw);
1194 $newpw ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1195 my $old = $project->{crypt};
1196 $project->{crypt} = (defined($random) && $random ne "random") ? $newpw : scrypt_sha1($newpw);
1197 if (defined($old) && $old eq $project->{crypt}) {
1198 warn $project->{name}, ": skipping update of password hash to same value\n" unless $quiet;
1199 } else {
1200 # Avoid touching anything other than the password hash
1201 $project->_group_update;
1202 warn $project->{name}, ": admin password $rmsg (new hash stored)\n" unless $quiet;
1204 return 0;
1207 sub cmd_checkpw {
1208 @ARGV == 1 or die_usage;
1209 my $project = get_project($ARGV[0]);
1210 my $pwhash = $project->{crypt};
1211 defined($pwhash) or $pwhash = "";
1212 if ($pwhash eq "") {
1213 warn $project->{name}, ": no password required\n" unless $quiet;
1214 return 0;
1216 if ($project->is_password_locked) {
1217 warn $project->{name}, ": password is locked\n" unless $quiet;
1218 exit 1;
1220 my $checkpw;
1221 if (-t STDIN) {
1222 $checkpw = prompt_noecho_nl_or_die("Admin password for project $ARGV[0] (echo is off)");
1223 $checkpw ne "" or warn "checking for empty password as hash (very unlikely)\n" unless $quiet;
1224 } else {
1225 $checkpw = <STDIN>;
1226 defined($checkpw) or die "missing admin password on STDIN\n";
1227 chomp($checkpw);
1229 unless (Girocco::CLIUtil::check_passwd_match($pwhash, $checkpw)) {
1230 warn "password check failure\n" unless $quiet;
1231 exit 1;
1233 warn "admin password match\n" unless $quiet;
1234 return 0;
1237 sub cmd_gc {
1238 my ($force, $auto, $redelta, $recompress);
1239 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, auto => \$auto,
1240 redelta => \$redelta, "no-reuse-delta" => \$redelta, aggressive => \$force,
1241 recompress => \$recompress, "no-reuse-object" => $recompress);
1242 $force && $auto and die "--force and --auto are mutually exclusive options\n";
1243 @ARGV or die "Please give project name on command line.\n";
1244 @ARGV == 1 or die_usage;
1245 my $project = get_project($ARGV[0]);
1246 delete $ENV{show_progress};
1247 delete $ENV{force_gc};
1248 $quiet or $ENV{"show_progress"} = 1;
1249 $force and $ENV{"force_gc"} = 1;
1250 if (!$auto && !$force && ! -e $project->{path}."/.needsgc") {
1251 open NEEDSGC, '>', $project->{path}."/.needsgc" and close NEEDSGC;
1253 my @args = ($Girocco::Config::basedir . "/jobd/gc.sh", $project->{name});
1254 $redelta && !$recompress and push(@args, "-f");
1255 $recompress and push(@args, "-F");
1256 my $lastgc = $project->{lastgc};
1257 system({$args[0]} @args) != 0 and return 1;
1258 # Do it again Sam, but only if lastgc was set, gc.sh succeeded and now it's not set
1259 if ($lastgc) {
1260 my $newlastgc = get_git("--git-dir=$project->{path}", 'config', '--get', 'gitweb.lastgc');
1261 if (!$newlastgc) {
1262 system({$args[0]} @args) != 0 and return 1;
1265 return 0;
1268 sub cmd_update {
1269 my ($force, $summary);
1270 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, summary => \$summary);
1271 $quiet && $summary and die "--quiet and --summary are mutually exclusive options\n";
1272 @ARGV or die "Please give project name on command line.\n";
1273 @ARGV == 1 or die_usage;
1274 my $project = get_project($ARGV[0]);
1275 $project->{mirror} or die "Project \"$ARGV[0]\" is a push project, not a mirror project.\n";
1276 delete $ENV{show_progress};
1277 delete $ENV{force_update};
1278 if ($quiet) {
1279 $ENV{"show_progress"} = 0;
1280 } else {
1281 $ENV{"show_progress"} = ($summary ? 1 : 2);
1283 $force and $ENV{"force_update"} = 1;
1284 system($Girocco::Config::basedir . "/jobd/update.sh", $project->{name}) != 0 and return 1;
1285 return 0;
1288 sub cmd_remirror {
1289 my $force = 0;
1290 parse_options(force => \$force, quiet => \$quiet, q => \$quiet);
1291 @ARGV or die "Please give project name on command line.\n";
1292 @ARGV == 1 or die_usage;
1293 my $project = get_project($ARGV[0]);
1294 $project->{mirror} or die "Project \"$ARGV[0]\" is a push project, not a mirror project.\n";
1295 if ($project->{clone_in_progress} && !$project->{clone_failed}) {
1296 warn "Project \"$ARGV[0]\" already seems to have a clone underway at this moment.\n" unless $quiet && $force;
1297 exit(255) unless $force;
1298 yes_to_continue_or_die("Are you sure you want to force a remirror");
1300 unlink($project->_clonefail_path);
1301 unlink($project->_clonelog_path);
1302 recreate_file($project->_clonep_path);
1303 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or
1304 die "cannot connect to taskd.socket: $!\n";
1305 select((select($sock),$|=1)[0]);
1306 $sock->print("clone ".$project->{name}."\n");
1307 # Just ignore reply, we are going to succeed anyway and the I/O
1308 # would apparently get quite hairy.
1309 $sock->flush();
1310 sleep 2; # *cough*
1311 $sock->close();
1312 warn "Project \"$ARGV[0]\" remirror initiated.\n" unless $quiet;
1313 return 0;
1316 sub cmd_setowner {
1317 my $force = 0;
1318 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1319 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1320 my $project = get_project($ARGV[0]);
1321 if (@ARGV == 2 && !valid_email($ARGV[1])) {
1322 die "invalid owner/email (use --force to accept): \"$ARGV[1]\"\n"
1323 unless $force;
1324 warn "using invalid owner/email with --force\n" unless $quiet;
1326 if (@ARGV == 2 && length($ARGV[1]) > 96) {
1327 die "owner/email longer than 96 chars (use --force to accept): \"$ARGV[1]\"\n"
1328 unless $force;
1329 warn "using longer than 96 char owner/email with --force\n" unless $quiet;
1331 my $old = $project->{email};
1332 if (@ARGV == 1) {
1333 print "$old\n" if defined($old);
1334 return 0;
1336 if (defined($old) && $old eq $ARGV[1]) {
1337 warn $project->{name}, ": skipping update of owner/email to same value\n" unless $quiet;
1338 } else {
1339 # Avoid touching anything other than "gitweb.owner"
1340 $project->_property_fput("email", $ARGV[1]);
1341 $project->_update_index;
1342 $project->_set_changed;
1343 warn $project->{name}, ": owner/email updated to \"$ARGV[1]\"\n" unless $quiet;
1345 return 0;
1348 sub cmd_setdesc {
1349 my $force = 0;
1350 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1351 @ARGV >= 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1352 my $project = get_project(shift @ARGV);
1353 if (@ARGV && !valid_desc(join(" ", @ARGV))) {
1354 die "invalid description (use --force to accept): \"".join(" ", @ARGV)."\"\n"
1355 unless $force;
1356 warn "using invalid description with --force\n" unless $quiet;
1358 my $desc = clean_desc(join(" ", @ARGV));
1359 if (@ARGV && length($desc) > 1024) {
1360 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
1361 unless $force;
1362 warn "using longer than 1024 char description with --force\n" unless $quiet;
1364 my $old = $project->{desc};
1365 if (!@ARGV) {
1366 print "$old\n" if defined($old);
1367 return 0;
1369 if (defined($old) && $old eq $desc) {
1370 warn $project->{name}, ": skipping update of description to same value\n" unless $quiet;
1371 } else {
1372 # Avoid touching anything other than description file
1373 $project->_property_fput("desc", $desc);
1374 $project->_set_changed;
1375 warn $project->{name}, ": description updated to \"$desc\"\n" unless $quiet;
1377 return 0;
1380 sub cmd_setreadme {
1381 my $force = 0;
1382 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1383 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1384 my $project = get_project($ARGV[0]);
1385 my $old = $project->{README};
1386 if (@ARGV == 1) {
1387 chomp $old if defined($old);
1388 print "$old\n" if defined($old) && $old ne "";
1389 return 0;
1391 my ($new, $raw, $newname);
1392 $newname = '';
1393 if ($ARGV[1] eq "-") {
1394 local $/;
1395 $new = <STDIN>;
1396 $raw = 1;
1397 $newname = "contents of <STDIN>";
1398 } elsif (lc($ARGV[1]) eq "automatic" || lc($ARGV[1]) eq "auto") {
1399 $new = "";
1400 } elsif (lc($ARGV[1]) eq "suppressed" || lc($ARGV[1]) eq "suppress") {
1401 $new = "<!-- suppress -->";
1402 } else {
1403 my $fn = $ARGV[1];
1404 $fn =~ s/^\@//;
1405 die "missing filename for README\n" unless $fn ne "";
1406 die "no such file: \"$fn\"\n" unless -f $fn && -r $fn;
1407 open F, '<', $fn or die "cannot open \"$fn\" for reading: $!\n";
1408 local $/;
1409 $new = <F>;
1410 close F;
1411 $raw = 1;
1412 $newname = "contents of \"$fn\"";
1414 defined($new) or $new = '';
1415 $project->{README} = to_utf8($new, 1);
1416 $project->_cleanup_readme;
1417 if (length($project->{README}) > 8192) {
1418 die "readme greater than 8192 chars is too long (use --force to override)\n"
1419 unless $force;
1420 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
1422 if ($raw) {
1423 my $rd = get_readme_desc($project->{README});
1424 if ($rd ne "automatic" && $rd ne "suppressed") {
1425 my $xmllint = qx(command -v xmllint); chomp $xmllint;
1426 if (-f $xmllint && -x $xmllint) {
1427 my ($cnt, $err) = $project->_lint_readme(0);
1428 if ($cnt) {
1429 my $msg = "xmllint: $cnt error";
1430 $msg .= "s" unless $cnt == 1;
1431 print STDERR "$msg\n", "-" x length($msg), "\n", $err
1432 unless $force && $quiet;
1433 exit(255) unless $force;
1434 warn $project->{name} . ": using invalid raw HTML with --force\n" unless $quiet;
1436 } else {
1437 die "xmllint not available, refusing to use raw HTML without --force\n"
1438 unless $force;
1439 warn "xmllint not available using unchecked raw HTML with --force\n" unless $quiet;
1443 if (defined($old) && $old eq $project->{README}) {
1444 warn $project->{name}, ": skipping update of README to same value\n" unless $quiet;
1445 } else {
1446 # Avoid touching anything other than README.html file
1447 $project->_property_fput("README", $project->{README});
1448 $project->_set_changed;
1449 my $desc = get_readme_desc($project->{README});
1450 if ($newname) {
1451 $newname .= " ($desc)";
1452 } else {
1453 $newname = $desc;
1455 warn $project->{name}, ": README updated to $newname\n" unless $quiet;
1457 return 0;
1460 sub valid_head {
1461 my ($proj, $newhead) = @_;
1462 my %okheads = map({($_ => 1)} $proj->get_heads);
1463 exists($okheads{$newhead});
1466 sub cmd_sethead {
1467 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1468 my $project = get_project($ARGV[0]);
1469 if (@ARGV == 2 && !valid_head($project, $ARGV[1])) {
1470 die "invalid head (try \"@{[basename($0)]} listheads $ARGV[0]\"): \"$ARGV[1]\"\n";
1472 my $old = $project->{HEAD};
1473 if (@ARGV == 1) {
1474 print "$old\n" if defined($old);
1475 return 0;
1477 if (defined($old) && $old eq $ARGV[1]) {
1478 warn $project->{name}, ": skipping update of HEAD symref to same value\n" unless $quiet;
1479 } else {
1480 # Avoid touching anything other than the HEAD symref
1481 $project->set_HEAD($ARGV[1]);
1482 $project->_set_changed;
1483 warn $project->{name}, ": HEAD symref updated to \"refs/heads/$ARGV[1]\"\n" unless $quiet;
1485 return 0;
1488 sub cmd_sethooks {
1489 my $force = 0;
1490 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1491 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1492 my $project = get_project($ARGV[0]);
1493 my $projconfig = read_config_file_hash($project->{path}."/config");
1494 my $ghp = $Girocco::Config::reporoot."/_global/hooks";
1495 my $rghp = realpath($ghp);
1496 my $lhp = $project->{path}."/hooks";
1497 my $rlhp = realpath($lhp);
1498 my $ahp = "";
1499 my $rahp = undef;
1500 if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) {
1501 $ahp = $projconfig->{"core.hookspath"};
1502 $rahp = realpath($ahp);
1504 if (@ARGV == 1) {
1505 if (defined($rahp) && $rahp ne "") {
1506 if ($rahp eq $rghp) {
1507 my $nc = ($ahp eq $ghp ? "" : " non-canonical");
1508 printf "%s \t(global%s)\n", $ahp, $nc;
1509 } elsif ($rahp eq $rlhp) {
1510 my $nc = ($ahp eq $lhp ? "" : " non-canonical");
1511 printf "%s \t(local%s)\n", $ahp, $nc;
1512 } elsif ($rahp ne $ahp) {
1513 print "$ahp \t($rahp)\n";
1514 } else {
1515 print "$ahp\n";
1517 } elsif ($ahp ne "") {
1518 print "$ahp \t(non-existent)\n";
1520 return 0;
1522 my $shp = $ARGV[1];
1523 if (lc($shp) eq "global") {
1524 $shp = $ghp;
1525 } elsif (lc($shp) eq "local") {
1526 $shp = $lhp;
1527 } elsif (substr($shp, 0, 2) eq "~/") {
1528 $shp = $ENV{"HOME"}.substr($shp,1);
1529 } elsif ($shp =~ m,^~([a-zA-Z_][a-zA-Z_0-9]*)((?:/.*)?)$,) {
1530 my $sfx = $2;
1531 my $hd = (getpwnam($1))[7];
1532 $shp = $hd . $sfx if defined($hd) && $hd ne "" && $hd ne "/" && -d $hd;
1534 $shp ne "" && -d $shp or die "no such directory: $ARGV[1]\n";
1535 my $rshp = realpath($shp);
1536 defined($rshp) && $rshp ne "" or die "could not realpath: $ARGV[1]\n";
1537 $rshp =~ m,^/[^/], or die "invalid hookspath: $rshp\n";
1538 die "refusing to switch from current non-global hookspath without --force\n"
1539 if !$force && defined($rahp) && $rahp ne "" && $rahp ne $rghp && $rshp ne $rahp;
1540 if (!$force && defined($rahp) && $rahp ne "") {
1541 if ($rshp eq $rahp && ($ahp eq $ghp || $ahp eq $lhp)) {
1542 warn $project->{name}, ": skipping update of hookspath to same effective value\n" unless $quiet;
1543 return 0;
1546 $rshp = $ghp if $rshp eq $rghp;
1547 $rshp = $lhp if $rshp eq $rlhp;
1548 if ($rshp eq $ahp) {
1549 warn $project->{name}, ": skipping update of hookspath to same value\n" unless $quiet;
1550 return 0;
1552 die "refusing to set neither local nor global hookspath without --force\n"
1553 if !$force && $rshp ne $ghp && $rshp ne $lhp;
1554 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1555 'config', "core.hookspath", $rshp);
1556 my $newval = '"'.$rshp.'"';
1557 $newval = "global" if $rshp eq $ghp;
1558 $newval = "local" if $rshp eq $lhp;
1559 warn $project->{name}, ": hookspath set to $newval\n" unless $quiet;
1560 return 0;
1563 our %boolfields;
1564 BEGIN {
1565 %boolfields = (
1566 cleanmirror => 1,
1567 reverseorder => 0,
1568 summaryonly => 0,
1569 statusupdates => 1,
1573 sub cmd_setbool {
1574 my $force = 0;
1575 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1576 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1577 my $project = get_project($ARGV[0]);
1578 if (!exists($boolfields{$ARGV[1]})) {
1579 die "invalid boolean field name: \"$ARGV[1]\" -- try \"help\"\n";
1581 if (@ARGV == 3 && $boolfields{$ARGV[1]} && !$project->{mirror}) {
1582 die "invalid boolean field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
1583 unless $force;
1584 warn "using mirror field on non-mirror with --force\n" unless $quiet;
1586 if (@ARGV == 3 && !valid_bool($ARGV[2])) {
1587 die "invalid boolean value: \"$ARGV[2]\"\n";
1589 my $bool = clean_bool($ARGV[2]);
1590 my $old = $project->{$ARGV[1]};
1591 if (@ARGV == 2) {
1592 print "$old\n" if defined($old);
1593 return 0;
1595 if (defined($old) && $old eq $bool) {
1596 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
1597 } else {
1598 # Avoid touching anything other than $ARGV[1] field
1599 $project->_property_fput($ARGV[1], $bool);
1600 warn $project->{name}, ": $ARGV[1] updated to $bool\n" unless $quiet;
1602 return 0;
1605 sub cmd_setautogchack {
1606 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1607 my $project = get_project($ARGV[0]);
1608 my $aghok = $Girocco::Config::autogchack &&
1609 ($project->{mirror} || $Girocco::Config::autogchack ne "mirror");
1610 my $old = defined($project->{autogchack}) ? clean_bool($project->{autogchack}) : "unset";
1611 if (@ARGV == 1) {
1612 print "$old\n" if $aghok;
1613 return 0;
1615 my $bool;
1616 if (lc($ARGV[1]) eq "unset") {
1617 $bool = "unset";
1618 } else {
1619 valid_bool($ARGV[1]) or die "invalid boolean value: \"$ARGV[1]\"\n";
1620 $bool = clean_bool($ARGV[1]);
1622 if (!$aghok) {
1623 die "\$Girocco::Config::autogchack is false\n" unless $Girocco::Config::autogchack;
1624 die "\$Girocco::Config::autogchack is only enabled for mirrors\n";
1626 if ($old eq $bool) {
1627 warn $project->{name}, ": autogchack value unchanged\n" unless $quiet;
1628 } else {
1629 if ($bool eq "unset") {
1630 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1631 'config', '--unset', "girocco.autogchack");
1632 } else {
1633 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1634 'config', '--bool', "girocco.autogchack", $bool);
1637 return system($Girocco::Config::basedir . "/jobd/maintain-auto-gc-hack.sh", $project->{name}) == 0
1638 ? 0 : 1;
1641 sub valid_url {
1642 my ($url, $type) = @_;
1643 $type ne 'baseurl' and return valid_web_url($url);
1644 valid_repo_url($url) or return 0;
1645 if ($Girocco::Config::restrict_mirror_hosts) {
1646 my $mh = extract_url_hostname($url);
1647 is_dns_hostname($mh) or return 0;
1648 !is_our_hostname($mh) or return 0;
1650 return 1;
1653 our %urlfields;
1654 BEGIN {
1655 %urlfields = (
1656 baseurl => ["url" , 1],
1657 homepage => ["hp" , 0],
1658 notifyjson => ["notifyjson", 0],
1662 sub cmd_seturl {
1663 my $force = 0;
1664 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1665 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1666 my $project = get_project($ARGV[0]);
1667 if (!exists($urlfields{$ARGV[1]})) {
1668 die "invalid URL field name: \"$ARGV[1]\" -- try \"help\"\n";
1670 if (@ARGV == 3 && ${$urlfields{$ARGV[1]}}[1] && !$project->{mirror}) {
1671 die "invalid URL field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
1672 unless $force;
1673 warn "using mirror field on non-mirror with --force\n" unless $quiet;
1675 if (@ARGV == 3 && !valid_url($ARGV[2], $ARGV[1])) {
1676 die "invalid URL (use --force to accept): \"$ARGV[2]\"\n"
1677 unless $force;
1678 warn "using invalid URL with --force\n" unless $quiet;
1680 my $old = $project->{${$urlfields{$ARGV[1]}}[0]};
1681 if (@ARGV == 2) {
1682 print "$old\n" if defined($old);
1683 return 0;
1685 if (defined($old) && $old eq $ARGV[2]) {
1686 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
1687 } else {
1688 # Avoid touching anything other than $ARGV[1]'s field
1689 $project->_property_fput(${$urlfields{$ARGV[1]}}[0], $ARGV[2]);
1690 if ($ARGV[1] eq "baseurl") {
1691 $project->{url} = $ARGV[2];
1692 $project->_set_bangagain;
1694 $project->_set_changed unless $ARGV[1] eq "notifyjson";
1695 warn $project->{name}, ": $ARGV[1] updated to $ARGV[2]\n" unless $quiet;
1697 return 0;
1700 our %msgsfields;
1701 BEGIN {
1702 %msgsfields = (
1703 notifymail => 1,
1704 notifytag => 1,
1708 sub cmd_setmsgs {
1709 my $force = 0;
1710 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1711 @ARGV >= 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1712 my $project = get_project(shift @ARGV);
1713 my $field = shift @ARGV;
1714 if (!exists($msgsfields{$field})) {
1715 die "invalid msgs field name: \"$field\" -- try \"help\"\n";
1717 if (@ARGV && !valid_addrlist(@ARGV)) {
1718 die "invalid email address list (use --force to accept): \"".join(" ",@ARGV)."\"\n"
1719 unless $force;
1720 warn "using invalid email address list with --force\n" unless $quiet;
1722 my $old = $project->{$field};
1723 if (!@ARGV) {
1724 printf "%s\n", clean_addrlist($old, " ") if defined($old);
1725 return 0;
1727 my $newlist = clean_addrlist(join(" ",@ARGV));
1728 if (defined($old) && $old eq $newlist) {
1729 warn $project->{name}, ": skipping update of $field to same value\n" unless $quiet;
1730 } else {
1731 # Avoid touching anything other than $field's field
1732 $project->_property_fput($field, $newlist);
1733 warn $project->{name}, ": $field updated to \"$newlist\"\n" unless $quiet;
1735 return 0;
1738 sub cmd_setusers {
1739 my $force = 0;
1740 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1741 @ARGV >= 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1742 my $project = get_project(shift @ARGV);
1743 my $projname = $project->{name};
1744 !@ARGV || !$project->{mirror} or die "cannot set users list for mirror project: \"$projname\"\n";
1745 my @newusers = ();
1746 if (@ARGV) {
1747 eval {@newusers = validate_users(join(" ", @ARGV), $force); 1;} or exit 255;
1748 die "refusing to set empty users list without --force\n" unless @newusers || $force;
1750 return 0 if !@ARGV && $project->{mirror};
1751 my $oldusers = $project->{users};
1752 if ($oldusers && ref($oldusers) eq "ARRAY") {
1753 $oldusers = join("\n", @$oldusers);
1754 } else {
1755 $oldusers = "";
1757 if (!@ARGV) {
1758 print "$oldusers\n" if $oldusers ne "";
1759 return 0;
1761 if ($oldusers eq join("\n", @newusers)) {
1762 warn "$projname: skipping update of users list to same value\n" unless $quiet;
1763 } else {
1764 # Avoid touching anything other than the users list
1765 $project->{users} = \@newusers;
1766 $project->_update_users;
1767 warn "$projname: users list updated to \"@{[join(',',@newusers)]}\"\n" unless $quiet;
1769 return 0;
1772 our %fieldnames;
1773 BEGIN {
1774 %fieldnames = (
1775 owner => [\&cmd_setowner, 0],
1776 desc => [\&cmd_setdesc, 0],
1777 description => [\&cmd_setdesc, 0],
1778 readme => [\&cmd_setreadme, 0],
1779 head => [\&cmd_sethead, 0],
1780 HEAD => [\&cmd_sethead, 0],
1781 hooks => [\&cmd_sethooks, 0],
1782 hookspath => [\&cmd_sethooks, 0],
1783 cleanmirror => [\&cmd_setbool, 1],
1784 reverseorder => [\&cmd_setbool, 1],
1785 summaryonly => [\&cmd_setbool, 1],
1786 statusupdates => [\&cmd_setbool, 1],
1787 autogchack => [\&cmd_setautogchack, 0],
1788 baseurl => [\&cmd_seturl, 1],
1789 homepage => [\&cmd_seturl, 1],
1790 notifyjson => [\&cmd_seturl, 1],
1791 notifymail => [\&cmd_setmsgs, 1],
1792 notifytag => [\&cmd_setmsgs, 1],
1793 users => [\&cmd_setusers, 0],
1797 sub do_getset {
1798 $setopt = shift;
1799 my @newargs = ();
1800 push(@newargs, shift) if @_ && $_[0] eq '--force';
1801 my $field = $_[1];
1802 (($setopt && @_ >= 3) || @_ == 2) && exists($fieldnames{$field}) or die_usage;
1803 push(@newargs, shift);
1804 shift unless ${$fieldnames{$field}}[1];
1805 push(@newargs, @_);
1806 diename(($setopt ? "set " : "get ") . $field);
1807 @ARGV = @newargs;
1808 &{${$fieldnames{$field}}[0]}(@ARGV);
1811 sub cmd_get {
1812 do_getset(0, @_);
1815 sub cmd_set {
1816 do_getset(1, @_);
1819 our %commands;
1820 BEGIN {
1821 %commands = (
1822 list => \&cmd_list,
1823 create => \&cmd_create,
1824 adopt => \&cmd_adopt,
1825 remove => \&cmd_remove,
1826 trash => \&cmd_remove,
1827 delete => \&cmd_remove,
1828 show => \&cmd_show,
1829 listheads => \&cmd_listheads,
1830 listtags => \&cmd_listtags,
1831 listctags => \&cmd_listtags,
1832 deltags => \&cmd_deltags,
1833 delctags => \&cmd_deltags,
1834 addtags => \&cmd_addtags,
1835 addctags => \&cmd_addtags,
1836 chpass => \&cmd_chpass,
1837 checkpw => \&cmd_checkpw,
1838 gc => \&cmd_gc,
1839 update => \&cmd_update,
1840 remirror => \&cmd_remirror,
1841 setowner => \&cmd_setowner,
1842 setdesc => \&cmd_setdesc,
1843 setdescription => \&cmd_setdesc,
1844 setreadme => \&cmd_setreadme,
1845 sethead => \&cmd_sethead,
1846 sethooks => \&cmd_sethooks,
1847 sethookspath => \&cmd_sethooks,
1848 setbool => \&cmd_setbool,
1849 setboolean => \&cmd_setbool,
1850 setflag => \&cmd_setbool,
1851 setautogchack => \&cmd_setautogchack,
1852 seturl => \&cmd_seturl,
1853 setmsgs => \&cmd_setmsgs,
1854 setusers => \&cmd_setusers,
1855 get => \&cmd_get,
1856 set => \&cmd_set,
1860 sub dohelp {
1861 my $cmd = shift;
1862 my $bn = basename($0);
1863 printf "%s version %s\n\n", $bn, $VERSION;
1864 if (defined($cmd) && $cmd ne '') {
1865 $cmd =~ s/^set(?=[a-zA-Z])//i;
1866 my $cmdhelp = '';
1867 my ($lastmt, $incmd);
1868 foreach (split('\n', sprintf($help, $bn))) {
1869 $lastmt || $incmd or $lastmt = /^\s*$/, next;
1870 $incmd = 1 if $lastmt && /^\s*(?:\[?set\]?)?$cmd\s/;
1871 last if $incmd && /^\s*$/;
1872 $incmd and $cmdhelp .= $_ . "\n";
1873 $lastmt = /^\s*$/;
1875 print $cmdhelp and exit 0 if $cmdhelp;
1877 printf $help, $bn;
1878 exit 0;
1881 sub main {
1882 local *ARGV = \@_;
1883 shift, $quiet=1 if @ARGV && $ARGV[0] =~ /^(?:-q|--quiet)$/i;
1884 dohelp($ARGV[1]) if !@ARGV || @ARGV && $ARGV[0] =~ /^(?:-h|-?-help|help)$/i;
1885 my $command = shift;
1886 diename($command);
1887 $setopt = 1;
1888 if (!exists($commands{$command}) && exists($commands{"set".$command})) {
1889 $setopt = 0;
1890 $command = "set" . $command;
1892 exists($commands{$command}) or die "Unknown command \"$command\" -- try \"help\"\n";
1893 dohelp($command) if @ARGV && ($ARGV[0] =~ /^(?:-h|-?-help)$/i ||
1894 $ARGV[0] =~ /^help$/i && !Girocco::Project::does_exist("help",1));
1895 &{$commands{$command}}(@ARGV);