Girocco/Project.pm: add _normalize_rmtype function
[girocco.git] / toolbox / projtool.pl
blob06c720a7a8eef57f54dd3188fbe525e08ee7e740
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_len {
222 my $rm = shift;
223 defined($rm) or $rm = '';
224 return "length " . length($rm);
227 sub get_readme_desc {
228 my $rm = shift;
229 defined($rm) or $rm = '';
230 if (length($rm)) {
231 my $test = $rm;
232 $test =~ s/<!--(?:[^-]|(?:-(?!-)))*-->//gs;
233 $test =~ s/\s+//s;
234 return $test eq '' ? "suppressed" : "length " . length($rm);
235 } else {
236 return "automatic";
240 sub get_ctag_counts {
241 my $project = shift;
242 my $compact = shift;
243 my @ctags = ();
244 foreach ($project->get_ctag_names) {
245 my $val = 0;
246 my $ct;
247 if (open $ct, '<', $project->{path}."/ctags/$_") {
248 my $count = <$ct>;
249 close $ct;
250 defined $count or $count = '';
251 chomp $count;
252 $val = $count =~ /^[1-9]\d*$/ ? $count : 1;
254 if ($compact) {
255 if ($val == 1) {
256 push(@ctags, $_);
257 } elsif ($val > 1) {
258 push(@ctags, $_."(".$val.")");
260 } else {
261 push(@ctags, [$_, $val]) if $val;
264 @ctags;
267 sub get_clean_project {
268 my $project = get_project(@_);
269 delete $project->{loaded};
270 delete $project->{base_path};
271 delete $project->{ccrypt};
272 /^orig/i || !defined($project->{$_}) and delete $project->{$_} foreach keys %$project;
273 $project->{owner} = $project->{email}; delete $project->{email};
274 $project->{homepage} = $project->{hp}; delete $project->{hp};
275 $project->{baseurl} = $project->{url}; delete $project->{url};
276 if (defined($project->{path}) && $project->{path} ne "") {
277 my $rp = realpath($project->{path});
278 defined($rp) && $rp ne "" and $project->{realpath} = $rp;
279 if (-f "$rp/objects/info/packs") {
280 my $ipt = (stat _)[9];
281 defined($ipt) and $project->{infopackstime} =
282 strftime("%Y-%m-%d %H:%M:%S %z", localtime($ipt));
285 my $owner = $project->{owner};
286 if ($owner) {
287 $owner = lc($owner);
288 my @owner_users = map {$owner eq lc($$_[4]) ? $$_[1] : ()} get_all_users;
289 $project->{owner_users} = \@owner_users if @owner_users;
291 my $projname = $project->{name};
292 my @forks = grep {$$_[1] =~ m,^$projname/,} get_all_projects;
293 $project->{has_forks} = 1 if @forks;
294 $project->{has_alternates} = 1 if $project->has_alternates;
295 my @bundles = $project->bundles;
296 for (my $i = 0; $i < @bundles; ++$i) {
297 my $secs = $bundles[$i]->[0];
298 $bundles[$i]->[0] = strftime("%Y-%m-%d %H:%M:%S %z", localtime($secs));
299 my $sz = $bundles[$i]->[2];
300 1 while $sz =~ s/(?<=\d)(\d{3})(?:,|$)/,$1/g;
301 $bundles[$i]->[2] = $sz;
303 delete $project->{bundles};
304 $project->{bundles} = \@bundles if @bundles;
305 $project->{mirror} = 0 unless $project->{mirror};
306 $project->{is_empty} = 1 if $project->is_empty;
307 delete $project->{showpush} unless $project->{showpush};
308 delete $project->{users} if $project->{mirror};
309 delete $project->{baseurl} unless $project->{mirror};
310 delete $project->{banged} unless $project->{mirror};
311 delete $project->{lastrefresh} unless $project->{mirror};
312 delete $project->{cleanmirror} unless $project->{mirror};
313 delete $project->{statusupdates} unless $project->{mirror};
314 delete $project->{lastparentgc} unless $projname =~ m,/,;
315 unless ($project->{banged}) {
316 delete $project->{bangcount};
317 delete $project->{bangfirstfail};
318 delete $project->{bangmessagesent};
320 my $projhook = $project->_has_notifyhook;
321 if (defined($projhook) && $projhook ne "") {
322 $project->{notifyhook} = $projhook;
323 } else {
324 delete $project->{notifyhook};
326 $project->{README} = get_readme_desc($project->{README}) if exists($project->{README});
327 $project->{READMEDATA} = get_readme_len($project->{READMEDATA}) if exists($project->{READMEDATA});
328 my @tags = get_ctag_counts($project, 1);
329 $project->{tags} = \@tags if @tags;
330 my $projconfig = read_config_file_hash($project->{path}."/config");
331 if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) {
332 my $ahp = $projconfig->{"core.hookspath"};
333 my $rahp = realpath($ahp);
334 my $lhp = $project->{path}."/hooks";
335 my $rlhp = realpath($lhp);
336 my $ghp = $Girocco::Config::reporoot."/_global/hooks";
337 my $rghp = realpath($ghp);
338 $project->{has_local_hooks} = 1 if
339 defined($rahp) && defined($rlhp) && $rahp eq $rlhp;
340 $project->{has_global_hooks} = 1 if
341 defined($rahp) && defined($rghp) && $rahp eq $rghp;
342 $project->{hookspath} = $ahp unless $ahp eq $lhp || $ahp eq $ghp;
344 $project;
347 sub clean_addrlist {
348 my %seen = ();
349 my @newlist = ();
350 foreach (split(/[,\s]+/, $_[0])) {
351 next unless $_;
352 $seen{lc($_)} = 1, push(@newlist, $_) unless $seen{lc($_)};
354 return join(($_[1]||","), @newlist);
357 sub valid_addrlist {
358 my $cleaned = clean_addrlist(join(" ", @_));
359 return 1 if $cleaned eq "";
360 valid_email_multi($cleaned) && length($cleaned) <= 512;
363 sub validate_users {
364 my ($userlist, $force, $nodie) = @_;
365 my @newusers = ();
366 my $badlist = 0;
367 my %seenuser = ();
368 my $mobok = $Girocco::Config::mob && $Girocco::Config::mob eq "mob";
369 my %users = map({($$_[1] => $_)} get_all_users);
370 foreach (split(/[\s,]+/, $userlist)) {
371 if (exists($users{$_}) || $_ eq "everyone" || ($mobok && $_ eq "mob")) {
372 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
373 next;
375 if (Girocco::User::does_exist($_, 1)) {
376 if ($force) {
377 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
378 } else {
379 $badlist = 1;
380 warn "refusing to allow questionable user \"$_\" without --force\n" unless $nodie && $quiet;
382 next;
384 $badlist = 1;
385 warn "invalid user: \"$_\"\n" unless $nodie && $quiet
387 die if $badlist && !$nodie;
388 return @newusers;
391 sub is_default_desc {
392 # "Unnamed repository; edit this file 'description' to name the repository."
393 # "Unnamed repository; edit this file to name it for gitweb."
394 local $_ = shift;
395 return 0 unless defined($_);
396 /Unnamed\s+repository;/i && /\s+edit\s+this\s+file\s+/i && /\s+to\s+name\s+/i;
399 sub valid_desc {
400 my $test = shift;
401 chomp $test;
402 return 0 if $test =~ /[\r\n]/;
403 $test =~ s/\s\s+/ /g;
404 $test =~ s/^\s+//;
405 $test =~ s/\s+$//;
406 return $test ne '';
409 sub clean_desc {
410 my $desc = shift;
411 defined($desc) or $desc = '';
412 chomp $desc;
413 $desc = to_utf8($desc, 1);
414 $desc =~ s/\s\s+/ /g;
415 $desc =~ s/^\s+//;
416 $desc =~ s/\s+$//;
417 return $desc;
420 sub parse_options {
421 Girocco::CLIUtil::_parse_options(
422 sub {
423 warn((($_[0]eq'?')?"unrecognized":"missing argument for")." option \"$_[1]\"\n")
424 unless $quiet;
425 die_usage;
426 }, @_);
429 sub cmd_list {
430 my %sortsub = (
431 lcname => sub {lc($$a[1]) cmp lc($$b[1])},
432 name => sub {$$a[1] cmp $$b[1]},
433 gid => sub {$$a[3] <=> $$b[3]},
434 owner => sub {lc($$a[4]) cmp lc($$b[4]) || lc($$a[1]) cmp lc($$b[1])},
435 no => sub {$$a[0] <=> $$b[0]},
437 my $sortopt = 'lcname';
438 my ($verbose, $owner);
439 parse_options(":sort" => \$sortopt, verbose => \$verbose, owner => \$owner);
440 my $regex;
441 if (@ARGV) {
442 my $val = shift @ARGV;
443 $regex = qr($val) or die "bad regex \"$val\"\n";
445 !@ARGV && exists($sortsub{$sortopt}) or die_usage;
446 my $sortsub = $sortsub{$sortopt};
447 my $grepsub = defined($regex) ? ($owner ? sub {$$_[4] =~ /$regex/} : sub {$$_[1] =~ /$regex/}) : sub {1};
448 my @projects = sort($sortsub grep {&$grepsub} get_all_projects);
449 if ($verbose) {
450 print map(sprintf("%s\n", join(":", (@$_)[1..5])), @projects);
451 } else {
452 print map(sprintf("%s: %s\n", $$_[1], $$_[5] =~ /^:/ ? "<mirror>" : $$_[5]), @projects);
454 return 0;
457 sub cmd_create {
458 my ($force, $noalternates, $orphanok, $optp, $nopasswd, $noowner, $defaults, $ispush, $pushusers,
459 $ismirror, $desc, $fullmirror, $homepage);
460 parse_options(
461 force => \$force, "no-alternates" => \$noalternates, orphan => \$orphanok, p => \$optp,
462 "no-password" => \$nopasswd, "no-owner" => \$noowner, defaults => \$defaults,
463 "push" => \$ispush, ":push" => \$pushusers, ":mirror" => \$ismirror, ":desc" => \$desc,
464 ":description" => \$desc, "full-mirror" => \$fullmirror, ":homepage" => \$homepage);
465 @ARGV == 1 or die_usage;
466 !defined($pushusers) || defined($ispush) or $ispush = 1;
467 defined($ismirror) && $ismirror =~ /^\s*$/ and die "--mirror url must not be empty\n";
468 die "--mirror and --push are mutually exclusive options\n" if $ismirror && $ispush;
469 die "--full-mirror requires use of --mirror=<url> option\n" if $fullmirror && !$ismirror;
470 !$defaults || defined($ispush) || defined($ismirror) or $ispush = 1;
471 !$defaults || defined($nopasswd) or $nopasswd = 1;
472 !$defaults || defined($noowner) or $noowner = 1;
473 !defined($ispush) || defined($pushusers) or $pushusers = "";
474 my $projname = $ARGV[0];
475 $projname =~ s/\.git$//i;
476 Girocco::Project::does_exist($projname, 1) and die "Project already exists: \"$projname\"\n";
477 if (!Girocco::Project::valid_name($projname, $orphanok, $optp)) {
478 warn "Refusing to create orphan project without --orphan\n"
479 if !$quiet && !$orphanok && Girocco::Project::valid_name($projname, 1, 1);
480 warn "Required orphan parent directory does not exist (use -p): ",
481 $Girocco::Config::reporoot.'/'.Girocco::Project::get_forkee_name($projname), "\n"
482 if !$quiet && $orphanok && Girocco::Project::valid_name($projname, 1, 1);
483 die "Invalid project name: \"$projname\"\n";
485 my ($forkee, $project) = ($projname =~ m#^(.*/)?([^/]+)$#);
486 my $newtype = $forkee ? 'fork' : 'project';
487 if (length($project) > 64) {
488 die "The $newtype name is longer than 64 characters. Do you really need that much?\n"
489 unless $force;
490 warn "Allowing $newtype name longer than 64 characters with --force\n" unless $quiet;
492 unless ($Girocco::Config::push || $Girocco::Config::mirror) {
493 die "Project creation disabled (no mirrors or push projects allowed)\n" unless $force;
494 warn "Continuing with --force even though both push and mirror projects are disabled\n" unless $quiet;
496 print "Enter settings for new project \"$projname\"\n" unless $defaults;
497 my %settings = ();
498 $settings{noalternates} = $noalternates;
499 if ($nopasswd) {
500 $settings{crypt} = "unknown";
501 } else {
502 my $np1 = prompt_noecho_nl_or_die("Admin password for project $projname (echo is off)");
503 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
504 my $np2 = prompt_noecho_nl_or_die("Retype admin password for project $projname");
505 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
506 "the admin passwords you have entered do not match each other.\n";
507 $settings{crypt} = scrypt_sha1($np1);
509 my $owner = "";
510 unless ($noowner) {
511 $owner = prompt_or_die("Owner/email name for project $projname");
512 unless (valid_email($owner)) {
513 unless ($force) {
514 warn "Your email sure looks weird...?\n";
515 redo;
517 warn "Allowing invalid email with --force\n" unless $quiet;
519 if (length($owner) > 96) {
520 unless ($force) {
521 warn "Your email is longer than 96 characters. Do you really need that much?\n";
522 redo;
524 warn "Allowing email longer than 96 characters with --force\n" unless $quiet;
527 $settings{email} = $owner;
528 my $baseurl = "";
529 my $checkmirror = sub {
530 my $checkurl = shift;
531 unless (valid_repo_url($checkurl)) {
532 unless ($force) {
533 warn "Invalid mirror URL: \"$checkurl\"\n";
534 return undef;
536 warn "Allowing invalid mirror URL with --force\n" unless $quiet;
538 if ($Girocco::Config::restrict_mirror_hosts) {
539 my $mh = extract_url_hostname($checkurl);
540 unless (is_dns_hostname($mh)) {
541 unless ($force) {
542 warn "Invalid non-DNS mirror URL: \"$checkurl\"\n";
543 return undef;
545 warn "Allowing non-DNS mirror URL with --force\n" unless $quiet;
547 if (is_our_hostname($mh)) {
548 unless ($force) {
549 warn "Invalid same-host mirror URL: \"$checkurl\"\n";
550 return undef;
552 warn "Allowing same-host mirror URL with --force\n" unless $quiet;
555 return $checkurl;
557 if ($ispush || $ismirror) {
558 !$ispush || $force || $Girocco::Config::push or
559 die "Push projects are disabled, create a mirror (or use --force)\n";
560 !$ismirror || $force || $Girocco::Config::mirror or
561 die "Mirror projects are disabled, create a push project (or use --force)\n";
562 if ($ismirror) {
563 &$checkmirror($ismirror) or die "Invalid --mirror URL\n";
564 $baseurl = $ismirror;
565 $settings{url} = $baseurl;
566 $settings{cleanmirror} = $fullmirror ? 0 : 1;
567 } else {
568 my @newusers = ();
569 if ($pushusers !~ /^[\s,]*$/) {
570 eval {@newusers = validate_users($pushusers, $force); 1;} or
571 die "Invalid --push user list\n";
573 $settings{users} = \@newusers;
575 } elsif ($force || $Girocco::Config::mirror) {{
576 if ($force || $Girocco::Config::push) {
577 $baseurl = prompt_or_die("URL to mirror from (leave blank for push project)", "");
578 } else {{
579 $baseurl = prompt_or_die("URL to mirror from");
580 unless ($baseurl ne "") {
581 warn "Push projects are disabled, you must enter a mirror URL (or use --force)\n";
582 redo;
585 if ($baseurl ne "") {
586 &$checkmirror($baseurl) or redo;
587 $settings{url} = $baseurl;
588 $settings{cleanmirror} =
589 ynprompt_or_die("Mirror only heads, tags and notes (Y/n)", "Yes");
592 my $mirror = ($baseurl eq "") ? 0 : 1;
593 my $checkdesc = sub {
594 my $d = shift;
595 if (length($d) > 1024) {
596 unless ($force) {
597 warn "Short description length greater than 1024 characters!\n";
598 return undef;
600 warn "Allowing short description length greater than 1024 characters\n" unless $quiet;
602 return $d;
604 if (defined($desc)) {
605 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
606 $desc eq "" || &$checkdesc($desc) or
607 die "Invalid --desc description\n";
608 } elsif (!$defaults) {
609 $desc = prompt_or_die("Short description", "");
610 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
611 $desc eq "" || &$checkdesc($desc) or redo;
612 $desc = undef if $desc eq "";
614 defined($desc) or $desc = $mirror ? "Mirror of $baseurl" : "Push project $projname";
615 $settings{desc} = $desc;
616 my $checkhp = sub {
617 my $hpurl = shift;
618 unless (valid_web_url($hpurl)) {
619 unless ($force) {
620 warn "Invalid home page URL: \"$hpurl\"\n";
621 return undef;
623 warn "Allowing invalid home page URL with --force\n" unless $quiet;
625 return $hpurl;
627 if (defined($homepage)) {
628 $homepage =~ s/^\s+//; $homepage =~ s/\s+$//;
629 $homepage eq "" || &$checkhp($homepage) or
630 die "Invalid --homepage URL\n";
631 } elsif (!$defaults) {
632 $homepage = prompt_or_die("Home page URL", "");
633 $homepage =~ s/^\s+//; $homepage =~ s/\s+$//;
634 $homepage eq "" || &$checkhp($homepage) or redo;
635 $homepage = undef if $homepage eq "";
637 $settings{hp} = $homepage;
638 my $jsonurl = "";
639 if (!$defaults) {
640 $jsonurl = prompt_or_die("JSON notify POST URL", "");
641 if ($jsonurl ne "" && !valid_web_url($jsonurl)) {
642 unless ($force) {
643 warn "Invalid JSON notify POST URL: \$jsonurl\"\n";
644 redo;
646 warn "Allowing invalid JSON notify POST URL with --force\n" unless $quiet;
649 $settings{notifyjson} = $jsonurl;
650 my $commitaddrs = "";
651 if (!$defaults) {
652 $commitaddrs = clean_addrlist(prompt_or_die("Commit notify email addr(s)", ""));
653 if ($commitaddrs ne "" && !valid_addrlist($commitaddrs)) {
654 unless ($force) {
655 warn"invalid commit notify email address list (use --force to accept): \"$commitaddrs\"\n";
656 redo;
658 warn "using invalid commit notify email address list with --force\n" unless $quiet;
661 $settings{notifymail} = $commitaddrs;
662 $settings{reverseorder} = 1;
663 $settings{reverseorder} = ynprompt_or_die("Oldest-to-newest commit order in emails", "Yes")
664 if !$defaults && $commitaddrs ne "";
665 $settings{summaryonly} = ynprompt_or_die("Summary only (no diff) in emails", "No")
666 if !$defaults && $commitaddrs ne "";
667 my $tagaddrs = "";
668 if (!$defaults) {
669 $tagaddrs = clean_addrlist(prompt_or_die("Tag notify email addr(s)", ""));
670 if ($tagaddrs ne "" && !valid_addrlist($tagaddrs)) {
671 unless ($force) {
672 warn"invalid tag notify email address list (use --force to accept): \"$tagaddrs\"\n";
673 redo;
675 warn "using invalid tag notify email address list with --force\n" unless $quiet;
678 $settings{notifytag} = $tagaddrs;
679 if (!$mirror && !$ispush) {
680 my @newusers = ();
682 my $userlist = prompt_or_die("Push users", join(",", @newusers));
683 eval {@newusers = validate_users($userlist, $force); 1;} or redo;
685 $settings{users} = \@newusers;
687 my $newproj = Girocco::Project->ghost($projname, $mirror, $orphanok, $optp)
688 or die "Girocco::Project->ghost call failed\n";
689 my ($k, $v);
690 $newproj->{$k} = $v while ($k, $v) = each(%settings);
691 my $killowner = sub {
692 system($Girocco::Config::git_bin, '--git-dir='.$newproj->{path},
693 'config', '--unset', "gitweb.owner");
695 if ($mirror) {
696 $newproj->premirror or die "Girocco::Project->premirror failed\n";
697 !$noowner or &$killowner;
698 $newproj->clone or die "Girocco::Project->clone failed\n";
699 warn "Project $projname created and cloning successfully initiated.\n"
700 unless $quiet;
701 } else {
702 $newproj->conjure or die "Girocco::Project->conjure failed\n";
703 !$noowner or &$killowner;
704 warn "New push project fork is empty due to use of --no-alternates\n"
705 if !$quiet && $projname =~ m,/, && $noalternates;
706 warn "Project $projname successfully created.\n" unless $quiet;
708 return 0;
711 sub git_config {
712 my $gd = shift;
713 system($Girocco::Config::git_bin, "--git-dir=$gd", 'config', @_) == 0
714 or die "\"git --git-dir='$gd' config ".join(" ", @_)."\" failed.\n";
717 sub cmd_adopt {
718 my ($force, $type, $nousers, $dryrun, $noowner, $owner, $users, $verbose);
719 parse_options(force => \$force, ":type" => \$type, "no-users" => \$nousers, "dry-run" => \$dryrun,
720 "no-owner" => \$noowner,":owner" => \$owner, quiet => \$quiet, q =>\$quiet, verbose => \$verbose);
721 @ARGV or die "Please give project name on command line.\n";
722 my $projname = shift @ARGV;
723 (!$noowner || !defined($owner)) && (!$nousers || !@ARGV) or die_usage;
724 !defined($type) || $type eq "mirror" || $type eq "push" or die_usage;
725 defined($type) or $type = "";
726 my $projdir;
727 if ($dryrun && $projname =~ m,^/[^.\s/\\:], && is_git_dir(realpath($projname))) {
728 $projdir = realpath($projname);
729 $projname = $projdir;
730 $projname =~ s/\.git$//i;
731 $projname =~ s,/+$,,;
732 $projname =~ s,^.*/,,;
733 $projname ne "" or $projname = $projdir;
734 } else {
735 $projname =~ s/\.git$//i;
736 $projname ne "" or die "Invalid project name \"\".\n";
737 unless (Girocco::Project::does_exist($projname, 1)) {
738 Girocco::Project::valid_name($projname, 1, 1)
739 or die "Invalid project name \"$projname\".\n";
740 die "No such project to adopt: $projname\n";
742 defined(Girocco::Project->load($projname))
743 and die "Project already known (no need to adopt): $projname\n";
744 $projdir = $Girocco::Config::reporoot . "/" . $projname . ".git";
745 is_git_dir($projdir) or die "Not a git directory: \"$projdir\"\n";
747 my $config = read_config_file($projdir . "/config");
748 my %config = ();
749 %config = map {($$_[0], defined($$_[1])?$$_[1]:"true")} @$config if defined($config);
750 git_bool($config{"core.bare"}) or die "Not a bare git repository: \"$projdir\"\n";
751 defined(read_HEAD_symref($projdir)) or die "Project with non-symbolic HEAD ref: \"$projdir\"\n";
752 @ARGV and $users = [validate_users(join(" ", @ARGV), $force, 1)];
753 my $desc = "";
754 if (-e "$projdir/description") {
755 open my $fd, '<', "$projdir/description" or die "Cannot open \"$projdir/description\": $!\n";
757 local $/;
758 $desc = <$fd>;
760 close $fd;
761 defined $desc or $desc = "";
762 chomp $desc;
763 $desc = to_utf8($desc, 1);
764 is_default_desc($desc) and $desc = "";
765 if ($desc ne "" && !valid_desc($desc)) {
766 die "invalid 'description' file contents (use --force to accept): \"$desc\"\n"
767 unless $force;
768 warn "using invalid 'description' file contents with --force\n" unless $quiet;
770 $desc = clean_desc($desc);
771 if (length($desc) > 1024) {
772 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
773 unless $force;
774 warn "using longer than 1024 char description with --force\n" unless $quiet;
777 my $readme = "";
778 if (-e "$projdir/README.html") {
779 open my $fd, '<', "$projdir/README.html" or die "Cannot open \"$projdir/README.html\": $!\n";
781 local $/;
782 $readme = <$fd>;
784 close $fd;
785 defined $readme or $readme = "";
786 $readme = to_utf8($readme, 1);
787 $readme =~ s/\r\n?/\n/gs;
788 $readme =~ s/^\s+//s;
789 $readme =~ s/\s+$//s;
790 $readme eq "" or $readme .= "\n";
791 if (length($readme) > 8192) {
792 die "readme greater than 8192 chars is too long (use --force to override)\n"
793 unless $force;
794 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
796 my $rd = get_readme_desc($readme);
797 if ($rd ne "automatic" && $rd ne "suppressed") {
798 my $xmllint = qx(sh -c 'command -v xmllint'); chomp $xmllint;
799 if (-f $xmllint && -x $xmllint) {
800 my $dummy = {README => $readme};
801 my ($cnt, $err) = Girocco::Project::_lint_readme($dummy, 0);
802 if ($cnt) {
803 my $msg = "xmllint: $cnt error";
804 $msg .= "s" unless $cnt == 1;
805 print STDERR "$msg\n", "-" x length($msg), "\n", $err
806 unless $force && $quiet;
807 exit(255) unless $force;
808 warn "$projname: using invalid raw HTML with --force\n" unless $quiet;
810 } else {
811 die "xmllint not available, refusing to use raw HTML without --force\n"
812 unless $force;
813 warn "xmllint not available using unchecked raw HTML with --force\n" unless $quiet;
817 # Inspect any remotes now
818 # Yes, Virginia, remote urls can be multi-valued
819 my %remotes = ();
820 foreach (@$config) {
821 my ($k,$v) = @$_;
822 next unless $k =~ /^remote\.([^\/].*?)\.([^.]+)$/; # remote name cannot start with "/"
823 my ($name, $subkey) = ($1, $2);
824 $remotes{$name}->{skip} = git_bool($v,1), next if $subkey eq "skipdefaultupdate" || $subkey eq "skipfetchall";
825 $remotes{$name}->{mirror} = git_bool($v,1), next if $subkey eq "mirror"; # we might want this
826 $remotes{$name}->{vcs} = $v, next if defined($v) && $v !~ /^\s*$/ && $subkey eq "vcs";
827 push(@{$remotes{$name}->{$subkey}}, $v), next if defined($v) && $v !~ /^\s*$/ &&
828 ($subkey eq "url" || $subkey eq "fetch" || $subkey eq "push" || $subkey eq "pushurl");
830 # remotes.default is the default remote group to fetch for "git remote update" otherwise --all
831 # the remote names in a group are separated by runs of [ \t\n] characters
832 # remote names "", ".", ".." and any name starting with "/" are invalid
833 # a remote with no url or vcs setting is not considered valid
834 my @check = ();
835 my $usingall = 0;
836 if (exists($config{"remotes.default"})) {
837 foreach (split(/[ \t\n]+/, $config{"remotes.default"})) {
838 next unless exists($remotes{$_});
839 my $rmt = $remotes{$_};
840 next if !exists($rmt->{url}) && !$rmt->{vcs};
841 push(@check, $_);
843 } else {
844 $usingall = 1;
845 my %seenrmt = ();
846 foreach (@$config) {
847 my ($k,$v) = @$_;
848 next unless $k =~ /^remote\.([^\/].*?)\.[^.]+$/;
849 next if $seenrmt{$1};
850 $seenrmt{$1} = 1;
851 next unless exists($remotes{$1});
852 my $rmt = $remotes{$1};
853 next if $rmt->{skip} || (!exists($rmt->{url}) && !$rmt->{vcs});
854 push(@check, $1);
857 my @needskip = (); # remotes that need skipDefaultUpdate set to true
858 my $foundvcs = 0;
859 my $foundfetch = 0;
860 my $foundfetchwithmirror = 0;
861 foreach (@check) {
862 my $rmt = $remotes{$_};
863 push(@needskip, $_) if $usingall && !exists($rmt->{fetch});
864 next unless exists($rmt->{fetch});
865 ++$foundfetch;
866 ++$foundfetchwithmirror if $rmt->{mirror};
867 ++$foundvcs if $rmt->{vcs} || (exists($rmt->{url}) && $rmt->{url}->[0] =~ /^[a-zA-Z0-9][a-zA-Z0-9+.-]*::/);
869 # if we have $foundvcs then we need to explicitly set fetch.prune to false
870 # if we have $foundfetch > 1 then we need to explicitly set fetch.prune to false
871 my $neednoprune = !exists($config{"fetch.prune"}) && ($foundvcs || $foundfetch > 1);
872 my $baseurl = "";
873 my $needfakeorigin = 0; # if true we need to set remote.origin.skipDefaultUpdate = true
874 # if remote "origin" exists we always pick up its first url or use ""
875 if (exists($remotes{origin})) {
876 my $rmt = $remotes{origin};
877 $baseurl = exists($rmt->{url}) ? $rmt->{url}->[0] : "";
878 $needfakeorigin = !exists($rmt->{url}) && !$rmt->{vcs} && !$rmt->{skip};
879 } else {
880 $needfakeorigin = 1;
881 # get the first url of the @check remotes
882 foreach (@check) {
883 my $rmt = $remotes{$_};
884 next unless exists($rmt->{url});
885 next unless defined($rmt->{url}->[0]) && $rmt->{url}->[0] ne "";
886 $baseurl = $rmt->{url}->[0];
887 last;
890 my $makemirror = $type eq "mirror" || ($type eq "" && $foundfetch);
892 # If we have $foundfetch we want to make a mirror but complain if
893 # we $foundfetchwithmirror as well unless we have --type=mirror.
894 # Warn if we have --type=push and $foundfetch and !$foundfetchwithmirror.
895 # Warn if we need to set fetch.prune=false when making a mirror
896 # Warn if we need to create remote.origin.skipDefaultUpdate when making a mirror
897 # Complain if @needskip AND !$usingall (warn with --force but don't set skip)
898 # Warn if $usingall and any @needskip (and set them) if making a mirror
899 # Warn if making a mirror and $baseurl eq ""
900 # Warn if we have --type=mirror and !$foundfetch
902 if ($makemirror) {
903 warn "No base URL to mirror from for adopted \"$projname\"\n" unless $quiet || $baseurl ne "";
904 warn "Adopting mirror \"$projname\" without any fetch remotes\n" unless $quiet || $foundfetch;
905 if ($foundfetchwithmirror) {
906 warn "Refusing to adopt mirror \"$projname\" with active remote.<name>.mirror=true remote(s)\n".
907 "(Use --type=mirror to override)\n"
908 unless $type eq "mirror";
909 exit(255) unless $type eq "mirror" || $dryrun;
910 warn "Adopting mirror \"$projname\" with active remote.<name>.mirror=true remotes\n"
911 unless $quiet || $type ne "mirror";
913 warn "Setting explicit fetch.prune=false for adoption of mirror \"$projname\"\n"
914 if !$quiet && $neednoprune;
915 warn "Setting remote.origin.skipDefaultUpdate=true for adoption of mirror \"$projname\"\n"
916 if !$quiet && $needfakeorigin;
917 if (!$usingall && @needskip) {
918 warn "Refusing to adopt mirror empty fetch remote(s) (override with --force)\n"
919 unless $force;
920 exit(255) unless $force || $dryrun;
921 warn "Adopting mirror with empty fetch remote(s) with --force\n"
922 unless $quiet || !$force;
924 warn "Will set skipDefaultUpdate=true on non-fetch remote(s)\n" if !$quiet && $usingall && @needskip;
925 warn "Adopting mirror with base URL \"$baseurl\"\n" unless $quiet || $baseurl eq "";
926 } else {
927 warn "Adopting push \"$projname\" but active non-mirror remotes are present\n"
928 if !$quiet && $foundfetch && !$foundfetchwithmirror;
931 if (!$noowner && !defined($owner)) {
932 # Select the owner
933 $owner = $config{"gitweb.owner"};
934 if (!defined($owner) || $owner eq "") {
935 $owner = $Girocco::Config::admin;
936 warn "Using owner \"$owner\" for adopted project\n" unless $quiet;
939 if (!$nousers && !$makemirror && !defined($users)) {
940 # select user list for push project
941 my $findowner = $owner;
942 defined($findowner) or $findowner = $config{"gitweb.owner"};
943 $findowner = lc($findowner) if defined($findowner);
944 my @owner_users = ();
945 @owner_users = map {$findowner eq lc($$_[4]) ? $$_[1] : ()} get_all_users
946 if defined($findowner) && $findowner ne "";
947 defined($findowner) or $findowner = "";
948 if (@owner_users <= 1) {
949 $users = \@owner_users;
950 warn "No users found that match owner \"$findowner\"\n" unless @owner_users || $quiet;
951 } else {
952 $users = [];
953 warn "Found ".scalar(@owner_users)." users for owner \"$findowner\" (" .
954 join(" ", @owner_users) . ") not setting any\n" unless $quiet;
957 defined($users) or $users = [];
959 # Warn if we preserve an existing receive.denyNonFastForwards or receive.denyDeleteCurrent setting
960 # Complain if core.logallrefupdates or logs subdir exists and contains any files (allow with --force
961 # and warn about preserving the setting)
963 warn "Preserving existing receive.denyNonFastForwards=true\n"
964 if !$quiet && git_bool($config{"receive.denynonfastforwards"});
965 warn "Preserving existing receive.denyDeleteCurrent=$config{'receive.denydeletecurrent'}\n"
966 if !$quiet && exists($config{"receive.denydeletecurrent"}) &&
967 $config{"receive.denydeletecurrent"} ne "warn";
969 my $reflogfiles = Girocco::Project::_contains_files("$projdir/logs");
970 my $reflogactive = git_bool($config{"core.logallrefupdates"});
971 if ($reflogactive || $reflogfiles) {
972 warn "Refusing to adopt \"$projname\" with active ref logs without --force\n" if $reflogfiles && !$force;
973 warn "Refusing to adopt \"$projname\" with core.logAllRefUpdates=true without --force\n" if $reflogactive && !$force;
974 exit(255) unless $force || $dryrun;
975 warn "Adopting \"$projname\" with active ref logs with --force\n" unless $quiet || ($reflogfiles && !$force);
976 warn "Adopting \"$projname\" with core.logAllRefUpdates=true with --force\n" unless $quiet || ($reflogactive && !$force);
979 return 0 if $dryrun && !$verbose;
981 my $newproj = eval {Girocco::Project->ghost($projname, $makemirror, 1, $dryrun)};
982 defined($newproj) or die "Girocco::Project::ghost failed: $@\n";
983 $newproj->{desc} = $desc;
984 $newproj->{README} = $readme;
985 $newproj->{url} = $baseurl if $makemirror || exists($config{"gitweb.baseurl"});
986 $newproj->{email} = $owner if defined($owner);
987 $newproj->{users} = $users;
988 $newproj->{crypt} = "unknown";
989 $newproj->{reverseorder} = 1 unless exists($config{"hooks.reverseorder"});
990 $newproj->{summaryonly} = 1 unless exists($config{"hooks.summaryonly"});
991 my $dummy = bless {}, "Girocco::Project";
992 $dummy->{path} = "$projdir";
993 $dummy->{configfilehash} = \%config;
994 $dummy->_properties_load;
995 delete $dummy->{origurl};
996 foreach my $k (keys(%$dummy)) {
997 $newproj->{$k} = $dummy->{$k}
998 if exists($dummy->{$k}) && !exists($newproj->{$k});
1001 if ($verbose) {
1002 use Data::Dumper;
1003 my %info = %$newproj;
1004 $info{README} = get_readme_desc($info{README}) if exists($info{README});
1005 my $d = Data::Dumper->new([\%info], ['*'.$newproj->{name}]);
1006 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
1007 print $d->Dump([\%info], ['*'.$newproj->{name}]);
1009 return 0 if $dryrun;
1011 # Make any changes as needed for @needskip, $neednoprune and $needfakeorigin
1012 if ($makemirror) {
1013 git_config($projdir, "fetch.prune", "false") if $neednoprune;
1014 git_config($projdir, "remote.origin.skipDefaultUpdate", "true") if $needfakeorigin;
1015 if ($usingall && @needskip) {
1016 git_config($projdir, "remote.$_.skipDefaultUpdate", "true") foreach @needskip;
1020 # Perform the actual adoption
1021 $newproj->adopt or die "Girocco::Project::adopt failed\n";
1023 # Perhaps restore core.logAllRefUpdates, receive.denyNonFastForwards and receive.denyDeleteCurrent
1024 git_config($projdir, "receive.denyNonFastForwards", "true")
1025 if git_bool($config{"receive.denynonfastforwards"});
1026 git_config($projdir, "receive.denyDeleteCurrent", $config{"receive.denydeletecurrent"})
1027 if exists($config{"receive.denydeletecurrent"}) &&
1028 $config{"receive.denydeletecurrent"} ne "warn";
1029 git_config($projdir, "core.logAllRefUpdates", "true")
1030 if $reflogactive;
1032 # Success
1033 if ($makemirror) {
1034 warn "Mirror project \"$projname\" successfully adopted.\n" unless $quiet;
1035 } else {
1036 warn "Push project \"$projname\" successfully adopted.\n" unless $quiet;
1038 return 0;
1041 sub cmd_remove {
1042 my ($force, $reallydel, $keepforks);
1043 parse_options(force => \$force, "really-delete" => \$reallydel,
1044 "keep-forks" => \$keepforks, quiet => \$quiet, q =>\$quiet);
1045 @ARGV or die "Please give project name on command line.\n";
1046 @ARGV == 1 or die_usage;
1047 my $project = get_project($ARGV[0]);
1048 my $projname = $project->{name};
1049 my $isempty = !$project->{mirror} && $project->is_empty;
1050 if (!$project->{mirror} && !$isempty && $reallydel) {
1051 die "refusing to remove and delete non-empty push project without --force: $projname\n" unless $force;
1052 warn "allowing removal and deletion of non-empty push project with --force\n" unless $quiet;
1054 my $altwarn;
1055 my $removenogc;
1056 if ($project->has_forks) {
1057 die "refusing to remove project with forks (use --keep-forks): $projname\n" unless $keepforks;
1058 warn "allowing removal of forked project while preserving its forks with --keep-forks\n" unless $quiet;
1059 # Run pseudo GC on that repository so that objects don't get lost within forks
1060 my $basedir = $Girocco::Config::basedir;
1061 my $projdir = $project->{path};
1062 warn "We have to run pseudo GC on the repo so that the forks don't lose data. Hang on...\n" unless $quiet;
1063 my $nogcrunning = sub {
1064 die "Error: GC appears to be currently running on $projname\n"
1065 if -e "$projdir/gc.pid" || -e "$projdir/.gc_in_progress";
1067 &$nogcrunning;
1068 $removenogc = ! -e "$projdir/.nogc";
1069 recreate_file("$projdir/.nogc") if $removenogc;
1070 die "unable to create \"$projdir/.nogc\"\n" unless -e "$projdir/.nogc";
1071 delete $ENV{show_progress};
1072 $ENV{'show_progress'} = 1 unless $quiet;
1073 sleep 2; # *cough*
1074 &$nogcrunning;
1075 system("$basedir/toolbox/perform-pre-gc-linking.sh", "--include-packs", $projname) == 0
1076 or die "Running pseudo GC on project $projname failed\n";
1077 $altwarn = 1;
1079 my $archived;
1080 if (!$project->{mirror} && !$isempty && !$reallydel) {
1081 $archived = $project->archive_and_delete;
1082 unlink("$archived/.nogc") if $removenogc && defined($archived) && $archived ne "";
1083 } else {
1084 $project->delete;
1086 warn "Project '$projname' removed from $Girocco::Config::name" .
1087 ($archived ? ", backup in '$archived'" : "") .".\n" unless $quiet;
1088 warn "Retained forks may now have unwanted objects/info/alternates lines\n" if $altwarn && !$quiet;
1089 return 0;
1092 sub cmd_show {
1093 use Data::Dumper;
1094 @ARGV == 1 or die_usage;
1095 my $project = get_clean_project($ARGV[0]);
1096 my %info = %$project;
1097 my $d = Data::Dumper->new([\%info], ['*'.$project->{name}]);
1098 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
1099 print $d->Dump([\%info], ['*'.$project->{name}]);
1100 return 0;
1103 sub cmd_listheads {
1104 @ARGV == 1 or die_usage;
1105 my $project = get_project($ARGV[0]);
1106 my @heads = sort({lc($a) cmp lc($b)} $project->get_heads);
1107 my $cur = $project->{HEAD};
1108 defined($cur) or $cur = '';
1109 my $curmark = '*';
1110 my $headhash = get_git("--git-dir=$project->{path}", 'rev-parse', '--quiet', '--verify', 'HEAD');
1111 defined($headhash) or $headhash = '';
1112 chomp $headhash;
1113 $headhash or $curmark = '!';
1114 foreach (@heads) {
1115 my $mark = $_ eq $cur ? $curmark : ' ';
1116 print "$mark $_\n";
1118 return 0;
1121 sub cmd_listtags {
1122 my $vcnt = 0;
1123 shift(@ARGV), $vcnt=1 if @ARGV && ($ARGV[0] eq '--verbose' || $ARGV[0] eq '-v');
1124 @ARGV == 1 or die_usage;
1125 my $project = get_project($ARGV[0]);
1126 if ($vcnt) {
1127 print map("$$_[0]\t$$_[1]\n", get_ctag_counts($project));
1128 } else {
1129 print map("$_\n", $project->get_ctag_names);
1131 return 0;
1134 sub cmd_deltags {
1135 my $ic = 0;
1136 shift(@ARGV), $ic=1 if @ARGV && $ARGV[0] =~ /^(?:--?ignore-case|-i)$/i;
1137 @ARGV >= 2 or die_usage;
1138 my $project = get_project(shift @ARGV);
1139 my %curtags;
1140 if ($ic) {
1141 push(@{$curtags{lc($_)}}, $_) foreach $project->get_ctag_names;
1142 } else {
1143 push(@{$curtags{$_}}, $_) foreach $project->get_ctag_names;
1145 my @deltags = ();
1146 my %seentag = ();
1147 my $ctags = join(" ", @ARGV);
1148 $ctags = lc($ctags) if $ic;
1149 foreach (split(/[\s,]+/, $ctags)) {
1150 next unless exists($curtags{$_});
1151 $seentag{$_}=1, push(@deltags, $_) unless $seentag{$_};
1153 if (!@deltags) {
1154 warn $project->{name}, ": skipping removal of only non-existent tags\n" unless $quiet;
1155 } else {
1156 # Avoid touching anything other than the ctags
1157 foreach my $tg (@deltags) {
1158 $project->delete_ctag($_) foreach @{$curtags{$tg}};
1160 $project->_set_changed;
1161 $project->_set_forkchange;
1162 warn $project->{name}, ": specified tags have been removed\n" unless $quiet;
1164 return 0;
1167 sub cmd_addtags {
1168 @ARGV >= 2 or die_usage;
1169 my $project = get_project(shift @ARGV);
1170 my $ctags = join(" ", @ARGV);
1171 $ctags =~ /[^, a-zA-Z0-9:.+#_-]/ and
1172 die "Content tag(s) \"$ctags\" contain(s) evil character(s).\n";
1173 my $oldmask = umask();
1174 umask($oldmask & ~0060);
1175 my $changed = 0;
1176 foreach (split(/[\s,]+/, $ctags)) {
1177 ++$changed if $project->add_ctag($_, 1);
1179 if ($changed) {
1180 $project->_set_changed;
1181 $project->_set_forkchange;
1183 umask($oldmask);
1184 my $cnt = ($changed == 1) ? "1 content tag has" : $changed . " content tags have";
1185 warn $project->{name}, ": $cnt been added/updated\n" unless $quiet;
1186 return 0;
1189 sub _get_random_val {
1190 my $p = shift;
1191 my $md5;
1193 no warnings;
1194 $md5 = md5_hex(time . $$ . rand() . join(':',%$p));
1196 $md5;
1199 sub cmd_chpass {
1200 my $force = 0;
1201 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1202 my $random = undef;
1203 pop(@ARGV), $random=lc($ARGV[1]) if @ARGV==2 && $ARGV[1] =~ /^(?:random|unknown)$/i;
1204 @ARGV == 1 or die_usage;
1205 my $project = get_project($ARGV[0]);
1206 die "refusing to change locked password of project \"$ARGV[0]\" without --force\n"
1207 if $project->is_password_locked;
1208 my ($newpw, $rmsg);
1209 if ($random) {
1210 if ($random eq "random") {
1211 die "refusing to set random password without --force\n" unless $force;
1212 $rmsg = "set to random value";
1213 $newpw = _get_random_val($project);
1214 } else {
1215 die "refusing to set password hash to '$random' without --force\n" unless $force;
1216 $rmsg = "hash set to '$random'";
1217 $newpw = $random;
1219 } else {
1220 $rmsg = "updated";
1221 if (-t STDIN) {
1222 print "Changing admin password for project $ARGV[0]\n";
1223 my $np1 = prompt_noecho_nl_or_die("New password for project $ARGV[0] (echo is off)");
1224 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1225 my $np2 = prompt_noecho_nl_or_die("Retype new password for project $ARGV[0]");
1226 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
1227 "the admin passwords you have entered do not match each other.\n";
1228 $newpw = $np1;
1229 } else {
1230 $newpw = <STDIN>;
1231 defined($newpw) or die "missing new password on STDIN\n";
1232 chomp($newpw);
1235 $newpw ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1236 my $old = $project->{crypt};
1237 $project->{crypt} = (defined($random) && $random ne "random") ? $newpw : scrypt_sha1($newpw);
1238 if (defined($old) && $old eq $project->{crypt}) {
1239 warn $project->{name}, ": skipping update of password hash to same value\n" unless $quiet;
1240 } else {
1241 # Avoid touching anything other than the password hash
1242 $project->_group_update;
1243 warn $project->{name}, ": admin password $rmsg (new hash stored)\n" unless $quiet;
1245 return 0;
1248 sub cmd_checkpw {
1249 @ARGV == 1 or die_usage;
1250 my $project = get_project($ARGV[0]);
1251 my $pwhash = $project->{crypt};
1252 defined($pwhash) or $pwhash = "";
1253 if ($pwhash eq "") {
1254 warn $project->{name}, ": no password required\n" unless $quiet;
1255 return 0;
1257 if ($project->is_password_locked) {
1258 warn $project->{name}, ": password is locked\n" unless $quiet;
1259 exit 1;
1261 my $checkpw;
1262 if (-t STDIN) {
1263 $checkpw = prompt_noecho_nl_or_die("Admin password for project $ARGV[0] (echo is off)");
1264 $checkpw ne "" or warn "checking for empty password as hash (very unlikely)\n" unless $quiet;
1265 } else {
1266 $checkpw = <STDIN>;
1267 defined($checkpw) or die "missing admin password on STDIN\n";
1268 chomp($checkpw);
1270 unless (Girocco::CLIUtil::check_passwd_match($pwhash, $checkpw)) {
1271 warn "password check failure\n" unless $quiet;
1272 exit 1;
1274 warn "admin password match\n" unless $quiet;
1275 return 0;
1278 sub cmd_gc {
1279 my ($force, $auto, $redelta, $recompress);
1280 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, auto => \$auto,
1281 redelta => \$redelta, "no-reuse-delta" => \$redelta, aggressive => \$force,
1282 recompress => \$recompress, "no-reuse-object" => $recompress);
1283 $force && $auto and die "--force and --auto are mutually exclusive options\n";
1284 @ARGV or die "Please give project name on command line.\n";
1285 @ARGV == 1 or die_usage;
1286 my $project = get_project($ARGV[0]);
1287 delete $ENV{show_progress};
1288 delete $ENV{force_gc};
1289 $quiet or $ENV{"show_progress"} = 1;
1290 $force and $ENV{"force_gc"} = 1;
1291 if (!$auto && !$force && ! -e $project->{path}."/.needsgc") {
1292 open NEEDSGC, '>', $project->{path}."/.needsgc" and close NEEDSGC;
1294 my @args = ($Girocco::Config::basedir . "/jobd/gc.sh", $project->{name});
1295 $redelta && !$recompress and push(@args, "-f");
1296 $recompress and push(@args, "-F");
1297 my $lastgc = $project->{lastgc};
1298 system({$args[0]} @args) != 0 and return 1;
1299 # Do it again Sam, but only if lastgc was set, gc.sh succeeded and now it's not set
1300 if ($lastgc) {
1301 my $newlastgc = get_git("--git-dir=$project->{path}", 'config', '--get', 'gitweb.lastgc');
1302 if (!$newlastgc) {
1303 system({$args[0]} @args) != 0 and return 1;
1306 return 0;
1309 sub cmd_update {
1310 my ($force, $summary);
1311 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, summary => \$summary);
1312 $quiet && $summary and die "--quiet and --summary are mutually exclusive options\n";
1313 @ARGV or die "Please give project name on command line.\n";
1314 @ARGV == 1 or die_usage;
1315 my $project = get_project($ARGV[0]);
1316 $project->{mirror} or die "Project \"$ARGV[0]\" is a push project, not a mirror project.\n";
1317 delete $ENV{show_progress};
1318 delete $ENV{force_update};
1319 if ($quiet) {
1320 $ENV{"show_progress"} = 0;
1321 } else {
1322 $ENV{"show_progress"} = ($summary ? 1 : 2);
1324 $force and $ENV{"force_update"} = 1;
1325 system($Girocco::Config::basedir . "/jobd/update.sh", $project->{name}) != 0 and return 1;
1326 return 0;
1329 sub cmd_remirror {
1330 my $force = 0;
1331 parse_options(force => \$force, quiet => \$quiet, q => \$quiet);
1332 @ARGV or die "Please give project name on command line.\n";
1333 @ARGV == 1 or die_usage;
1334 my $project = get_project($ARGV[0]);
1335 $project->{mirror} or die "Project \"$ARGV[0]\" is a push project, not a mirror project.\n";
1336 if ($project->{clone_in_progress} && !$project->{clone_failed}) {
1337 warn "Project \"$ARGV[0]\" already seems to have a clone underway at this moment.\n" unless $quiet && $force;
1338 exit(255) unless $force;
1339 yes_to_continue_or_die("Are you sure you want to force a remirror");
1341 unlink($project->_clonefail_path);
1342 unlink($project->_clonelog_path);
1343 recreate_file($project->_clonep_path);
1344 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or
1345 die "cannot connect to taskd.socket: $!\n";
1346 select((select($sock),$|=1)[0]);
1347 $sock->print("clone ".$project->{name}."\n");
1348 # Just ignore reply, we are going to succeed anyway and the I/O
1349 # would apparently get quite hairy.
1350 $sock->flush();
1351 sleep 2; # *cough*
1352 $sock->close();
1353 warn "Project \"$ARGV[0]\" remirror initiated.\n" unless $quiet;
1354 return 0;
1357 sub cmd_setowner {
1358 my $force = 0;
1359 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1360 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1361 my $project = get_project($ARGV[0]);
1362 if (@ARGV == 2 && !valid_email($ARGV[1])) {
1363 die "invalid owner/email (use --force to accept): \"$ARGV[1]\"\n"
1364 unless $force;
1365 warn "using invalid owner/email with --force\n" unless $quiet;
1367 if (@ARGV == 2 && length($ARGV[1]) > 96) {
1368 die "owner/email longer than 96 chars (use --force to accept): \"$ARGV[1]\"\n"
1369 unless $force;
1370 warn "using longer than 96 char owner/email with --force\n" unless $quiet;
1372 my $old = $project->{email};
1373 if (@ARGV == 1) {
1374 print "$old\n" if defined($old);
1375 return 0;
1377 if (defined($old) && $old eq $ARGV[1]) {
1378 warn $project->{name}, ": skipping update of owner/email to same value\n" unless $quiet;
1379 } else {
1380 # Avoid touching anything other than "gitweb.owner"
1381 $project->_property_fput("email", $ARGV[1]);
1382 $project->_update_index;
1383 $project->_set_changed;
1384 warn $project->{name}, ": owner/email updated to \"$ARGV[1]\"\n" unless $quiet;
1386 return 0;
1389 sub cmd_setdesc {
1390 my $force = 0;
1391 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1392 @ARGV >= 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1393 my $project = get_project(shift @ARGV);
1394 if (@ARGV && !valid_desc(join(" ", @ARGV))) {
1395 die "invalid description (use --force to accept): \"".join(" ", @ARGV)."\"\n"
1396 unless $force;
1397 warn "using invalid description with --force\n" unless $quiet;
1399 my $desc = clean_desc(join(" ", @ARGV));
1400 if (@ARGV && length($desc) > 1024) {
1401 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
1402 unless $force;
1403 warn "using longer than 1024 char description with --force\n" unless $quiet;
1405 my $old = $project->{desc};
1406 if (!@ARGV) {
1407 print "$old\n" if defined($old);
1408 return 0;
1410 if (defined($old) && $old eq $desc) {
1411 warn $project->{name}, ": skipping update of description to same value\n" unless $quiet;
1412 } else {
1413 # Avoid touching anything other than description file
1414 $project->_property_fput("desc", $desc);
1415 $project->_set_changed;
1416 warn $project->{name}, ": description updated to \"$desc\"\n" unless $quiet;
1418 return 0;
1421 sub cmd_setreadme {
1422 my $force = 0;
1423 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1424 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1425 my $project = get_project($ARGV[0]);
1426 my $old = $project->{README};
1427 if (@ARGV == 1) {
1428 chomp $old if defined($old);
1429 print "$old\n" if defined($old) && $old ne "";
1430 return 0;
1432 my ($new, $raw, $newname);
1433 $newname = '';
1434 if ($ARGV[1] eq "-") {
1435 local $/;
1436 $new = <STDIN>;
1437 $raw = 1;
1438 $newname = "contents of <STDIN>";
1439 } elsif (lc($ARGV[1]) eq "automatic" || lc($ARGV[1]) eq "auto") {
1440 $new = "";
1441 } elsif (lc($ARGV[1]) eq "suppressed" || lc($ARGV[1]) eq "suppress") {
1442 $new = "<!-- suppress -->";
1443 } else {
1444 my $fn = $ARGV[1];
1445 $fn =~ s/^\@//;
1446 die "missing filename for README\n" unless $fn ne "";
1447 die "no such file: \"$fn\"\n" unless -f $fn && -r $fn;
1448 open F, '<', $fn or die "cannot open \"$fn\" for reading: $!\n";
1449 local $/;
1450 $new = <F>;
1451 close F;
1452 $raw = 1;
1453 $newname = "contents of \"$fn\"";
1455 defined($new) or $new = '';
1456 $project->{README} = to_utf8($new, 1);
1457 $project->_cleanup_readme;
1458 if (length($project->{README}) > 8192) {
1459 die "readme greater than 8192 chars is too long (use --force to override)\n"
1460 unless $force;
1461 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
1463 if ($raw) {
1464 my $rd = get_readme_desc($project->{README});
1465 if ($rd ne "automatic" && $rd ne "suppressed") {
1466 my $xmllint = qx(command -v xmllint); chomp $xmllint;
1467 if (-f $xmllint && -x $xmllint) {
1468 my ($cnt, $err) = $project->_lint_readme(0);
1469 if ($cnt) {
1470 my $msg = "xmllint: $cnt error";
1471 $msg .= "s" unless $cnt == 1;
1472 print STDERR "$msg\n", "-" x length($msg), "\n", $err
1473 unless $force && $quiet;
1474 exit(255) unless $force;
1475 warn $project->{name} . ": using invalid raw HTML with --force\n" unless $quiet;
1477 } else {
1478 die "xmllint not available, refusing to use raw HTML without --force\n"
1479 unless $force;
1480 warn "xmllint not available using unchecked raw HTML with --force\n" unless $quiet;
1484 if (defined($old) && $old eq $project->{README}) {
1485 warn $project->{name}, ": skipping update of README to same value\n" unless $quiet;
1486 } else {
1487 # Avoid touching anything other than README.html file
1488 $project->_property_fput("README", $project->{README});
1489 $project->_set_changed;
1490 my $desc = get_readme_desc($project->{README});
1491 if ($newname) {
1492 $newname .= " ($desc)";
1493 } else {
1494 $newname = $desc;
1496 warn $project->{name}, ": README updated to $newname\n" unless $quiet;
1498 return 0;
1501 sub valid_head {
1502 my ($proj, $newhead) = @_;
1503 my %okheads = map({($_ => 1)} $proj->get_heads);
1504 exists($okheads{$newhead});
1507 sub cmd_sethead {
1508 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1509 my $project = get_project($ARGV[0]);
1510 if (@ARGV == 2 && !valid_head($project, $ARGV[1])) {
1511 die "invalid head (try \"@{[basename($0)]} listheads $ARGV[0]\"): \"$ARGV[1]\"\n";
1513 my $old = $project->{HEAD};
1514 if (@ARGV == 1) {
1515 print "$old\n" if defined($old);
1516 return 0;
1518 if (defined($old) && $old eq $ARGV[1]) {
1519 warn $project->{name}, ": skipping update of HEAD symref to same value\n" unless $quiet;
1520 } else {
1521 # Avoid touching anything other than the HEAD symref
1522 $project->set_HEAD($ARGV[1]);
1523 $project->_set_changed;
1524 warn $project->{name}, ": HEAD symref updated to \"refs/heads/$ARGV[1]\"\n" unless $quiet;
1526 return 0;
1529 sub cmd_sethooks {
1530 my $force = 0;
1531 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1532 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1533 my $project = get_project($ARGV[0]);
1534 my $projconfig = read_config_file_hash($project->{path}."/config");
1535 my $ghp = $Girocco::Config::reporoot."/_global/hooks";
1536 my $rghp = realpath($ghp);
1537 my $lhp = $project->{path}."/hooks";
1538 my $rlhp = realpath($lhp);
1539 my $ahp = "";
1540 my $rahp = undef;
1541 if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) {
1542 $ahp = $projconfig->{"core.hookspath"};
1543 $rahp = realpath($ahp);
1545 if (@ARGV == 1) {
1546 if (defined($rahp) && $rahp ne "") {
1547 if ($rahp eq $rghp) {
1548 my $nc = ($ahp eq $ghp ? "" : " non-canonical");
1549 printf "%s \t(global%s)\n", $ahp, $nc;
1550 } elsif ($rahp eq $rlhp) {
1551 my $nc = ($ahp eq $lhp ? "" : " non-canonical");
1552 printf "%s \t(local%s)\n", $ahp, $nc;
1553 } elsif ($rahp ne $ahp) {
1554 print "$ahp \t($rahp)\n";
1555 } else {
1556 print "$ahp\n";
1558 } elsif ($ahp ne "") {
1559 print "$ahp \t(non-existent)\n";
1561 return 0;
1563 my $shp = $ARGV[1];
1564 if (lc($shp) eq "global") {
1565 $shp = $ghp;
1566 } elsif (lc($shp) eq "local") {
1567 $shp = $lhp;
1568 } elsif (substr($shp, 0, 2) eq "~/") {
1569 $shp = $ENV{"HOME"}.substr($shp,1);
1570 } elsif ($shp =~ m,^~([a-zA-Z_][a-zA-Z_0-9]*)((?:/.*)?)$,) {
1571 my $sfx = $2;
1572 my $hd = (getpwnam($1))[7];
1573 $shp = $hd . $sfx if defined($hd) && $hd ne "" && $hd ne "/" && -d $hd;
1575 $shp ne "" && -d $shp or die "no such directory: $ARGV[1]\n";
1576 my $rshp = realpath($shp);
1577 defined($rshp) && $rshp ne "" or die "could not realpath: $ARGV[1]\n";
1578 $rshp =~ m,^/[^/], or die "invalid hookspath: $rshp\n";
1579 die "refusing to switch from current non-global hookspath without --force\n"
1580 if !$force && defined($rahp) && $rahp ne "" && $rahp ne $rghp && $rshp ne $rahp;
1581 if (!$force && defined($rahp) && $rahp ne "") {
1582 if ($rshp eq $rahp && ($ahp eq $ghp || $ahp eq $lhp)) {
1583 warn $project->{name}, ": skipping update of hookspath to same effective value\n" unless $quiet;
1584 return 0;
1587 $rshp = $ghp if $rshp eq $rghp;
1588 $rshp = $lhp if $rshp eq $rlhp;
1589 if ($rshp eq $ahp) {
1590 warn $project->{name}, ": skipping update of hookspath to same value\n" unless $quiet;
1591 return 0;
1593 die "refusing to set neither local nor global hookspath without --force\n"
1594 if !$force && $rshp ne $ghp && $rshp ne $lhp;
1595 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1596 'config', "core.hookspath", $rshp);
1597 my $newval = '"'.$rshp.'"';
1598 $newval = "global" if $rshp eq $ghp;
1599 $newval = "local" if $rshp eq $lhp;
1600 warn $project->{name}, ": hookspath set to $newval\n" unless $quiet;
1601 return 0;
1604 our %boolfields;
1605 BEGIN {
1606 %boolfields = (
1607 cleanmirror => 1,
1608 reverseorder => 0,
1609 summaryonly => 0,
1610 statusupdates => 1,
1614 sub cmd_setbool {
1615 my $force = 0;
1616 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1617 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1618 my $project = get_project($ARGV[0]);
1619 if (!exists($boolfields{$ARGV[1]})) {
1620 die "invalid boolean field name: \"$ARGV[1]\" -- try \"help\"\n";
1622 if (@ARGV == 3 && $boolfields{$ARGV[1]} && !$project->{mirror}) {
1623 die "invalid boolean field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
1624 unless $force;
1625 warn "using mirror field on non-mirror with --force\n" unless $quiet;
1627 if (@ARGV == 3 && !valid_bool($ARGV[2])) {
1628 die "invalid boolean value: \"$ARGV[2]\"\n";
1630 my $bool = clean_bool($ARGV[2]);
1631 my $old = $project->{$ARGV[1]};
1632 if (@ARGV == 2) {
1633 print "$old\n" if defined($old);
1634 return 0;
1636 if (defined($old) && $old eq $bool) {
1637 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
1638 } else {
1639 # Avoid touching anything other than $ARGV[1] field
1640 $project->_property_fput($ARGV[1], $bool);
1641 warn $project->{name}, ": $ARGV[1] updated to $bool\n" unless $quiet;
1643 return 0;
1646 sub cmd_setjsontype {
1647 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1648 my $project = get_project($ARGV[0]);
1649 my $jsontype;
1650 if (@ARGV == 2) {
1651 my $jt = lc($ARGV[1]);
1652 index($jt, "/") >= 0 or $jt = "application/".$jt;
1653 $jt eq 'application/x-www-form-urlencoded' ||
1654 $jt eq 'application/json' or
1655 die "invalid jsontype value: \"$ARGV[1]\"\n";
1656 $jsontype = $jt;
1658 my $old = $project->{jsontype};
1659 if (@ARGV == 1) {
1660 print "$old\n" if defined($old);
1661 return 0;
1663 if (defined($old) && $old eq $jsontype) {
1664 warn $project->{name}, ": skipping update of jsontype to same value\n" unless $quiet;
1665 } else {
1666 # Avoid touching anything other than jsontype field
1667 $project->_property_fput('jsontype', $jsontype);
1668 warn $project->{name}, ": jsontype updated to $jsontype\n" unless $quiet;
1670 return 0;
1673 sub cmd_setjsonsecret {
1674 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1675 my $project = get_project($ARGV[0]);
1676 my $jsonsecret;
1677 if (@ARGV == 2) {
1678 my $js = $ARGV[1];
1679 $js =~ s/^\s+//; $js =~ s/\s+$//;
1680 $jsonsecret = $js;
1682 my $old = $project->{jsonsecret};
1683 if (@ARGV == 1) {
1684 print "$old\n" if defined($old);
1685 return 0;
1687 if (defined($old) && $old eq $jsonsecret) {
1688 warn $project->{name}, ": skipping update of jsonsecret to same value\n" unless $quiet;
1689 } else {
1690 # Avoid touching anything other than jsonsecret field
1691 $project->_property_fput('jsonsecret', $jsonsecret);
1692 warn $project->{name}, ": jsonsecret updated to \"$jsonsecret\"\n" unless $quiet;
1694 return 0;
1697 sub cmd_setautogchack {
1698 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1699 my $project = get_project($ARGV[0]);
1700 my $aghok = $Girocco::Config::autogchack &&
1701 ($project->{mirror} || $Girocco::Config::autogchack ne "mirror");
1702 my $old = defined($project->{autogchack}) ? clean_bool($project->{autogchack}) : "unset";
1703 if (@ARGV == 1) {
1704 print "$old\n" if $aghok;
1705 return 0;
1707 my $bool;
1708 if (lc($ARGV[1]) eq "unset") {
1709 $bool = "unset";
1710 } else {
1711 valid_bool($ARGV[1]) or die "invalid boolean value: \"$ARGV[1]\"\n";
1712 $bool = clean_bool($ARGV[1]);
1714 if (!$aghok) {
1715 die "\$Girocco::Config::autogchack is false\n" unless $Girocco::Config::autogchack;
1716 die "\$Girocco::Config::autogchack is only enabled for mirrors\n";
1718 if ($old eq $bool) {
1719 warn $project->{name}, ": autogchack value unchanged\n" unless $quiet;
1720 } else {
1721 if ($bool eq "unset") {
1722 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1723 'config', '--unset', "girocco.autogchack");
1724 } else {
1725 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1726 'config', '--bool', "girocco.autogchack", $bool);
1729 return system($Girocco::Config::basedir . "/jobd/maintain-auto-gc-hack.sh", $project->{name}) == 0
1730 ? 0 : 1;
1733 sub valid_url {
1734 my ($url, $type) = @_;
1735 $type ne 'baseurl' and return valid_web_url($url);
1736 valid_repo_url($url) or return 0;
1737 if ($Girocco::Config::restrict_mirror_hosts) {
1738 my $mh = extract_url_hostname($url);
1739 is_dns_hostname($mh) or return 0;
1740 !is_our_hostname($mh) or return 0;
1742 return 1;
1745 our %urlfields;
1746 BEGIN {
1747 %urlfields = (
1748 baseurl => ["url" , 1],
1749 homepage => ["hp" , 0],
1750 notifyjson => ["notifyjson", 0],
1754 sub cmd_seturl {
1755 my $force = 0;
1756 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1757 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1758 my $project = get_project($ARGV[0]);
1759 if (!exists($urlfields{$ARGV[1]})) {
1760 die "invalid URL field name: \"$ARGV[1]\" -- try \"help\"\n";
1762 if (@ARGV == 3 && ${$urlfields{$ARGV[1]}}[1] && !$project->{mirror}) {
1763 die "invalid URL field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
1764 unless $force;
1765 warn "using mirror field on non-mirror with --force\n" unless $quiet;
1767 if (@ARGV == 3 && !valid_url($ARGV[2], $ARGV[1])) {
1768 die "invalid URL (use --force to accept): \"$ARGV[2]\"\n"
1769 unless $force;
1770 warn "using invalid URL with --force\n" unless $quiet;
1772 my $old = $project->{${$urlfields{$ARGV[1]}}[0]};
1773 if (@ARGV == 2) {
1774 print "$old\n" if defined($old);
1775 return 0;
1777 if (defined($old) && $old eq $ARGV[2]) {
1778 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
1779 } else {
1780 # Avoid touching anything other than $ARGV[1]'s field
1781 $project->_property_fput(${$urlfields{$ARGV[1]}}[0], $ARGV[2]);
1782 if ($ARGV[1] eq "baseurl") {
1783 $project->{url} = $ARGV[2];
1784 $project->_set_bangagain;
1786 $project->_set_changed unless $ARGV[1] eq "notifyjson";
1787 warn $project->{name}, ": $ARGV[1] updated to $ARGV[2]\n" unless $quiet;
1789 return 0;
1792 our %msgsfields;
1793 BEGIN {
1794 %msgsfields = (
1795 notifymail => 1,
1796 notifytag => 1,
1800 sub cmd_setmsgs {
1801 my $force = 0;
1802 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1803 @ARGV >= 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1804 my $project = get_project(shift @ARGV);
1805 my $field = shift @ARGV;
1806 if (!exists($msgsfields{$field})) {
1807 die "invalid msgs field name: \"$field\" -- try \"help\"\n";
1809 if (@ARGV && !valid_addrlist(@ARGV)) {
1810 die "invalid email address list (use --force to accept): \"".join(" ",@ARGV)."\"\n"
1811 unless $force;
1812 warn "using invalid email address list with --force\n" unless $quiet;
1814 my $old = $project->{$field};
1815 if (!@ARGV) {
1816 printf "%s\n", clean_addrlist($old, " ") if defined($old);
1817 return 0;
1819 my $newlist = clean_addrlist(join(" ",@ARGV));
1820 if (defined($old) && $old eq $newlist) {
1821 warn $project->{name}, ": skipping update of $field to same value\n" unless $quiet;
1822 } else {
1823 # Avoid touching anything other than $field's field
1824 $project->_property_fput($field, $newlist);
1825 warn $project->{name}, ": $field updated to \"$newlist\"\n" unless $quiet;
1827 return 0;
1830 sub cmd_setusers {
1831 my $force = 0;
1832 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1833 @ARGV >= 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1834 my $project = get_project(shift @ARGV);
1835 my $projname = $project->{name};
1836 !@ARGV || !$project->{mirror} or die "cannot set users list for mirror project: \"$projname\"\n";
1837 my @newusers = ();
1838 if (@ARGV) {
1839 eval {@newusers = validate_users(join(" ", @ARGV), $force); 1;} or exit 255;
1840 die "refusing to set empty users list without --force\n" unless @newusers || $force;
1842 return 0 if !@ARGV && $project->{mirror};
1843 my $oldusers = $project->{users};
1844 if ($oldusers && ref($oldusers) eq "ARRAY") {
1845 $oldusers = join("\n", @$oldusers);
1846 } else {
1847 $oldusers = "";
1849 if (!@ARGV) {
1850 print "$oldusers\n" if $oldusers ne "";
1851 return 0;
1853 if ($oldusers eq join("\n", @newusers)) {
1854 warn "$projname: skipping update of users list to same value\n" unless $quiet;
1855 } else {
1856 # Avoid touching anything other than the users list
1857 $project->{users} = \@newusers;
1858 $project->_update_users;
1859 warn "$projname: users list updated to \"@{[join(',',@newusers)]}\"\n" unless $quiet;
1861 return 0;
1864 our %fieldnames;
1865 BEGIN {
1866 %fieldnames = (
1867 owner => [\&cmd_setowner, 0],
1868 desc => [\&cmd_setdesc, 0],
1869 description => [\&cmd_setdesc, 0],
1870 readme => [\&cmd_setreadme, 0],
1871 head => [\&cmd_sethead, 0],
1872 HEAD => [\&cmd_sethead, 0],
1873 hooks => [\&cmd_sethooks, 0],
1874 hookspath => [\&cmd_sethooks, 0],
1875 cleanmirror => [\&cmd_setbool, 1],
1876 reverseorder => [\&cmd_setbool, 1],
1877 summaryonly => [\&cmd_setbool, 1],
1878 statusupdates => [\&cmd_setbool, 1],
1879 autogchack => [\&cmd_setautogchack, 0],
1880 baseurl => [\&cmd_seturl, 1],
1881 homepage => [\&cmd_seturl, 1],
1882 notifyjson => [\&cmd_seturl, 1],
1883 jsontype => [\&cmd_setjsontype, 0],
1884 jsonsecret => [\&cmd_setjsonsecret, 0],
1885 notifymail => [\&cmd_setmsgs, 1],
1886 notifytag => [\&cmd_setmsgs, 1],
1887 users => [\&cmd_setusers, 0],
1891 sub do_getset {
1892 $setopt = shift;
1893 my @newargs = ();
1894 push(@newargs, shift) if @_ && $_[0] eq '--force';
1895 my $field = $_[1];
1896 (($setopt && @_ >= 3) || @_ == 2) && exists($fieldnames{$field}) or die_usage;
1897 push(@newargs, shift);
1898 shift unless ${$fieldnames{$field}}[1];
1899 push(@newargs, @_);
1900 diename(($setopt ? "set " : "get ") . $field);
1901 @ARGV = @newargs;
1902 &{${$fieldnames{$field}}[0]}(@ARGV);
1905 sub cmd_get {
1906 do_getset(0, @_);
1909 sub cmd_set {
1910 do_getset(1, @_);
1913 our %commands;
1914 BEGIN {
1915 %commands = (
1916 list => \&cmd_list,
1917 create => \&cmd_create,
1918 adopt => \&cmd_adopt,
1919 remove => \&cmd_remove,
1920 trash => \&cmd_remove,
1921 delete => \&cmd_remove,
1922 show => \&cmd_show,
1923 listheads => \&cmd_listheads,
1924 listtags => \&cmd_listtags,
1925 listctags => \&cmd_listtags,
1926 deltags => \&cmd_deltags,
1927 delctags => \&cmd_deltags,
1928 addtags => \&cmd_addtags,
1929 addctags => \&cmd_addtags,
1930 chpass => \&cmd_chpass,
1931 checkpw => \&cmd_checkpw,
1932 gc => \&cmd_gc,
1933 update => \&cmd_update,
1934 remirror => \&cmd_remirror,
1935 setowner => \&cmd_setowner,
1936 setdesc => \&cmd_setdesc,
1937 setdescription => \&cmd_setdesc,
1938 setreadme => \&cmd_setreadme,
1939 sethead => \&cmd_sethead,
1940 sethooks => \&cmd_sethooks,
1941 sethookspath => \&cmd_sethooks,
1942 setbool => \&cmd_setbool,
1943 setboolean => \&cmd_setbool,
1944 setflag => \&cmd_setbool,
1945 setautogchack => \&cmd_setautogchack,
1946 seturl => \&cmd_seturl,
1947 setjsontype => \&cmd_setjsontype,
1948 setjsonsecret => \&cmd_setjsonsecret,
1949 setmsgs => \&cmd_setmsgs,
1950 setusers => \&cmd_setusers,
1951 get => \&cmd_get,
1952 set => \&cmd_set,
1955 our %nopager;
1956 BEGIN {
1957 %nopager = map({$_ => 1} qw(
1958 create
1959 chpass
1960 checkpw
1964 sub dohelp {
1965 my $cmd = shift;
1966 my $bn = basename($0);
1967 setup_pager_stdout($usepager);
1968 printf "%s version %s\n\n", $bn, $VERSION;
1969 if (defined($cmd) && $cmd ne '') {
1970 $cmd =~ s/^set(?=[a-zA-Z])//i;
1971 my $cmdhelp = '';
1972 my ($lastmt, $incmd);
1973 foreach (split('\n', sprintf($help, $bn))) {
1974 $lastmt || $incmd or $lastmt = /^\s*$/, next;
1975 $incmd = 1 if $lastmt && /^\s*(?:\[?set\]?)?$cmd\s/;
1976 last if $incmd && /^\s*$/;
1977 $incmd and $cmdhelp .= $_ . "\n";
1978 $lastmt = /^\s*$/;
1980 print $cmdhelp and exit 0 if $cmdhelp;
1982 printf $help, $bn;
1983 exit 0;
1986 sub main {
1987 local *ARGV = \@_;
1989 shift, $quiet=1, redo if @ARGV && $ARGV[0] =~ /^(?:-q|--quiet)$/i;
1990 shift, $usepager=1, redo if @ARGV && $ARGV[0] =~ /^(?:-p|--pager|--paginate)$/i;
1991 shift, $usepager=0, redo if @ARGV && $ARGV[0] =~ /^(?:--no-pager|--no-paginate)$/i;
1993 dohelp($ARGV[1]) if !@ARGV || @ARGV && $ARGV[0] =~ /^(?:-h|-?-help|help)$/i;
1994 my $command = shift;
1995 diename($command);
1996 $setopt = 1;
1997 if (!exists($commands{$command}) && exists($commands{"set".$command})) {
1998 $setopt = 0;
1999 $command = "set" . $command;
2001 exists($commands{$command}) or die "Unknown command \"$command\" -- try \"help\"\n";
2002 dohelp($command) if @ARGV && ($ARGV[0] =~ /^(?:-h|-?-help)$/i ||
2003 $ARGV[0] =~ /^help$/i && !Girocco::Project::does_exist("help",1));
2004 $nopager{$command} and $usepager = 0;
2005 setup_pager_stdout($usepager);
2006 &{$commands{$command}}(@ARGV);