install.sh: add installation preconditions check
[girocco.git] / toolbox / projtool.pl
bloba717e6598992bcb497447cdb2acb67db1dc29238
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.3'}
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 {my @a; /^(.*)$/s && push(@a, $1) foreach @ARGV; @ARGV=@a;}
31 BEGIN {$help = <<'HELP'}
32 Usage: %s [<global option>...] <command> <options>
34 global options:
35 -q | --quiet suppress warning messages
36 -p | --pager force output to be paginated
37 --no-pager never paginate output
39 help [<command>]
40 show full help or just for <command> if given
42 list [--verbose] [--sort=lcname|name|owner|gid|no] [--owner] [<regexp>]
43 list all projects (default is --sort=lcname)
44 limit to project names matching <regex> if given
45 match <regex> against owner instead of project name with --owner
47 create [--force] [--no-alternates] [--orphan] [<option>...] <project>
48 create new project <project> (prompted)
49 <option> can be:
50 --no-alternates skip setup of objects/info/alternates
51 --orphan allow creation of subproject w/o a parent
52 -p use mkdir -p during --orphan creation
53 --no-password set password crypt to invalid value "unknown"
54 --no-owner leave the gitweb.owner config unset
55 --mirror=<url> create a mirror from <url>
56 --full-mirror mirror all refs
57 --push[=<list>] create a push project
58 --desc=<string> specify project description w/o prompt
59 --homepage=<url> specify project homepage URL w/o prompt
60 --defaults do no interactive prompting at all
61 Using --no-password skips the prompts for password, using
62 --no-owner skips the prompt for owner and using --mirror=<url>
63 or --push[=<list>] skips the prompts for mirror URL and
64 heads-only and push users. With --defaults if neither
65 --mirror=<url> nor --push[=<list>] is given then --push will
66 be implied. Using --desc=<string> will force a specific
67 description (including an empty string) and skip the prompt for
68 it. Otherwise a non-empty default description will always be
69 supplied in lieu of an empty or omitted description.
71 adopt [--force] [--type=mirror|push] [<option>...] <project> [<users>]
72 adopt project <project>
73 type of project is guessed if --type=<type> omitted
74 <users> is same as <newuserslist> for setusers command
75 <option> can be:
76 --dry-run do all the checks but don't perform adoption
77 --verbose show project info dump (useful with --dry-run)
78 --no-users no push users at all (<users> must be omitted)
79 --no-owner leave the gitweb.owner config totally unchanged
80 --owner=<val> set the gitweb.owner config to <val>
81 Both --no-owner and --owner=<val> may NOT be given, with neither
82 take owner from preexisting gitweb.owner else use admin setting.
83 For mirrors <users> is ignored otherwise if no <users> and no
84 --no-users option the push users list will consist of the single
85 user name matching the owner or empty if none or more than one.
86 With --dry-run <project> can be an absolute path to a git dir.
88 remove [--force] [--really-delete] [--keep-forks] <project>
89 remove project <project>
90 do not move to _recyclebin with --really-delete (just rm -rf)
91 remove projects with forks (by keeping forks) using --keep-forks
93 show <project>
94 show project <project>
96 listheads <project>
97 list all available heads for <project> and indicate current head
99 listtags [--verbose] <project>
100 list all ctags on project <project>
102 deltags <project> [-i] <tagstodel>
103 remove any ctags on project <project> present in <tagstodel>
104 <tagstodel> is space or comma separated list of tags to remove
105 with -i match against <tagstodel> without regard to letter case
107 addtags <project> <tagstoadd>
108 add ctags to project <project>
109 <tagstoadd> is space or comma separated list of tags to add
111 chpass [--force] <project> [random | unknown]
112 change project <project> password (prompted)
113 with "random" set to random password
114 with "unknown" set password hash to invalid value "unknown"
116 checkpw <project>
117 check project <project> password for a match (prompted)
119 gc [--force | --auto] [--redelta | --recompress] <project>
120 run the gc.sh script on project <project>
121 with --auto let the gc.sh script decide what to do
122 with --force cause a full gc to take place (force_gc=1)
123 with neither --auto nor --force do a mini or if needed a full gc
124 (in other words just touch .needsgc and run gc.sh)
125 with --redelta a full gc will use pack-objects --no-reuse-delta
126 with --recompress a full gc uses pack-objects --no-reuse-object
127 (--no-reuse-delta and --no-reuse-object are accepted as aliases)
128 unless the global --quiet option is given show_progress=1 is used
130 update [--force] [--quiet | --summary] <project>
131 run the update.sh script on project <project>
132 with --force cause a fetch to always take place (force_update=1)
133 with --quiet only show errors (show_progress is left unset)
134 with --summary show progress and ref summary (show_progress=1)
135 with neither --quiet nor --summary show it all (show_progress=2)
137 remirror [--force] <project>
138 initiate a remirror of project <project>
140 [set]owner [--force] <project> <newowner>
141 set project <project> owner to <newowner>
142 without "set" and only 1 arg, just show current project owner
144 [set]desc [--force] <project> <newdesc>
145 set project <project> description to <newdesc>
146 without "set" and only 1 arg, just show current project desc
148 [set]readme [--force] <project> <newsetting>
149 set project <project> readme to <newsetting>
150 <newsetting> is automatic|suppressed|-|[@]filename
151 without "set" and only 2 args, just show current readme setting
153 [set]head <project> <newhead>
154 set project <project> HEAD symbolic ref to <newhead>
155 without "set" and only 1 arg, just show current project HEAD
157 [set]bool [--force] <project> <flagname> <boolvalue>
158 set project <project> boolean <flagname> to <boolvalue>
159 <flagname> is cleanmirror|reverseorder|summaryonly|statusupdtaes
160 without "set" and only 2 args, just show current flag value
162 [set]hooks [--force] <project> local | global | <path>
163 set project <project> hookspath to local, global or <path>
164 without "set" and only 1 arg, just show current hookspath
166 [set]autogchack <project> <boolvalue> | unset
167 set project <project> autogchack to <boolvalue> or "unset" it
168 without "set" just show current autogchack setting if enabled
169 with "set" autogchack must be enabled in Config.pm for the
170 type of project and maintain-auto-gc-hack.sh is always run
172 [set]url [--force] <project> <urlname> <newurlvalue>
173 set project <project> url <urlname> to <newurlvalue>
174 <urlname> is baseurl|homepage|notifyjson
175 without "set" and only 2 args, just show current url value
177 [set]msgs [--force] <project> <msgsname> <eaddrlist>
178 set project <project> msgs <msgsname> to <addrlist>
179 <msgsname> is notifymail|notifytag
180 <eaddrlist> is space or comma separated list of email addresses
181 without "set" and only 2 args, just show current msgs value
183 [set]users [--force] <project> <newuserslist>
184 set push project <project> users list to <newuserslist>
185 <newuserslist> is space or comma separated list of user names
186 without "set" and only 1 arg, just show current users list
188 [set]jsontype <project> <newjsontype>
189 set project <project> JSON Content-Type to <newjsontype>
190 <newjsontype> is x-www-form-urlencoded or json
191 without "set" and only 1 arg, just show current jsontype
193 [set]jsonsecret <project> <newjsonsecret>
194 set project <project> JSON secret to <newjsonsecret>
195 <newjsonsecret> is a string (empty string disables signatures)
196 without "set" and only 1 arg, just show current jsonsecret
198 get <project> <fieldname>
199 show project <project> field <fieldname>
200 <fieldname> is owner|desc|readme|head|hooks|users|jsontype
201 or jsonsecret|<flagname>|autogchack|<urlname>|<msgsname>
203 set [--force] <project> <fieldname> <newfieldvalue>
204 set project <project> field <fieldname> to <newfieldvalue>
205 <fieldname> same as for get
206 <newfieldvalue> same as for corresponding set... command
207 HELP
209 our $quiet;
210 our $usepager;
211 our $setopt;
212 sub die_usage {
213 my $sub = shift || diename;
214 if ($sub) {
215 die "Invalid arguments to $sub command -- try \"help\"\n";
216 } else {
217 die "Invalid arguments -- try \"help\"\n";
221 sub get_readme_desc {
222 my $rm = shift;
223 defined($rm) or $rm = '';
224 if (length($rm)) {
225 my $test = $rm;
226 $test =~ s/<!--(?:[^-]|(?:-(?!-)))*-->//gs;
227 $test =~ s/\s+//s;
228 return $test eq '' ? "suppressed" : "length " . length($rm);
229 } else {
230 return "automatic";
234 sub get_ctag_counts {
235 my $project = shift;
236 my $compact = shift;
237 my @ctags = ();
238 foreach ($project->get_ctag_names) {
239 my $val = 0;
240 my $ct;
241 if (open $ct, '<', $project->{path}."/ctags/$_") {
242 my $count = <$ct>;
243 close $ct;
244 defined $count or $count = '';
245 chomp $count;
246 $val = $count =~ /^[1-9]\d*$/ ? $count : 1;
248 if ($compact) {
249 if ($val == 1) {
250 push(@ctags, $_);
251 } elsif ($val > 1) {
252 push(@ctags, $_."(".$val.")");
254 } else {
255 push(@ctags, [$_, $val]) if $val;
258 @ctags;
261 sub get_clean_project {
262 my $project = get_project(@_);
263 delete $project->{loaded};
264 delete $project->{base_path};
265 delete $project->{ccrypt};
266 /^orig/i || !defined($project->{$_}) and delete $project->{$_} foreach keys %$project;
267 $project->{owner} = $project->{email}; delete $project->{email};
268 $project->{homepage} = $project->{hp}; delete $project->{hp};
269 $project->{baseurl} = $project->{url}; delete $project->{url};
270 if (defined($project->{path}) && $project->{path} ne "") {
271 my $rp = realpath($project->{path});
272 defined($rp) && $rp ne "" and $project->{realpath} = $rp;
273 if (-f "$rp/objects/info/packs") {
274 my $ipt = (stat _)[9];
275 defined($ipt) and $project->{infopackstime} =
276 strftime("%Y-%m-%d %H:%M:%S %z", localtime($ipt));
279 my $owner = $project->{owner};
280 if ($owner) {
281 $owner = lc($owner);
282 my @owner_users = map {$owner eq lc($$_[4]) ? $$_[1] : ()} get_all_users;
283 $project->{owner_users} = \@owner_users if @owner_users;
285 my $projname = $project->{name};
286 my @forks = grep {$$_[1] =~ m,^$projname/,} get_all_projects;
287 $project->{has_forks} = 1 if @forks;
288 $project->{has_alternates} = 1 if $project->has_alternates;
289 my @bundles = $project->bundles;
290 for (my $i = 0; $i < @bundles; ++$i) {
291 my $secs = $bundles[$i]->[0];
292 $bundles[$i]->[0] = strftime("%Y-%m-%d %H:%M:%S %z", localtime($secs));
293 my $sz = $bundles[$i]->[2];
294 1 while $sz =~ s/(?<=\d)(\d{3})(?:,|$)/,$1/g;
295 $bundles[$i]->[2] = $sz;
297 delete $project->{bundles};
298 $project->{bundles} = \@bundles if @bundles;
299 $project->{mirror} = 0 unless $project->{mirror};
300 $project->{is_empty} = 1 if $project->is_empty;
301 delete $project->{showpush} unless $project->{showpush};
302 delete $project->{users} if $project->{mirror};
303 delete $project->{baseurl} unless $project->{mirror};
304 delete $project->{banged} unless $project->{mirror};
305 delete $project->{lastrefresh} unless $project->{mirror};
306 delete $project->{cleanmirror} unless $project->{mirror};
307 delete $project->{statusupdates} unless $project->{mirror};
308 delete $project->{lastparentgc} unless $projname =~ m,/,;
309 unless ($project->{banged}) {
310 delete $project->{bangcount};
311 delete $project->{bangfirstfail};
312 delete $project->{bangmessagesent};
314 my $projhook = $project->_has_notifyhook;
315 if (defined($projhook) && $projhook ne "") {
316 $project->{notifyhook} = $projhook;
317 } else {
318 delete $project->{notifyhook};
320 $project->{README} = get_readme_desc($project->{README}) if exists($project->{README});
321 my @tags = get_ctag_counts($project, 1);
322 $project->{tags} = \@tags if @tags;
323 my $projconfig = read_config_file_hash($project->{path}."/config");
324 if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) {
325 my $ahp = $projconfig->{"core.hookspath"};
326 my $rahp = realpath($ahp);
327 my $lhp = $project->{path}."/hooks";
328 my $rlhp = realpath($lhp);
329 my $ghp = $Girocco::Config::reporoot."/_global/hooks";
330 my $rghp = realpath($ghp);
331 $project->{has_local_hooks} = 1 if
332 defined($rahp) && defined($rlhp) && $rahp eq $rlhp;
333 $project->{has_global_hooks} = 1 if
334 defined($rahp) && defined($rghp) && $rahp eq $rghp;
335 $project->{hookspath} = $ahp unless $ahp eq $lhp || $ahp eq $ghp;
337 $project;
340 sub clean_addrlist {
341 my %seen = ();
342 my @newlist = ();
343 foreach (split(/[,\s]+/, $_[0])) {
344 next unless $_;
345 $seen{lc($_)} = 1, push(@newlist, $_) unless $seen{lc($_)};
347 return join(($_[1]||","), @newlist);
350 sub valid_addrlist {
351 my $cleaned = clean_addrlist(join(" ", @_));
352 return 1 if $cleaned eq "";
353 valid_email_multi($cleaned) && length($cleaned) <= 512;
356 sub validate_users {
357 my ($userlist, $force, $nodie) = @_;
358 my @newusers = ();
359 my $badlist = 0;
360 my %seenuser = ();
361 my $mobok = $Girocco::Config::mob && $Girocco::Config::mob eq "mob";
362 my %users = map({($$_[1] => $_)} get_all_users);
363 foreach (split(/[\s,]+/, $userlist)) {
364 if (exists($users{$_}) || $_ eq "everyone" || ($mobok && $_ eq "mob")) {
365 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
366 next;
368 if (Girocco::User::does_exist($_, 1)) {
369 if ($force) {
370 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
371 } else {
372 $badlist = 1;
373 warn "refusing to allow questionable user \"$_\" without --force\n" unless $nodie && $quiet;
375 next;
377 $badlist = 1;
378 warn "invalid user: \"$_\"\n" unless $nodie && $quiet
380 die if $badlist && !$nodie;
381 return @newusers;
384 sub is_default_desc {
385 # "Unnamed repository; edit this file 'description' to name the repository."
386 # "Unnamed repository; edit this file to name it for gitweb."
387 local $_ = shift;
388 return 0 unless defined($_);
389 /Unnamed\s+repository;/i && /\s+edit\s+this\s+file\s+/i && /\s+to\s+name\s+/i;
392 sub valid_desc {
393 my $test = shift;
394 chomp $test;
395 return 0 if $test =~ /[\r\n]/;
396 $test =~ s/\s\s+/ /g;
397 $test =~ s/^\s+//;
398 $test =~ s/\s+$//;
399 return $test ne '';
402 sub clean_desc {
403 my $desc = shift;
404 defined($desc) or $desc = '';
405 chomp $desc;
406 $desc = to_utf8($desc, 1);
407 $desc =~ s/\s\s+/ /g;
408 $desc =~ s/^\s+//;
409 $desc =~ s/\s+$//;
410 return $desc;
413 sub parse_options {
414 Girocco::CLIUtil::_parse_options(
415 sub {
416 warn((($_[0]eq'?')?"unrecognized":"missing argument for")." option \"$_[1]\"\n")
417 unless $quiet;
418 die_usage;
419 }, @_);
422 sub cmd_list {
423 my %sortsub = (
424 lcname => sub {lc($$a[1]) cmp lc($$b[1])},
425 name => sub {$$a[1] cmp $$b[1]},
426 gid => sub {$$a[3] <=> $$b[3]},
427 owner => sub {lc($$a[4]) cmp lc($$b[4]) || lc($$a[1]) cmp lc($$b[1])},
428 no => sub {$$a[0] <=> $$b[0]},
430 my $sortopt = 'lcname';
431 my ($verbose, $owner);
432 parse_options(":sort" => \$sortopt, verbose => \$verbose, owner => \$owner);
433 my $regex;
434 if (@ARGV) {
435 my $val = shift @ARGV;
436 $regex = qr($val) or die "bad regex \"$val\"\n";
438 !@ARGV && exists($sortsub{$sortopt}) or die_usage;
439 my $sortsub = $sortsub{$sortopt};
440 my $grepsub = defined($regex) ? ($owner ? sub {$$_[4] =~ /$regex/} : sub {$$_[1] =~ /$regex/}) : sub {1};
441 my @projects = sort($sortsub grep {&$grepsub} get_all_projects);
442 if ($verbose) {
443 print map(sprintf("%s\n", join(":", (@$_)[1..5])), @projects);
444 } else {
445 print map(sprintf("%s: %s\n", $$_[1], $$_[5] =~ /^:/ ? "<mirror>" : $$_[5]), @projects);
447 return 0;
450 sub cmd_create {
451 my ($force, $noalternates, $orphanok, $optp, $nopasswd, $noowner, $defaults, $ispush, $pushusers,
452 $ismirror, $desc, $fullmirror, $homepage);
453 parse_options(
454 force => \$force, "no-alternates" => \$noalternates, orphan => \$orphanok, p => \$optp,
455 "no-password" => \$nopasswd, "no-owner" => \$noowner, defaults => \$defaults,
456 "push" => \$ispush, ":push" => \$pushusers, ":mirror" => \$ismirror, ":desc" => \$desc,
457 ":description" => \$desc, "full-mirror" => \$fullmirror, ":homepage" => \$homepage);
458 @ARGV == 1 or die_usage;
459 !defined($pushusers) || defined($ispush) or $ispush = 1;
460 defined($ismirror) && $ismirror =~ /^\s*$/ and die "--mirror url must not be empty\n";
461 die "--mirror and --push are mutually exclusive options\n" if $ismirror && $ispush;
462 die "--full-mirror requires use of --mirror=<url> option\n" if $fullmirror && !$ismirror;
463 !$defaults || defined($ispush) || defined($ismirror) or $ispush = 1;
464 !$defaults || defined($nopasswd) or $nopasswd = 1;
465 !$defaults || defined($noowner) or $noowner = 1;
466 !defined($ispush) || defined($pushusers) or $pushusers = "";
467 my $projname = $ARGV[0];
468 $projname =~ s/\.git$//i;
469 Girocco::Project::does_exist($projname, 1) and die "Project already exists: \"$projname\"\n";
470 if (!Girocco::Project::valid_name($projname, $orphanok, $optp)) {
471 warn "Refusing to create orphan project without --orphan\n"
472 if !$quiet && !$orphanok && Girocco::Project::valid_name($projname, 1, 1);
473 warn "Required orphan parent directory does not exist (use -p): ",
474 $Girocco::Config::reporoot.'/'.Girocco::Project::get_forkee_name($projname), "\n"
475 if !$quiet && $orphanok && Girocco::Project::valid_name($projname, 1, 1);
476 die "Invalid project name: \"$projname\"\n";
478 my ($forkee, $project) = ($projname =~ m#^(.*/)?([^/]+)$#);
479 my $newtype = $forkee ? 'fork' : 'project';
480 if (length($project) > 64) {
481 die "The $newtype name is longer than 64 characters. Do you really need that much?\n"
482 unless $force;
483 warn "Allowing $newtype name longer than 64 characters with --force\n" unless $quiet;
485 unless ($Girocco::Config::push || $Girocco::Config::mirror) {
486 die "Project creation disabled (no mirrors or push projects allowed)\n" unless $force;
487 warn "Continuing with --force even though both push and mirror projects are disabled\n" unless $quiet;
489 print "Enter settings for new project \"$projname\"\n" unless $defaults;
490 my %settings = ();
491 $settings{noalternates} = $noalternates;
492 if ($nopasswd) {
493 $settings{crypt} = "unknown";
494 } else {
495 my $np1 = prompt_noecho_nl_or_die("Admin password for project $projname (echo is off)");
496 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
497 my $np2 = prompt_noecho_nl_or_die("Retype admin password for project $projname");
498 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
499 "the admin passwords you have entered do not match each other.\n";
500 $settings{crypt} = scrypt_sha1($np1);
502 my $owner = "";
503 unless ($noowner) {
504 $owner = prompt_or_die("Owner/email name for project $projname");
505 unless (valid_email($owner)) {
506 unless ($force) {
507 warn "Your email sure looks weird...?\n";
508 redo;
510 warn "Allowing invalid email with --force\n" unless $quiet;
512 if (length($owner) > 96) {
513 unless ($force) {
514 warn "Your email is longer than 96 characters. Do you really need that much?\n";
515 redo;
517 warn "Allowing email longer than 96 characters with --force\n" unless $quiet;
520 $settings{email} = $owner;
521 my $baseurl = "";
522 my $checkmirror = sub {
523 my $checkurl = shift;
524 unless (valid_repo_url($checkurl)) {
525 unless ($force) {
526 warn "Invalid mirror URL: \"$checkurl\"\n";
527 return undef;
529 warn "Allowing invalid mirror URL with --force\n" unless $quiet;
531 if ($Girocco::Config::restrict_mirror_hosts) {
532 my $mh = extract_url_hostname($checkurl);
533 unless (is_dns_hostname($mh)) {
534 unless ($force) {
535 warn "Invalid non-DNS mirror URL: \"$checkurl\"\n";
536 return undef;
538 warn "Allowing non-DNS mirror URL with --force\n" unless $quiet;
540 if (is_our_hostname($mh)) {
541 unless ($force) {
542 warn "Invalid same-host mirror URL: \"$checkurl\"\n";
543 return undef;
545 warn "Allowing same-host mirror URL with --force\n" unless $quiet;
548 return $checkurl;
550 if ($ispush || $ismirror) {
551 !$ispush || $force || $Girocco::Config::push or
552 die "Push projects are disabled, create a mirror (or use --force)\n";
553 !$ismirror || $force || $Girocco::Config::mirror or
554 die "Mirror projects are disabled, create a push project (or use --force)\n";
555 if ($ismirror) {
556 &$checkmirror($ismirror) or die "Invalid --mirror URL\n";
557 $baseurl = $ismirror;
558 $settings{url} = $baseurl;
559 $settings{cleanmirror} = $fullmirror ? 0 : 1;
560 } else {
561 my @newusers = ();
562 if ($pushusers !~ /^[\s,]*$/) {
563 eval {@newusers = validate_users($pushusers, $force); 1;} or
564 die "Invalid --push user list\n";
566 $settings{users} = \@newusers;
568 } elsif ($force || $Girocco::Config::mirror) {{
569 if ($force || $Girocco::Config::push) {
570 $baseurl = prompt_or_die("URL to mirror from (leave blank for push project)", "");
571 } else {{
572 $baseurl = prompt_or_die("URL to mirror from");
573 unless ($baseurl ne "") {
574 warn "Push projects are disabled, you must enter a mirror URL (or use --force)\n";
575 redo;
578 if ($baseurl ne "") {
579 &$checkmirror($baseurl) or redo;
580 $settings{url} = $baseurl;
581 $settings{cleanmirror} =
582 ynprompt_or_die("Mirror only heads, tags and notes (Y/n)", "Yes");
585 my $mirror = ($baseurl eq "") ? 0 : 1;
586 my $checkdesc = sub {
587 my $d = shift;
588 if (length($d) > 1024) {
589 unless ($force) {
590 warn "Short description length greater than 1024 characters!\n";
591 return undef;
593 warn "Allowing short description length greater than 1024 characters\n" unless $quiet;
595 return $d;
597 if (defined($desc)) {
598 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
599 $desc eq "" || &$checkdesc($desc) or
600 die "Invalid --desc description\n";
601 } elsif (!$defaults) {
602 $desc = prompt_or_die("Short description", "");
603 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
604 $desc eq "" || &$checkdesc($desc) or redo;
605 $desc = undef if $desc eq "";
607 defined($desc) or $desc = $mirror ? "Mirror of $baseurl" : "Push project $projname";
608 $settings{desc} = $desc;
609 my $checkhp = sub {
610 my $hpurl = shift;
611 unless (valid_web_url($hpurl)) {
612 unless ($force) {
613 warn "Invalid home page URL: \"$hpurl\"\n";
614 return undef;
616 warn "Allowing invalid home page URL with --force\n" unless $quiet;
618 return $hpurl;
620 if (defined($homepage)) {
621 $homepage =~ s/^\s+//; $homepage =~ s/\s+$//;
622 $homepage eq "" || &$checkhp($homepage) or
623 die "Invalid --homepage URL\n";
624 } elsif (!$defaults) {
625 $homepage = prompt_or_die("Home page URL", "");
626 $homepage =~ s/^\s+//; $homepage =~ s/\s+$//;
627 $homepage eq "" || &$checkhp($homepage) or redo;
628 $homepage = undef if $homepage eq "";
630 $settings{hp} = $homepage;
631 my $jsonurl = "";
632 if (!$defaults) {
633 $jsonurl = prompt_or_die("JSON notify POST URL", "");
634 if ($jsonurl ne "" && !valid_web_url($jsonurl)) {
635 unless ($force) {
636 warn "Invalid JSON notify POST URL: \$jsonurl\"\n";
637 redo;
639 warn "Allowing invalid JSON notify POST URL with --force\n" unless $quiet;
642 $settings{notifyjson} = $jsonurl;
643 my $commitaddrs = "";
644 if (!$defaults) {
645 $commitaddrs = clean_addrlist(prompt_or_die("Commit notify email addr(s)", ""));
646 if ($commitaddrs ne "" && !valid_addrlist($commitaddrs)) {
647 unless ($force) {
648 warn"invalid commit notify email address list (use --force to accept): \"$commitaddrs\"\n";
649 redo;
651 warn "using invalid commit notify email address list with --force\n" unless $quiet;
654 $settings{notifymail} = $commitaddrs;
655 $settings{reverseorder} = 1;
656 $settings{reverseorder} = ynprompt_or_die("Oldest-to-newest commit order in emails", "Yes")
657 if !$defaults && $commitaddrs ne "";
658 $settings{summaryonly} = ynprompt_or_die("Summary only (no diff) in emails", "No")
659 if !$defaults && $commitaddrs ne "";
660 my $tagaddrs = "";
661 if (!$defaults) {
662 $tagaddrs = clean_addrlist(prompt_or_die("Tag notify email addr(s)", ""));
663 if ($tagaddrs ne "" && !valid_addrlist($tagaddrs)) {
664 unless ($force) {
665 warn"invalid tag notify email address list (use --force to accept): \"$tagaddrs\"\n";
666 redo;
668 warn "using invalid tag notify email address list with --force\n" unless $quiet;
671 $settings{notifytag} = $tagaddrs;
672 if (!$mirror && !$ispush) {
673 my @newusers = ();
675 my $userlist = prompt_or_die("Push users", join(",", @newusers));
676 eval {@newusers = validate_users($userlist, $force); 1;} or redo;
678 $settings{users} = \@newusers;
680 my $newproj = Girocco::Project->ghost($projname, $mirror, $orphanok, $optp)
681 or die "Girocco::Project->ghost call failed\n";
682 my ($k, $v);
683 $newproj->{$k} = $v while ($k, $v) = each(%settings);
684 my $killowner = sub {
685 system($Girocco::Config::git_bin, '--git-dir='.$newproj->{path},
686 'config', '--unset', "gitweb.owner");
688 if ($mirror) {
689 $newproj->premirror or die "Girocco::Project->premirror failed\n";
690 !$noowner or &$killowner;
691 $newproj->clone or die "Girocco::Project->clone failed\n";
692 warn "Project $projname created and cloning successfully initiated.\n"
693 unless $quiet;
694 } else {
695 $newproj->conjure or die "Girocco::Project->conjure failed\n";
696 !$noowner or &$killowner;
697 warn "New push project fork is empty due to use of --no-alternates\n"
698 if !$quiet && $projname =~ m,/, && $noalternates;
699 warn "Project $projname successfully created.\n" unless $quiet;
701 return 0;
704 sub git_config {
705 my $gd = shift;
706 system($Girocco::Config::git_bin, "--git-dir=$gd", 'config', @_) == 0
707 or die "\"git --git-dir='$gd' config ".join(" ", @_)."\" failed.\n";
710 sub cmd_adopt {
711 my ($force, $type, $nousers, $dryrun, $noowner, $owner, $users, $verbose);
712 parse_options(force => \$force, ":type" => \$type, "no-users" => \$nousers, "dry-run" => \$dryrun,
713 "no-owner" => \$noowner,":owner" => \$owner, quiet => \$quiet, q =>\$quiet, verbose => \$verbose);
714 @ARGV or die "Please give project name on command line.\n";
715 my $projname = shift @ARGV;
716 (!$noowner || !defined($owner)) && (!$nousers || !@ARGV) or die_usage;
717 !defined($type) || $type eq "mirror" || $type eq "push" or die_usage;
718 defined($type) or $type = "";
719 my $projdir;
720 if ($dryrun && $projname =~ m,^/[^.\s/\\:], && is_git_dir(realpath($projname))) {
721 $projdir = realpath($projname);
722 $projname = $projdir;
723 $projname =~ s/\.git$//i;
724 $projname =~ s,/+$,,;
725 $projname =~ s,^.*/,,;
726 $projname ne "" or $projname = $projdir;
727 } else {
728 $projname =~ s/\.git$//i;
729 $projname ne "" or die "Invalid project name \"\".\n";
730 unless (Girocco::Project::does_exist($projname, 1)) {
731 Girocco::Project::valid_name($projname, 1, 1)
732 or die "Invalid project name \"$projname\".\n";
733 die "No such project to adopt: $projname\n";
735 defined(Girocco::Project->load($projname))
736 and die "Project already known (no need to adopt): $projname\n";
737 $projdir = $Girocco::Config::reporoot . "/" . $projname . ".git";
738 is_git_dir($projdir) or die "Not a git directory: \"$projdir\"\n";
740 my $config = read_config_file($projdir . "/config");
741 my %config = ();
742 %config = map {($$_[0], defined($$_[1])?$$_[1]:"true")} @$config if defined($config);
743 git_bool($config{"core.bare"}) or die "Not a bare git repository: \"$projdir\"\n";
744 defined(read_HEAD_symref($projdir)) or die "Project with non-symbolic HEAD ref: \"$projdir\"\n";
745 @ARGV and $users = [validate_users(join(" ", @ARGV), $force, 1)];
746 my $desc = "";
747 if (-e "$projdir/description") {
748 open my $fd, '<', "$projdir/description" or die "Cannot open \"$projdir/description\": $!\n";
750 local $/;
751 $desc = <$fd>;
753 close $fd;
754 defined $desc or $desc = "";
755 chomp $desc;
756 $desc = to_utf8($desc, 1);
757 is_default_desc($desc) and $desc = "";
758 if ($desc ne "" && !valid_desc($desc)) {
759 die "invalid 'description' file contents (use --force to accept): \"$desc\"\n"
760 unless $force;
761 warn "using invalid 'description' file contents with --force\n" unless $quiet;
763 $desc = clean_desc($desc);
764 if (length($desc) > 1024) {
765 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
766 unless $force;
767 warn "using longer than 1024 char description with --force\n" unless $quiet;
770 my $readme = "";
771 if (-e "$projdir/README.html") {
772 open my $fd, '<', "$projdir/README.html" or die "Cannot open \"$projdir/README.html\": $!\n";
774 local $/;
775 $readme = <$fd>;
777 close $fd;
778 defined $readme or $readme = "";
779 $readme = to_utf8($readme, 1);
780 $readme =~ s/\r\n?/\n/gs;
781 $readme =~ s/^\s+//s;
782 $readme =~ s/\s+$//s;
783 $readme eq "" or $readme .= "\n";
784 if (length($readme) > 8192) {
785 die "readme greater than 8192 chars is too long (use --force to override)\n"
786 unless $force;
787 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
789 my $rd = get_readme_desc($readme);
790 if ($rd ne "automatic" && $rd ne "suppressed") {
791 my $xmllint = qx(sh -c 'command -v xmllint'); chomp $xmllint;
792 if (-f $xmllint && -x $xmllint) {
793 my $dummy = {README => $readme};
794 my ($cnt, $err) = Girocco::Project::_lint_readme($dummy, 0);
795 if ($cnt) {
796 my $msg = "xmllint: $cnt error";
797 $msg .= "s" unless $cnt == 1;
798 print STDERR "$msg\n", "-" x length($msg), "\n", $err
799 unless $force && $quiet;
800 exit(255) unless $force;
801 warn "$projname: using invalid raw HTML with --force\n" unless $quiet;
803 } else {
804 die "xmllint not available, refusing to use raw HTML without --force\n"
805 unless $force;
806 warn "xmllint not available using unchecked raw HTML with --force\n" unless $quiet;
810 # Inspect any remotes now
811 # Yes, Virginia, remote urls can be multi-valued
812 my %remotes = ();
813 foreach (@$config) {
814 my ($k,$v) = @$_;
815 next unless $k =~ /^remote\.([^\/].*?)\.([^.]+)$/; # remote name cannot start with "/"
816 my ($name, $subkey) = ($1, $2);
817 $remotes{$name}->{skip} = git_bool($v,1), next if $subkey eq "skipdefaultupdate" || $subkey eq "skipfetchall";
818 $remotes{$name}->{mirror} = git_bool($v,1), next if $subkey eq "mirror"; # we might want this
819 $remotes{$name}->{vcs} = $v, next if defined($v) && $v !~ /^\s*$/ && $subkey eq "vcs";
820 push(@{$remotes{$name}->{$subkey}}, $v), next if defined($v) && $v !~ /^\s*$/ &&
821 ($subkey eq "url" || $subkey eq "fetch" || $subkey eq "push" || $subkey eq "pushurl");
823 # remotes.default is the default remote group to fetch for "git remote update" otherwise --all
824 # the remote names in a group are separated by runs of [ \t\n] characters
825 # remote names "", ".", ".." and any name starting with "/" are invalid
826 # a remote with no url or vcs setting is not considered valid
827 my @check = ();
828 my $usingall = 0;
829 if (exists($config{"remotes.default"})) {
830 foreach (split(/[ \t\n]+/, $config{"remotes.default"})) {
831 next unless exists($remotes{$_});
832 my $rmt = $remotes{$_};
833 next if !exists($rmt->{url}) && !$rmt->{vcs};
834 push(@check, $_);
836 } else {
837 $usingall = 1;
838 my %seenrmt = ();
839 foreach (@$config) {
840 my ($k,$v) = @$_;
841 next unless $k =~ /^remote\.([^\/].*?)\.[^.]+$/;
842 next if $seenrmt{$1};
843 $seenrmt{$1} = 1;
844 next unless exists($remotes{$1});
845 my $rmt = $remotes{$1};
846 next if $rmt->{skip} || (!exists($rmt->{url}) && !$rmt->{vcs});
847 push(@check, $1);
850 my @needskip = (); # remotes that need skipDefaultUpdate set to true
851 my $foundvcs = 0;
852 my $foundfetch = 0;
853 my $foundfetchwithmirror = 0;
854 foreach (@check) {
855 my $rmt = $remotes{$_};
856 push(@needskip, $_) if $usingall && !exists($rmt->{fetch});
857 next unless exists($rmt->{fetch});
858 ++$foundfetch;
859 ++$foundfetchwithmirror if $rmt->{mirror};
860 ++$foundvcs if $rmt->{vcs} || (exists($rmt->{url}) && $rmt->{url}->[0] =~ /^[a-zA-Z0-9][a-zA-Z0-9+.-]*::/);
862 # if we have $foundvcs then we need to explicitly set fetch.prune to false
863 # if we have $foundfetch > 1 then we need to explicitly set fetch.prune to false
864 my $neednoprune = !exists($config{"fetch.prune"}) && ($foundvcs || $foundfetch > 1);
865 my $baseurl = "";
866 my $needfakeorigin = 0; # if true we need to set remote.origin.skipDefaultUpdate = true
867 # if remote "origin" exists we always pick up its first url or use ""
868 if (exists($remotes{origin})) {
869 my $rmt = $remotes{origin};
870 $baseurl = exists($rmt->{url}) ? $rmt->{url}->[0] : "";
871 $needfakeorigin = !exists($rmt->{url}) && !$rmt->{vcs} && !$rmt->{skip};
872 } else {
873 $needfakeorigin = 1;
874 # get the first url of the @check remotes
875 foreach (@check) {
876 my $rmt = $remotes{$_};
877 next unless exists($rmt->{url});
878 next unless defined($rmt->{url}->[0]) && $rmt->{url}->[0] ne "";
879 $baseurl = $rmt->{url}->[0];
880 last;
883 my $makemirror = $type eq "mirror" || ($type eq "" && $foundfetch);
885 # If we have $foundfetch we want to make a mirror but complain if
886 # we $foundfetchwithmirror as well unless we have --type=mirror.
887 # Warn if we have --type=push and $foundfetch and !$foundfetchwithmirror.
888 # Warn if we need to set fetch.prune=false when making a mirror
889 # Warn if we need to create remote.origin.skipDefaultUpdate when making a mirror
890 # Complain if @needskip AND !$usingall (warn with --force but don't set skip)
891 # Warn if $usingall and any @needskip (and set them) if making a mirror
892 # Warn if making a mirror and $baseurl eq ""
893 # Warn if we have --type=mirror and !$foundfetch
895 if ($makemirror) {
896 warn "No base URL to mirror from for adopted \"$projname\"\n" unless $quiet || $baseurl ne "";
897 warn "Adopting mirror \"$projname\" without any fetch remotes\n" unless $quiet || $foundfetch;
898 if ($foundfetchwithmirror) {
899 warn "Refusing to adopt mirror \"$projname\" with active remote.<name>.mirror=true remote(s)\n".
900 "(Use --type=mirror to override)\n"
901 unless $type eq "mirror";
902 exit(255) unless $type eq "mirror" || $dryrun;
903 warn "Adopting mirror \"$projname\" with active remote.<name>.mirror=true remotes\n"
904 unless $quiet || $type ne "mirror";
906 warn "Setting explicit fetch.prune=false for adoption of mirror \"$projname\"\n"
907 if !$quiet && $neednoprune;
908 warn "Setting remote.origin.skipDefaultUpdate=true for adoption of mirror \"$projname\"\n"
909 if !$quiet && $needfakeorigin;
910 if (!$usingall && @needskip) {
911 warn "Refusing to adopt mirror empty fetch remote(s) (override with --force)\n"
912 unless $force;
913 exit(255) unless $force || $dryrun;
914 warn "Adopting mirror with empty fetch remote(s) with --force\n"
915 unless $quiet || !$force;
917 warn "Will set skipDefaultUpdate=true on non-fetch remote(s)\n" if !$quiet && $usingall && @needskip;
918 warn "Adopting mirror with base URL \"$baseurl\"\n" unless $quiet || $baseurl eq "";
919 } else {
920 warn "Adopting push \"$projname\" but active non-mirror remotes are present\n"
921 if !$quiet && $foundfetch && !$foundfetchwithmirror;
924 if (!$noowner && !defined($owner)) {
925 # Select the owner
926 $owner = $config{"gitweb.owner"};
927 if (!defined($owner) || $owner eq "") {
928 $owner = $Girocco::Config::admin;
929 warn "Using owner \"$owner\" for adopted project\n" unless $quiet;
932 if (!$nousers && !$makemirror && !defined($users)) {
933 # select user list for push project
934 my $findowner = $owner;
935 defined($findowner) or $findowner = $config{"gitweb.owner"};
936 $findowner = lc($findowner) if defined($findowner);
937 my @owner_users = ();
938 @owner_users = map {$findowner eq lc($$_[4]) ? $$_[1] : ()} get_all_users
939 if defined($findowner) && $findowner ne "";
940 defined($findowner) or $findowner = "";
941 if (@owner_users <= 1) {
942 $users = \@owner_users;
943 warn "No users found that match owner \"$findowner\"\n" unless @owner_users || $quiet;
944 } else {
945 $users = [];
946 warn "Found ".scalar(@owner_users)." users for owner \"$findowner\" (" .
947 join(" ", @owner_users) . ") not setting any\n" unless $quiet;
950 defined($users) or $users = [];
952 # Warn if we preserve an existing receive.denyNonFastForwards or receive.denyDeleteCurrent setting
953 # Complain if core.logallrefupdates or logs subdir exists and contains any files (allow with --force
954 # and warn about preserving the setting)
956 warn "Preserving existing receive.denyNonFastForwards=true\n"
957 if !$quiet && git_bool($config{"receive.denynonfastforwards"});
958 warn "Preserving existing receive.denyDeleteCurrent=$config{'receive.denydeletecurrent'}\n"
959 if !$quiet && exists($config{"receive.denydeletecurrent"}) &&
960 $config{"receive.denydeletecurrent"} ne "warn";
962 my $reflogfiles = Girocco::Project::_contains_files("$projdir/logs");
963 my $reflogactive = git_bool($config{"core.logallrefupdates"});
964 if ($reflogactive || $reflogfiles) {
965 warn "Refusing to adopt \"$projname\" with active ref logs without --force\n" if $reflogfiles && !$force;
966 warn "Refusing to adopt \"$projname\" with core.logAllRefUpdates=true without --force\n" if $reflogactive && !$force;
967 exit(255) unless $force || $dryrun;
968 warn "Adopting \"$projname\" with active ref logs with --force\n" unless $quiet || ($reflogfiles && !$force);
969 warn "Adopting \"$projname\" with core.logAllRefUpdates=true with --force\n" unless $quiet || ($reflogactive && !$force);
972 return 0 if $dryrun && !$verbose;
974 my $newproj = eval {Girocco::Project->ghost($projname, $makemirror, 1, $dryrun)};
975 defined($newproj) or die "Girocco::Project::ghost failed: $@\n";
976 $newproj->{desc} = $desc;
977 $newproj->{README} = $readme;
978 $newproj->{url} = $baseurl if $makemirror || exists($config{"gitweb.baseurl"});
979 $newproj->{email} = $owner if defined($owner);
980 $newproj->{users} = $users;
981 $newproj->{crypt} = "unknown";
982 $newproj->{reverseorder} = 1 unless exists($config{"hooks.reverseorder"});
983 $newproj->{summaryonly} = 1 unless exists($config{"hooks.summaryonly"});
984 my $dummy = bless {}, "Girocco::Project";
985 $dummy->{path} = "$projdir";
986 $dummy->{configfilehash} = \%config;
987 $dummy->_properties_load;
988 delete $dummy->{origurl};
989 foreach my $k (keys(%$dummy)) {
990 $newproj->{$k} = $dummy->{$k}
991 if exists($dummy->{$k}) && !exists($newproj->{$k});
994 if ($verbose) {
995 use Data::Dumper;
996 my %info = %$newproj;
997 $info{README} = get_readme_desc($info{README}) if exists($info{README});
998 my $d = Data::Dumper->new([\%info], ['*'.$newproj->{name}]);
999 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
1000 print $d->Dump([\%info], ['*'.$newproj->{name}]);
1002 return 0 if $dryrun;
1004 # Make any changes as needed for @needskip, $neednoprune and $needfakeorigin
1005 if ($makemirror) {
1006 git_config($projdir, "fetch.prune", "false") if $neednoprune;
1007 git_config($projdir, "remote.origin.skipDefaultUpdate", "true") if $needfakeorigin;
1008 if ($usingall && @needskip) {
1009 git_config($projdir, "remote.$_.skipDefaultUpdate", "true") foreach @needskip;
1013 # Perform the actual adoption
1014 $newproj->adopt or die "Girocco::Project::adopt failed\n";
1016 # Perhaps restore core.logAllRefUpdates, receive.denyNonFastForwards and receive.denyDeleteCurrent
1017 git_config($projdir, "receive.denyNonFastForwards", "true")
1018 if git_bool($config{"receive.denynonfastforwards"});
1019 git_config($projdir, "receive.denyDeleteCurrent", $config{"receive.denydeletecurrent"})
1020 if exists($config{"receive.denydeletecurrent"}) &&
1021 $config{"receive.denydeletecurrent"} ne "warn";
1022 git_config($projdir, "core.logAllRefUpdates", "true")
1023 if $reflogactive;
1025 # Success
1026 if ($makemirror) {
1027 warn "Mirror project \"$projname\" successfully adopted.\n" unless $quiet;
1028 } else {
1029 warn "Push project \"$projname\" successfully adopted.\n" unless $quiet;
1031 return 0;
1034 sub cmd_remove {
1035 my ($force, $reallydel, $keepforks);
1036 parse_options(force => \$force, "really-delete" => \$reallydel,
1037 "keep-forks" => \$keepforks, quiet => \$quiet, q =>\$quiet);
1038 @ARGV or die "Please give project name on command line.\n";
1039 @ARGV == 1 or die_usage;
1040 my $project = get_project($ARGV[0]);
1041 my $projname = $project->{name};
1042 my $isempty = !$project->{mirror} && $project->is_empty;
1043 if (!$project->{mirror} && !$isempty && $reallydel) {
1044 die "refusing to remove and delete non-empty push project without --force: $projname\n" unless $force;
1045 warn "allowing removal and deletion of non-empty push project with --force\n" unless $quiet;
1047 my $altwarn;
1048 my $removenogc;
1049 if ($project->has_forks) {
1050 die "refusing to remove project with forks (use --keep-forks): $projname\n" unless $keepforks;
1051 warn "allowing removal of forked project while preserving its forks with --keep-forks\n" unless $quiet;
1052 # Run pseudo GC on that repository so that objects don't get lost within forks
1053 my $basedir = $Girocco::Config::basedir;
1054 my $projdir = $project->{path};
1055 warn "We have to run pseudo GC on the repo so that the forks don't lose data. Hang on...\n" unless $quiet;
1056 my $nogcrunning = sub {
1057 die "Error: GC appears to be currently running on $projname\n"
1058 if -e "$projdir/gc.pid" || -e "$projdir/.gc_in_progress";
1060 &$nogcrunning;
1061 $removenogc = ! -e "$projdir/.nogc";
1062 recreate_file("$projdir/.nogc") if $removenogc;
1063 die "unable to create \"$projdir/.nogc\"\n" unless -e "$projdir/.nogc";
1064 delete $ENV{show_progress};
1065 $ENV{'show_progress'} = 1 unless $quiet;
1066 sleep 2; # *cough*
1067 &$nogcrunning;
1068 system("$basedir/toolbox/perform-pre-gc-linking.sh", "--include-packs", $projname) == 0
1069 or die "Running pseudo GC on project $projname failed\n";
1070 $altwarn = 1;
1072 my $archived;
1073 if (!$project->{mirror} && !$isempty && !$reallydel) {
1074 $archived = $project->archive_and_delete;
1075 unlink("$archived/.nogc") if $removenogc && defined($archived) && $archived ne "";
1076 } else {
1077 $project->delete;
1079 warn "Project '$projname' removed from $Girocco::Config::name" .
1080 ($archived ? ", backup in '$archived'" : "") .".\n" unless $quiet;
1081 warn "Retained forks may now have unwanted objects/info/alternates lines\n" if $altwarn && !$quiet;
1082 return 0;
1085 sub cmd_show {
1086 use Data::Dumper;
1087 @ARGV == 1 or die_usage;
1088 my $project = get_clean_project($ARGV[0]);
1089 my %info = %$project;
1090 my $d = Data::Dumper->new([\%info], ['*'.$project->{name}]);
1091 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
1092 print $d->Dump([\%info], ['*'.$project->{name}]);
1093 return 0;
1096 sub cmd_listheads {
1097 @ARGV == 1 or die_usage;
1098 my $project = get_project($ARGV[0]);
1099 my @heads = sort({lc($a) cmp lc($b)} $project->get_heads);
1100 my $cur = $project->{HEAD};
1101 defined($cur) or $cur = '';
1102 my $curmark = '*';
1103 my $headhash = get_git("--git-dir=$project->{path}", 'rev-parse', '--quiet', '--verify', 'HEAD');
1104 defined($headhash) or $headhash = '';
1105 chomp $headhash;
1106 $headhash or $curmark = '!';
1107 foreach (@heads) {
1108 my $mark = $_ eq $cur ? $curmark : ' ';
1109 print "$mark $_\n";
1111 return 0;
1114 sub cmd_listtags {
1115 my $vcnt = 0;
1116 shift(@ARGV), $vcnt=1 if @ARGV && ($ARGV[0] eq '--verbose' || $ARGV[0] eq '-v');
1117 @ARGV == 1 or die_usage;
1118 my $project = get_project($ARGV[0]);
1119 if ($vcnt) {
1120 print map("$$_[0]\t$$_[1]\n", get_ctag_counts($project));
1121 } else {
1122 print map("$_\n", $project->get_ctag_names);
1124 return 0;
1127 sub cmd_deltags {
1128 my $ic = 0;
1129 shift(@ARGV), $ic=1 if @ARGV && $ARGV[0] =~ /^(?:--?ignore-case|-i)$/i;
1130 @ARGV >= 2 or die_usage;
1131 my $project = get_project(shift @ARGV);
1132 my %curtags;
1133 if ($ic) {
1134 push(@{$curtags{lc($_)}}, $_) foreach $project->get_ctag_names;
1135 } else {
1136 push(@{$curtags{$_}}, $_) foreach $project->get_ctag_names;
1138 my @deltags = ();
1139 my %seentag = ();
1140 my $ctags = join(" ", @ARGV);
1141 $ctags = lc($ctags) if $ic;
1142 foreach (split(/[\s,]+/, $ctags)) {
1143 next unless exists($curtags{$_});
1144 $seentag{$_}=1, push(@deltags, $_) unless $seentag{$_};
1146 if (!@deltags) {
1147 warn $project->{name}, ": skipping removal of only non-existent tags\n" unless $quiet;
1148 } else {
1149 # Avoid touching anything other than the ctags
1150 foreach my $tg (@deltags) {
1151 $project->delete_ctag($_) foreach @{$curtags{$tg}};
1153 $project->_set_changed;
1154 $project->_set_forkchange;
1155 warn $project->{name}, ": specified tags have been removed\n" unless $quiet;
1157 return 0;
1160 sub cmd_addtags {
1161 @ARGV >= 2 or die_usage;
1162 my $project = get_project(shift @ARGV);
1163 my $ctags = join(" ", @ARGV);
1164 $ctags =~ /[^, a-zA-Z0-9:.+#_-]/ and
1165 die "Content tag(s) \"$ctags\" contain(s) evil character(s).\n";
1166 my $oldmask = umask();
1167 umask($oldmask & ~0060);
1168 my $changed = 0;
1169 foreach (split(/[\s,]+/, $ctags)) {
1170 ++$changed if $project->add_ctag($_, 1);
1172 if ($changed) {
1173 $project->_set_changed;
1174 $project->_set_forkchange;
1176 umask($oldmask);
1177 my $cnt = ($changed == 1) ? "1 content tag has" : $changed . " content tags have";
1178 warn $project->{name}, ": $cnt been added/updated\n" unless $quiet;
1179 return 0;
1182 sub _get_random_val {
1183 my $p = shift;
1184 my $md5;
1186 no warnings;
1187 $md5 = md5_hex(time . $$ . rand() . join(':',%$p));
1189 $md5;
1192 sub cmd_chpass {
1193 my $force = 0;
1194 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1195 my $random = undef;
1196 pop(@ARGV), $random=lc($ARGV[1]) if @ARGV==2 && $ARGV[1] =~ /^(?:random|unknown)$/i;
1197 @ARGV == 1 or die_usage;
1198 my $project = get_project($ARGV[0]);
1199 die "refusing to change locked password of project \"$ARGV[0]\" without --force\n"
1200 if $project->is_password_locked;
1201 my ($newpw, $rmsg);
1202 if ($random) {
1203 if ($random eq "random") {
1204 die "refusing to set random password without --force\n" unless $force;
1205 $rmsg = "set to random value";
1206 $newpw = _get_random_val($project);
1207 } else {
1208 die "refusing to set password hash to '$random' without --force\n" unless $force;
1209 $rmsg = "hash set to '$random'";
1210 $newpw = $random;
1212 } else {
1213 $rmsg = "updated";
1214 if (-t STDIN) {
1215 print "Changing admin password for project $ARGV[0]\n";
1216 my $np1 = prompt_noecho_nl_or_die("New password for project $ARGV[0] (echo is off)");
1217 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1218 my $np2 = prompt_noecho_nl_or_die("Retype new password for project $ARGV[0]");
1219 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
1220 "the admin passwords you have entered do not match each other.\n";
1221 $newpw = $np1;
1222 } else {
1223 $newpw = <STDIN>;
1224 defined($newpw) or die "missing new password on STDIN\n";
1225 chomp($newpw);
1228 $newpw ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1229 my $old = $project->{crypt};
1230 $project->{crypt} = (defined($random) && $random ne "random") ? $newpw : scrypt_sha1($newpw);
1231 if (defined($old) && $old eq $project->{crypt}) {
1232 warn $project->{name}, ": skipping update of password hash to same value\n" unless $quiet;
1233 } else {
1234 # Avoid touching anything other than the password hash
1235 $project->_group_update;
1236 warn $project->{name}, ": admin password $rmsg (new hash stored)\n" unless $quiet;
1238 return 0;
1241 sub cmd_checkpw {
1242 @ARGV == 1 or die_usage;
1243 my $project = get_project($ARGV[0]);
1244 my $pwhash = $project->{crypt};
1245 defined($pwhash) or $pwhash = "";
1246 if ($pwhash eq "") {
1247 warn $project->{name}, ": no password required\n" unless $quiet;
1248 return 0;
1250 if ($project->is_password_locked) {
1251 warn $project->{name}, ": password is locked\n" unless $quiet;
1252 exit 1;
1254 my $checkpw;
1255 if (-t STDIN) {
1256 $checkpw = prompt_noecho_nl_or_die("Admin password for project $ARGV[0] (echo is off)");
1257 $checkpw ne "" or warn "checking for empty password as hash (very unlikely)\n" unless $quiet;
1258 } else {
1259 $checkpw = <STDIN>;
1260 defined($checkpw) or die "missing admin password on STDIN\n";
1261 chomp($checkpw);
1263 unless (Girocco::CLIUtil::check_passwd_match($pwhash, $checkpw)) {
1264 warn "password check failure\n" unless $quiet;
1265 exit 1;
1267 warn "admin password match\n" unless $quiet;
1268 return 0;
1271 sub cmd_gc {
1272 my ($force, $auto, $redelta, $recompress);
1273 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, auto => \$auto,
1274 redelta => \$redelta, "no-reuse-delta" => \$redelta, aggressive => \$force,
1275 recompress => \$recompress, "no-reuse-object" => $recompress);
1276 $force && $auto and die "--force and --auto are mutually exclusive options\n";
1277 @ARGV or die "Please give project name on command line.\n";
1278 @ARGV == 1 or die_usage;
1279 my $project = get_project($ARGV[0]);
1280 delete $ENV{show_progress};
1281 delete $ENV{force_gc};
1282 $quiet or $ENV{"show_progress"} = 1;
1283 $force and $ENV{"force_gc"} = 1;
1284 if (!$auto && !$force && ! -e $project->{path}."/.needsgc") {
1285 open NEEDSGC, '>', $project->{path}."/.needsgc" and close NEEDSGC;
1287 my @args = ($Girocco::Config::basedir . "/jobd/gc.sh", $project->{name});
1288 $redelta && !$recompress and push(@args, "-f");
1289 $recompress and push(@args, "-F");
1290 my $lastgc = $project->{lastgc};
1291 system({$args[0]} @args) != 0 and return 1;
1292 # Do it again Sam, but only if lastgc was set, gc.sh succeeded and now it's not set
1293 if ($lastgc) {
1294 my $newlastgc = get_git("--git-dir=$project->{path}", 'config', '--get', 'gitweb.lastgc');
1295 if (!$newlastgc) {
1296 system({$args[0]} @args) != 0 and return 1;
1299 return 0;
1302 sub cmd_update {
1303 my ($force, $summary);
1304 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, summary => \$summary);
1305 $quiet && $summary and die "--quiet and --summary are mutually exclusive options\n";
1306 @ARGV or die "Please give project name on command line.\n";
1307 @ARGV == 1 or die_usage;
1308 my $project = get_project($ARGV[0]);
1309 $project->{mirror} or die "Project \"$ARGV[0]\" is a push project, not a mirror project.\n";
1310 delete $ENV{show_progress};
1311 delete $ENV{force_update};
1312 if ($quiet) {
1313 $ENV{"show_progress"} = 0;
1314 } else {
1315 $ENV{"show_progress"} = ($summary ? 1 : 2);
1317 $force and $ENV{"force_update"} = 1;
1318 system($Girocco::Config::basedir . "/jobd/update.sh", $project->{name}) != 0 and return 1;
1319 return 0;
1322 sub cmd_remirror {
1323 my $force = 0;
1324 parse_options(force => \$force, quiet => \$quiet, q => \$quiet);
1325 @ARGV or die "Please give project name on command line.\n";
1326 @ARGV == 1 or die_usage;
1327 my $project = get_project($ARGV[0]);
1328 $project->{mirror} or die "Project \"$ARGV[0]\" is a push project, not a mirror project.\n";
1329 if ($project->{clone_in_progress} && !$project->{clone_failed}) {
1330 warn "Project \"$ARGV[0]\" already seems to have a clone underway at this moment.\n" unless $quiet && $force;
1331 exit(255) unless $force;
1332 yes_to_continue_or_die("Are you sure you want to force a remirror");
1334 unlink($project->_clonefail_path);
1335 unlink($project->_clonelog_path);
1336 recreate_file($project->_clonep_path);
1337 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or
1338 die "cannot connect to taskd.socket: $!\n";
1339 select((select($sock),$|=1)[0]);
1340 $sock->print("clone ".$project->{name}."\n");
1341 # Just ignore reply, we are going to succeed anyway and the I/O
1342 # would apparently get quite hairy.
1343 $sock->flush();
1344 sleep 2; # *cough*
1345 $sock->close();
1346 warn "Project \"$ARGV[0]\" remirror initiated.\n" unless $quiet;
1347 return 0;
1350 sub cmd_setowner {
1351 my $force = 0;
1352 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1353 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1354 my $project = get_project($ARGV[0]);
1355 if (@ARGV == 2 && !valid_email($ARGV[1])) {
1356 die "invalid owner/email (use --force to accept): \"$ARGV[1]\"\n"
1357 unless $force;
1358 warn "using invalid owner/email with --force\n" unless $quiet;
1360 if (@ARGV == 2 && length($ARGV[1]) > 96) {
1361 die "owner/email longer than 96 chars (use --force to accept): \"$ARGV[1]\"\n"
1362 unless $force;
1363 warn "using longer than 96 char owner/email with --force\n" unless $quiet;
1365 my $old = $project->{email};
1366 if (@ARGV == 1) {
1367 print "$old\n" if defined($old);
1368 return 0;
1370 if (defined($old) && $old eq $ARGV[1]) {
1371 warn $project->{name}, ": skipping update of owner/email to same value\n" unless $quiet;
1372 } else {
1373 # Avoid touching anything other than "gitweb.owner"
1374 $project->_property_fput("email", $ARGV[1]);
1375 $project->_update_index;
1376 $project->_set_changed;
1377 warn $project->{name}, ": owner/email updated to \"$ARGV[1]\"\n" unless $quiet;
1379 return 0;
1382 sub cmd_setdesc {
1383 my $force = 0;
1384 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1385 @ARGV >= 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1386 my $project = get_project(shift @ARGV);
1387 if (@ARGV && !valid_desc(join(" ", @ARGV))) {
1388 die "invalid description (use --force to accept): \"".join(" ", @ARGV)."\"\n"
1389 unless $force;
1390 warn "using invalid description with --force\n" unless $quiet;
1392 my $desc = clean_desc(join(" ", @ARGV));
1393 if (@ARGV && length($desc) > 1024) {
1394 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
1395 unless $force;
1396 warn "using longer than 1024 char description with --force\n" unless $quiet;
1398 my $old = $project->{desc};
1399 if (!@ARGV) {
1400 print "$old\n" if defined($old);
1401 return 0;
1403 if (defined($old) && $old eq $desc) {
1404 warn $project->{name}, ": skipping update of description to same value\n" unless $quiet;
1405 } else {
1406 # Avoid touching anything other than description file
1407 $project->_property_fput("desc", $desc);
1408 $project->_set_changed;
1409 warn $project->{name}, ": description updated to \"$desc\"\n" unless $quiet;
1411 return 0;
1414 sub cmd_setreadme {
1415 my $force = 0;
1416 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1417 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1418 my $project = get_project($ARGV[0]);
1419 my $old = $project->{README};
1420 if (@ARGV == 1) {
1421 chomp $old if defined($old);
1422 print "$old\n" if defined($old) && $old ne "";
1423 return 0;
1425 my ($new, $raw, $newname);
1426 $newname = '';
1427 if ($ARGV[1] eq "-") {
1428 local $/;
1429 $new = <STDIN>;
1430 $raw = 1;
1431 $newname = "contents of <STDIN>";
1432 } elsif (lc($ARGV[1]) eq "automatic" || lc($ARGV[1]) eq "auto") {
1433 $new = "";
1434 } elsif (lc($ARGV[1]) eq "suppressed" || lc($ARGV[1]) eq "suppress") {
1435 $new = "<!-- suppress -->";
1436 } else {
1437 my $fn = $ARGV[1];
1438 $fn =~ s/^\@//;
1439 die "missing filename for README\n" unless $fn ne "";
1440 die "no such file: \"$fn\"\n" unless -f $fn && -r $fn;
1441 open F, '<', $fn or die "cannot open \"$fn\" for reading: $!\n";
1442 local $/;
1443 $new = <F>;
1444 close F;
1445 $raw = 1;
1446 $newname = "contents of \"$fn\"";
1448 defined($new) or $new = '';
1449 $project->{README} = to_utf8($new, 1);
1450 $project->_cleanup_readme;
1451 if (length($project->{README}) > 8192) {
1452 die "readme greater than 8192 chars is too long (use --force to override)\n"
1453 unless $force;
1454 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
1456 if ($raw) {
1457 my $rd = get_readme_desc($project->{README});
1458 if ($rd ne "automatic" && $rd ne "suppressed") {
1459 my $xmllint = qx(command -v xmllint); chomp $xmllint;
1460 if (-f $xmllint && -x $xmllint) {
1461 my ($cnt, $err) = $project->_lint_readme(0);
1462 if ($cnt) {
1463 my $msg = "xmllint: $cnt error";
1464 $msg .= "s" unless $cnt == 1;
1465 print STDERR "$msg\n", "-" x length($msg), "\n", $err
1466 unless $force && $quiet;
1467 exit(255) unless $force;
1468 warn $project->{name} . ": using invalid raw HTML with --force\n" unless $quiet;
1470 } else {
1471 die "xmllint not available, refusing to use raw HTML without --force\n"
1472 unless $force;
1473 warn "xmllint not available using unchecked raw HTML with --force\n" unless $quiet;
1477 if (defined($old) && $old eq $project->{README}) {
1478 warn $project->{name}, ": skipping update of README to same value\n" unless $quiet;
1479 } else {
1480 # Avoid touching anything other than README.html file
1481 $project->_property_fput("README", $project->{README});
1482 $project->_set_changed;
1483 my $desc = get_readme_desc($project->{README});
1484 if ($newname) {
1485 $newname .= " ($desc)";
1486 } else {
1487 $newname = $desc;
1489 warn $project->{name}, ": README updated to $newname\n" unless $quiet;
1491 return 0;
1494 sub valid_head {
1495 my ($proj, $newhead) = @_;
1496 my %okheads = map({($_ => 1)} $proj->get_heads);
1497 exists($okheads{$newhead});
1500 sub cmd_sethead {
1501 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1502 my $project = get_project($ARGV[0]);
1503 if (@ARGV == 2 && !valid_head($project, $ARGV[1])) {
1504 die "invalid head (try \"@{[basename($0)]} listheads $ARGV[0]\"): \"$ARGV[1]\"\n";
1506 my $old = $project->{HEAD};
1507 if (@ARGV == 1) {
1508 print "$old\n" if defined($old);
1509 return 0;
1511 if (defined($old) && $old eq $ARGV[1]) {
1512 warn $project->{name}, ": skipping update of HEAD symref to same value\n" unless $quiet;
1513 } else {
1514 # Avoid touching anything other than the HEAD symref
1515 $project->set_HEAD($ARGV[1]);
1516 $project->_set_changed;
1517 warn $project->{name}, ": HEAD symref updated to \"refs/heads/$ARGV[1]\"\n" unless $quiet;
1519 return 0;
1522 sub cmd_sethooks {
1523 my $force = 0;
1524 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1525 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1526 my $project = get_project($ARGV[0]);
1527 my $projconfig = read_config_file_hash($project->{path}."/config");
1528 my $ghp = $Girocco::Config::reporoot."/_global/hooks";
1529 my $rghp = realpath($ghp);
1530 my $lhp = $project->{path}."/hooks";
1531 my $rlhp = realpath($lhp);
1532 my $ahp = "";
1533 my $rahp = undef;
1534 if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) {
1535 $ahp = $projconfig->{"core.hookspath"};
1536 $rahp = realpath($ahp);
1538 if (@ARGV == 1) {
1539 if (defined($rahp) && $rahp ne "") {
1540 if ($rahp eq $rghp) {
1541 my $nc = ($ahp eq $ghp ? "" : " non-canonical");
1542 printf "%s \t(global%s)\n", $ahp, $nc;
1543 } elsif ($rahp eq $rlhp) {
1544 my $nc = ($ahp eq $lhp ? "" : " non-canonical");
1545 printf "%s \t(local%s)\n", $ahp, $nc;
1546 } elsif ($rahp ne $ahp) {
1547 print "$ahp \t($rahp)\n";
1548 } else {
1549 print "$ahp\n";
1551 } elsif ($ahp ne "") {
1552 print "$ahp \t(non-existent)\n";
1554 return 0;
1556 my $shp = $ARGV[1];
1557 if (lc($shp) eq "global") {
1558 $shp = $ghp;
1559 } elsif (lc($shp) eq "local") {
1560 $shp = $lhp;
1561 } elsif (substr($shp, 0, 2) eq "~/") {
1562 $shp = $ENV{"HOME"}.substr($shp,1);
1563 } elsif ($shp =~ m,^~([a-zA-Z_][a-zA-Z_0-9]*)((?:/.*)?)$,) {
1564 my $sfx = $2;
1565 my $hd = (getpwnam($1))[7];
1566 $shp = $hd . $sfx if defined($hd) && $hd ne "" && $hd ne "/" && -d $hd;
1568 $shp ne "" && -d $shp or die "no such directory: $ARGV[1]\n";
1569 my $rshp = realpath($shp);
1570 defined($rshp) && $rshp ne "" or die "could not realpath: $ARGV[1]\n";
1571 $rshp =~ m,^/[^/], or die "invalid hookspath: $rshp\n";
1572 die "refusing to switch from current non-global hookspath without --force\n"
1573 if !$force && defined($rahp) && $rahp ne "" && $rahp ne $rghp && $rshp ne $rahp;
1574 if (!$force && defined($rahp) && $rahp ne "") {
1575 if ($rshp eq $rahp && ($ahp eq $ghp || $ahp eq $lhp)) {
1576 warn $project->{name}, ": skipping update of hookspath to same effective value\n" unless $quiet;
1577 return 0;
1580 $rshp = $ghp if $rshp eq $rghp;
1581 $rshp = $lhp if $rshp eq $rlhp;
1582 if ($rshp eq $ahp) {
1583 warn $project->{name}, ": skipping update of hookspath to same value\n" unless $quiet;
1584 return 0;
1586 die "refusing to set neither local nor global hookspath without --force\n"
1587 if !$force && $rshp ne $ghp && $rshp ne $lhp;
1588 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1589 'config', "core.hookspath", $rshp);
1590 my $newval = '"'.$rshp.'"';
1591 $newval = "global" if $rshp eq $ghp;
1592 $newval = "local" if $rshp eq $lhp;
1593 warn $project->{name}, ": hookspath set to $newval\n" unless $quiet;
1594 return 0;
1597 our %boolfields;
1598 BEGIN {
1599 %boolfields = (
1600 cleanmirror => 1,
1601 reverseorder => 0,
1602 summaryonly => 0,
1603 statusupdates => 1,
1607 sub cmd_setbool {
1608 my $force = 0;
1609 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1610 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1611 my $project = get_project($ARGV[0]);
1612 if (!exists($boolfields{$ARGV[1]})) {
1613 die "invalid boolean field name: \"$ARGV[1]\" -- try \"help\"\n";
1615 if (@ARGV == 3 && $boolfields{$ARGV[1]} && !$project->{mirror}) {
1616 die "invalid boolean field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
1617 unless $force;
1618 warn "using mirror field on non-mirror with --force\n" unless $quiet;
1620 if (@ARGV == 3 && !valid_bool($ARGV[2])) {
1621 die "invalid boolean value: \"$ARGV[2]\"\n";
1623 my $bool = clean_bool($ARGV[2]);
1624 my $old = $project->{$ARGV[1]};
1625 if (@ARGV == 2) {
1626 print "$old\n" if defined($old);
1627 return 0;
1629 if (defined($old) && $old eq $bool) {
1630 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
1631 } else {
1632 # Avoid touching anything other than $ARGV[1] field
1633 $project->_property_fput($ARGV[1], $bool);
1634 warn $project->{name}, ": $ARGV[1] updated to $bool\n" unless $quiet;
1636 return 0;
1639 sub cmd_setjsontype {
1640 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1641 my $project = get_project($ARGV[0]);
1642 my $jsontype;
1643 if (@ARGV == 2) {
1644 my $jt = lc($ARGV[1]);
1645 index($jt, "/") >= 0 or $jt = "application/".$jt;
1646 $jt eq 'application/x-www-form-urlencoded' ||
1647 $jt eq 'application/json' or
1648 die "invalid jsontype value: \"$ARGV[1]\"\n";
1649 $jsontype = $jt;
1651 my $old = $project->{jsontype};
1652 if (@ARGV == 1) {
1653 print "$old\n" if defined($old);
1654 return 0;
1656 if (defined($old) && $old eq $jsontype) {
1657 warn $project->{name}, ": skipping update of jsontype to same value\n" unless $quiet;
1658 } else {
1659 # Avoid touching anything other than jsontype field
1660 $project->_property_fput('jsontype', $jsontype);
1661 warn $project->{name}, ": jsontype updated to $jsontype\n" unless $quiet;
1663 return 0;
1666 sub cmd_setjsonsecret {
1667 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1668 my $project = get_project($ARGV[0]);
1669 my $jsonsecret;
1670 if (@ARGV == 2) {
1671 my $js = $ARGV[1];
1672 $js =~ s/^\s+//; $js =~ s/\s+$//;
1673 $jsonsecret = $js;
1675 my $old = $project->{jsonsecret};
1676 if (@ARGV == 1) {
1677 print "$old\n" if defined($old);
1678 return 0;
1680 if (defined($old) && $old eq $jsonsecret) {
1681 warn $project->{name}, ": skipping update of jsonsecret to same value\n" unless $quiet;
1682 } else {
1683 # Avoid touching anything other than jsonsecret field
1684 $project->_property_fput('jsonsecret', $jsonsecret);
1685 warn $project->{name}, ": jsonsecret updated to \"$jsonsecret\"\n" unless $quiet;
1687 return 0;
1690 sub cmd_setautogchack {
1691 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1692 my $project = get_project($ARGV[0]);
1693 my $aghok = $Girocco::Config::autogchack &&
1694 ($project->{mirror} || $Girocco::Config::autogchack ne "mirror");
1695 my $old = defined($project->{autogchack}) ? clean_bool($project->{autogchack}) : "unset";
1696 if (@ARGV == 1) {
1697 print "$old\n" if $aghok;
1698 return 0;
1700 my $bool;
1701 if (lc($ARGV[1]) eq "unset") {
1702 $bool = "unset";
1703 } else {
1704 valid_bool($ARGV[1]) or die "invalid boolean value: \"$ARGV[1]\"\n";
1705 $bool = clean_bool($ARGV[1]);
1707 if (!$aghok) {
1708 die "\$Girocco::Config::autogchack is false\n" unless $Girocco::Config::autogchack;
1709 die "\$Girocco::Config::autogchack is only enabled for mirrors\n";
1711 if ($old eq $bool) {
1712 warn $project->{name}, ": autogchack value unchanged\n" unless $quiet;
1713 } else {
1714 if ($bool eq "unset") {
1715 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1716 'config', '--unset', "girocco.autogchack");
1717 } else {
1718 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1719 'config', '--bool', "girocco.autogchack", $bool);
1722 return system($Girocco::Config::basedir . "/jobd/maintain-auto-gc-hack.sh", $project->{name}) == 0
1723 ? 0 : 1;
1726 sub valid_url {
1727 my ($url, $type) = @_;
1728 $type ne 'baseurl' and return valid_web_url($url);
1729 valid_repo_url($url) or return 0;
1730 if ($Girocco::Config::restrict_mirror_hosts) {
1731 my $mh = extract_url_hostname($url);
1732 is_dns_hostname($mh) or return 0;
1733 !is_our_hostname($mh) or return 0;
1735 return 1;
1738 our %urlfields;
1739 BEGIN {
1740 %urlfields = (
1741 baseurl => ["url" , 1],
1742 homepage => ["hp" , 0],
1743 notifyjson => ["notifyjson", 0],
1747 sub cmd_seturl {
1748 my $force = 0;
1749 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1750 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1751 my $project = get_project($ARGV[0]);
1752 if (!exists($urlfields{$ARGV[1]})) {
1753 die "invalid URL field name: \"$ARGV[1]\" -- try \"help\"\n";
1755 if (@ARGV == 3 && ${$urlfields{$ARGV[1]}}[1] && !$project->{mirror}) {
1756 die "invalid URL field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
1757 unless $force;
1758 warn "using mirror field on non-mirror with --force\n" unless $quiet;
1760 if (@ARGV == 3 && !valid_url($ARGV[2], $ARGV[1])) {
1761 die "invalid URL (use --force to accept): \"$ARGV[2]\"\n"
1762 unless $force;
1763 warn "using invalid URL with --force\n" unless $quiet;
1765 my $old = $project->{${$urlfields{$ARGV[1]}}[0]};
1766 if (@ARGV == 2) {
1767 print "$old\n" if defined($old);
1768 return 0;
1770 if (defined($old) && $old eq $ARGV[2]) {
1771 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
1772 } else {
1773 # Avoid touching anything other than $ARGV[1]'s field
1774 $project->_property_fput(${$urlfields{$ARGV[1]}}[0], $ARGV[2]);
1775 if ($ARGV[1] eq "baseurl") {
1776 $project->{url} = $ARGV[2];
1777 $project->_set_bangagain;
1779 $project->_set_changed unless $ARGV[1] eq "notifyjson";
1780 warn $project->{name}, ": $ARGV[1] updated to $ARGV[2]\n" unless $quiet;
1782 return 0;
1785 our %msgsfields;
1786 BEGIN {
1787 %msgsfields = (
1788 notifymail => 1,
1789 notifytag => 1,
1793 sub cmd_setmsgs {
1794 my $force = 0;
1795 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1796 @ARGV >= 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1797 my $project = get_project(shift @ARGV);
1798 my $field = shift @ARGV;
1799 if (!exists($msgsfields{$field})) {
1800 die "invalid msgs field name: \"$field\" -- try \"help\"\n";
1802 if (@ARGV && !valid_addrlist(@ARGV)) {
1803 die "invalid email address list (use --force to accept): \"".join(" ",@ARGV)."\"\n"
1804 unless $force;
1805 warn "using invalid email address list with --force\n" unless $quiet;
1807 my $old = $project->{$field};
1808 if (!@ARGV) {
1809 printf "%s\n", clean_addrlist($old, " ") if defined($old);
1810 return 0;
1812 my $newlist = clean_addrlist(join(" ",@ARGV));
1813 if (defined($old) && $old eq $newlist) {
1814 warn $project->{name}, ": skipping update of $field to same value\n" unless $quiet;
1815 } else {
1816 # Avoid touching anything other than $field's field
1817 $project->_property_fput($field, $newlist);
1818 warn $project->{name}, ": $field updated to \"$newlist\"\n" unless $quiet;
1820 return 0;
1823 sub cmd_setusers {
1824 my $force = 0;
1825 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1826 @ARGV >= 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1827 my $project = get_project(shift @ARGV);
1828 my $projname = $project->{name};
1829 !@ARGV || !$project->{mirror} or die "cannot set users list for mirror project: \"$projname\"\n";
1830 my @newusers = ();
1831 if (@ARGV) {
1832 eval {@newusers = validate_users(join(" ", @ARGV), $force); 1;} or exit 255;
1833 die "refusing to set empty users list without --force\n" unless @newusers || $force;
1835 return 0 if !@ARGV && $project->{mirror};
1836 my $oldusers = $project->{users};
1837 if ($oldusers && ref($oldusers) eq "ARRAY") {
1838 $oldusers = join("\n", @$oldusers);
1839 } else {
1840 $oldusers = "";
1842 if (!@ARGV) {
1843 print "$oldusers\n" if $oldusers ne "";
1844 return 0;
1846 if ($oldusers eq join("\n", @newusers)) {
1847 warn "$projname: skipping update of users list to same value\n" unless $quiet;
1848 } else {
1849 # Avoid touching anything other than the users list
1850 $project->{users} = \@newusers;
1851 $project->_update_users;
1852 warn "$projname: users list updated to \"@{[join(',',@newusers)]}\"\n" unless $quiet;
1854 return 0;
1857 our %fieldnames;
1858 BEGIN {
1859 %fieldnames = (
1860 owner => [\&cmd_setowner, 0],
1861 desc => [\&cmd_setdesc, 0],
1862 description => [\&cmd_setdesc, 0],
1863 readme => [\&cmd_setreadme, 0],
1864 head => [\&cmd_sethead, 0],
1865 HEAD => [\&cmd_sethead, 0],
1866 hooks => [\&cmd_sethooks, 0],
1867 hookspath => [\&cmd_sethooks, 0],
1868 cleanmirror => [\&cmd_setbool, 1],
1869 reverseorder => [\&cmd_setbool, 1],
1870 summaryonly => [\&cmd_setbool, 1],
1871 statusupdates => [\&cmd_setbool, 1],
1872 autogchack => [\&cmd_setautogchack, 0],
1873 baseurl => [\&cmd_seturl, 1],
1874 homepage => [\&cmd_seturl, 1],
1875 notifyjson => [\&cmd_seturl, 1],
1876 jsontype => [\&cmd_setjsontype, 0],
1877 jsonsecret => [\&cmd_setjsonsecret, 0],
1878 notifymail => [\&cmd_setmsgs, 1],
1879 notifytag => [\&cmd_setmsgs, 1],
1880 users => [\&cmd_setusers, 0],
1884 sub do_getset {
1885 $setopt = shift;
1886 my @newargs = ();
1887 push(@newargs, shift) if @_ && $_[0] eq '--force';
1888 my $field = $_[1];
1889 (($setopt && @_ >= 3) || @_ == 2) && exists($fieldnames{$field}) or die_usage;
1890 push(@newargs, shift);
1891 shift unless ${$fieldnames{$field}}[1];
1892 push(@newargs, @_);
1893 diename(($setopt ? "set " : "get ") . $field);
1894 @ARGV = @newargs;
1895 &{${$fieldnames{$field}}[0]}(@ARGV);
1898 sub cmd_get {
1899 do_getset(0, @_);
1902 sub cmd_set {
1903 do_getset(1, @_);
1906 our %commands;
1907 BEGIN {
1908 %commands = (
1909 list => \&cmd_list,
1910 create => \&cmd_create,
1911 adopt => \&cmd_adopt,
1912 remove => \&cmd_remove,
1913 trash => \&cmd_remove,
1914 delete => \&cmd_remove,
1915 show => \&cmd_show,
1916 listheads => \&cmd_listheads,
1917 listtags => \&cmd_listtags,
1918 listctags => \&cmd_listtags,
1919 deltags => \&cmd_deltags,
1920 delctags => \&cmd_deltags,
1921 addtags => \&cmd_addtags,
1922 addctags => \&cmd_addtags,
1923 chpass => \&cmd_chpass,
1924 checkpw => \&cmd_checkpw,
1925 gc => \&cmd_gc,
1926 update => \&cmd_update,
1927 remirror => \&cmd_remirror,
1928 setowner => \&cmd_setowner,
1929 setdesc => \&cmd_setdesc,
1930 setdescription => \&cmd_setdesc,
1931 setreadme => \&cmd_setreadme,
1932 sethead => \&cmd_sethead,
1933 sethooks => \&cmd_sethooks,
1934 sethookspath => \&cmd_sethooks,
1935 setbool => \&cmd_setbool,
1936 setboolean => \&cmd_setbool,
1937 setflag => \&cmd_setbool,
1938 setautogchack => \&cmd_setautogchack,
1939 seturl => \&cmd_seturl,
1940 setjsontype => \&cmd_setjsontype,
1941 setjsonsecret => \&cmd_setjsonsecret,
1942 setmsgs => \&cmd_setmsgs,
1943 setusers => \&cmd_setusers,
1944 get => \&cmd_get,
1945 set => \&cmd_set,
1948 our %nopager;
1949 BEGIN {
1950 %nopager = map({$_ => 1} qw(
1951 create
1952 chpass
1953 checkpw
1957 sub dohelp {
1958 my $cmd = shift;
1959 my $bn = basename($0);
1960 setup_pager_stdout($usepager);
1961 printf "%s version %s\n\n", $bn, $VERSION;
1962 if (defined($cmd) && $cmd ne '') {
1963 $cmd =~ s/^set(?=[a-zA-Z])//i;
1964 my $cmdhelp = '';
1965 my ($lastmt, $incmd);
1966 foreach (split('\n', sprintf($help, $bn))) {
1967 $lastmt || $incmd or $lastmt = /^\s*$/, next;
1968 $incmd = 1 if $lastmt && /^\s*(?:\[?set\]?)?$cmd\s/;
1969 last if $incmd && /^\s*$/;
1970 $incmd and $cmdhelp .= $_ . "\n";
1971 $lastmt = /^\s*$/;
1973 print $cmdhelp and exit 0 if $cmdhelp;
1975 printf $help, $bn;
1976 exit 0;
1979 sub main {
1980 local *ARGV = \@_;
1982 shift, $quiet=1, redo if @ARGV && $ARGV[0] =~ /^(?:-q|--quiet)$/i;
1983 shift, $usepager=1, redo if @ARGV && $ARGV[0] =~ /^(?:-p|--pager|--paginate)$/i;
1984 shift, $usepager=0, redo if @ARGV && $ARGV[0] =~ /^(?:--no-pager|--no-paginate)$/i;
1986 dohelp($ARGV[1]) if !@ARGV || @ARGV && $ARGV[0] =~ /^(?:-h|-?-help|help)$/i;
1987 my $command = shift;
1988 diename($command);
1989 $setopt = 1;
1990 if (!exists($commands{$command}) && exists($commands{"set".$command})) {
1991 $setopt = 0;
1992 $command = "set" . $command;
1994 exists($commands{$command}) or die "Unknown command \"$command\" -- try \"help\"\n";
1995 dohelp($command) if @ARGV && ($ARGV[0] =~ /^(?:-h|-?-help)$/i ||
1996 $ARGV[0] =~ /^help$/i && !Girocco::Project::does_exist("help",1));
1997 $nopager{$command} and $usepager = 0;
1998 setup_pager_stdout($usepager);
1999 &{$commands{$command}}(@ARGV);