projtool.pl: provide command line jsonsecret manipulation
[girocco/readme.git] / toolbox / projtool.pl
blob4e1840edf6d18b1825c36468c1f7f98ce7398c14
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 [--quiet] <command> <options>
34 help [<command>]
35 show full help or just for <command> if given
37 list [--verbose] [--sort=lcname|name|owner|gid|no] [--owner] [<regexp>]
38 list all projects (default is --sort=lcname)
39 limit to project names matching <regex> if given
40 match <regex> against owner instead of project name with --owner
42 create [--force] [--no-alternates] [--orphan] [<option>...] <project>
43 create new project <project> (prompted)
44 <option> can be:
45 --no-alternates skip setup of objects/info/alternates
46 --orphan allow creation of subproject w/o a parent
47 -p use mkdir -p during --orphan creation
48 --no-password set password crypt to invalid value "unknown"
49 --no-owner leave the gitweb.owner config unset
50 --mirror=<url> create a mirror from <url>
51 --full-mirror mirror all refs
52 --push[=<list>] create a push project
53 --desc=<string> specify project description w/o prompt
54 --homepage=<url> specify project homepage URL w/o prompt
55 --defaults do no interactive prompting at all
56 Using --no-password skips the prompts for password, using
57 --no-owner skips the prompt for owner and using --mirror=<url>
58 or --push[=<list>] skips the prompts for mirror URL and
59 heads-only and push users. With --defaults if neither
60 --mirror=<url> nor --push[=<list>] is given then --push will
61 be implied. Using --desc=<string> will force a specific
62 description (including an empty string) and skip the prompt for
63 it. Otherwise a non-empty default description will always be
64 supplied in lieu of an empty or omitted description.
66 adopt [--force] [--type=mirror|push] [<option>...] <project> [<users>]
67 adopt project <project>
68 type of project is guessed if --type=<type> omitted
69 <users> is same as <newuserslist> for setusers command
70 <option> can be:
71 --dry-run do all the checks but don't perform adoption
72 --verbose show project info dump (useful with --dry-run)
73 --no-users no push users at all (<users> must be omitted)
74 --no-owner leave the gitweb.owner config totally unchanged
75 --owner=<val> set the gitweb.owner config to <val>
76 Both --no-owner and --owner=<val> may NOT be given, with neither
77 take owner from preexisting gitweb.owner else use admin setting.
78 For mirrors <users> is ignored otherwise if no <users> and no
79 --no-users option the push users list will consist of the single
80 user name matching the owner or empty if none or more than one.
81 With --dry-run <project> can be an absolute path to a git dir.
83 remove [--force] [--really-delete] [--keep-forks] <project>
84 remove project <project>
85 do not move to _recyclebin with --really-delete (just rm -rf)
86 remove projects with forks (by keeping forks) using --keep-forks
88 show <project>
89 show project <project>
91 listheads <project>
92 list all available heads for <project> and indicate current head
94 listtags [--verbose] <project>
95 list all ctags on project <project>
97 deltags <project> [-i] <tagstodel>
98 remove any ctags on project <project> present in <tagstodel>
99 <tagstodel> is space or comma separated list of tags to remove
100 with -i match against <tagstodel> without regard to letter case
102 addtags <project> <tagstoadd>
103 add ctags to project <project>
104 <tagstoadd> is space or comma separated list of tags to add
106 chpass [--force] <project> [random | unknown]
107 change project <project> password (prompted)
108 with "random" set to random password
109 with "unknown" set password hash to invalid value "unknown"
111 checkpw <project>
112 check project <project> password for a match (prompted)
114 gc [--force | --auto] [--redelta | --recompress] <project>
115 run the gc.sh script on project <project>
116 with --auto let the gc.sh script decide what to do
117 with --force cause a full gc to take place (force_gc=1)
118 with neither --auto nor --force do a mini or if needed a full gc
119 (in other words just touch .needsgc and run gc.sh)
120 with --redelta a full gc will use pack-objects --no-reuse-delta
121 with --recompress a full gc uses pack-objects --no-reuse-object
122 (--no-reuse-delta and --no-reuse-object are accepted as aliases)
123 unless the global --quiet option is given show_progress=1 is used
125 update [--force] [--quiet | --summary] <project>
126 run the update.sh script on project <project>
127 with --force cause a fetch to always take place (force_update=1)
128 with --quiet only show errors (show_progress is left unset)
129 with --summary show progress and ref summary (show_progress=1)
130 with neither --quiet nor --summary show it all (show_progress=2)
132 remirror [--force] <project>
133 initiate a remirror of project <project>
135 [set]owner [--force] <project> <newowner>
136 set project <project> owner to <newowner>
137 without "set" and only 1 arg, just show current project owner
139 [set]desc [--force] <project> <newdesc>
140 set project <project> description to <newdesc>
141 without "set" and only 1 arg, just show current project desc
143 [set]readme [--force] <project> <newsetting>
144 set project <project> readme to <newsetting>
145 <newsetting> is automatic|suppressed|-|[@]filename
146 without "set" and only 2 args, just show current readme setting
148 [set]head <project> <newhead>
149 set project <project> HEAD symbolic ref to <newhead>
150 without "set" and only 1 arg, just show current project HEAD
152 [set]bool [--force] <project> <flagname> <boolvalue>
153 set project <project> boolean <flagname> to <boolvalue>
154 <flagname> is cleanmirror|reverseorder|summaryonly|statusupdtaes
155 without "set" and only 2 args, just show current flag value
157 [set]hooks [--force] <project> local | global | <path>
158 set project <project> hookspath to local, global or <path>
159 without "set" and only 1 arg, just show current hookspath
161 [set]autogchack <project> <boolvalue> | unset
162 set project <project> autogchack to <boolvalue> or "unset" it
163 without "set" just show current autogchack setting if enabled
164 with "set" autogchack must be enabled in Config.pm for the
165 type of project and maintain-auto-gc-hack.sh is always run
167 [set]url [--force] <project> <urlname> <newurlvalue>
168 set project <project> url <urlname> to <newurlvalue>
169 <urlname> is baseurl|homepage|notifyjson
170 without "set" and only 2 args, just show current url value
172 [set]msgs [--force] <project> <msgsname> <eaddrlist>
173 set project <project> msgs <msgsname> to <addrlist>
174 <msgsname> is notifymail|notifytag
175 <eaddrlist> is space or comma separated list of email addresses
176 without "set" and only 2 args, just show current msgs value
178 [set]users [--force] <project> <newuserslist>
179 set push project <project> users list to <newuserslist>
180 <newuserslist> is space or comma separated list of user names
181 without "set" and only 1 arg, just show current users list
183 [set]jsontype <project> <newjsontype>
184 set project <project> JSON Content-Type to <newjsontype>
185 <newjsontype> is x-www-form-urlencoded or json
186 without "set" and only 1 arg, just show current jsontype
188 [set]jsonsecret <project> <newjsonsecret>
189 set project <project> JSON secret to <newjsonsecret>
190 <newjsonsecret> is a string (empty string disables signatures)
191 without "set" and only 1 arg, just show current jsonsecret
193 get <project> <fieldname>
194 show project <project> field <fieldname>
195 <fieldname> is owner|desc|readme|head|hooks|users|jsontype
196 or jsonsecret|<flagname>|autogchack|<urlname>|<msgsname>
198 set [--force] <project> <fieldname> <newfieldvalue>
199 set project <project> field <fieldname> to <newfieldvalue>
200 <fieldname> same as for get
201 <newfieldvalue> same as for corresponding set... command
202 HELP
204 our $quiet;
205 our $setopt;
206 sub die_usage {
207 my $sub = shift || diename;
208 if ($sub) {
209 die "Invalid arguments to $sub command -- try \"help\"\n";
210 } else {
211 die "Invalid arguments -- try \"help\"\n";
215 sub get_readme_desc {
216 my $rm = shift;
217 defined($rm) or $rm = '';
218 if (length($rm)) {
219 my $test = $rm;
220 $test =~ s/<!--(?:[^-]|(?:-(?!-)))*-->//gs;
221 $test =~ s/\s+//s;
222 return $test eq '' ? "suppressed" : "length " . length($rm);
223 } else {
224 return "automatic";
228 sub get_ctag_counts {
229 my $project = shift;
230 my $compact = shift;
231 my @ctags = ();
232 foreach ($project->get_ctag_names) {
233 my $val = 0;
234 my $ct;
235 if (open $ct, '<', $project->{path}."/ctags/$_") {
236 my $count = <$ct>;
237 close $ct;
238 defined $count or $count = '';
239 chomp $count;
240 $val = $count =~ /^[1-9]\d*$/ ? $count : 1;
242 if ($compact) {
243 if ($val == 1) {
244 push(@ctags, $_);
245 } elsif ($val > 1) {
246 push(@ctags, $_."(".$val.")");
248 } else {
249 push(@ctags, [$_, $val]) if $val;
252 @ctags;
255 sub get_clean_project {
256 my $project = get_project(@_);
257 delete $project->{loaded};
258 delete $project->{base_path};
259 delete $project->{ccrypt};
260 /^orig/i || !defined($project->{$_}) and delete $project->{$_} foreach keys %$project;
261 $project->{owner} = $project->{email}; delete $project->{email};
262 $project->{homepage} = $project->{hp}; delete $project->{hp};
263 $project->{baseurl} = $project->{url}; delete $project->{url};
264 if (defined($project->{path}) && $project->{path} ne "") {
265 my $rp = realpath($project->{path});
266 defined($rp) && $rp ne "" and $project->{realpath} = $rp;
267 if (-f "$rp/objects/info/packs") {
268 my $ipt = (stat _)[9];
269 defined($ipt) and $project->{infopackstime} =
270 strftime("%Y-%m-%d %H:%M:%S %z", localtime($ipt));
273 my $owner = $project->{owner};
274 if ($owner) {
275 $owner = lc($owner);
276 my @owner_users = map {$owner eq lc($$_[4]) ? $$_[1] : ()} get_all_users;
277 $project->{owner_users} = \@owner_users if @owner_users;
279 my $projname = $project->{name};
280 my @forks = grep {$$_[1] =~ m,^$projname/,} get_all_projects;
281 $project->{has_forks} = 1 if @forks;
282 $project->{has_alternates} = 1 if $project->has_alternates;
283 my @bundles = $project->bundles;
284 for (my $i = 0; $i < @bundles; ++$i) {
285 my $secs = $bundles[$i]->[0];
286 $bundles[$i]->[0] = strftime("%Y-%m-%d %H:%M:%S %z", localtime($secs));
287 my $sz = $bundles[$i]->[2];
288 1 while $sz =~ s/(?<=\d)(\d{3})(?:,|$)/,$1/g;
289 $bundles[$i]->[2] = $sz;
291 delete $project->{bundles};
292 $project->{bundles} = \@bundles if @bundles;
293 $project->{mirror} = 0 unless $project->{mirror};
294 $project->{is_empty} = 1 if $project->is_empty;
295 delete $project->{showpush} unless $project->{showpush};
296 delete $project->{users} if $project->{mirror};
297 delete $project->{baseurl} unless $project->{mirror};
298 delete $project->{banged} unless $project->{mirror};
299 delete $project->{lastrefresh} unless $project->{mirror};
300 delete $project->{cleanmirror} unless $project->{mirror};
301 delete $project->{statusupdates} unless $project->{mirror};
302 delete $project->{lastparentgc} unless $projname =~ m,/,;
303 unless ($project->{banged}) {
304 delete $project->{bangcount};
305 delete $project->{bangfirstfail};
306 delete $project->{bangmessagesent};
308 my $projhook = $project->_has_notifyhook;
309 if (defined($projhook) && $projhook ne "") {
310 $project->{notifyhook} = $projhook;
311 } else {
312 delete $project->{notifyhook};
314 $project->{README} = get_readme_desc($project->{README}) if exists($project->{README});
315 my @tags = get_ctag_counts($project, 1);
316 $project->{tags} = \@tags if @tags;
317 my $projconfig = read_config_file_hash($project->{path}."/config");
318 if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) {
319 my $ahp = $projconfig->{"core.hookspath"};
320 my $rahp = realpath($ahp);
321 my $lhp = $project->{path}."/hooks";
322 my $rlhp = realpath($lhp);
323 my $ghp = $Girocco::Config::reporoot."/_global/hooks";
324 my $rghp = realpath($ghp);
325 $project->{has_local_hooks} = 1 if
326 defined($rahp) && defined($rlhp) && $rahp eq $rlhp;
327 $project->{has_global_hooks} = 1 if
328 defined($rahp) && defined($rghp) && $rahp eq $rghp;
329 $project->{hookspath} = $ahp unless $ahp eq $lhp || $ahp eq $ghp;
331 $project;
334 sub clean_addrlist {
335 my %seen = ();
336 my @newlist = ();
337 foreach (split(/[,\s]+/, $_[0])) {
338 next unless $_;
339 $seen{lc($_)} = 1, push(@newlist, $_) unless $seen{lc($_)};
341 return join(($_[1]||","), @newlist);
344 sub valid_addrlist {
345 my $cleaned = clean_addrlist(join(" ", @_));
346 return 1 if $cleaned eq "";
347 valid_email_multi($cleaned) && length($cleaned) <= 512;
350 sub validate_users {
351 my ($userlist, $force, $nodie, $quiet) = @_;
352 my @newusers = ();
353 my $badlist = 0;
354 my %seenuser = ();
355 my $mobok = $Girocco::Config::mob && $Girocco::Config::mob eq "mob";
356 my %users = map({($$_[1] => $_)} get_all_users);
357 foreach (split(/[\s,]+/, $userlist)) {
358 if (exists($users{$_}) || $_ eq "everyone" || ($mobok && $_ eq "mob")) {
359 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
360 next;
362 if (Girocco::User::does_exist($_, 1)) {
363 if ($force) {
364 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
365 } else {
366 $badlist = 1;
367 warn "refusing to allow questionable user \"$_\" without --force\n" unless $nodie && $quiet;
369 next;
371 $badlist = 1;
372 warn "invalid user: \"$_\"\n" unless $nodie && $quiet
374 die if $badlist && !$nodie;
375 return @newusers;
378 sub is_default_desc {
379 # "Unnamed repository; edit this file 'description' to name the repository."
380 # "Unnamed repository; edit this file to name it for gitweb."
381 local $_ = shift;
382 return 0 unless defined($_);
383 /Unnamed\s+repository;/i && /\s+edit\s+this\s+file\s+/i && /\s+to\s+name\s+/i;
386 sub valid_desc {
387 my $test = shift;
388 chomp $test;
389 return 0 if $test =~ /[\r\n]/;
390 $test =~ s/\s\s+/ /g;
391 $test =~ s/^\s+//;
392 $test =~ s/\s+$//;
393 return $test ne '';
396 sub clean_desc {
397 my $desc = shift;
398 defined($desc) or $desc = '';
399 chomp $desc;
400 $desc = to_utf8($desc, 1);
401 $desc =~ s/\s\s+/ /g;
402 $desc =~ s/^\s+//;
403 $desc =~ s/\s+$//;
404 return $desc;
407 sub parse_options {
408 Girocco::CLIUtil::_parse_options(
409 sub {
410 warn((($_[0]eq'?')?"unrecognized":"missing argument for")." option \"$_[1]\"\n")
411 unless $quiet;
412 die_usage;
413 }, @_);
416 sub cmd_list {
417 my %sortsub = (
418 lcname => sub {lc($$a[1]) cmp lc($$b[1])},
419 name => sub {$$a[1] cmp $$b[1]},
420 gid => sub {$$a[3] <=> $$b[3]},
421 owner => sub {lc($$a[4]) cmp lc($$b[4]) || lc($$a[1]) cmp lc($$b[1])},
422 no => sub {$$a[0] <=> $$b[0]},
424 my $sortopt = 'lcname';
425 my ($verbose, $owner);
426 parse_options(":sort" => \$sortopt, verbose => \$verbose, owner => \$owner);
427 my $regex;
428 if (@ARGV) {
429 my $val = shift @ARGV;
430 $regex = qr($val) or die "bad regex \"$val\"\n";
432 !@ARGV && exists($sortsub{$sortopt}) or die_usage;
433 my $sortsub = $sortsub{$sortopt};
434 my $grepsub = defined($regex) ? ($owner ? sub {$$_[4] =~ /$regex/} : sub {$$_[1] =~ /$regex/}) : sub {1};
435 my @projects = sort($sortsub grep {&$grepsub} get_all_projects);
436 if ($verbose) {
437 print map(sprintf("%s\n", join(":", (@$_)[1..5])), @projects);
438 } else {
439 print map(sprintf("%s: %s\n", $$_[1], $$_[5] =~ /^:/ ? "<mirror>" : $$_[5]), @projects);
441 return 0;
444 sub cmd_create {
445 my ($force, $noalternates, $orphanok, $optp, $nopasswd, $noowner, $defaults, $ispush, $pushusers,
446 $ismirror, $desc, $fullmirror, $homepage);
447 parse_options(
448 force => \$force, "no-alternates" => \$noalternates, orphan => \$orphanok, p => \$optp,
449 "no-password" => \$nopasswd, "no-owner" => \$noowner, defaults => \$defaults,
450 "push" => \$ispush, ":push" => \$pushusers, ":mirror" => \$ismirror, ":desc" => \$desc,
451 ":description" => \$desc, "full-mirror" => \$fullmirror, ":homepage" => \$homepage);
452 @ARGV == 1 or die_usage;
453 !defined($pushusers) || defined($ispush) or $ispush = 1;
454 defined($ismirror) && $ismirror =~ /^\s*$/ and die "--mirror url must not be empty\n";
455 die "--mirror and --push are mutually exclusive options\n" if $ismirror && $ispush;
456 die "--full-mirror requires use of --mirror=<url> option\n" if $fullmirror && !$ismirror;
457 !$defaults || defined($ispush) || defined($ismirror) or $ispush = 1;
458 !$defaults || defined($nopasswd) or $nopasswd = 1;
459 !$defaults || defined($noowner) or $noowner = 1;
460 !defined($ispush) || defined($pushusers) or $pushusers = "";
461 my $projname = $ARGV[0];
462 $projname =~ s/\.git$//i;
463 Girocco::Project::does_exist($projname, 1) and die "Project already exists: \"$projname\"\n";
464 if (!Girocco::Project::valid_name($projname, $orphanok, $optp)) {
465 warn "Refusing to create orphan project without --orphan\n"
466 if !$quiet && !$orphanok && Girocco::Project::valid_name($projname, 1, 1);
467 warn "Required orphan parent directory does not exist (use -p): ",
468 $Girocco::Config::reporoot.'/'.Girocco::Project::get_forkee_name($projname), "\n"
469 if !$quiet && $orphanok && Girocco::Project::valid_name($projname, 1, 1);
470 die "Invalid project name: \"$projname\"\n";
472 my ($forkee, $project) = ($projname =~ m#^(.*/)?([^/]+)$#);
473 my $newtype = $forkee ? 'fork' : 'project';
474 if (length($project) > 64) {
475 die "The $newtype name is longer than 64 characters. Do you really need that much?\n"
476 unless $force;
477 warn "Allowing $newtype name longer than 64 characters with --force\n" unless $quiet;
479 unless ($Girocco::Config::push || $Girocco::Config::mirror) {
480 die "Project creation disabled (no mirrors or push projects allowed)\n" unless $force;
481 warn "Continuing with --force even though both push and mirror projects are disabled\n" unless $quiet;
483 print "Enter settings for new project \"$projname\"\n" unless $defaults;
484 my %settings = ();
485 $settings{noalternates} = $noalternates;
486 if ($nopasswd) {
487 $settings{crypt} = "unknown";
488 } else {
489 my $np1 = prompt_noecho_nl_or_die("Admin password for project $projname (echo is off)");
490 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
491 my $np2 = prompt_noecho_nl_or_die("Retype admin password for project $projname");
492 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
493 "the admin passwords you have entered do not match each other.\n";
494 $settings{crypt} = scrypt_sha1($np1);
496 my $owner = "";
497 unless ($noowner) {
498 $owner = prompt_or_die("Owner/email name for project $projname");
499 unless (valid_email($owner)) {
500 unless ($force) {
501 warn "Your email sure looks weird...?\n";
502 redo;
504 warn "Allowing invalid email with --force\n" unless $quiet;
506 if (length($owner) > 96) {
507 unless ($force) {
508 warn "Your email is longer than 96 characters. Do you really need that much?\n";
509 redo;
511 warn "Allowing email longer than 96 characters with --force\n" unless $quiet;
514 $settings{email} = $owner;
515 my $baseurl = "";
516 my $checkmirror = sub {
517 my $checkurl = shift;
518 unless (valid_repo_url($checkurl)) {
519 unless ($force) {
520 warn "Invalid mirror URL: \"$checkurl\"\n";
521 return undef;
523 warn "Allowing invalid mirror URL with --force\n" unless $quiet;
525 if ($Girocco::Config::restrict_mirror_hosts) {
526 my $mh = extract_url_hostname($checkurl);
527 unless (is_dns_hostname($mh)) {
528 unless ($force) {
529 warn "Invalid non-DNS mirror URL: \"$checkurl\"\n";
530 return undef;
532 warn "Allowing non-DNS mirror URL with --force\n" unless $quiet;
534 if (is_our_hostname($mh)) {
535 unless ($force) {
536 warn "Invalid same-host mirror URL: \"$checkurl\"\n";
537 return undef;
539 warn "Allowing same-host mirror URL with --force\n" unless $quiet;
542 return $checkurl;
544 if ($ispush || $ismirror) {
545 !$ispush || $force || $Girocco::Config::push or
546 die "Push projects are disabled, create a mirror (or use --force)\n";
547 !$ismirror || $force || $Girocco::Config::mirror or
548 die "Mirror projects are disabled, create a push project (or use --force)\n";
549 if ($ismirror) {
550 &$checkmirror($ismirror) or die "Invalid --mirror URL\n";
551 $baseurl = $ismirror;
552 $settings{url} = $baseurl;
553 $settings{cleanmirror} = $fullmirror ? 0 : 1;
554 } else {
555 my @newusers = ();
556 if ($pushusers !~ /^[\s,]*$/) {
557 eval {@newusers = validate_users($pushusers, $force); 1;} or
558 die "Invalid --push user list\n";
560 $settings{users} = \@newusers;
562 } elsif ($force || $Girocco::Config::mirror) {{
563 if ($force || $Girocco::Config::push) {
564 $baseurl = prompt_or_die("URL to mirror from (leave blank for push project)", "");
565 } else {{
566 $baseurl = prompt_or_die("URL to mirror from");
567 unless ($baseurl ne "") {
568 warn "Push projects are disabled, you must enter a mirror URL (or use --force)\n";
569 redo;
572 if ($baseurl ne "") {
573 &$checkmirror($baseurl) or redo;
574 $settings{url} = $baseurl;
575 $settings{cleanmirror} =
576 ynprompt_or_die("Mirror only heads, tags and notes (Y/n)", "Yes");
579 my $mirror = ($baseurl eq "") ? 0 : 1;
580 my $checkdesc = sub {
581 my $d = shift;
582 if (length($d) > 1024) {
583 unless ($force) {
584 warn "Short description length greater than 1024 characters!\n";
585 return undef;
587 warn "Allowing short description length greater than 1024 characters\n" unless $quiet;
589 return $d;
591 if (defined($desc)) {
592 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
593 $desc eq "" || &$checkdesc($desc) or
594 die "Invalid --desc description\n";
595 } elsif (!$defaults) {
596 $desc = prompt_or_die("Short description", "");
597 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
598 $desc eq "" || &$checkdesc($desc) or redo;
599 $desc = undef if $desc eq "";
601 defined($desc) or $desc = $mirror ? "Mirror of $baseurl" : "Push project $projname";
602 $settings{desc} = $desc;
603 my $checkhp = sub {
604 my $hpurl = shift;
605 unless (valid_web_url($hpurl)) {
606 unless ($force) {
607 warn "Invalid home page URL: \"$hpurl\"\n";
608 return undef;
610 warn "Allowing invalid home page URL with --force\n" unless $quiet;
612 return $hpurl;
614 if (defined($homepage)) {
615 $homepage =~ s/^\s+//; $homepage =~ s/\s+$//;
616 $homepage eq "" || &$checkhp($homepage) or
617 die "Invalid --homepage URL\n";
618 } elsif (!$defaults) {
619 $homepage = prompt_or_die("Home page URL", "");
620 $homepage =~ s/^\s+//; $homepage =~ s/\s+$//;
621 $homepage eq "" || &$checkhp($homepage) or redo;
622 $homepage = undef if $homepage eq "";
624 $settings{hp} = $homepage;
625 my $jsonurl = "";
626 if (!$defaults) {
627 $jsonurl = prompt_or_die("JSON notify POST URL", "");
628 if ($jsonurl ne "" && !valid_web_url($jsonurl)) {
629 unless ($force) {
630 warn "Invalid JSON notify POST URL: \$jsonurl\"\n";
631 redo;
633 warn "Allowing invalid JSON notify POST URL with --force\n" unless $quiet;
636 $settings{notifyjson} = $jsonurl;
637 my $commitaddrs = "";
638 if (!$defaults) {
639 $commitaddrs = clean_addrlist(prompt_or_die("Commit notify email addr(s)", ""));
640 if ($commitaddrs ne "" && !valid_addrlist($commitaddrs)) {
641 unless ($force) {
642 warn"invalid commit notify email address list (use --force to accept): \"$commitaddrs\"\n";
643 redo;
645 warn "using invalid commit notify email address list with --force\n" unless $quiet;
648 $settings{notifymail} = $commitaddrs;
649 $settings{reverseorder} = 1;
650 $settings{reverseorder} = ynprompt_or_die("Oldest-to-newest commit order in emails", "Yes")
651 if !$defaults && $commitaddrs ne "";
652 $settings{summaryonly} = ynprompt_or_die("Summary only (no diff) in emails", "No")
653 if !$defaults && $commitaddrs ne "";
654 my $tagaddrs = "";
655 if (!$defaults) {
656 $tagaddrs = clean_addrlist(prompt_or_die("Tag notify email addr(s)", ""));
657 if ($tagaddrs ne "" && !valid_addrlist($tagaddrs)) {
658 unless ($force) {
659 warn"invalid tag notify email address list (use --force to accept): \"$tagaddrs\"\n";
660 redo;
662 warn "using invalid tag notify email address list with --force\n" unless $quiet;
665 $settings{notifytag} = $tagaddrs;
666 if (!$mirror && !$ispush) {
667 my @newusers = ();
669 my $userlist = prompt_or_die("Push users", join(",", @newusers));
670 eval {@newusers = validate_users($userlist, $force); 1;} or redo;
672 $settings{users} = \@newusers;
674 my $newproj = Girocco::Project->ghost($projname, $mirror, $orphanok, $optp)
675 or die "Girocco::Project->ghost call failed\n";
676 my ($k, $v);
677 $newproj->{$k} = $v while ($k, $v) = each(%settings);
678 my $killowner = sub {
679 system($Girocco::Config::git_bin, '--git-dir='.$newproj->{path},
680 'config', '--unset', "gitweb.owner");
682 if ($mirror) {
683 $newproj->premirror or die "Girocco::Project->premirror failed\n";
684 !$noowner or &$killowner;
685 $newproj->clone or die "Girocco::Project->clone failed\n";
686 warn "Project $projname created and cloning successfully initiated.\n"
687 unless $quiet;
688 } else {
689 $newproj->conjure or die "Girocco::Project->conjure failed\n";
690 !$noowner or &$killowner;
691 warn "New push project fork is empty due to use of --no-alternates\n"
692 if !$quiet && $projname =~ m,/, && $noalternates;
693 warn "Project $projname successfully created.\n" unless $quiet;
695 return 0;
698 sub git_config {
699 my $gd = shift;
700 system($Girocco::Config::git_bin, "--git-dir=$gd", 'config', @_) == 0
701 or die "\"git --git-dir='$gd' config ".join(" ", @_)."\" failed.\n";
704 sub cmd_adopt {
705 my ($force, $type, $nousers, $dryrun, $noowner, $owner, $users, $verbose);
706 parse_options(force => \$force, ":type" => \$type, "no-users" => \$nousers, "dry-run" => \$dryrun,
707 "no-owner" => \$noowner,":owner" => \$owner, quiet => \$quiet, q =>\$quiet, verbose => \$verbose);
708 @ARGV or die "Please give project name on command line.\n";
709 my $projname = shift @ARGV;
710 (!$noowner || !defined($owner)) && (!$nousers || !@ARGV) or die_usage;
711 !defined($type) || $type eq "mirror" || $type eq "push" or die_usage;
712 defined($type) or $type = "";
713 my $projdir;
714 if ($dryrun && $projname =~ m,^/[^.\s/\\:], && is_git_dir(realpath($projname))) {
715 $projdir = realpath($projname);
716 $projname = $projdir;
717 $projname =~ s/\.git$//i;
718 $projname =~ s,/+$,,;
719 $projname =~ s,^.*/,,;
720 $projname ne "" or $projname = $projdir;
721 } else {
722 $projname =~ s/\.git$//i;
723 $projname ne "" or die "Invalid project name \"\".\n";
724 unless (Girocco::Project::does_exist($projname, 1)) {
725 Girocco::Project::valid_name($projname, 1, 1)
726 or die "Invalid project name \"$projname\".\n";
727 die "No such project to adopt: $projname\n";
729 defined(Girocco::Project->load($projname))
730 and die "Project already known (no need to adopt): $projname\n";
731 $projdir = $Girocco::Config::reporoot . "/" . $projname . ".git";
732 is_git_dir($projdir) or die "Not a git directory: \"$projdir\"\n";
734 my $config = read_config_file($projdir . "/config");
735 my %config = ();
736 %config = map {($$_[0], defined($$_[1])?$$_[1]:"true")} @$config if defined($config);
737 git_bool($config{"core.bare"}) or die "Not a bare git repository: \"$projdir\"\n";
738 defined(read_HEAD_symref($projdir)) or die "Project with non-symbolic HEAD ref: \"$projdir\"\n";
739 @ARGV and $users = [validate_users(join(" ", @ARGV), $force, 1, $quiet)];
740 my $desc = "";
741 if (-e "$projdir/description") {
742 open my $fd, '<', "$projdir/description" or die "Cannot open \"$projdir/description\": $!\n";
744 local $/;
745 $desc = <$fd>;
747 close $fd;
748 defined $desc or $desc = "";
749 chomp $desc;
750 $desc = to_utf8($desc, 1);
751 is_default_desc($desc) and $desc = "";
752 if ($desc ne "" && !valid_desc($desc)) {
753 die "invalid 'description' file contents (use --force to accept): \"$desc\"\n"
754 unless $force;
755 warn "using invalid 'description' file contents with --force\n" unless $quiet;
757 $desc = clean_desc($desc);
758 if (length($desc) > 1024) {
759 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
760 unless $force;
761 warn "using longer than 1024 char description with --force\n" unless $quiet;
764 my $readme = "";
765 if (-e "$projdir/README.html") {
766 open my $fd, '<', "$projdir/README.html" or die "Cannot open \"$projdir/README.html\": $!\n";
768 local $/;
769 $readme = <$fd>;
771 close $fd;
772 defined $readme or $readme = "";
773 $readme = to_utf8($readme, 1);
774 $readme =~ s/\r\n?/\n/gs;
775 $readme =~ s/^\s+//s;
776 $readme =~ s/\s+$//s;
777 $readme eq "" or $readme .= "\n";
778 if (length($readme) > 8192) {
779 die "readme greater than 8192 chars is too long (use --force to override)\n"
780 unless $force;
781 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
783 my $rd = get_readme_desc($readme);
784 if ($rd ne "automatic" && $rd ne "suppressed") {
785 my $xmllint = qx(sh -c 'command -v xmllint'); chomp $xmllint;
786 if (-f $xmllint && -x $xmllint) {
787 my $dummy = {README => $readme};
788 my ($cnt, $err) = Girocco::Project::_lint_readme($dummy, 0);
789 if ($cnt) {
790 my $msg = "xmllint: $cnt error";
791 $msg .= "s" unless $cnt == 1;
792 print STDERR "$msg\n", "-" x length($msg), "\n", $err
793 unless $force && $quiet;
794 exit(255) unless $force;
795 warn "$projname: using invalid raw HTML with --force\n" unless $quiet;
797 } else {
798 die "xmllint not available, refusing to use raw HTML without --force\n"
799 unless $force;
800 warn "xmllint not available using unchecked raw HTML with --force\n" unless $quiet;
804 # Inspect any remotes now
805 # Yes, Virginia, remote urls can be multi-valued
806 my %remotes = ();
807 foreach (@$config) {
808 my ($k,$v) = @$_;
809 next unless $k =~ /^remote\.([^\/].*?)\.([^.]+)$/; # remote name cannot start with "/"
810 my ($name, $subkey) = ($1, $2);
811 $remotes{$name}->{skip} = git_bool($v,1), next if $subkey eq "skipdefaultupdate" || $subkey eq "skipfetchall";
812 $remotes{$name}->{mirror} = git_bool($v,1), next if $subkey eq "mirror"; # we might want this
813 $remotes{$name}->{vcs} = $v, next if defined($v) && $v !~ /^\s*$/ && $subkey eq "vcs";
814 push(@{$remotes{$name}->{$subkey}}, $v), next if defined($v) && $v !~ /^\s*$/ &&
815 ($subkey eq "url" || $subkey eq "fetch" || $subkey eq "push" || $subkey eq "pushurl");
817 # remotes.default is the default remote group to fetch for "git remote update" otherwise --all
818 # the remote names in a group are separated by runs of [ \t\n] characters
819 # remote names "", ".", ".." and any name starting with "/" are invalid
820 # a remote with no url or vcs setting is not considered valid
821 my @check = ();
822 my $usingall = 0;
823 if (exists($config{"remotes.default"})) {
824 foreach (split(/[ \t\n]+/, $config{"remotes.default"})) {
825 next unless exists($remotes{$_});
826 my $rmt = $remotes{$_};
827 next if !exists($rmt->{url}) && !$rmt->{vcs};
828 push(@check, $_);
830 } else {
831 $usingall = 1;
832 my %seenrmt = ();
833 foreach (@$config) {
834 my ($k,$v) = @$_;
835 next unless $k =~ /^remote\.([^\/].*?)\.[^.]+$/;
836 next if $seenrmt{$1};
837 $seenrmt{$1} = 1;
838 next unless exists($remotes{$1});
839 my $rmt = $remotes{$1};
840 next if $rmt->{skip} || (!exists($rmt->{url}) && !$rmt->{vcs});
841 push(@check, $1);
844 my @needskip = (); # remotes that need skipDefaultUpdate set to true
845 my $foundvcs = 0;
846 my $foundfetch = 0;
847 my $foundfetchwithmirror = 0;
848 foreach (@check) {
849 my $rmt = $remotes{$_};
850 push(@needskip, $_) if $usingall && !exists($rmt->{fetch});
851 next unless exists($rmt->{fetch});
852 ++$foundfetch;
853 ++$foundfetchwithmirror if $rmt->{mirror};
854 ++$foundvcs if $rmt->{vcs} || (exists($rmt->{url}) && $rmt->{url}->[0] =~ /^[a-zA-Z0-9][a-zA-Z0-9+.-]*::/);
856 # if we have $foundvcs then we need to explicitly set fetch.prune to false
857 # if we have $foundfetch > 1 then we need to explicitly set fetch.prune to false
858 my $neednoprune = !exists($config{"fetch.prune"}) && ($foundvcs || $foundfetch > 1);
859 my $baseurl = "";
860 my $needfakeorigin = 0; # if true we need to set remote.origin.skipDefaultUpdate = true
861 # if remote "origin" exists we always pick up its first url or use ""
862 if (exists($remotes{origin})) {
863 my $rmt = $remotes{origin};
864 $baseurl = exists($rmt->{url}) ? $rmt->{url}->[0] : "";
865 $needfakeorigin = !exists($rmt->{url}) && !$rmt->{vcs} && !$rmt->{skip};
866 } else {
867 $needfakeorigin = 1;
868 # get the first url of the @check remotes
869 foreach (@check) {
870 my $rmt = $remotes{$_};
871 next unless exists($rmt->{url});
872 next unless defined($rmt->{url}->[0]) && $rmt->{url}->[0] ne "";
873 $baseurl = $rmt->{url}->[0];
874 last;
877 my $makemirror = $type eq "mirror" || ($type eq "" && $foundfetch);
879 # If we have $foundfetch we want to make a mirror but complain if
880 # we $foundfetchwithmirror as well unless we have --type=mirror.
881 # Warn if we have --type=push and $foundfetch and !$foundfetchwithmirror.
882 # Warn if we need to set fetch.prune=false when making a mirror
883 # Warn if we need to create remote.origin.skipDefaultUpdate when making a mirror
884 # Complain if @needskip AND !$usingall (warn with --force but don't set skip)
885 # Warn if $usingall and any @needskip (and set them) if making a mirror
886 # Warn if making a mirror and $baseurl eq ""
887 # Warn if we have --type=mirror and !$foundfetch
889 if ($makemirror) {
890 warn "No base URL to mirror from for adopted \"$projname\"\n" unless $quiet || $baseurl ne "";
891 warn "Adopting mirror \"$projname\" without any fetch remotes\n" unless $quiet || $foundfetch;
892 if ($foundfetchwithmirror) {
893 warn "Refusing to adopt mirror \"$projname\" with active remote.<name>.mirror=true remote(s)\n".
894 "(Use --type=mirror to override)\n"
895 unless $type eq "mirror";
896 exit(255) unless $type eq "mirror" || $dryrun;
897 warn "Adopting mirror \"$projname\" with active remote.<name>.mirror=true remotes\n"
898 unless $quiet || $type ne "mirror";
900 warn "Setting explicit fetch.prune=false for adoption of mirror \"$projname\"\n"
901 if !$quiet && $neednoprune;
902 warn "Setting remote.origin.skipDefaultUpdate=true for adoption of mirror \"$projname\"\n"
903 if !$quiet && $needfakeorigin;
904 if (!$usingall && @needskip) {
905 warn "Refusing to adopt mirror empty fetch remote(s) (override with --force)\n"
906 unless $force;
907 exit(255) unless $force || $dryrun;
908 warn "Adopting mirror with empty fetch remote(s) with --force\n"
909 unless $quiet || !$force;
911 warn "Will set skipDefaultUpdate=true on non-fetch remote(s)\n" if !$quiet && $usingall && @needskip;
912 warn "Adopting mirror with base URL \"$baseurl\"\n" unless $quiet || $baseurl eq "";
913 } else {
914 warn "Adopting push \"$projname\" but active non-mirror remotes are present\n"
915 if !$quiet && $foundfetch && !$foundfetchwithmirror;
918 if (!$noowner && !defined($owner)) {
919 # Select the owner
920 $owner = $config{"gitweb.owner"};
921 if (!defined($owner) || $owner eq "") {
922 $owner = $Girocco::Config::admin;
923 warn "Using owner \"$owner\" for adopted project\n" unless $quiet;
926 if (!$nousers && !$makemirror && !defined($users)) {
927 # select user list for push project
928 my $findowner = $owner;
929 defined($findowner) or $findowner = $config{"gitweb.owner"};
930 $findowner = lc($findowner) if defined($findowner);
931 my @owner_users = ();
932 @owner_users = map {$findowner eq lc($$_[4]) ? $$_[1] : ()} get_all_users
933 if defined($findowner) && $findowner ne "";
934 defined($findowner) or $findowner = "";
935 if (@owner_users <= 1) {
936 $users = \@owner_users;
937 warn "No users found that match owner \"$findowner\"\n" unless @owner_users || $quiet;
938 } else {
939 $users = [];
940 warn "Found ".scalar(@owner_users)." users for owner \"$findowner\" (" .
941 join(" ", @owner_users) . ") not setting any\n" unless $quiet;
944 defined($users) or $users = [];
946 # Warn if we preserve an existing receive.denyNonFastForwards or receive.denyDeleteCurrent setting
947 # Complain if core.logallrefupdates or logs subdir exists and contains any files (allow with --force
948 # and warn about preserving the setting)
950 warn "Preserving existing receive.denyNonFastForwards=true\n"
951 if !$quiet && git_bool($config{"receive.denynonfastforwards"});
952 warn "Preserving existing receive.denyDeleteCurrent=$config{'receive.denydeletecurrent'}\n"
953 if !$quiet && exists($config{"receive.denydeletecurrent"}) &&
954 $config{"receive.denydeletecurrent"} ne "warn";
956 my $reflogfiles = Girocco::Project::_contains_files("$projdir/logs");
957 my $reflogactive = git_bool($config{"core.logallrefupdates"});
958 if ($reflogactive || $reflogfiles) {
959 warn "Refusing to adopt \"$projname\" with active ref logs without --force\n" if $reflogfiles && !$force;
960 warn "Refusing to adopt \"$projname\" with core.logAllRefUpdates=true without --force\n" if $reflogactive && !$force;
961 exit(255) unless $force || $dryrun;
962 warn "Adopting \"$projname\" with active ref logs with --force\n" unless $quiet || ($reflogfiles && !$force);
963 warn "Adopting \"$projname\" with core.logAllRefUpdates=true with --force\n" unless $quiet || ($reflogactive && !$force);
966 return 0 if $dryrun && !$verbose;
968 my $newproj = eval {Girocco::Project->ghost($projname, $makemirror, 1, $dryrun)};
969 defined($newproj) or die "Girocco::Project::ghost failed: $@\n";
970 $newproj->{desc} = $desc;
971 $newproj->{README} = $readme;
972 $newproj->{url} = $baseurl if $makemirror || exists($config{"gitweb.baseurl"});
973 $newproj->{email} = $owner if defined($owner);
974 $newproj->{users} = $users;
975 $newproj->{crypt} = "unknown";
976 $newproj->{reverseorder} = 1 unless exists($config{"hooks.reverseorder"});
977 $newproj->{summaryonly} = 1 unless exists($config{"hooks.summaryonly"});
978 my $dummy = bless {}, "Girocco::Project";
979 $dummy->{path} = "$projdir";
980 $dummy->{configfilehash} = \%config;
981 $dummy->_properties_load;
982 delete $dummy->{origurl};
983 foreach my $k (keys(%$dummy)) {
984 $newproj->{$k} = $dummy->{$k}
985 if exists($dummy->{$k}) && !exists($newproj->{$k});
988 if ($verbose) {
989 use Data::Dumper;
990 my %info = %$newproj;
991 $info{README} = get_readme_desc($info{README}) if exists($info{README});
992 my $d = Data::Dumper->new([\%info], ['*'.$newproj->{name}]);
993 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
994 print $d->Dump([\%info], ['*'.$newproj->{name}]);
996 return 0 if $dryrun;
998 # Make any changes as needed for @needskip, $neednoprune and $needfakeorigin
999 if ($makemirror) {
1000 git_config($projdir, "fetch.prune", "false") if $neednoprune;
1001 git_config($projdir, "remote.origin.skipDefaultUpdate", "true") if $needfakeorigin;
1002 if ($usingall && @needskip) {
1003 git_config($projdir, "remote.$_.skipDefaultUpdate", "true") foreach @needskip;
1007 # Perform the actual adoption
1008 $newproj->adopt or die "Girocco::Project::adopt failed\n";
1010 # Perhaps restore core.logAllRefUpdates, receive.denyNonFastForwards and receive.denyDeleteCurrent
1011 git_config($projdir, "receive.denyNonFastForwards", "true")
1012 if git_bool($config{"receive.denynonfastforwards"});
1013 git_config($projdir, "receive.denyDeleteCurrent", $config{"receive.denydeletecurrent"})
1014 if exists($config{"receive.denydeletecurrent"}) &&
1015 $config{"receive.denydeletecurrent"} ne "warn";
1016 git_config($projdir, "core.logAllRefUpdates", "true")
1017 if $reflogactive;
1019 # Success
1020 if ($makemirror) {
1021 warn "Mirror project \"$projname\" successfully adopted.\n" unless $quiet;
1022 } else {
1023 warn "Push project \"$projname\" successfully adopted.\n" unless $quiet;
1025 return 0;
1028 sub cmd_remove {
1029 my ($force, $reallydel, $keepforks);
1030 parse_options(force => \$force, "really-delete" => \$reallydel,
1031 "keep-forks" => \$keepforks, quiet => \$quiet, q =>\$quiet);
1032 @ARGV or die "Please give project name on command line.\n";
1033 @ARGV == 1 or die_usage;
1034 my $project = get_project($ARGV[0]);
1035 my $projname = $project->{name};
1036 my $isempty = !$project->{mirror} && $project->is_empty;
1037 if (!$project->{mirror} && !$isempty && $reallydel) {
1038 die "refusing to remove and delete non-empty push project without --force: $projname\n" unless $force;
1039 warn "allowing removal and deletion of non-empty push project with --force\n" unless $quiet;
1041 my $altwarn;
1042 my $removenogc;
1043 if ($project->has_forks) {
1044 die "refusing to remove project with forks (use --keep-forks): $projname\n" unless $keepforks;
1045 warn "allowing removal of forked project while preserving its forks with --keep-forks\n" unless $quiet;
1046 # Run pseudo GC on that repository so that objects don't get lost within forks
1047 my $basedir = $Girocco::Config::basedir;
1048 my $projdir = $project->{path};
1049 warn "We have to run pseudo GC on the repo so that the forks don't lose data. Hang on...\n" unless $quiet;
1050 my $nogcrunning = sub {
1051 die "Error: GC appears to be currently running on $projname\n"
1052 if -e "$projdir/gc.pid" || -e "$projdir/.gc_in_progress";
1054 &$nogcrunning;
1055 $removenogc = ! -e "$projdir/.nogc";
1056 recreate_file("$projdir/.nogc") if $removenogc;
1057 die "unable to create \"$projdir/.nogc\"\n" unless -e "$projdir/.nogc";
1058 delete $ENV{show_progress};
1059 $ENV{'show_progress'} = 1 unless $quiet;
1060 sleep 2; # *cough*
1061 &$nogcrunning;
1062 system("$basedir/toolbox/perform-pre-gc-linking.sh", "--include-packs", $projname) == 0
1063 or die "Running pseudo GC on project $projname failed\n";
1064 $altwarn = 1;
1066 my $archived;
1067 if (!$project->{mirror} && !$isempty && !$reallydel) {
1068 $archived = $project->archive_and_delete;
1069 unlink("$archived/.nogc") if $removenogc && defined($archived) && $archived ne "";
1070 } else {
1071 $project->delete;
1073 warn "Project '$projname' removed from $Girocco::Config::name" .
1074 ($archived ? ", backup in '$archived'" : "") .".\n" unless $quiet;
1075 warn "Retained forks may now have unwanted objects/info/alternates lines\n" if $altwarn && !$quiet;
1076 return 0;
1079 sub cmd_show {
1080 use Data::Dumper;
1081 @ARGV == 1 or die_usage;
1082 my $project = get_clean_project($ARGV[0]);
1083 my %info = %$project;
1084 my $d = Data::Dumper->new([\%info], ['*'.$project->{name}]);
1085 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
1086 print $d->Dump([\%info], ['*'.$project->{name}]);
1087 return 0;
1090 sub cmd_listheads {
1091 @ARGV == 1 or die_usage;
1092 my $project = get_project($ARGV[0]);
1093 my @heads = sort({lc($a) cmp lc($b)} $project->get_heads);
1094 my $cur = $project->{HEAD};
1095 defined($cur) or $cur = '';
1096 my $curmark = '*';
1097 my $headhash = get_git("--git-dir=$project->{path}", 'rev-parse', '--quiet', '--verify', 'HEAD');
1098 defined($headhash) or $headhash = '';
1099 chomp $headhash;
1100 $headhash or $curmark = '!';
1101 foreach (@heads) {
1102 my $mark = $_ eq $cur ? $curmark : ' ';
1103 print "$mark $_\n";
1105 return 0;
1108 sub cmd_listtags {
1109 my $vcnt = 0;
1110 shift(@ARGV), $vcnt=1 if @ARGV && ($ARGV[0] eq '--verbose' || $ARGV[0] eq '-v');
1111 @ARGV == 1 or die_usage;
1112 my $project = get_project($ARGV[0]);
1113 if ($vcnt) {
1114 print map("$$_[0]\t$$_[1]\n", get_ctag_counts($project));
1115 } else {
1116 print map("$_\n", $project->get_ctag_names);
1118 return 0;
1121 sub cmd_deltags {
1122 my $ic = 0;
1123 shift(@ARGV), $ic=1 if @ARGV && $ARGV[0] =~ /^(?:--?ignore-case|-i)$/i;
1124 @ARGV >= 2 or die_usage;
1125 my $project = get_project(shift @ARGV);
1126 my %curtags;
1127 if ($ic) {
1128 push(@{$curtags{lc($_)}}, $_) foreach $project->get_ctag_names;
1129 } else {
1130 push(@{$curtags{$_}}, $_) foreach $project->get_ctag_names;
1132 my @deltags = ();
1133 my %seentag = ();
1134 my $ctags = join(" ", @ARGV);
1135 $ctags = lc($ctags) if $ic;
1136 foreach (split(/[\s,]+/, $ctags)) {
1137 next unless exists($curtags{$_});
1138 $seentag{$_}=1, push(@deltags, $_) unless $seentag{$_};
1140 if (!@deltags) {
1141 warn $project->{name}, ": skipping removal of only non-existent tags\n" unless $quiet;
1142 } else {
1143 # Avoid touching anything other than the ctags
1144 foreach my $tg (@deltags) {
1145 $project->delete_ctag($_) foreach @{$curtags{$tg}};
1147 $project->_set_changed;
1148 $project->_set_forkchange;
1149 warn $project->{name}, ": specified tags have been removed\n" unless $quiet;
1151 return 0;
1154 sub cmd_addtags {
1155 @ARGV >= 2 or die_usage;
1156 my $project = get_project(shift @ARGV);
1157 my $ctags = join(" ", @ARGV);
1158 $ctags =~ /[^, a-zA-Z0-9:.+#_-]/ and
1159 die "Content tag(s) \"$ctags\" contain(s) evil character(s).\n";
1160 my $oldmask = umask();
1161 umask($oldmask & ~0060);
1162 my $changed = 0;
1163 foreach (split(/[\s,]+/, $ctags)) {
1164 ++$changed if $project->add_ctag($_, 1);
1166 if ($changed) {
1167 $project->_set_changed;
1168 $project->_set_forkchange;
1170 umask($oldmask);
1171 my $cnt = ($changed == 1) ? "1 content tag has" : $changed . " content tags have";
1172 warn $project->{name}, ": $cnt been added/updated\n" unless $quiet;
1173 return 0;
1176 sub _get_random_val {
1177 my $p = shift;
1178 my $md5;
1180 no warnings;
1181 $md5 = md5_hex(time . $$ . rand() . join(':',%$p));
1183 $md5;
1186 sub cmd_chpass {
1187 my $force = 0;
1188 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1189 my $random = undef;
1190 pop(@ARGV), $random=lc($ARGV[1]) if @ARGV==2 && $ARGV[1] =~ /^(?:random|unknown)$/i;
1191 @ARGV == 1 or die_usage;
1192 my $project = get_project($ARGV[0]);
1193 die "refusing to change locked password of project \"$ARGV[0]\" without --force\n"
1194 if $project->is_password_locked;
1195 my ($newpw, $rmsg);
1196 if ($random) {
1197 if ($random eq "random") {
1198 die "refusing to set random password without --force\n" unless $force;
1199 $rmsg = "set to random value";
1200 $newpw = _get_random_val($project);
1201 } else {
1202 die "refusing to set password hash to '$random' without --force\n" unless $force;
1203 $rmsg = "hash set to '$random'";
1204 $newpw = $random;
1206 } else {
1207 $rmsg = "updated";
1208 if (-t STDIN) {
1209 print "Changing admin password for project $ARGV[0]\n";
1210 my $np1 = prompt_noecho_nl_or_die("New password for project $ARGV[0] (echo is off)");
1211 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1212 my $np2 = prompt_noecho_nl_or_die("Retype new password for project $ARGV[0]");
1213 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
1214 "the admin passwords you have entered do not match each other.\n";
1215 $newpw = $np1;
1216 } else {
1217 $newpw = <STDIN>;
1218 defined($newpw) or die "missing new password on STDIN\n";
1219 chomp($newpw);
1222 $newpw ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1223 my $old = $project->{crypt};
1224 $project->{crypt} = (defined($random) && $random ne "random") ? $newpw : scrypt_sha1($newpw);
1225 if (defined($old) && $old eq $project->{crypt}) {
1226 warn $project->{name}, ": skipping update of password hash to same value\n" unless $quiet;
1227 } else {
1228 # Avoid touching anything other than the password hash
1229 $project->_group_update;
1230 warn $project->{name}, ": admin password $rmsg (new hash stored)\n" unless $quiet;
1232 return 0;
1235 sub cmd_checkpw {
1236 @ARGV == 1 or die_usage;
1237 my $project = get_project($ARGV[0]);
1238 my $pwhash = $project->{crypt};
1239 defined($pwhash) or $pwhash = "";
1240 if ($pwhash eq "") {
1241 warn $project->{name}, ": no password required\n" unless $quiet;
1242 return 0;
1244 if ($project->is_password_locked) {
1245 warn $project->{name}, ": password is locked\n" unless $quiet;
1246 exit 1;
1248 my $checkpw;
1249 if (-t STDIN) {
1250 $checkpw = prompt_noecho_nl_or_die("Admin password for project $ARGV[0] (echo is off)");
1251 $checkpw ne "" or warn "checking for empty password as hash (very unlikely)\n" unless $quiet;
1252 } else {
1253 $checkpw = <STDIN>;
1254 defined($checkpw) or die "missing admin password on STDIN\n";
1255 chomp($checkpw);
1257 unless (Girocco::CLIUtil::check_passwd_match($pwhash, $checkpw)) {
1258 warn "password check failure\n" unless $quiet;
1259 exit 1;
1261 warn "admin password match\n" unless $quiet;
1262 return 0;
1265 sub cmd_gc {
1266 my ($force, $auto, $redelta, $recompress);
1267 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, auto => \$auto,
1268 redelta => \$redelta, "no-reuse-delta" => \$redelta, aggressive => \$force,
1269 recompress => \$recompress, "no-reuse-object" => $recompress);
1270 $force && $auto and die "--force and --auto are mutually exclusive options\n";
1271 @ARGV or die "Please give project name on command line.\n";
1272 @ARGV == 1 or die_usage;
1273 my $project = get_project($ARGV[0]);
1274 delete $ENV{show_progress};
1275 delete $ENV{force_gc};
1276 $quiet or $ENV{"show_progress"} = 1;
1277 $force and $ENV{"force_gc"} = 1;
1278 if (!$auto && !$force && ! -e $project->{path}."/.needsgc") {
1279 open NEEDSGC, '>', $project->{path}."/.needsgc" and close NEEDSGC;
1281 my @args = ($Girocco::Config::basedir . "/jobd/gc.sh", $project->{name});
1282 $redelta && !$recompress and push(@args, "-f");
1283 $recompress and push(@args, "-F");
1284 my $lastgc = $project->{lastgc};
1285 system({$args[0]} @args) != 0 and return 1;
1286 # Do it again Sam, but only if lastgc was set, gc.sh succeeded and now it's not set
1287 if ($lastgc) {
1288 my $newlastgc = get_git("--git-dir=$project->{path}", 'config', '--get', 'gitweb.lastgc');
1289 if (!$newlastgc) {
1290 system({$args[0]} @args) != 0 and return 1;
1293 return 0;
1296 sub cmd_update {
1297 my ($force, $summary);
1298 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, summary => \$summary);
1299 $quiet && $summary and die "--quiet and --summary are mutually exclusive options\n";
1300 @ARGV or die "Please give project name on command line.\n";
1301 @ARGV == 1 or die_usage;
1302 my $project = get_project($ARGV[0]);
1303 $project->{mirror} or die "Project \"$ARGV[0]\" is a push project, not a mirror project.\n";
1304 delete $ENV{show_progress};
1305 delete $ENV{force_update};
1306 if ($quiet) {
1307 $ENV{"show_progress"} = 0;
1308 } else {
1309 $ENV{"show_progress"} = ($summary ? 1 : 2);
1311 $force and $ENV{"force_update"} = 1;
1312 system($Girocco::Config::basedir . "/jobd/update.sh", $project->{name}) != 0 and return 1;
1313 return 0;
1316 sub cmd_remirror {
1317 my $force = 0;
1318 parse_options(force => \$force, quiet => \$quiet, q => \$quiet);
1319 @ARGV or die "Please give project name on command line.\n";
1320 @ARGV == 1 or die_usage;
1321 my $project = get_project($ARGV[0]);
1322 $project->{mirror} or die "Project \"$ARGV[0]\" is a push project, not a mirror project.\n";
1323 if ($project->{clone_in_progress} && !$project->{clone_failed}) {
1324 warn "Project \"$ARGV[0]\" already seems to have a clone underway at this moment.\n" unless $quiet && $force;
1325 exit(255) unless $force;
1326 yes_to_continue_or_die("Are you sure you want to force a remirror");
1328 unlink($project->_clonefail_path);
1329 unlink($project->_clonelog_path);
1330 recreate_file($project->_clonep_path);
1331 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or
1332 die "cannot connect to taskd.socket: $!\n";
1333 select((select($sock),$|=1)[0]);
1334 $sock->print("clone ".$project->{name}."\n");
1335 # Just ignore reply, we are going to succeed anyway and the I/O
1336 # would apparently get quite hairy.
1337 $sock->flush();
1338 sleep 2; # *cough*
1339 $sock->close();
1340 warn "Project \"$ARGV[0]\" remirror initiated.\n" unless $quiet;
1341 return 0;
1344 sub cmd_setowner {
1345 my $force = 0;
1346 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1347 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1348 my $project = get_project($ARGV[0]);
1349 if (@ARGV == 2 && !valid_email($ARGV[1])) {
1350 die "invalid owner/email (use --force to accept): \"$ARGV[1]\"\n"
1351 unless $force;
1352 warn "using invalid owner/email with --force\n" unless $quiet;
1354 if (@ARGV == 2 && length($ARGV[1]) > 96) {
1355 die "owner/email longer than 96 chars (use --force to accept): \"$ARGV[1]\"\n"
1356 unless $force;
1357 warn "using longer than 96 char owner/email with --force\n" unless $quiet;
1359 my $old = $project->{email};
1360 if (@ARGV == 1) {
1361 print "$old\n" if defined($old);
1362 return 0;
1364 if (defined($old) && $old eq $ARGV[1]) {
1365 warn $project->{name}, ": skipping update of owner/email to same value\n" unless $quiet;
1366 } else {
1367 # Avoid touching anything other than "gitweb.owner"
1368 $project->_property_fput("email", $ARGV[1]);
1369 $project->_update_index;
1370 $project->_set_changed;
1371 warn $project->{name}, ": owner/email updated to \"$ARGV[1]\"\n" unless $quiet;
1373 return 0;
1376 sub cmd_setdesc {
1377 my $force = 0;
1378 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1379 @ARGV >= 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1380 my $project = get_project(shift @ARGV);
1381 if (@ARGV && !valid_desc(join(" ", @ARGV))) {
1382 die "invalid description (use --force to accept): \"".join(" ", @ARGV)."\"\n"
1383 unless $force;
1384 warn "using invalid description with --force\n" unless $quiet;
1386 my $desc = clean_desc(join(" ", @ARGV));
1387 if (@ARGV && length($desc) > 1024) {
1388 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
1389 unless $force;
1390 warn "using longer than 1024 char description with --force\n" unless $quiet;
1392 my $old = $project->{desc};
1393 if (!@ARGV) {
1394 print "$old\n" if defined($old);
1395 return 0;
1397 if (defined($old) && $old eq $desc) {
1398 warn $project->{name}, ": skipping update of description to same value\n" unless $quiet;
1399 } else {
1400 # Avoid touching anything other than description file
1401 $project->_property_fput("desc", $desc);
1402 $project->_set_changed;
1403 warn $project->{name}, ": description updated to \"$desc\"\n" unless $quiet;
1405 return 0;
1408 sub cmd_setreadme {
1409 my $force = 0;
1410 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1411 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1412 my $project = get_project($ARGV[0]);
1413 my $old = $project->{README};
1414 if (@ARGV == 1) {
1415 chomp $old if defined($old);
1416 print "$old\n" if defined($old) && $old ne "";
1417 return 0;
1419 my ($new, $raw, $newname);
1420 $newname = '';
1421 if ($ARGV[1] eq "-") {
1422 local $/;
1423 $new = <STDIN>;
1424 $raw = 1;
1425 $newname = "contents of <STDIN>";
1426 } elsif (lc($ARGV[1]) eq "automatic" || lc($ARGV[1]) eq "auto") {
1427 $new = "";
1428 } elsif (lc($ARGV[1]) eq "suppressed" || lc($ARGV[1]) eq "suppress") {
1429 $new = "<!-- suppress -->";
1430 } else {
1431 my $fn = $ARGV[1];
1432 $fn =~ s/^\@//;
1433 die "missing filename for README\n" unless $fn ne "";
1434 die "no such file: \"$fn\"\n" unless -f $fn && -r $fn;
1435 open F, '<', $fn or die "cannot open \"$fn\" for reading: $!\n";
1436 local $/;
1437 $new = <F>;
1438 close F;
1439 $raw = 1;
1440 $newname = "contents of \"$fn\"";
1442 defined($new) or $new = '';
1443 $project->{README} = to_utf8($new, 1);
1444 $project->_cleanup_readme;
1445 if (length($project->{README}) > 8192) {
1446 die "readme greater than 8192 chars is too long (use --force to override)\n"
1447 unless $force;
1448 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
1450 if ($raw) {
1451 my $rd = get_readme_desc($project->{README});
1452 if ($rd ne "automatic" && $rd ne "suppressed") {
1453 my $xmllint = qx(command -v xmllint); chomp $xmllint;
1454 if (-f $xmllint && -x $xmllint) {
1455 my ($cnt, $err) = $project->_lint_readme(0);
1456 if ($cnt) {
1457 my $msg = "xmllint: $cnt error";
1458 $msg .= "s" unless $cnt == 1;
1459 print STDERR "$msg\n", "-" x length($msg), "\n", $err
1460 unless $force && $quiet;
1461 exit(255) unless $force;
1462 warn $project->{name} . ": using invalid raw HTML with --force\n" unless $quiet;
1464 } else {
1465 die "xmllint not available, refusing to use raw HTML without --force\n"
1466 unless $force;
1467 warn "xmllint not available using unchecked raw HTML with --force\n" unless $quiet;
1471 if (defined($old) && $old eq $project->{README}) {
1472 warn $project->{name}, ": skipping update of README to same value\n" unless $quiet;
1473 } else {
1474 # Avoid touching anything other than README.html file
1475 $project->_property_fput("README", $project->{README});
1476 $project->_set_changed;
1477 my $desc = get_readme_desc($project->{README});
1478 if ($newname) {
1479 $newname .= " ($desc)";
1480 } else {
1481 $newname = $desc;
1483 warn $project->{name}, ": README updated to $newname\n" unless $quiet;
1485 return 0;
1488 sub valid_head {
1489 my ($proj, $newhead) = @_;
1490 my %okheads = map({($_ => 1)} $proj->get_heads);
1491 exists($okheads{$newhead});
1494 sub cmd_sethead {
1495 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1496 my $project = get_project($ARGV[0]);
1497 if (@ARGV == 2 && !valid_head($project, $ARGV[1])) {
1498 die "invalid head (try \"@{[basename($0)]} listheads $ARGV[0]\"): \"$ARGV[1]\"\n";
1500 my $old = $project->{HEAD};
1501 if (@ARGV == 1) {
1502 print "$old\n" if defined($old);
1503 return 0;
1505 if (defined($old) && $old eq $ARGV[1]) {
1506 warn $project->{name}, ": skipping update of HEAD symref to same value\n" unless $quiet;
1507 } else {
1508 # Avoid touching anything other than the HEAD symref
1509 $project->set_HEAD($ARGV[1]);
1510 $project->_set_changed;
1511 warn $project->{name}, ": HEAD symref updated to \"refs/heads/$ARGV[1]\"\n" unless $quiet;
1513 return 0;
1516 sub cmd_sethooks {
1517 my $force = 0;
1518 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1519 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1520 my $project = get_project($ARGV[0]);
1521 my $projconfig = read_config_file_hash($project->{path}."/config");
1522 my $ghp = $Girocco::Config::reporoot."/_global/hooks";
1523 my $rghp = realpath($ghp);
1524 my $lhp = $project->{path}."/hooks";
1525 my $rlhp = realpath($lhp);
1526 my $ahp = "";
1527 my $rahp = undef;
1528 if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) {
1529 $ahp = $projconfig->{"core.hookspath"};
1530 $rahp = realpath($ahp);
1532 if (@ARGV == 1) {
1533 if (defined($rahp) && $rahp ne "") {
1534 if ($rahp eq $rghp) {
1535 my $nc = ($ahp eq $ghp ? "" : " non-canonical");
1536 printf "%s \t(global%s)\n", $ahp, $nc;
1537 } elsif ($rahp eq $rlhp) {
1538 my $nc = ($ahp eq $lhp ? "" : " non-canonical");
1539 printf "%s \t(local%s)\n", $ahp, $nc;
1540 } elsif ($rahp ne $ahp) {
1541 print "$ahp \t($rahp)\n";
1542 } else {
1543 print "$ahp\n";
1545 } elsif ($ahp ne "") {
1546 print "$ahp \t(non-existent)\n";
1548 return 0;
1550 my $shp = $ARGV[1];
1551 if (lc($shp) eq "global") {
1552 $shp = $ghp;
1553 } elsif (lc($shp) eq "local") {
1554 $shp = $lhp;
1555 } elsif (substr($shp, 0, 2) eq "~/") {
1556 $shp = $ENV{"HOME"}.substr($shp,1);
1557 } elsif ($shp =~ m,^~([a-zA-Z_][a-zA-Z_0-9]*)((?:/.*)?)$,) {
1558 my $sfx = $2;
1559 my $hd = (getpwnam($1))[7];
1560 $shp = $hd . $sfx if defined($hd) && $hd ne "" && $hd ne "/" && -d $hd;
1562 $shp ne "" && -d $shp or die "no such directory: $ARGV[1]\n";
1563 my $rshp = realpath($shp);
1564 defined($rshp) && $rshp ne "" or die "could not realpath: $ARGV[1]\n";
1565 $rshp =~ m,^/[^/], or die "invalid hookspath: $rshp\n";
1566 die "refusing to switch from current non-global hookspath without --force\n"
1567 if !$force && defined($rahp) && $rahp ne "" && $rahp ne $rghp && $rshp ne $rahp;
1568 if (!$force && defined($rahp) && $rahp ne "") {
1569 if ($rshp eq $rahp && ($ahp eq $ghp || $ahp eq $lhp)) {
1570 warn $project->{name}, ": skipping update of hookspath to same effective value\n" unless $quiet;
1571 return 0;
1574 $rshp = $ghp if $rshp eq $rghp;
1575 $rshp = $lhp if $rshp eq $rlhp;
1576 if ($rshp eq $ahp) {
1577 warn $project->{name}, ": skipping update of hookspath to same value\n" unless $quiet;
1578 return 0;
1580 die "refusing to set neither local nor global hookspath without --force\n"
1581 if !$force && $rshp ne $ghp && $rshp ne $lhp;
1582 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1583 'config', "core.hookspath", $rshp);
1584 my $newval = '"'.$rshp.'"';
1585 $newval = "global" if $rshp eq $ghp;
1586 $newval = "local" if $rshp eq $lhp;
1587 warn $project->{name}, ": hookspath set to $newval\n" unless $quiet;
1588 return 0;
1591 our %boolfields;
1592 BEGIN {
1593 %boolfields = (
1594 cleanmirror => 1,
1595 reverseorder => 0,
1596 summaryonly => 0,
1597 statusupdates => 1,
1601 sub cmd_setbool {
1602 my $force = 0;
1603 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1604 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1605 my $project = get_project($ARGV[0]);
1606 if (!exists($boolfields{$ARGV[1]})) {
1607 die "invalid boolean field name: \"$ARGV[1]\" -- try \"help\"\n";
1609 if (@ARGV == 3 && $boolfields{$ARGV[1]} && !$project->{mirror}) {
1610 die "invalid boolean field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
1611 unless $force;
1612 warn "using mirror field on non-mirror with --force\n" unless $quiet;
1614 if (@ARGV == 3 && !valid_bool($ARGV[2])) {
1615 die "invalid boolean value: \"$ARGV[2]\"\n";
1617 my $bool = clean_bool($ARGV[2]);
1618 my $old = $project->{$ARGV[1]};
1619 if (@ARGV == 2) {
1620 print "$old\n" if defined($old);
1621 return 0;
1623 if (defined($old) && $old eq $bool) {
1624 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
1625 } else {
1626 # Avoid touching anything other than $ARGV[1] field
1627 $project->_property_fput($ARGV[1], $bool);
1628 warn $project->{name}, ": $ARGV[1] updated to $bool\n" unless $quiet;
1630 return 0;
1633 sub cmd_setjsontype {
1634 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1635 my $project = get_project($ARGV[0]);
1636 my $jsontype;
1637 if (@ARGV == 2) {
1638 my $jt = lc($ARGV[1]);
1639 index($jt, "/") >= 0 or $jt = "application/".$jt;
1640 $jt eq 'application/x-www-form-urlencoded' ||
1641 $jt eq 'application/json' or
1642 die "invalid jsontype value: \"$ARGV[1]\"\n";
1643 $jsontype = $jt;
1645 my $old = $project->{jsontype};
1646 if (@ARGV == 1) {
1647 print "$old\n" if defined($old);
1648 return 0;
1650 if (defined($old) && $old eq $jsontype) {
1651 warn $project->{name}, ": skipping update of jsontype to same value\n" unless $quiet;
1652 } else {
1653 # Avoid touching anything other than jsontype field
1654 $project->_property_fput('jsontype', $jsontype);
1655 warn $project->{name}, ": jsontype updated to $jsontype\n" unless $quiet;
1657 return 0;
1660 sub cmd_setjsonsecret {
1661 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1662 my $project = get_project($ARGV[0]);
1663 my $jsonsecret;
1664 if (@ARGV == 2) {
1665 my $js = $ARGV[1];
1666 $js =~ s/^\s+//; $js =~ s/\s+$//;
1667 $jsonsecret = $js;
1669 my $old = $project->{jsonsecret};
1670 if (@ARGV == 1) {
1671 print "$old\n" if defined($old);
1672 return 0;
1674 if (defined($old) && $old eq $jsonsecret) {
1675 warn $project->{name}, ": skipping update of jsonsecret to same value\n" unless $quiet;
1676 } else {
1677 # Avoid touching anything other than jsonsecret field
1678 $project->_property_fput('jsonsecret', $jsonsecret);
1679 warn $project->{name}, ": jsonsecret updated to \"$jsonsecret\"\n" unless $quiet;
1681 return 0;
1684 sub cmd_setautogchack {
1685 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1686 my $project = get_project($ARGV[0]);
1687 my $aghok = $Girocco::Config::autogchack &&
1688 ($project->{mirror} || $Girocco::Config::autogchack ne "mirror");
1689 my $old = defined($project->{autogchack}) ? clean_bool($project->{autogchack}) : "unset";
1690 if (@ARGV == 1) {
1691 print "$old\n" if $aghok;
1692 return 0;
1694 my $bool;
1695 if (lc($ARGV[1]) eq "unset") {
1696 $bool = "unset";
1697 } else {
1698 valid_bool($ARGV[1]) or die "invalid boolean value: \"$ARGV[1]\"\n";
1699 $bool = clean_bool($ARGV[1]);
1701 if (!$aghok) {
1702 die "\$Girocco::Config::autogchack is false\n" unless $Girocco::Config::autogchack;
1703 die "\$Girocco::Config::autogchack is only enabled for mirrors\n";
1705 if ($old eq $bool) {
1706 warn $project->{name}, ": autogchack value unchanged\n" unless $quiet;
1707 } else {
1708 if ($bool eq "unset") {
1709 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1710 'config', '--unset', "girocco.autogchack");
1711 } else {
1712 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1713 'config', '--bool', "girocco.autogchack", $bool);
1716 return system($Girocco::Config::basedir . "/jobd/maintain-auto-gc-hack.sh", $project->{name}) == 0
1717 ? 0 : 1;
1720 sub valid_url {
1721 my ($url, $type) = @_;
1722 $type ne 'baseurl' and return valid_web_url($url);
1723 valid_repo_url($url) or return 0;
1724 if ($Girocco::Config::restrict_mirror_hosts) {
1725 my $mh = extract_url_hostname($url);
1726 is_dns_hostname($mh) or return 0;
1727 !is_our_hostname($mh) or return 0;
1729 return 1;
1732 our %urlfields;
1733 BEGIN {
1734 %urlfields = (
1735 baseurl => ["url" , 1],
1736 homepage => ["hp" , 0],
1737 notifyjson => ["notifyjson", 0],
1741 sub cmd_seturl {
1742 my $force = 0;
1743 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1744 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1745 my $project = get_project($ARGV[0]);
1746 if (!exists($urlfields{$ARGV[1]})) {
1747 die "invalid URL field name: \"$ARGV[1]\" -- try \"help\"\n";
1749 if (@ARGV == 3 && ${$urlfields{$ARGV[1]}}[1] && !$project->{mirror}) {
1750 die "invalid URL field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
1751 unless $force;
1752 warn "using mirror field on non-mirror with --force\n" unless $quiet;
1754 if (@ARGV == 3 && !valid_url($ARGV[2], $ARGV[1])) {
1755 die "invalid URL (use --force to accept): \"$ARGV[2]\"\n"
1756 unless $force;
1757 warn "using invalid URL with --force\n" unless $quiet;
1759 my $old = $project->{${$urlfields{$ARGV[1]}}[0]};
1760 if (@ARGV == 2) {
1761 print "$old\n" if defined($old);
1762 return 0;
1764 if (defined($old) && $old eq $ARGV[2]) {
1765 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
1766 } else {
1767 # Avoid touching anything other than $ARGV[1]'s field
1768 $project->_property_fput(${$urlfields{$ARGV[1]}}[0], $ARGV[2]);
1769 if ($ARGV[1] eq "baseurl") {
1770 $project->{url} = $ARGV[2];
1771 $project->_set_bangagain;
1773 $project->_set_changed unless $ARGV[1] eq "notifyjson";
1774 warn $project->{name}, ": $ARGV[1] updated to $ARGV[2]\n" unless $quiet;
1776 return 0;
1779 our %msgsfields;
1780 BEGIN {
1781 %msgsfields = (
1782 notifymail => 1,
1783 notifytag => 1,
1787 sub cmd_setmsgs {
1788 my $force = 0;
1789 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1790 @ARGV >= 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1791 my $project = get_project(shift @ARGV);
1792 my $field = shift @ARGV;
1793 if (!exists($msgsfields{$field})) {
1794 die "invalid msgs field name: \"$field\" -- try \"help\"\n";
1796 if (@ARGV && !valid_addrlist(@ARGV)) {
1797 die "invalid email address list (use --force to accept): \"".join(" ",@ARGV)."\"\n"
1798 unless $force;
1799 warn "using invalid email address list with --force\n" unless $quiet;
1801 my $old = $project->{$field};
1802 if (!@ARGV) {
1803 printf "%s\n", clean_addrlist($old, " ") if defined($old);
1804 return 0;
1806 my $newlist = clean_addrlist(join(" ",@ARGV));
1807 if (defined($old) && $old eq $newlist) {
1808 warn $project->{name}, ": skipping update of $field to same value\n" unless $quiet;
1809 } else {
1810 # Avoid touching anything other than $field's field
1811 $project->_property_fput($field, $newlist);
1812 warn $project->{name}, ": $field updated to \"$newlist\"\n" unless $quiet;
1814 return 0;
1817 sub cmd_setusers {
1818 my $force = 0;
1819 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1820 @ARGV >= 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1821 my $project = get_project(shift @ARGV);
1822 my $projname = $project->{name};
1823 !@ARGV || !$project->{mirror} or die "cannot set users list for mirror project: \"$projname\"\n";
1824 my @newusers = ();
1825 if (@ARGV) {
1826 eval {@newusers = validate_users(join(" ", @ARGV), $force); 1;} or exit 255;
1827 die "refusing to set empty users list without --force\n" unless @newusers || $force;
1829 return 0 if !@ARGV && $project->{mirror};
1830 my $oldusers = $project->{users};
1831 if ($oldusers && ref($oldusers) eq "ARRAY") {
1832 $oldusers = join("\n", @$oldusers);
1833 } else {
1834 $oldusers = "";
1836 if (!@ARGV) {
1837 print "$oldusers\n" if $oldusers ne "";
1838 return 0;
1840 if ($oldusers eq join("\n", @newusers)) {
1841 warn "$projname: skipping update of users list to same value\n" unless $quiet;
1842 } else {
1843 # Avoid touching anything other than the users list
1844 $project->{users} = \@newusers;
1845 $project->_update_users;
1846 warn "$projname: users list updated to \"@{[join(',',@newusers)]}\"\n" unless $quiet;
1848 return 0;
1851 our %fieldnames;
1852 BEGIN {
1853 %fieldnames = (
1854 owner => [\&cmd_setowner, 0],
1855 desc => [\&cmd_setdesc, 0],
1856 description => [\&cmd_setdesc, 0],
1857 readme => [\&cmd_setreadme, 0],
1858 head => [\&cmd_sethead, 0],
1859 HEAD => [\&cmd_sethead, 0],
1860 hooks => [\&cmd_sethooks, 0],
1861 hookspath => [\&cmd_sethooks, 0],
1862 cleanmirror => [\&cmd_setbool, 1],
1863 reverseorder => [\&cmd_setbool, 1],
1864 summaryonly => [\&cmd_setbool, 1],
1865 statusupdates => [\&cmd_setbool, 1],
1866 autogchack => [\&cmd_setautogchack, 0],
1867 baseurl => [\&cmd_seturl, 1],
1868 homepage => [\&cmd_seturl, 1],
1869 notifyjson => [\&cmd_seturl, 1],
1870 jsontype => [\&cmd_setjsontype, 0],
1871 jsonsecret => [\&cmd_setjsonsecret, 0],
1872 notifymail => [\&cmd_setmsgs, 1],
1873 notifytag => [\&cmd_setmsgs, 1],
1874 users => [\&cmd_setusers, 0],
1878 sub do_getset {
1879 $setopt = shift;
1880 my @newargs = ();
1881 push(@newargs, shift) if @_ && $_[0] eq '--force';
1882 my $field = $_[1];
1883 (($setopt && @_ >= 3) || @_ == 2) && exists($fieldnames{$field}) or die_usage;
1884 push(@newargs, shift);
1885 shift unless ${$fieldnames{$field}}[1];
1886 push(@newargs, @_);
1887 diename(($setopt ? "set " : "get ") . $field);
1888 @ARGV = @newargs;
1889 &{${$fieldnames{$field}}[0]}(@ARGV);
1892 sub cmd_get {
1893 do_getset(0, @_);
1896 sub cmd_set {
1897 do_getset(1, @_);
1900 our %commands;
1901 BEGIN {
1902 %commands = (
1903 list => \&cmd_list,
1904 create => \&cmd_create,
1905 adopt => \&cmd_adopt,
1906 remove => \&cmd_remove,
1907 trash => \&cmd_remove,
1908 delete => \&cmd_remove,
1909 show => \&cmd_show,
1910 listheads => \&cmd_listheads,
1911 listtags => \&cmd_listtags,
1912 listctags => \&cmd_listtags,
1913 deltags => \&cmd_deltags,
1914 delctags => \&cmd_deltags,
1915 addtags => \&cmd_addtags,
1916 addctags => \&cmd_addtags,
1917 chpass => \&cmd_chpass,
1918 checkpw => \&cmd_checkpw,
1919 gc => \&cmd_gc,
1920 update => \&cmd_update,
1921 remirror => \&cmd_remirror,
1922 setowner => \&cmd_setowner,
1923 setdesc => \&cmd_setdesc,
1924 setdescription => \&cmd_setdesc,
1925 setreadme => \&cmd_setreadme,
1926 sethead => \&cmd_sethead,
1927 sethooks => \&cmd_sethooks,
1928 sethookspath => \&cmd_sethooks,
1929 setbool => \&cmd_setbool,
1930 setboolean => \&cmd_setbool,
1931 setflag => \&cmd_setbool,
1932 setautogchack => \&cmd_setautogchack,
1933 seturl => \&cmd_seturl,
1934 setjsontype => \&cmd_setjsontype,
1935 setjsonsecret => \&cmd_setjsonsecret,
1936 setmsgs => \&cmd_setmsgs,
1937 setusers => \&cmd_setusers,
1938 get => \&cmd_get,
1939 set => \&cmd_set,
1943 sub dohelp {
1944 my $cmd = shift;
1945 my $bn = basename($0);
1946 printf "%s version %s\n\n", $bn, $VERSION;
1947 if (defined($cmd) && $cmd ne '') {
1948 $cmd =~ s/^set(?=[a-zA-Z])//i;
1949 my $cmdhelp = '';
1950 my ($lastmt, $incmd);
1951 foreach (split('\n', sprintf($help, $bn))) {
1952 $lastmt || $incmd or $lastmt = /^\s*$/, next;
1953 $incmd = 1 if $lastmt && /^\s*(?:\[?set\]?)?$cmd\s/;
1954 last if $incmd && /^\s*$/;
1955 $incmd and $cmdhelp .= $_ . "\n";
1956 $lastmt = /^\s*$/;
1958 print $cmdhelp and exit 0 if $cmdhelp;
1960 printf $help, $bn;
1961 exit 0;
1964 sub main {
1965 local *ARGV = \@_;
1966 shift, $quiet=1 if @ARGV && $ARGV[0] =~ /^(?:-q|--quiet)$/i;
1967 dohelp($ARGV[1]) if !@ARGV || @ARGV && $ARGV[0] =~ /^(?:-h|-?-help|help)$/i;
1968 my $command = shift;
1969 diename($command);
1970 $setopt = 1;
1971 if (!exists($commands{$command}) && exists($commands{"set".$command})) {
1972 $setopt = 0;
1973 $command = "set" . $command;
1975 exists($commands{$command}) or die "Unknown command \"$command\" -- try \"help\"\n";
1976 dohelp($command) if @ARGV && ($ARGV[0] =~ /^(?:-h|-?-help)$/i ||
1977 $ARGV[0] =~ /^help$/i && !Girocco::Project::does_exist("help",1));
1978 &{$commands{$command}}(@ARGV);