projtool.pl: format bundle sizes with comma separators
[girocco.git] / toolbox / projtool.pl
blob7a6f49edf4fd3918fe4fdb0d44156d9574890ccf
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 my $owner = $project->{owner};
253 if ($owner) {
254 $owner = lc($owner);
255 my @owner_users = map {$owner eq lc($$_[4]) ? $$_[1] : ()} get_all_users;
256 $project->{owner_users} = \@owner_users if @owner_users;
258 my $projname = $project->{name};
259 my @forks = grep {$$_[1] =~ m,^$projname/,} get_all_projects;
260 $project->{has_forks} = 1 if @forks;
261 $project->{has_alternates} = 1 if $project->has_alternates;
262 my @bundles = $project->bundles;
263 for (my $i = 0; $i < @bundles; ++$i) {
264 my $secs = $bundles[$i]->[0];
265 $bundles[$i]->[0] = strftime("%Y-%m-%dT%H:%M:%S%z", localtime($secs));
266 my $sz = $bundles[$i]->[2];
267 1 while $sz =~ s/(?<=\d)(\d{3})(?:,|$)/,$1/g;
268 $bundles[$i]->[2] = $sz;
270 delete $project->{bundles};
271 $project->{bundles} = \@bundles if @bundles;
272 $project->{mirror} = 0 unless $project->{mirror};
273 $project->{is_empty} = 1 if $project->is_empty;
274 delete $project->{showpush} unless $project->{showpush};
275 delete $project->{users} if $project->{mirror};
276 delete $project->{baseurl} unless $project->{mirror};
277 delete $project->{banged} unless $project->{mirror};
278 delete $project->{lastrefresh} unless $project->{mirror};
279 delete $project->{cleanmirror} unless $project->{mirror};
280 delete $project->{statusupdates} unless $project->{mirror};
281 delete $project->{lastparentgc} unless $projname =~ m,/,;
282 unless ($project->{banged}) {
283 delete $project->{bangcount};
284 delete $project->{bangfirstfail};
285 delete $project->{bangmessagesent};
287 my $projhook = $project->_has_notifyhook;
288 if (defined($projhook) && $projhook ne "") {
289 $project->{notifyhook} = $projhook;
290 } else {
291 delete $project->{notifyhook};
293 $project->{README} = get_readme_desc($project->{README}) if exists($project->{README});
294 my @tags = get_ctag_counts($project, 1);
295 $project->{tags} = \@tags if @tags;
296 my $projconfig = read_config_file_hash($project->{path}."/config");
297 if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) {
298 my $ahp = $projconfig->{"core.hookspath"};
299 my $rahp = realpath($ahp);
300 my $lhp = $project->{path}."/hooks";
301 my $rlhp = realpath($lhp);
302 my $ghp = $Girocco::Config::reporoot."/_global/hooks";
303 my $rghp = realpath($ghp);
304 $project->{has_local_hooks} = 1 if
305 defined($rahp) && defined($rlhp) && $rahp eq $rlhp;
306 $project->{has_global_hooks} = 1 if
307 defined($rahp) && defined($rghp) && $rahp eq $rghp;
308 $project->{hookspath} = $ahp unless $ahp eq $lhp || $ahp eq $ghp;
310 $project;
313 sub clean_addrlist {
314 my %seen = ();
315 my @newlist = ();
316 foreach (split(/[,\s]+/, $_[0])) {
317 next unless $_;
318 $seen{lc($_)} = 1, push(@newlist, $_) unless $seen{lc($_)};
320 return join(($_[1]||","), @newlist);
323 sub valid_addrlist {
324 my $cleaned = clean_addrlist(join(" ", @_));
325 return 1 if $cleaned eq "";
326 valid_email_multi($cleaned) && length($cleaned) <= 512;
329 sub validate_users {
330 my ($userlist, $force, $nodie, $quiet) = @_;
331 my @newusers = ();
332 my $badlist = 0;
333 my %seenuser = ();
334 my $mobok = $Girocco::Config::mob && $Girocco::Config::mob eq "mob";
335 my %users = map({($$_[1] => $_)} get_all_users);
336 foreach (split(/[\s,]+/, $userlist)) {
337 if (exists($users{$_}) || $_ eq "everyone" || ($mobok && $_ eq "mob")) {
338 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
339 next;
341 if (Girocco::User::does_exist($_, 1)) {
342 if ($force) {
343 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
344 } else {
345 $badlist = 1;
346 warn "refusing to allow questionable user \"$_\" without --force\n" unless $nodie && $quiet;
348 next;
350 $badlist = 1;
351 warn "invalid user: \"$_\"\n" unless $nodie && $quiet
353 die if $badlist && !$nodie;
354 return @newusers;
357 sub is_default_desc {
358 # "Unnamed repository; edit this file 'description' to name the repository."
359 # "Unnamed repository; edit this file to name it for gitweb."
360 local $_ = shift;
361 return 0 unless defined($_);
362 /Unnamed\s+repository;/i && /\s+edit\s+this\s+file\s+/i && /\s+to\s+name\s+/i;
365 sub valid_desc {
366 my $test = shift;
367 chomp $test;
368 return 0 if $test =~ /[\r\n]/;
369 $test =~ s/\s\s+/ /g;
370 $test =~ s/^\s+//;
371 $test =~ s/\s+$//;
372 return $test ne '';
375 sub clean_desc {
376 my $desc = shift;
377 defined($desc) or $desc = '';
378 chomp $desc;
379 $desc = to_utf8($desc, 1);
380 $desc =~ s/\s\s+/ /g;
381 $desc =~ s/^\s+//;
382 $desc =~ s/\s+$//;
383 return $desc;
386 sub parse_options {
387 Girocco::CLIUtil::_parse_options(
388 sub {
389 warn((($_[0]eq'?')?"unrecognized":"missing argument for")." option \"$_[1]\"\n")
390 unless $quiet;
391 die_usage;
392 }, @_);
395 sub cmd_list {
396 my %sortsub = (
397 lcname => sub {lc($$a[1]) cmp lc($$b[1])},
398 name => sub {$$a[1] cmp $$b[1]},
399 gid => sub {$$a[3] <=> $$b[3]},
400 owner => sub {lc($$a[4]) cmp lc($$b[4]) || lc($$a[1]) cmp lc($$b[1])},
401 no => sub {$$a[0] <=> $$b[0]},
403 my $sortopt = 'lcname';
404 my ($verbose, $owner);
405 parse_options(":sort" => \$sortopt, verbose => \$verbose, owner => \$owner);
406 my $regex;
407 if (@ARGV) {
408 my $val = shift @ARGV;
409 $regex = qr($val) or die "bad regex \"$val\"\n";
411 !@ARGV && exists($sortsub{$sortopt}) or die_usage;
412 my $sortsub = $sortsub{$sortopt};
413 my $grepsub = defined($regex) ? ($owner ? sub {$$_[4] =~ /$regex/} : sub {$$_[1] =~ /$regex/}) : sub {1};
414 my @projects = sort($sortsub grep {&$grepsub} get_all_projects);
415 if ($verbose) {
416 print map(sprintf("%s\n", join(":", (@$_)[1..5])), @projects);
417 } else {
418 print map(sprintf("%s: %s\n", $$_[1], $$_[5] =~ /^:/ ? "<mirror>" : $$_[5]), @projects);
420 return 0;
423 sub cmd_create {
424 my ($force, $noalternates, $orphanok, $optp, $nopasswd, $noowner, $defaults, $ispush, $pushusers,
425 $ismirror, $desc, $fullmirror);
426 parse_options(
427 force => \$force, "no-alternates" => \$noalternates, orphan => \$orphanok, p => \$optp,
428 "no-password" => \$nopasswd, "no-owner" => \$noowner, defaults => \$defaults,
429 "push" => \$ispush, ":push" => \$pushusers, ":mirror" => \$ismirror, ":desc" => \$desc,
430 ":description" => \$desc, "full-mirror" => \$fullmirror);
431 @ARGV == 1 or die_usage;
432 !defined($pushusers) || defined($ispush) or $ispush = 1;
433 defined($ismirror) && $ismirror =~ /^\s*$/ and die "--mirror url must not be empty\n";
434 die "--mirror and --push are mutually exclusive options\n" if $ismirror && $ispush;
435 die "--full-mirror requires use of --mirror=<url> option\n" if $fullmirror && !$ismirror;
436 !$defaults || defined($ispush) || defined($ismirror) or $ispush = 1;
437 !$defaults || defined($nopasswd) or $nopasswd = 1;
438 !$defaults || defined($noowner) or $noowner = 1;
439 !defined($ispush) || defined($pushusers) or $pushusers = "";
440 my $projname = $ARGV[0];
441 $projname =~ s/\.git$//i;
442 Girocco::Project::does_exist($projname, 1) and die "Project already exists: \"$projname\"\n";
443 if (!Girocco::Project::valid_name($projname, $orphanok, $optp)) {
444 warn "Refusing to create orphan project without --orphan\n"
445 if !$quiet && !$orphanok && Girocco::Project::valid_name($projname, 1, 1);
446 warn "Required orphan parent directory does not exist (use -p): ",
447 $Girocco::Config::reporoot.'/'.Girocco::Project::get_forkee_name($projname), "\n"
448 if !$quiet && $orphanok && Girocco::Project::valid_name($projname, 1, 1);
449 die "Invalid project name: \"$projname\"\n";
451 my ($forkee, $project) = ($projname =~ m#^(.*/)?([^/]+)$#);
452 my $newtype = $forkee ? 'fork' : 'project';
453 if (length($project) > 64) {
454 die "The $newtype name is longer than 64 characters. Do you really need that much?\n"
455 unless $force;
456 warn "Allowing $newtype name longer than 64 characters with --force\n" unless $quiet;
458 unless ($Girocco::Config::push || $Girocco::Config::mirror) {
459 die "Project creation disabled (no mirrors or push projects allowed)\n" unless $force;
460 warn "Continuing with --force even though both push and mirror projects are disabled\n" unless $quiet;
462 print "Enter settings for new project \"$projname\"\n" unless $defaults;
463 my %settings = ();
464 $settings{noalternates} = $noalternates;
465 if ($nopasswd) {
466 $settings{crypt} = "unknown";
467 } else {
468 my $np1 = prompt_noecho_nl_or_die("Admin password for project $projname (echo is off)");
469 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
470 my $np2 = prompt_noecho_nl_or_die("Retype admin password for project $projname");
471 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
472 "the admin passwords you have entered do not match each other.\n";
473 $settings{crypt} = scrypt_sha1($np1);
475 my $owner = "";
476 unless ($noowner) {
477 $owner = prompt_or_die("Owner/email name for project $projname");
478 unless (valid_email($owner)) {
479 unless ($force) {
480 warn "Your email sure looks weird...?\n";
481 redo;
483 warn "Allowing invalid email with --force\n" unless $quiet;
485 if (length($owner) > 96) {
486 unless ($force) {
487 warn "Your email is longer than 96 characters. Do you really need that much?\n";
488 redo;
490 warn "Allowing email longer than 96 characters with --force\n" unless $quiet;
493 $settings{email} = $owner;
494 my $baseurl = "";
495 my $checkmirror = sub {
496 my $checkurl = shift;
497 unless (valid_repo_url($checkurl)) {
498 unless ($force) {
499 warn "Invalid mirror URL: \"$checkurl\"\n";
500 return undef;
502 warn "Allowing invalid mirror URL with --force\n" unless $quiet;
504 if ($Girocco::Config::restrict_mirror_hosts) {
505 my $mh = extract_url_hostname($checkurl);
506 unless (is_dns_hostname($mh)) {
507 unless ($force) {
508 warn "Invalid non-DNS mirror URL: \"$checkurl\"\n";
509 return undef;
511 warn "Allowing non-DNS mirror URL with --force\n" unless $quiet;
513 if (is_our_hostname($mh)) {
514 unless ($force) {
515 warn "Invalid same-host mirror URL: \"$checkurl\"\n";
516 return undef;
518 warn "Allowing same-host mirror URL with --force\n" unless $quiet;
521 return $checkurl;
523 if ($ispush || $ismirror) {
524 !$ispush || $force || $Girocco::Config::push or
525 die "Push projects are disabled, create a mirror (or use --force)\n";
526 !$ismirror || $force || $Girocco::Config::mirror or
527 die "Mirror projects are disabled, create a push project (or use --force)\n";
528 if ($ismirror) {
529 &$checkmirror($ismirror) or die "Invalid --mirror URL\n";
530 $baseurl = $ismirror;
531 $settings{url} = $baseurl;
532 $settings{cleanmirror} = $fullmirror ? 0 : 1;
533 } else {
534 my @newusers = ();
535 if ($pushusers !~ /^[\s,]*$/) {
536 eval {@newusers = validate_users($pushusers, $force); 1;} or
537 die "Invalid --push user list\n";
539 $settings{users} = \@newusers;
541 } elsif ($force || $Girocco::Config::mirror) {{
542 if ($force || $Girocco::Config::push) {
543 $baseurl = prompt_or_die("URL to mirror from (leave blank for push project)", "");
544 } else {{
545 $baseurl = prompt_or_die("URL to mirror from");
546 unless ($baseurl ne "") {
547 warn "Push projects are disabled, you must enter a mirror URL (or use --force)\n";
548 redo;
551 if ($baseurl ne "") {
552 &$checkmirror($baseurl) or redo;
553 $settings{url} = $baseurl;
554 $settings{cleanmirror} =
555 ynprompt_or_die("Mirror only heads, tags and notes (Y/n)", "Yes");
558 my $mirror = ($baseurl eq "") ? 0 : 1;
559 my $checkdesc = sub {
560 my $d = shift;
561 if (length($d) > 1024) {
562 unless ($force) {
563 warn "Short description length greater than 1024 characters!\n";
564 return undef;
566 warn "Allowing short description length greater than 1024 characters\n" unless $quiet;
568 return $d;
570 if (defined($desc)) {
571 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
572 $desc eq "" || &$checkdesc($desc) or
573 die "Invalid --desc description\n";
574 } elsif (!$defaults) {
575 $desc = prompt_or_die("Short description", "");
576 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
577 $desc eq "" || &$checkdesc($desc) or redo;
578 $desc = undef if $desc eq "";
580 defined($desc) or $desc = $mirror ? "Mirror of $baseurl" : "Push project $projname";
581 $settings{desc} = $desc;
582 my $homepage = "";
583 if (!$defaults) {
584 $homepage = prompt_or_die("Home page URL", "");
585 if ($homepage ne "" && !valid_web_url($homepage)) {
586 unless ($force) {
587 warn "Invalid home page URL: \$homepage\"\n";
588 redo;
590 warn "Allowing invalid home page URL with --force\n" unless $quiet;
593 $settings{hp} = $homepage;
594 my $jsonurl = "";
595 if (!$defaults) {
596 $jsonurl = prompt_or_die("JSON notify POST URL", "");
597 if ($jsonurl ne "" && !valid_web_url($jsonurl)) {
598 unless ($force) {
599 warn "Invalid JSON notify POST URL: \$jsonurl\"\n";
600 redo;
602 warn "Allowing invalid JSON notify POST URL with --force\n" unless $quiet;
605 $settings{notifyjson} = $jsonurl;
606 my $commitaddrs = "";
607 if (!$defaults) {
608 $commitaddrs = clean_addrlist(prompt_or_die("Commit notify email addr(s)", ""));
609 if ($commitaddrs ne "" && !valid_addrlist($commitaddrs)) {
610 unless ($force) {
611 warn"invalid commit notify email address list (use --force to accept): \"$commitaddrs\"\n";
612 redo;
614 warn "using invalid commit notify email address list with --force\n" unless $quiet;
617 $settings{notifymail} = $commitaddrs;
618 $settings{reverseorder} = 1;
619 $settings{reverseorder} = ynprompt_or_die("Oldest-to-newest commit order in emails", "Yes")
620 if !$defaults && $commitaddrs ne "";
621 $settings{summaryonly} = ynprompt_or_die("Summary only (no diff) in emails", "No")
622 if !$defaults && $commitaddrs ne "";
623 my $tagaddrs = "";
624 if (!$defaults) {
625 $tagaddrs = clean_addrlist(prompt_or_die("Tag notify email addr(s)", ""));
626 if ($tagaddrs ne "" && !valid_addrlist($tagaddrs)) {
627 unless ($force) {
628 warn"invalid tag notify email address list (use --force to accept): \"$tagaddrs\"\n";
629 redo;
631 warn "using invalid tag notify email address list with --force\n" unless $quiet;
634 $settings{notifytag} = $tagaddrs;
635 if (!$mirror && !$ispush) {
636 my @newusers = ();
638 my $userlist = prompt_or_die("Push users", join(",", @newusers));
639 eval {@newusers = validate_users($userlist, $force); 1;} or redo;
641 $settings{users} = \@newusers;
643 my $newproj = Girocco::Project->ghost($projname, $mirror, $orphanok, $optp)
644 or die "Girocco::Project->ghost call failed\n";
645 my ($k, $v);
646 $newproj->{$k} = $v while ($k, $v) = each(%settings);
647 my $killowner = sub {
648 system($Girocco::Config::git_bin, '--git-dir='.$newproj->{path},
649 'config', '--unset', "gitweb.owner");
651 if ($mirror) {
652 $newproj->premirror or die "Girocco::Project->premirror failed\n";
653 !$noowner or &$killowner;
654 $newproj->clone or die "Girocco::Project->clone failed\n";
655 warn "Project $projname created and cloning successfully initiated.\n"
656 unless $quiet;
657 } else {
658 $newproj->conjure or die "Girocco::Project->conjure failed\n";
659 !$noowner or &$killowner;
660 warn "New push project fork is empty due to use of --no-alternates\n"
661 if !$quiet && $projname =~ m,/, && $noalternates;
662 warn "Project $projname successfully created.\n" unless $quiet;
664 return 0;
667 sub git_config {
668 my $gd = shift;
669 system($Girocco::Config::git_bin, "--git-dir=$gd", 'config', @_) == 0
670 or die "\"git --git-dir='$gd' config ".join(" ", @_)."\" failed.\n";
673 sub cmd_adopt {
674 my ($force, $type, $nousers, $dryrun, $noowner, $owner, $users, $verbose);
675 parse_options(force => \$force, ":type" => \$type, "no-users" => \$nousers, "dry-run" => \$dryrun,
676 "no-owner" => \$noowner,":owner" => \$owner, quiet => \$quiet, q =>\$quiet, verbose => \$verbose);
677 @ARGV or die "Please give project name on command line.\n";
678 my $projname = shift @ARGV;
679 (!$noowner || !defined($owner)) && (!$nousers || !@ARGV) or die_usage;
680 !defined($type) || $type eq "mirror" || $type eq "push" or die_usage;
681 defined($type) or $type = "";
682 my $projdir;
683 if ($dryrun && $projname =~ m,^/[^.\s/\\:], && is_git_dir(realpath($projname))) {
684 $projdir = realpath($projname);
685 $projname = $projdir;
686 $projname =~ s/\.git$//i;
687 $projname =~ s,/+$,,;
688 $projname =~ s,^.*/,,;
689 $projname ne "" or $projname = $projdir;
690 } else {
691 $projname =~ s/\.git$//i;
692 $projname ne "" or die "Invalid project name \"\".\n";
693 unless (Girocco::Project::does_exist($projname, 1)) {
694 Girocco::Project::valid_name($projname, 1, 1)
695 or die "Invalid project name \"$projname\".\n";
696 die "No such project to adopt: $projname\n";
698 defined(Girocco::Project->load($projname))
699 and die "Project already known (no need to adopt): $projname\n";
700 $projdir = $Girocco::Config::reporoot . "/" . $projname . ".git";
701 is_git_dir($projdir) or die "Not a git directory: \"$projdir\"\n";
703 my $config = read_config_file($projdir . "/config");
704 my %config = ();
705 %config = map {($$_[0], defined($$_[1])?$$_[1]:"true")} @$config if defined($config);
706 git_bool($config{"core.bare"}) or die "Not a bare git repository: \"$projdir\"\n";
707 defined(read_HEAD_symref($projdir)) or die "Project with non-symbolic HEAD ref: \"$projdir\"\n";
708 @ARGV and $users = [validate_users(join(" ", @ARGV), $force, 1, $quiet)];
709 my $desc = "";
710 if (-e "$projdir/description") {
711 open my $fd, '<', "$projdir/description" or die "Cannot open \"$projdir/description\": $!\n";
713 local $/;
714 $desc = <$fd>;
716 close $fd;
717 defined $desc or $desc = "";
718 chomp $desc;
719 $desc = to_utf8($desc, 1);
720 is_default_desc($desc) and $desc = "";
721 if ($desc ne "" && !valid_desc($desc)) {
722 die "invalid 'description' file contents (use --force to accept): \"$desc\"\n"
723 unless $force;
724 warn "using invalid 'description' file contents with --force\n" unless $quiet;
726 $desc = clean_desc($desc);
727 if (length($desc) > 1024) {
728 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
729 unless $force;
730 warn "using longer than 1024 char description with --force\n" unless $quiet;
733 my $readme = "";
734 if (-e "$projdir/README.html") {
735 open my $fd, '<', "$projdir/README.html" or die "Cannot open \"$projdir/README.html\": $!\n";
737 local $/;
738 $readme = <$fd>;
740 close $fd;
741 defined $readme or $readme = "";
742 $readme = to_utf8($readme, 1);
743 $readme =~ s/\r\n?/\n/gs;
744 $readme =~ s/^\s+//s;
745 $readme =~ s/\s+$//s;
746 $readme eq "" or $readme .= "\n";
747 if (length($readme) > 8192) {
748 die "readme greater than 8192 chars is too long (use --force to override)\n"
749 unless $force;
750 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
752 my $rd = get_readme_desc($readme);
753 if ($rd ne "automatic" && $rd ne "suppressed") {
754 my $xmllint = qx(sh -c 'command -v xmllint'); chomp $xmllint;
755 if (-f $xmllint && -x $xmllint) {
756 my $dummy = {README => $readme};
757 my ($cnt, $err) = Girocco::Project::_lint_readme($dummy, 0);
758 if ($cnt) {
759 my $msg = "xmllint: $cnt error";
760 $msg .= "s" unless $cnt == 1;
761 print STDERR "$msg\n", "-" x length($msg), "\n", $err
762 unless $force && $quiet;
763 exit(255) unless $force;
764 warn "$projname: using invalid raw HTML with --force\n" unless $quiet;
766 } else {
767 die "xmllint not available, refusing to use raw HTML without --force\n"
768 unless $force;
769 warn "xmllint not available using unchecked raw HTML with --force\n" unless $quiet;
773 # Inspect any remotes now
774 # Yes, Virginia, remote urls can be multi-valued
775 my %remotes = ();
776 foreach (@$config) {
777 my ($k,$v) = @$_;
778 next unless $k =~ /^remote\.([^\/].*?)\.([^.]+)$/; # remote name cannot start with "/"
779 my ($name, $subkey) = ($1, $2);
780 $remotes{$name}->{skip} = git_bool($v,1), next if $subkey eq "skipdefaultupdate" || $subkey eq "skipfetchall";
781 $remotes{$name}->{mirror} = git_bool($v,1), next if $subkey eq "mirror"; # we might want this
782 $remotes{$name}->{vcs} = $v, next if defined($v) && $v !~ /^\s*$/ && $subkey eq "vcs";
783 push(@{$remotes{$name}->{$subkey}}, $v), next if defined($v) && $v !~ /^\s*$/ &&
784 ($subkey eq "url" || $subkey eq "fetch" || $subkey eq "push" || $subkey eq "pushurl");
786 # remotes.default is the default remote group to fetch for "git remote update" otherwise --all
787 # the remote names in a group are separated by runs of [ \t\n] characters
788 # remote names "", ".", ".." and any name starting with "/" are invalid
789 # a remote with no url or vcs setting is not considered valid
790 my @check = ();
791 my $usingall = 0;
792 if (exists($config{"remotes.default"})) {
793 foreach (split(/[ \t\n]+/, $config{"remotes.default"})) {
794 next unless exists($remotes{$_});
795 my $rmt = $remotes{$_};
796 next if !exists($rmt->{url}) && !$rmt->{vcs};
797 push(@check, $_);
799 } else {
800 $usingall = 1;
801 my %seenrmt = ();
802 foreach (@$config) {
803 my ($k,$v) = @$_;
804 next unless $k =~ /^remote\.([^\/].*?)\.[^.]+$/;
805 next if $seenrmt{$1};
806 $seenrmt{$1} = 1;
807 next unless exists($remotes{$1});
808 my $rmt = $remotes{$1};
809 next if $rmt->{skip} || (!exists($rmt->{url}) && !$rmt->{vcs});
810 push(@check, $1);
813 my @needskip = (); # remotes that need skipDefaultUpdate set to true
814 my $foundvcs = 0;
815 my $foundfetch = 0;
816 my $foundfetchwithmirror = 0;
817 foreach (@check) {
818 my $rmt = $remotes{$_};
819 push(@needskip, $_) if $usingall && !exists($rmt->{fetch});
820 next unless exists($rmt->{fetch});
821 ++$foundfetch;
822 ++$foundfetchwithmirror if $rmt->{mirror};
823 ++$foundvcs if $rmt->{vcs} || (exists($rmt->{url}) && $rmt->{url}->[0] =~ /^[a-zA-Z0-9][a-zA-Z0-9+.-]*::/);
825 # if we have $foundvcs then we need to explicitly set fetch.prune to false
826 # if we have $foundfetch > 1 then we need to explicitly set fetch.prune to false
827 my $neednoprune = !exists($config{"fetch.prune"}) && ($foundvcs || $foundfetch > 1);
828 my $baseurl = "";
829 my $needfakeorigin = 0; # if true we need to set remote.origin.skipDefaultUpdate = true
830 # if remote "origin" exists we always pick up its first url or use ""
831 if (exists($remotes{origin})) {
832 my $rmt = $remotes{origin};
833 $baseurl = exists($rmt->{url}) ? $rmt->{url}->[0] : "";
834 $needfakeorigin = !exists($rmt->{url}) && !$rmt->{vcs} && !$rmt->{skip};
835 } else {
836 $needfakeorigin = 1;
837 # get the first url of the @check remotes
838 foreach (@check) {
839 my $rmt = $remotes{$_};
840 next unless exists($rmt->{url});
841 next unless defined($rmt->{url}->[0]) && $rmt->{url}->[0] ne "";
842 $baseurl = $rmt->{url}->[0];
843 last;
846 my $makemirror = $type eq "mirror" || ($type eq "" && $foundfetch);
848 # If we have $foundfetch we want to make a mirror but complain if
849 # we $foundfetchwithmirror as well unless we have --type=mirror.
850 # Warn if we have --type=push and $foundfetch and !$foundfetchwithmirror.
851 # Warn if we need to set fetch.prune=false when making a mirror
852 # Warn if we need to create remote.origin.skipDefaultUpdate when making a mirror
853 # Complain if @needskip AND !$usingall (warn with --force but don't set skip)
854 # Warn if $usingall and any @needskip (and set them) if making a mirror
855 # Warn if making a mirror and $baseurl eq ""
856 # Warn if we have --type=mirror and !$foundfetch
858 if ($makemirror) {
859 warn "No base URL to mirror from for adopted \"$projname\"\n" unless $quiet || $baseurl ne "";
860 warn "Adopting mirror \"$projname\" without any fetch remotes\n" unless $quiet || $foundfetch;
861 if ($foundfetchwithmirror) {
862 warn "Refusing to adopt mirror \"$projname\" with active remote.<name>.mirror=true remote(s)\n".
863 "(Use --type=mirror to override)\n"
864 unless $type eq "mirror";
865 exit(255) unless $type eq "mirror" || $dryrun;
866 warn "Adopting mirror \"$projname\" with active remote.<name>.mirror=true remotes\n"
867 unless $quiet || $type ne "mirror";
869 warn "Setting explicit fetch.prune=false for adoption of mirror \"$projname\"\n"
870 if !$quiet && $neednoprune;
871 warn "Setting remote.origin.skipDefaultUpdate=true for adoption of mirror \"$projname\"\n"
872 if !$quiet && $needfakeorigin;
873 if (!$usingall && @needskip) {
874 warn "Refusing to adopt mirror empty fetch remote(s) (override with --force)\n"
875 unless $force;
876 exit(255) unless $force || $dryrun;
877 warn "Adopting mirror with empty fetch remote(s) with --force\n"
878 unless $quiet || !$force;
880 warn "Will set skipDefaultUpdate=true on non-fetch remote(s)\n" if !$quiet && $usingall && @needskip;
881 warn "Adopting mirror with base URL \"$baseurl\"\n" unless $quiet || $baseurl eq "";
882 } else {
883 warn "Adopting push \"$projname\" but active non-mirror remotes are present\n"
884 if !$quiet && $foundfetch && !$foundfetchwithmirror;
887 if (!$noowner && !defined($owner)) {
888 # Select the owner
889 $owner = $config{"gitweb.owner"};
890 if (!defined($owner) || $owner eq "") {
891 $owner = $Girocco::Config::admin;
892 warn "Using owner \"$owner\" for adopted project\n" unless $quiet;
895 if (!$nousers && !$makemirror && !defined($users)) {
896 # select user list for push project
897 my $findowner = $owner;
898 defined($findowner) or $findowner = $config{"gitweb.owner"};
899 $findowner = lc($findowner) if defined($findowner);
900 my @owner_users = ();
901 @owner_users = map {$findowner eq lc($$_[4]) ? $$_[1] : ()} get_all_users
902 if defined($findowner) && $findowner ne "";
903 if (@owner_users <= 1) {
904 $users = \@owner_users;
905 warn "No users found that match owner \"$findowner\"\n" unless @owner_users || $quiet;
906 } else {
907 $users = [];
908 warn "Found ".scalar(@owner_users)." users for owner \"$findowner\" (" .
909 join(" ", @owner_users) . ") not setting any\n" unless $quiet;
912 defined($users) or $users = [];
914 # Warn if we preserve an existing receive.denyNonFastForwards or receive.denyDeleteCurrent setting
915 # Complain if core.logallrefupdates or logs subdir exists and contains any files (allow with --force
916 # and warn about preserving the setting)
918 warn "Preserving existing receive.denyNonFastForwards=true\n"
919 if !$quiet && git_bool($config{"receive.denynonfastforwards"});
920 warn "Preserving existing receive.denyDeleteCurrent=$config{'receive.denydeletecurrent'}\n"
921 if !$quiet && exists($config{"receive.denydeletecurrent"}) &&
922 $config{"receive.denydeletecurrent"} ne "warn";
924 my $reflogfiles = Girocco::Project::_contains_files("$projdir/logs");
925 my $reflogactive = git_bool($config{"core.logallrefupdates"});
926 if ($reflogactive || $reflogfiles) {
927 warn "Refusing to adopt \"$projname\" with active ref logs without --force\n" if $reflogfiles && !$force;
928 warn "Refusing to adopt \"$projname\" with core.logAllRefUpdates=true without --force\n" if $reflogactive && !$force;
929 exit(255) unless $force || $dryrun;
930 warn "Adopting \"$projname\" with active ref logs with --force\n" unless $quiet || ($reflogfiles && !$force);
931 warn "Adopting \"$projname\" with core.logAllRefUpdates=true with --force\n" unless $quiet || ($reflogactive && !$force);
934 return 0 if $dryrun && !$verbose;
936 my $newproj = eval {Girocco::Project->ghost($projname, $makemirror, 1, $dryrun)};
937 defined($newproj) or die "Girocco::Project::ghost failed: $@\n";
938 $newproj->{desc} = $desc;
939 $newproj->{README} = $readme;
940 $newproj->{url} = $baseurl if $makemirror || exists($config{"gitweb.baseurl"});
941 $newproj->{email} = $owner if defined($owner);
942 $newproj->{users} = $users;
943 $newproj->{crypt} = "unknown";
944 $newproj->{reverseorder} = 1 unless exists($config{"hooks.reverseorder"});
945 $newproj->{summaryonly} = 1 unless exists($config{"hooks.summaryonly"});
946 my $dummy = bless {}, "Girocco::Project";
947 $dummy->{path} = "$projdir";
948 $dummy->{configfilehash} = \%config;
949 $dummy->_properties_load;
950 delete $dummy->{origurl};
951 foreach my $k (keys(%$dummy)) {
952 $newproj->{$k} = $dummy->{$k}
953 if exists($dummy->{$k}) && !exists($newproj->{$k});
956 if ($verbose) {
957 use Data::Dumper;
958 my %info = %$newproj;
959 $info{README} = get_readme_desc($info{README}) if exists($info{README});
960 my $d = Data::Dumper->new([\%info], ['*'.$newproj->{name}]);
961 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
962 print $d->Dump([\%info], ['*'.$newproj->{name}]);
964 return 0 if $dryrun;
966 # Make any changes as needed for @needskip, $neednoprune and $needfakeorigin
967 if ($makemirror) {
968 git_config($projdir, "fetch.prune", "false") if $neednoprune;
969 git_config($projdir, "remote.origin.skipDefaultUpdate", "true") if $needfakeorigin;
970 if ($usingall && @needskip) {
971 git_config($projdir, "remote.$_.skipDefaultUpdate", "true") foreach @needskip;
975 # Perform the actual adoption
976 $newproj->adopt or die "Girocco::Project::adopt failed\n";
978 # Perhaps restore core.logAllRefUpdates, receive.denyNonFastForwards and receive.denyDeleteCurrent
979 git_config($projdir, "receive.denyNonFastForwards", "true")
980 if git_bool($config{"receive.denynonfastforwards"});
981 git_config($projdir, "receive.denyDeleteCurrent", $config{"receive.denydeletecurrent"})
982 if exists($config{"receive.denydeletecurrent"}) &&
983 $config{"receive.denydeletecurrent"} ne "warn";
984 git_config($projdir, "core.logAllRefUpdates", "true")
985 if $reflogactive;
987 # Success
988 if ($makemirror) {
989 warn "Mirror project \"$projname\" successfully adopted.\n" unless $quiet;
990 } else {
991 warn "Push project \"$projname\" successfully adopted.\n" unless $quiet;
993 return 0;
996 sub cmd_remove {
997 my ($force, $reallydel, $keepforks);
998 parse_options(force => \$force, "really-delete" => \$reallydel,
999 "keep-forks" => \$keepforks, quiet => \$quiet, q =>\$quiet);
1000 @ARGV or die "Please give project name on command line.\n";
1001 @ARGV == 1 or die_usage;
1002 my $project = get_project($ARGV[0]);
1003 my $projname = $project->{name};
1004 my $isempty = !$project->{mirror} && $project->is_empty;
1005 if (!$project->{mirror} && !$isempty && $reallydel) {
1006 die "refusing to remove and delete non-empty push project without --force: $projname\n" unless $force;
1007 warn "allowing removal and deletion of non-empty push project with --force\n" unless $quiet;
1009 my $altwarn;
1010 my $removenogc;
1011 if ($project->has_forks) {
1012 die "refusing to remove project with forks (use --keep-forks): $projname\n" unless $keepforks;
1013 warn "allowing removal of forked project while preserving its forks with --keep-forks\n" unless $quiet;
1014 # Run pseudo GC on that repository so that objects don't get lost within forks
1015 my $basedir = $Girocco::Config::basedir;
1016 my $projdir = $project->{path};
1017 warn "We have to run pseudo GC on the repo so that the forks don't lose data. Hang on...\n" unless $quiet;
1018 my $nogcrunning = sub {
1019 die "Error: GC appears to be currently running on $projname\n"
1020 if -e "$projdir/gc.pid" || -e "$projdir/.gc_in_progress";
1022 &$nogcrunning;
1023 $removenogc = ! -e "$projdir/.nogc";
1024 recreate_file("$projdir/.nogc") if $removenogc;
1025 die "unable to create \"$projdir/.nogc\"\n" unless -e "$projdir/.nogc";
1026 delete $ENV{show_progress};
1027 $ENV{'show_progress'} = 1 unless $quiet;
1028 sleep 2; # *cough*
1029 &$nogcrunning;
1030 system("$basedir/toolbox/perform-pre-gc-linking.sh", "--include-packs", $projname) == 0
1031 or die "Running pseudo GC on project $projname failed\n";
1032 $altwarn = 1;
1034 my $archived;
1035 if (!$project->{mirror} && !$isempty && !$reallydel) {
1036 $archived = $project->archive_and_delete;
1037 unlink("$archived/.nogc") if $removenogc && defined($archived) && $archived ne "";
1038 } else {
1039 $project->delete;
1041 warn "Project '$projname' removed from $Girocco::Config::name" .
1042 ($archived ? ", backup in '$archived'" : "") .".\n" unless $quiet;
1043 warn "Retained forks may now have unwanted objects/info/alternates lines\n" if $altwarn && !$quiet;
1044 return 0;
1047 sub cmd_show {
1048 use Data::Dumper;
1049 @ARGV == 1 or die_usage;
1050 my $project = get_clean_project($ARGV[0]);
1051 my %info = %$project;
1052 my $d = Data::Dumper->new([\%info], ['*'.$project->{name}]);
1053 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
1054 print $d->Dump([\%info], ['*'.$project->{name}]);
1055 return 0;
1058 sub cmd_listheads {
1059 @ARGV == 1 or die_usage;
1060 my $project = get_project($ARGV[0]);
1061 my @heads = sort({lc($a) cmp lc($b)} $project->get_heads);
1062 my $cur = $project->{HEAD};
1063 defined($cur) or $cur = '';
1064 my $curmark = '*';
1065 my $headhash = get_git("--git-dir=$project->{path}", 'rev-parse', '--quiet', '--verify', 'HEAD');
1066 defined($headhash) or $headhash = '';
1067 chomp $headhash;
1068 $headhash or $curmark = '!';
1069 foreach (@heads) {
1070 my $mark = $_ eq $cur ? $curmark : ' ';
1071 print "$mark $_\n";
1073 return 0;
1076 sub cmd_listtags {
1077 my $vcnt = 0;
1078 shift(@ARGV), $vcnt=1 if @ARGV && ($ARGV[0] eq '--verbose' || $ARGV[0] eq '-v');
1079 @ARGV == 1 or die_usage;
1080 my $project = get_project($ARGV[0]);
1081 if ($vcnt) {
1082 print map("$$_[0]\t$$_[1]\n", get_ctag_counts($project));
1083 } else {
1084 print map("$_\n", $project->get_ctag_names);
1086 return 0;
1089 sub cmd_deltags {
1090 my $ic = 0;
1091 shift(@ARGV), $ic=1 if @ARGV && $ARGV[0] =~ /^(?:--?ignore-case|-i)$/i;
1092 @ARGV >= 2 or die_usage;
1093 my $project = get_project(shift @ARGV);
1094 my %curtags;
1095 if ($ic) {
1096 push(@{$curtags{lc($_)}}, $_) foreach $project->get_ctag_names;
1097 } else {
1098 push(@{$curtags{$_}}, $_) foreach $project->get_ctag_names;
1100 my @deltags = ();
1101 my %seentag = ();
1102 my $ctags = join(" ", @ARGV);
1103 $ctags = lc($ctags) if $ic;
1104 foreach (split(/[\s,]+/, $ctags)) {
1105 next unless exists($curtags{$_});
1106 $seentag{$_}=1, push(@deltags, $_) unless $seentag{$_};
1108 if (!@deltags) {
1109 warn $project->{name}, ": skipping removal of only non-existent tags\n" unless $quiet;
1110 } else {
1111 # Avoid touching anything other than the ctags
1112 foreach my $tg (@deltags) {
1113 $project->delete_ctag($_) foreach @{$curtags{$tg}};
1115 $project->_set_changed;
1116 $project->_set_forkchange;
1117 warn $project->{name}, ": specified tags have been removed\n" unless $quiet;
1119 return 0;
1122 sub cmd_addtags {
1123 @ARGV >= 2 or die_usage;
1124 my $project = get_project(shift @ARGV);
1125 my $ctags = join(" ", @ARGV);
1126 $ctags =~ /[^, a-zA-Z0-9:.+#_-]/ and
1127 die "Content tag(s) \"$ctags\" contain(s) evil character(s).\n";
1128 my $oldmask = umask();
1129 umask($oldmask & ~0060);
1130 my $changed = 0;
1131 foreach (split(/[\s,]+/, $ctags)) {
1132 ++$changed if $project->add_ctag($_, 1);
1134 if ($changed) {
1135 $project->_set_changed;
1136 $project->_set_forkchange;
1138 umask($oldmask);
1139 my $cnt = ($changed == 1) ? "1 content tag has" : $changed . " content tags have";
1140 warn $project->{name}, ": $cnt been added/updated\n" unless $quiet;
1141 return 0;
1144 sub _get_random_val {
1145 my $p = shift;
1146 my $md5;
1148 no warnings;
1149 $md5 = md5_hex(time . $$ . rand() . join(':',%$p));
1151 $md5;
1154 sub cmd_chpass {
1155 my $force = 0;
1156 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1157 my $random = undef;
1158 pop(@ARGV), $random=lc($ARGV[1]) if @ARGV==2 && $ARGV[1] =~ /^(?:random|unknown)$/i;
1159 @ARGV == 1 or die_usage;
1160 my $project = get_project($ARGV[0]);
1161 die "refusing to change locked password of project \"$ARGV[0]\" without --force\n"
1162 if $project->is_password_locked;
1163 my ($newpw, $rmsg);
1164 if ($random) {
1165 if ($random eq "random") {
1166 die "refusing to set random password without --force\n" unless $force;
1167 $rmsg = "set to random value";
1168 $newpw = _get_random_val($project);
1169 } else {
1170 die "refusing to set password hash to '$random' without --force\n" unless $force;
1171 $rmsg = "hash set to '$random'";
1172 $newpw = $random;
1174 } else {
1175 $rmsg = "updated";
1176 if (-t STDIN) {
1177 print "Changing admin password for project $ARGV[0]\n";
1178 my $np1 = prompt_noecho_nl_or_die("New password for project $ARGV[0] (echo is off)");
1179 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1180 my $np2 = prompt_noecho_nl_or_die("Retype new password for project $ARGV[0]");
1181 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
1182 "the admin passwords you have entered do not match each other.\n";
1183 $newpw = $np1;
1184 } else {
1185 $newpw = <STDIN>;
1186 defined($newpw) or die "missing new password on STDIN\n";
1187 chomp($newpw);
1190 $newpw ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1191 my $old = $project->{crypt};
1192 $project->{crypt} = (defined($random) && $random ne "random") ? $newpw : scrypt_sha1($newpw);
1193 if (defined($old) && $old eq $project->{crypt}) {
1194 warn $project->{name}, ": skipping update of password hash to same value\n" unless $quiet;
1195 } else {
1196 # Avoid touching anything other than the password hash
1197 $project->_group_update;
1198 warn $project->{name}, ": admin password $rmsg (new hash stored)\n" unless $quiet;
1200 return 0;
1203 sub cmd_checkpw {
1204 @ARGV == 1 or die_usage;
1205 my $project = get_project($ARGV[0]);
1206 my $pwhash = $project->{crypt};
1207 defined($pwhash) or $pwhash = "";
1208 if ($pwhash eq "") {
1209 warn $project->{name}, ": no password required\n" unless $quiet;
1210 return 0;
1212 if ($project->is_password_locked) {
1213 warn $project->{name}, ": password is locked\n" unless $quiet;
1214 exit 1;
1216 my $checkpw;
1217 if (-t STDIN) {
1218 $checkpw = prompt_noecho_nl_or_die("Admin password for project $ARGV[0] (echo is off)");
1219 $checkpw ne "" or warn "checking for empty password as hash (very unlikely)\n" unless $quiet;
1220 } else {
1221 $checkpw = <STDIN>;
1222 defined($checkpw) or die "missing admin password on STDIN\n";
1223 chomp($checkpw);
1225 unless (Girocco::CLIUtil::check_passwd_match($pwhash, $checkpw)) {
1226 warn "password check failure\n" unless $quiet;
1227 exit 1;
1229 warn "admin password match\n" unless $quiet;
1230 return 0;
1233 sub cmd_gc {
1234 my ($force, $auto, $redelta, $recompress);
1235 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, auto => \$auto,
1236 redelta => \$redelta, "no-reuse-delta" => \$redelta, aggressive => \$force,
1237 recompress => \$recompress, "no-reuse-object" => $recompress);
1238 $force && $auto and die "--force and --auto are mutually exclusive options\n";
1239 @ARGV or die "Please give project name on command line.\n";
1240 @ARGV == 1 or die_usage;
1241 my $project = get_project($ARGV[0]);
1242 delete $ENV{show_progress};
1243 delete $ENV{force_gc};
1244 $quiet or $ENV{"show_progress"} = 1;
1245 $force and $ENV{"force_gc"} = 1;
1246 if (!$auto && !$force && ! -e $project->{path}."/.needsgc") {
1247 open NEEDSGC, '>', $project->{path}."/.needsgc" and close NEEDSGC;
1249 my @args = ($Girocco::Config::basedir . "/jobd/gc.sh", $project->{name});
1250 $redelta && !$recompress and push(@args, "-f");
1251 $recompress and push(@args, "-F");
1252 my $lastgc = $project->{lastgc};
1253 system({$args[0]} @args) != 0 and return 1;
1254 # Do it again Sam, but only if lastgc was set, gc.sh succeeded and now it's not set
1255 if ($lastgc) {
1256 my $newlastgc = get_git("--git-dir=$project->{path}", 'config', '--get', 'gitweb.lastgc');
1257 if (!$newlastgc) {
1258 system({$args[0]} @args) != 0 and return 1;
1261 return 0;
1264 sub cmd_update {
1265 my ($force, $summary);
1266 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, summary => \$summary);
1267 $quiet && $summary and die "--quiet and --summary are mutually exclusive options\n";
1268 @ARGV or die "Please give project name on command line.\n";
1269 @ARGV == 1 or die_usage;
1270 my $project = get_project($ARGV[0]);
1271 $project->{mirror} or die "Project \"$ARGV[0]\" is a push project, not a mirror project.\n";
1272 delete $ENV{show_progress};
1273 delete $ENV{force_update};
1274 if ($quiet) {
1275 $ENV{"show_progress"} = 0;
1276 } else {
1277 $ENV{"show_progress"} = ($summary ? 1 : 2);
1279 $force and $ENV{"force_update"} = 1;
1280 system($Girocco::Config::basedir . "/jobd/update.sh", $project->{name}) != 0 and return 1;
1281 return 0;
1284 sub cmd_remirror {
1285 my $force = 0;
1286 parse_options(force => \$force, quiet => \$quiet, q => \$quiet);
1287 @ARGV or die "Please give project name on command line.\n";
1288 @ARGV == 1 or die_usage;
1289 my $project = get_project($ARGV[0]);
1290 $project->{mirror} or die "Project \"$ARGV[0]\" is a push project, not a mirror project.\n";
1291 if ($project->{clone_in_progress} && !$project->{clone_failed}) {
1292 warn "Project \"$ARGV[0]\" already seems to have a clone underway at this moment.\n" unless $quiet && $force;
1293 exit(255) unless $force;
1294 yes_to_continue_or_die("Are you sure you want to force a remirror");
1296 unlink($project->_clonefail_path);
1297 unlink($project->_clonelog_path);
1298 recreate_file($project->_clonep_path);
1299 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or
1300 die "cannot connect to taskd.socket: $!\n";
1301 select((select($sock),$|=1)[0]);
1302 $sock->print("clone ".$project->{name}."\n");
1303 # Just ignore reply, we are going to succeed anyway and the I/O
1304 # would apparently get quite hairy.
1305 $sock->flush();
1306 sleep 2; # *cough*
1307 $sock->close();
1308 warn "Project \"$ARGV[0]\" remirror initiated.\n" unless $quiet;
1309 return 0;
1312 sub cmd_setowner {
1313 my $force = 0;
1314 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1315 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1316 my $project = get_project($ARGV[0]);
1317 if (@ARGV == 2 && !valid_email($ARGV[1])) {
1318 die "invalid owner/email (use --force to accept): \"$ARGV[1]\"\n"
1319 unless $force;
1320 warn "using invalid owner/email with --force\n" unless $quiet;
1322 if (@ARGV == 2 && length($ARGV[1]) > 96) {
1323 die "owner/email longer than 96 chars (use --force to accept): \"$ARGV[1]\"\n"
1324 unless $force;
1325 warn "using longer than 96 char owner/email with --force\n" unless $quiet;
1327 my $old = $project->{email};
1328 if (@ARGV == 1) {
1329 print "$old\n" if defined($old);
1330 return 0;
1332 if (defined($old) && $old eq $ARGV[1]) {
1333 warn $project->{name}, ": skipping update of owner/email to same value\n" unless $quiet;
1334 } else {
1335 # Avoid touching anything other than "gitweb.owner"
1336 $project->_property_fput("email", $ARGV[1]);
1337 $project->_update_index;
1338 $project->_set_changed;
1339 warn $project->{name}, ": owner/email updated to \"$ARGV[1]\"\n" unless $quiet;
1341 return 0;
1344 sub cmd_setdesc {
1345 my $force = 0;
1346 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1347 @ARGV >= 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1348 my $project = get_project(shift @ARGV);
1349 if (@ARGV && !valid_desc(join(" ", @ARGV))) {
1350 die "invalid description (use --force to accept): \"".join(" ", @ARGV)."\"\n"
1351 unless $force;
1352 warn "using invalid description with --force\n" unless $quiet;
1354 my $desc = clean_desc(join(" ", @ARGV));
1355 if (@ARGV && length($desc) > 1024) {
1356 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
1357 unless $force;
1358 warn "using longer than 1024 char description with --force\n" unless $quiet;
1360 my $old = $project->{desc};
1361 if (!@ARGV) {
1362 print "$old\n" if defined($old);
1363 return 0;
1365 if (defined($old) && $old eq $desc) {
1366 warn $project->{name}, ": skipping update of description to same value\n" unless $quiet;
1367 } else {
1368 # Avoid touching anything other than description file
1369 $project->_property_fput("desc", $desc);
1370 $project->_set_changed;
1371 warn $project->{name}, ": description updated to \"$desc\"\n" unless $quiet;
1373 return 0;
1376 sub cmd_setreadme {
1377 my $force = 0;
1378 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1379 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1380 my $project = get_project($ARGV[0]);
1381 my $old = $project->{README};
1382 if (@ARGV == 1) {
1383 chomp $old if defined($old);
1384 print "$old\n" if defined($old) && $old ne "";
1385 return 0;
1387 my ($new, $raw, $newname);
1388 $newname = '';
1389 if ($ARGV[1] eq "-") {
1390 local $/;
1391 $new = <STDIN>;
1392 $raw = 1;
1393 $newname = "contents of <STDIN>";
1394 } elsif (lc($ARGV[1]) eq "automatic" || lc($ARGV[1]) eq "auto") {
1395 $new = "";
1396 } elsif (lc($ARGV[1]) eq "suppressed" || lc($ARGV[1]) eq "suppress") {
1397 $new = "<!-- suppress -->";
1398 } else {
1399 my $fn = $ARGV[1];
1400 $fn =~ s/^\@//;
1401 die "missing filename for README\n" unless $fn ne "";
1402 die "no such file: \"$fn\"\n" unless -f $fn && -r $fn;
1403 open F, '<', $fn or die "cannot open \"$fn\" for reading: $!\n";
1404 local $/;
1405 $new = <F>;
1406 close F;
1407 $raw = 1;
1408 $newname = "contents of \"$fn\"";
1410 defined($new) or $new = '';
1411 $project->{README} = to_utf8($new, 1);
1412 $project->_cleanup_readme;
1413 if (length($project->{README}) > 8192) {
1414 die "readme greater than 8192 chars is too long (use --force to override)\n"
1415 unless $force;
1416 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
1418 if ($raw) {
1419 my $rd = get_readme_desc($project->{README});
1420 if ($rd ne "automatic" && $rd ne "suppressed") {
1421 my $xmllint = qx(command -v xmllint); chomp $xmllint;
1422 if (-f $xmllint && -x $xmllint) {
1423 my ($cnt, $err) = $project->_lint_readme(0);
1424 if ($cnt) {
1425 my $msg = "xmllint: $cnt error";
1426 $msg .= "s" unless $cnt == 1;
1427 print STDERR "$msg\n", "-" x length($msg), "\n", $err
1428 unless $force && $quiet;
1429 exit(255) unless $force;
1430 warn $project->{name} . ": using invalid raw HTML with --force\n" unless $quiet;
1432 } else {
1433 die "xmllint not available, refusing to use raw HTML without --force\n"
1434 unless $force;
1435 warn "xmllint not available using unchecked raw HTML with --force\n" unless $quiet;
1439 if (defined($old) && $old eq $project->{README}) {
1440 warn $project->{name}, ": skipping update of README to same value\n" unless $quiet;
1441 } else {
1442 # Avoid touching anything other than README.html file
1443 $project->_property_fput("README", $project->{README});
1444 $project->_set_changed;
1445 my $desc = get_readme_desc($project->{README});
1446 if ($newname) {
1447 $newname .= " ($desc)";
1448 } else {
1449 $newname = $desc;
1451 warn $project->{name}, ": README updated to $newname\n" unless $quiet;
1453 return 0;
1456 sub valid_head {
1457 my ($proj, $newhead) = @_;
1458 my %okheads = map({($_ => 1)} $proj->get_heads);
1459 exists($okheads{$newhead});
1462 sub cmd_sethead {
1463 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1464 my $project = get_project($ARGV[0]);
1465 if (@ARGV == 2 && !valid_head($project, $ARGV[1])) {
1466 die "invalid head (try \"@{[basename($0)]} listheads $ARGV[0]\"): \"$ARGV[1]\"\n";
1468 my $old = $project->{HEAD};
1469 if (@ARGV == 1) {
1470 print "$old\n" if defined($old);
1471 return 0;
1473 if (defined($old) && $old eq $ARGV[1]) {
1474 warn $project->{name}, ": skipping update of HEAD symref to same value\n" unless $quiet;
1475 } else {
1476 # Avoid touching anything other than the HEAD symref
1477 $project->set_HEAD($ARGV[1]);
1478 $project->_set_changed;
1479 warn $project->{name}, ": HEAD symref updated to \"refs/heads/$ARGV[1]\"\n" unless $quiet;
1481 return 0;
1484 sub cmd_sethooks {
1485 my $force = 0;
1486 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1487 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1488 my $project = get_project($ARGV[0]);
1489 my $projconfig = read_config_file_hash($project->{path}."/config");
1490 my $ghp = $Girocco::Config::reporoot."/_global/hooks";
1491 my $rghp = realpath($ghp);
1492 my $lhp = $project->{path}."/hooks";
1493 my $rlhp = realpath($lhp);
1494 my $ahp = "";
1495 my $rahp = undef;
1496 if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) {
1497 $ahp = $projconfig->{"core.hookspath"};
1498 $rahp = realpath($ahp);
1500 if (@ARGV == 1) {
1501 if (defined($rahp) && $rahp ne "") {
1502 if ($rahp eq $rghp) {
1503 my $nc = ($ahp eq $ghp ? "" : " non-canonical");
1504 printf "%s \t(global%s)\n", $ahp, $nc;
1505 } elsif ($rahp eq $rlhp) {
1506 my $nc = ($ahp eq $lhp ? "" : " non-canonical");
1507 printf "%s \t(local%s)\n", $ahp, $nc;
1508 } elsif ($rahp ne $ahp) {
1509 print "$ahp \t($rahp)\n";
1510 } else {
1511 print "$ahp\n";
1513 } elsif ($ahp ne "") {
1514 print "$ahp \t(non-existent)\n";
1516 return 0;
1518 my $shp = $ARGV[1];
1519 if (lc($shp) eq "global") {
1520 $shp = $ghp;
1521 } elsif (lc($shp) eq "local") {
1522 $shp = $lhp;
1523 } elsif (substr($shp, 0, 2) eq "~/") {
1524 $shp = $ENV{"HOME"}.substr($shp,1);
1525 } elsif ($shp =~ m,^~([a-zA-Z_][a-zA-Z_0-9]*)((?:/.*)?)$,) {
1526 my $sfx = $2;
1527 my $hd = (getpwnam($1))[7];
1528 $shp = $hd . $sfx if defined($hd) && $hd ne "" && $hd ne "/" && -d $hd;
1530 $shp ne "" && -d $shp or die "no such directory: $ARGV[1]\n";
1531 my $rshp = realpath($shp);
1532 defined($rshp) && $rshp ne "" or die "could not realpath: $ARGV[1]\n";
1533 $rshp =~ m,^/[^/], or die "invalid hookspath: $rshp\n";
1534 die "refusing to switch from current non-global hookspath without --force\n"
1535 if !$force && defined($rahp) && $rahp ne "" && $rahp ne $rghp && $rshp ne $rahp;
1536 if (!$force && defined($rahp) && $rahp ne "") {
1537 if ($rshp eq $rahp && ($ahp eq $ghp || $ahp eq $lhp)) {
1538 warn $project->{name}, ": skipping update of hookspath to same effective value\n" unless $quiet;
1539 return 0;
1542 $rshp = $ghp if $rshp eq $rghp;
1543 $rshp = $lhp if $rshp eq $rlhp;
1544 if ($rshp eq $ahp) {
1545 warn $project->{name}, ": skipping update of hookspath to same value\n" unless $quiet;
1546 return 0;
1548 die "refusing to set neither local nor global hookspath without --force\n"
1549 if !$force && $rshp ne $ghp && $rshp ne $lhp;
1550 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1551 'config', "core.hookspath", $rshp);
1552 my $newval = '"'.$rshp.'"';
1553 $newval = "global" if $rshp eq $ghp;
1554 $newval = "local" if $rshp eq $lhp;
1555 warn $project->{name}, ": hookspath set to $newval\n" unless $quiet;
1556 return 0;
1559 our %boolfields;
1560 BEGIN {
1561 %boolfields = (
1562 cleanmirror => 1,
1563 reverseorder => 0,
1564 summaryonly => 0,
1565 statusupdates => 1,
1569 sub cmd_setbool {
1570 my $force = 0;
1571 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1572 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1573 my $project = get_project($ARGV[0]);
1574 if (!exists($boolfields{$ARGV[1]})) {
1575 die "invalid boolean field name: \"$ARGV[1]\" -- try \"help\"\n";
1577 if (@ARGV == 3 && $boolfields{$ARGV[1]} && !$project->{mirror}) {
1578 die "invalid boolean field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
1579 unless $force;
1580 warn "using mirror field on non-mirror with --force\n" unless $quiet;
1582 if (@ARGV == 3 && !valid_bool($ARGV[2])) {
1583 die "invalid boolean value: \"$ARGV[2]\"\n";
1585 my $bool = clean_bool($ARGV[2]);
1586 my $old = $project->{$ARGV[1]};
1587 if (@ARGV == 2) {
1588 print "$old\n" if defined($old);
1589 return 0;
1591 if (defined($old) && $old eq $bool) {
1592 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
1593 } else {
1594 # Avoid touching anything other than $ARGV[1] field
1595 $project->_property_fput($ARGV[1], $bool);
1596 warn $project->{name}, ": $ARGV[1] updated to $bool\n" unless $quiet;
1598 return 0;
1601 sub cmd_setautogchack {
1602 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1603 my $project = get_project($ARGV[0]);
1604 my $aghok = $Girocco::Config::autogchack &&
1605 ($project->{mirror} || $Girocco::Config::autogchack ne "mirror");
1606 my $old = defined($project->{autogchack}) ? clean_bool($project->{autogchack}) : "unset";
1607 if (@ARGV == 1) {
1608 print "$old\n" if $aghok;
1609 return 0;
1611 my $bool;
1612 if (lc($ARGV[1]) eq "unset") {
1613 $bool = "unset";
1614 } else {
1615 valid_bool($ARGV[1]) or die "invalid boolean value: \"$ARGV[1]\"\n";
1616 $bool = clean_bool($ARGV[1]);
1618 if (!$aghok) {
1619 die "\$Girocco::Config::autogchack is false\n" unless $Girocco::Config::autogchack;
1620 die "\$Girocco::Config::autogchack is only enabled for mirrors\n";
1622 if ($old eq $bool) {
1623 warn $project->{name}, ": autogchack value unchanged\n" unless $quiet;
1624 } else {
1625 if ($bool eq "unset") {
1626 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1627 'config', '--unset', "girocco.autogchack");
1628 } else {
1629 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1630 'config', '--bool', "girocco.autogchack", $bool);
1633 return system($Girocco::Config::basedir . "/jobd/maintain-auto-gc-hack.sh", $project->{name}) == 0
1634 ? 0 : 1;
1637 sub valid_url {
1638 my ($url, $type) = @_;
1639 $type ne 'baseurl' and return valid_web_url($url);
1640 valid_repo_url($url) or return 0;
1641 if ($Girocco::Config::restrict_mirror_hosts) {
1642 my $mh = extract_url_hostname($url);
1643 is_dns_hostname($mh) or return 0;
1644 !is_our_hostname($mh) or return 0;
1646 return 1;
1649 our %urlfields;
1650 BEGIN {
1651 %urlfields = (
1652 baseurl => ["url" , 1],
1653 homepage => ["hp" , 0],
1654 notifyjson => ["notifyjson", 0],
1658 sub cmd_seturl {
1659 my $force = 0;
1660 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1661 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1662 my $project = get_project($ARGV[0]);
1663 if (!exists($urlfields{$ARGV[1]})) {
1664 die "invalid URL field name: \"$ARGV[1]\" -- try \"help\"\n";
1666 if (@ARGV == 3 && ${$urlfields{$ARGV[1]}}[1] && !$project->{mirror}) {
1667 die "invalid URL field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
1668 unless $force;
1669 warn "using mirror field on non-mirror with --force\n" unless $quiet;
1671 if (@ARGV == 3 && !valid_url($ARGV[2], $ARGV[1])) {
1672 die "invalid URL (use --force to accept): \"$ARGV[2]\"\n"
1673 unless $force;
1674 warn "using invalid URL with --force\n" unless $quiet;
1676 my $old = $project->{${$urlfields{$ARGV[1]}}[0]};
1677 if (@ARGV == 2) {
1678 print "$old\n" if defined($old);
1679 return 0;
1681 if (defined($old) && $old eq $ARGV[2]) {
1682 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
1683 } else {
1684 # Avoid touching anything other than $ARGV[1]'s field
1685 $project->_property_fput(${$urlfields{$ARGV[1]}}[0], $ARGV[2]);
1686 if ($ARGV[1] eq "baseurl") {
1687 $project->{url} = $ARGV[2];
1688 $project->_set_bangagain;
1690 $project->_set_changed unless $ARGV[1] eq "notifyjson";
1691 warn $project->{name}, ": $ARGV[1] updated to $ARGV[2]\n" unless $quiet;
1693 return 0;
1696 our %msgsfields;
1697 BEGIN {
1698 %msgsfields = (
1699 notifymail => 1,
1700 notifytag => 1,
1704 sub cmd_setmsgs {
1705 my $force = 0;
1706 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1707 @ARGV >= 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1708 my $project = get_project(shift @ARGV);
1709 my $field = shift @ARGV;
1710 if (!exists($msgsfields{$field})) {
1711 die "invalid msgs field name: \"$field\" -- try \"help\"\n";
1713 if (@ARGV && !valid_addrlist(@ARGV)) {
1714 die "invalid email address list (use --force to accept): \"".join(" ",@ARGV)."\"\n"
1715 unless $force;
1716 warn "using invalid email address list with --force\n" unless $quiet;
1718 my $old = $project->{$field};
1719 if (!@ARGV) {
1720 printf "%s\n", clean_addrlist($old, " ") if defined($old);
1721 return 0;
1723 my $newlist = clean_addrlist(join(" ",@ARGV));
1724 if (defined($old) && $old eq $newlist) {
1725 warn $project->{name}, ": skipping update of $field to same value\n" unless $quiet;
1726 } else {
1727 # Avoid touching anything other than $field's field
1728 $project->_property_fput($field, $newlist);
1729 warn $project->{name}, ": $field updated to \"$newlist\"\n" unless $quiet;
1731 return 0;
1734 sub cmd_setusers {
1735 my $force = 0;
1736 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1737 @ARGV >= 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1738 my $project = get_project(shift @ARGV);
1739 my $projname = $project->{name};
1740 !@ARGV || !$project->{mirror} or die "cannot set users list for mirror project: \"$projname\"\n";
1741 my @newusers = ();
1742 if (@ARGV) {
1743 eval {@newusers = validate_users(join(" ", @ARGV), $force); 1;} or exit 255;
1744 die "refusing to set empty users list without --force\n" unless @newusers || $force;
1746 return 0 if !@ARGV && $project->{mirror};
1747 my $oldusers = $project->{users};
1748 if ($oldusers && ref($oldusers) eq "ARRAY") {
1749 $oldusers = join("\n", @$oldusers);
1750 } else {
1751 $oldusers = "";
1753 if (!@ARGV) {
1754 print "$oldusers\n" if $oldusers ne "";
1755 return 0;
1757 if ($oldusers eq join("\n", @newusers)) {
1758 warn "$projname: skipping update of users list to same value\n" unless $quiet;
1759 } else {
1760 # Avoid touching anything other than the users list
1761 $project->{users} = \@newusers;
1762 $project->_update_users;
1763 warn "$projname: users list updated to \"@{[join(',',@newusers)]}\"\n" unless $quiet;
1765 return 0;
1768 our %fieldnames;
1769 BEGIN {
1770 %fieldnames = (
1771 owner => [\&cmd_setowner, 0],
1772 desc => [\&cmd_setdesc, 0],
1773 description => [\&cmd_setdesc, 0],
1774 readme => [\&cmd_setreadme, 0],
1775 head => [\&cmd_sethead, 0],
1776 HEAD => [\&cmd_sethead, 0],
1777 hooks => [\&cmd_sethooks, 0],
1778 hookspath => [\&cmd_sethooks, 0],
1779 cleanmirror => [\&cmd_setbool, 1],
1780 reverseorder => [\&cmd_setbool, 1],
1781 summaryonly => [\&cmd_setbool, 1],
1782 statusupdates => [\&cmd_setbool, 1],
1783 autogchack => [\&cmd_setautogchack, 0],
1784 baseurl => [\&cmd_seturl, 1],
1785 homepage => [\&cmd_seturl, 1],
1786 notifyjson => [\&cmd_seturl, 1],
1787 notifymail => [\&cmd_setmsgs, 1],
1788 notifytag => [\&cmd_setmsgs, 1],
1789 users => [\&cmd_setusers, 0],
1793 sub do_getset {
1794 $setopt = shift;
1795 my @newargs = ();
1796 push(@newargs, shift) if @_ && $_[0] eq '--force';
1797 my $field = $_[1];
1798 (($setopt && @_ >= 3) || @_ == 2) && exists($fieldnames{$field}) or die_usage;
1799 push(@newargs, shift);
1800 shift unless ${$fieldnames{$field}}[1];
1801 push(@newargs, @_);
1802 diename(($setopt ? "set " : "get ") . $field);
1803 @ARGV = @newargs;
1804 &{${$fieldnames{$field}}[0]}(@ARGV);
1807 sub cmd_get {
1808 do_getset(0, @_);
1811 sub cmd_set {
1812 do_getset(1, @_);
1815 our %commands;
1816 BEGIN {
1817 %commands = (
1818 list => \&cmd_list,
1819 create => \&cmd_create,
1820 adopt => \&cmd_adopt,
1821 remove => \&cmd_remove,
1822 trash => \&cmd_remove,
1823 delete => \&cmd_remove,
1824 show => \&cmd_show,
1825 listheads => \&cmd_listheads,
1826 listtags => \&cmd_listtags,
1827 listctags => \&cmd_listtags,
1828 deltags => \&cmd_deltags,
1829 delctags => \&cmd_deltags,
1830 addtags => \&cmd_addtags,
1831 addctags => \&cmd_addtags,
1832 chpass => \&cmd_chpass,
1833 checkpw => \&cmd_checkpw,
1834 gc => \&cmd_gc,
1835 update => \&cmd_update,
1836 remirror => \&cmd_remirror,
1837 setowner => \&cmd_setowner,
1838 setdesc => \&cmd_setdesc,
1839 setdescription => \&cmd_setdesc,
1840 setreadme => \&cmd_setreadme,
1841 sethead => \&cmd_sethead,
1842 sethooks => \&cmd_sethooks,
1843 sethookspath => \&cmd_sethooks,
1844 setbool => \&cmd_setbool,
1845 setboolean => \&cmd_setbool,
1846 setflag => \&cmd_setbool,
1847 setautogchack => \&cmd_setautogchack,
1848 seturl => \&cmd_seturl,
1849 setmsgs => \&cmd_setmsgs,
1850 setusers => \&cmd_setusers,
1851 get => \&cmd_get,
1852 set => \&cmd_set,
1856 sub dohelp {
1857 my $cmd = shift;
1858 my $bn = basename($0);
1859 printf "%s version %s\n\n", $bn, $VERSION;
1860 if (defined($cmd) && $cmd ne '') {
1861 $cmd =~ s/^set(?=[a-zA-Z])//i;
1862 my $cmdhelp = '';
1863 my ($lastmt, $incmd);
1864 foreach (split('\n', sprintf($help, $bn))) {
1865 $lastmt || $incmd or $lastmt = /^\s*$/, next;
1866 $incmd = 1 if $lastmt && /^\s*(?:\[?set\]?)?$cmd\s/;
1867 last if $incmd && /^\s*$/;
1868 $incmd and $cmdhelp .= $_ . "\n";
1869 $lastmt = /^\s*$/;
1871 print $cmdhelp and exit 0 if $cmdhelp;
1873 printf $help, $bn;
1874 exit 0;
1877 sub main {
1878 local *ARGV = \@_;
1879 shift, $quiet=1 if @ARGV && $ARGV[0] =~ /^(?:-q|--quiet)$/i;
1880 dohelp($ARGV[1]) if !@ARGV || @ARGV && $ARGV[0] =~ /^(?:-h|-?-help|help)$/i;
1881 my $command = shift;
1882 diename($command);
1883 $setopt = 1;
1884 if (!exists($commands{$command}) && exists($commands{"set".$command})) {
1885 $setopt = 0;
1886 $command = "set" . $command;
1888 exists($commands{$command}) or die "Unknown command \"$command\" -- try \"help\"\n";
1889 dohelp($command) if @ARGV && $ARGV[0] =~ /^(?:-h|-?-help)$/i ||
1890 $ARGV[0] =~ /^help$/i && !Girocco::Project::does_exist("help",1);
1891 &{$commands{$command}}(@ARGV);