sanity-check.pl: detect .delaygc non-empty projects
[girocco/readme.git] / toolbox / projtool.pl
blobba2d37c6c68a5de2a1159d2fbc8d35ec0a8b1d02
1 #!/usr/bin/perl
3 # projtool.pl - command line Girocco project maintenance tool
4 # Copyright (C) 2016,2017,2020,2021 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.8'}
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);
20 my $origHOME;
21 BEGIN {$origHOME = $ENV{HOME}}
23 use lib "__BASEDIR__";
24 use Girocco::Config;
25 use Girocco::Util;
26 use Girocco::HashUtil;
27 use Girocco::CLIUtil;
28 use Girocco::Project;
29 use Girocco::User;
31 exit(&main(@ARGV)||0);
33 our $help;
34 BEGIN {my @a; /^(.*)$/s && push(@a, $1) foreach @ARGV; @ARGV=@a;}
35 BEGIN {$help = <<'HELP'}
36 Usage: %s [<global option>...] <command> <options>
38 global options:
39 -q | --quiet suppress warning messages
40 -p | --pager force output to be paginated
41 --no-pager never paginate output
43 Note that as a convenience, where an existing <project> is required as
44 an argument, a path to the project may be given instead of the name in
45 most places. The "remove" and "prune" commands only accept names.
46 Since a matching project name takes precedence over a path, to force
47 interpretation as a path, start the path with "/" or "./" or "../".
48 Giving "." will find the project matching the current working directory.
49 Non-destructive commands that take only a <project> name will attempt to
50 use "." as the project name if it has been omitted.
52 help [<command>]
53 show full help or just for <command> if given
55 list [--verbose] [--sort=lcname|name|owner|gid|no] [--owner] [<regexp>]
56 list all projects (default is --sort=lcname)
57 limit to project names matching <regex> if given
58 match <regex> against owner instead of project name with --owner
60 create [--force] [--no-alternates] [--orphan] [<option>...] <project>
61 create new project <project> (prompted)
62 <option> can be:
63 --no-alternates skip setup of objects/info/alternates
64 --orphan allow creation of subproject w/o a parent
65 -p use mkdir -p during --orphan creation
66 --no-password set password crypt to invalid value "unknown"
67 --no-owner leave the gitweb.owner config unset
68 --mirror=<url> create a mirror from <url>
69 --full-mirror mirror all refs
70 --push[=<list>] create a push project
71 --desc=<string> specify project description w/o prompt
72 --homepage=<url> specify project homepage URL w/o prompt
73 --defaults do no interactive prompting at all
74 Using --no-password skips the prompts for password, using
75 --no-owner skips the prompt for owner and using --mirror=<url>
76 or --push[=<list>] skips the prompts for mirror URL and
77 heads-only and push users. With --defaults if neither
78 --mirror=<url> nor --push[=<list>] is given then --push will
79 be implied. Using --desc=<string> will force a specific
80 description (including an empty string) and skip the prompt for
81 it. Otherwise a non-empty default description will always be
82 supplied in lieu of an empty or omitted description.
84 adopt [--force] [--type=mirror|push] [<option>...] <project> [<users>]
85 adopt project <project>
86 type of project is guessed if --type=<type> omitted
87 <users> is same as <newuserslist> for setusers command
88 <option> can be:
89 --dry-run do all the checks but don't perform adoption
90 --verbose show project info dump (useful with --dry-run)
91 --no-users no push users at all (<users> must be omitted)
92 --no-owner leave the gitweb.owner config totally unchanged
93 --owner=<val> set the gitweb.owner config to <val>
94 Both --no-owner and --owner=<val> may NOT be given, with neither
95 take owner from preexisting gitweb.owner else use admin setting.
96 For mirrors <users> is ignored otherwise if no <users> and no
97 --no-users option the push users list will consist of the single
98 user name matching the owner or empty if none or more than one.
99 With --dry-run <project> can be an absolute path to a git dir.
101 remove [--force] [--really-delete] [--keep-forks] <project>
102 remove project <project>
103 do not move to _recyclebin with --really-delete (just rm -rf)
104 remove projects with forks (by keeping forks) using --keep-forks
106 prune [--quiet] (--force | --dry-run) [<project>...]
107 check to see if any projects (default is all projects) are
108 missing the associated project directory on disk.
109 Requires either --force or --dry-run option to operate.
110 With --dry-run only show what would be done.
111 With --prune actually remove any extraneous project(s).
112 With --dry-run exit code is non-zero if any action needed.
113 With --quiet, suppress output message, if any.
115 show <project>
116 show project <project>
118 verify [--quiet] [--dir] <project>
119 show the canonical project name for <project> (which might
120 be a path) if and only if it exists. With --dir show the
121 canonical full path to the project directory instead.
122 If the project name is invalid or does not exist, display
123 an error (unless --quiet is used). Exit status will be 0
124 if project found, non-zero otherwise.
126 worktree[s] [--force] <project> [<git worktree arguments>]
127 run 'git --git-dir=<project-git-dir> worktree <worktree args>'
128 except that if <git worktree arguments> consists of the
129 subcommand "add" and a single non-option argument and the
130 <project>'s git-dir is bare (the usual case) and the <project>'s
131 HEAD branch is not already checked out in any other worktree,
132 then suitable arguments will be passed to the `worktree`
133 command to make the newly created worktree checkout the HEAD
134 branch of the project (special logic makes that work even
135 for an unborn HEAD).
136 With --force allow running on a mirror project
137 With no <git worktree arguments> use "list"
139 urls [--push] <project>
140 show available fetch/push URLs for <project>
141 Note that this does NOT include non-Git protocol URLs such
142 as any home page or any upstream URL for a mirror project --
143 those are all accessible via the "show" command.
144 The URLs shown are those that would be shown by gitweb.cgi.
145 With --push only show push urls (mirrors have no push urls)
147 listheads <project>
148 list all available heads for <project> and indicate current head
150 listtags [--verbose] <project>
151 list all ctags on project <project>
152 with --verbose include tag counts
154 deltags <project> [-i] <tagstodel>
155 remove any ctags on project <project> present in <tagstodel>
156 <tagstodel> is space or comma separated list of tags to remove
157 with -i match against <tagstodel> without regard to letter case
159 addtags <project> <tagstoadd>
160 add ctags to project <project>
161 <tagstoadd> is space or comma separated list of tags to add
163 chpass [--force] <project> [random | unknown]
164 change project <project> password (prompted)
165 with "random" set to random password
166 with "unknown" set password hash to invalid value "unknown"
168 checkpw <project>
169 check project <project> password for a match (prompted)
171 gc [--force | --auto] [--redelta | --recompress] <project>
172 run the gc.sh script on project <project>
173 with --auto let the gc.sh script decide what to do
174 with --force cause a full gc to take place (force_gc=1)
175 with neither --auto nor --force do a mini or if needed a full gc
176 (in other words just touch .needsgc and run gc.sh)
177 with --redelta a full gc will use pack-objects --no-reuse-delta
178 with --recompress a full gc uses pack-objects --no-reuse-object
179 (--no-reuse-delta and --no-reuse-object are accepted as aliases)
180 with --aggressive activate the --force and --redelta options
181 unless the global --quiet option is given show_progress=1 is used
183 update [--force] [--quiet | --summary] <project>
184 run the update.sh script on project <project>
185 with --force cause a fetch to always take place (force_update=1)
186 with --quiet only show errors (show_progress is left unset)
187 with --summary show progress and ref summary (show_progress=1)
188 with neither --quiet nor --summary show it all (show_progress=2)
190 remirror [--force] <project>
191 initiate a remirror of project <project>
193 [set]owner [--force] <project> <newowner>
194 set project <project> owner to <newowner>
195 without "set" and only 1 arg, just show current project owner
197 [set]desc [--force] <project> <newdesc>
198 set project <project> description to <newdesc>
199 without "set" and only 1 arg, just show current project desc
201 [set]readme [--force] [--format=<readmetype>] <project> [<newsetting>]
202 set project <project> readme to <newsetting>
203 <readmetype> is markdown|plain|html (default is no change)
204 <newsetting> is automatic|suppressed|-|[@]filename
205 with "set" <readmetype> and/or <newsetting> is required
206 without "set" and only 1 arg, just show current readme setting
208 [set]head [--force] <project> <newhead>
209 set project <project> HEAD symbolic ref to <newhead>
210 without "set" and only 1 arg, just show current project HEAD
212 [set]bool [--force] <project> <flagname> <boolvalue>
213 set project <project> boolean <flagname> to <boolvalue>
214 <flagname> is cleanmirror|reverseorder|summaryonly|statusupdtaes
215 without "set" and only 2 args, just show current flag value
217 [set]hooks [--force] <project> local | global | <path>
218 set project <project> hookspath to local, global or <path>
219 without "set" and only 1 arg, just show current hookspath
221 [set]autogchack <project> <boolvalue> | unset
222 set project <project> autogchack to <boolvalue> or "unset" it
223 without "set" just show current autogchack setting if enabled
224 with "set" autogchack must be enabled in Config.pm for the
225 type of project and maintain-auto-gc-hack.sh is always run
227 [set]url [--force] <project> <urlname> <newurlvalue>
228 set project <project> url <urlname> to <newurlvalue>
229 <urlname> is baseurl|homepage|notifyjson
230 without "set" and only 2 args, just show current url value
232 [set]msgs [--force] <project> <msgsname> <eaddrlist>
233 set project <project> msgs <msgsname> to <addrlist>
234 <msgsname> is notifymail|notifytag
235 <eaddrlist> is space or comma separated list of email addresses
236 without "set" and only 2 args, just show current msgs value
238 [set]users [--force] <project> <newuserslist>
239 set push project <project> users list to <newuserslist>
240 <newuserslist> is space or comma separated list of user names
241 without "set" and only 1 arg, just show current users list
243 [set]jsontype <project> <newjsontype>
244 set project <project> JSON Content-Type to <newjsontype>
245 <newjsontype> is x-www-form-urlencoded or json
246 without "set" and only 1 arg, just show current jsontype
248 [set]jsonsecret <project> <newjsonsecret>
249 set project <project> JSON secret to <newjsonsecret>
250 <newjsonsecret> is a string (empty string disables signatures)
251 without "set" and only 1 arg, just show current jsonsecret
253 get <project> <fieldname>
254 show project <project> field <fieldname>
255 <fieldname> is owner|desc|readme|head|hooks|users|jsontype
256 or jsonsecret|<flagname>|autogchack|<urlname>|<msgsname>
258 set [--force] <project> <fieldname> <newfieldvalue>
259 set project <project> field <fieldname> to <newfieldvalue>
260 <fieldname> same as for get
261 <newfieldvalue> same as for corresponding set... command
262 HELP
264 our $quiet;
265 our $usepager;
266 our $setopt;
267 sub die_usage {
268 my $sub = shift || diename;
269 if ($sub) {
270 die "Invalid arguments to $sub command -- try \"help\"\n";
271 } else {
272 die "Invalid arguments -- try \"help\"\n";
276 sub get_readme_len {
277 my $rm = shift;
278 defined($rm) or $rm = '';
279 return "length " . length($rm);
282 sub get_readme_desc {
283 my $rm = shift;
284 defined($rm) or $rm = '';
285 if (length($rm)) {
286 my $test = $rm;
287 $test =~ s/<!--(?:[^-]|(?:-(?!-)))*-->//gs;
288 $test =~ s/\s+//s;
289 return $test eq '' ? "suppressed" : "length " . length($rm);
290 } else {
291 return "automatic";
295 sub get_ctag_counts {
296 my $project = shift;
297 my $compact = shift;
298 my @ctags = ();
299 foreach ($project->get_ctag_names) {
300 my $val = 0;
301 my $ct;
302 if (open $ct, '<', $project->{path}."/ctags/$_") {
303 my $count = <$ct>;
304 close $ct;
305 defined $count or $count = '';
306 chomp $count;
307 $val = $count =~ /^[1-9]\d*$/ ? $count : 1;
309 if ($compact) {
310 if ($val == 1) {
311 push(@ctags, $_);
312 } elsif ($val > 1) {
313 push(@ctags, $_."(".$val.")");
315 } else {
316 push(@ctags, [$_, $val]) if $val;
319 @ctags;
322 sub reftype {
323 use Scalar::Util ();
324 return ref($_[0]) ? Scalar::Util::reftype($_[0]) : ''
327 sub get_project_harder_gently {
328 defined($_[0]) && return get_project_harder(@_);
329 my $proj = eval { get_project_harder(".") };
330 reftype($proj) eq 'HASH' or $proj = undef;
331 return $proj;
334 sub get_clean_project_gently {
335 defined($_[0]) && return get_clean_project(@_);
336 my $proj = eval { get_clean_project(".") };
337 reftype($proj) eq 'HASH' or $proj = undef;
338 return $proj;
341 sub get_clean_project {
342 my $project = get_project_harder(@_);
343 delete $project->{loaded};
344 delete $project->{base_path};
345 delete $project->{ccrypt};
346 /^orig/i || !defined($project->{$_}) and delete $project->{$_} foreach keys %$project;
347 $project->{owner} = $project->{email}; delete $project->{email};
348 $project->{homepage} = $project->{hp}; delete $project->{hp};
349 $project->{baseurl} = $project->{url}; delete $project->{url};
350 if (defined($project->{path}) && $project->{path} ne "") {
351 my $rp = realpath($project->{path});
352 defined($rp) && $rp ne "" and $project->{realpath} = $rp;
353 if (-f "$rp/objects/info/packs") {
354 my $ipt = (stat _)[9];
355 defined($ipt) and $project->{infopackstime} =
356 strftime("%Y-%m-%d %H:%M:%S %z", localtime($ipt));
359 my $owner = $project->{owner};
360 if ($owner) {
361 $owner = lc($owner);
362 my @owner_users = map {$owner eq lc($$_[4]) ? $$_[1] : ()} get_all_users;
363 $project->{owner_users} = \@owner_users if @owner_users;
365 my $projname = $project->{name};
366 my @forks = grep {$$_[1] =~ m,^$projname/,} get_all_projects;
367 $project->{has_forks} = 1 if @forks;
368 $project->{has_alternates} = 1 if $project->has_alternates;
369 my @bundles = $project->bundles;
370 for (my $i = 0; $i < @bundles; ++$i) {
371 my $secs = $bundles[$i]->[0];
372 $bundles[$i]->[0] = strftime("%Y-%m-%d %H:%M:%S %z", localtime($secs));
373 my $sz = $bundles[$i]->[2];
374 1 while $sz =~ s/(?<=\d)(\d{3})(?:,|$)/,$1/g;
375 $bundles[$i]->[2] = $sz;
377 delete $project->{bundles};
378 $project->{bundles} = \@bundles if @bundles;
379 $project->{mirror} = 0 unless $project->{mirror};
380 $project->{is_empty} = 1 if $project->is_empty;
381 delete $project->{showpush} unless $project->{showpush};
382 delete $project->{users} if $project->{mirror};
383 delete $project->{baseurl} unless $project->{mirror};
384 delete $project->{banged} unless $project->{mirror};
385 delete $project->{lastrefresh} unless $project->{mirror};
386 delete $project->{cleanmirror} unless $project->{mirror};
387 delete $project->{statusupdates} unless $project->{mirror};
388 delete $project->{lastparentgc} unless $projname =~ m,/,;
389 unless ($project->{banged}) {
390 delete $project->{bangcount};
391 delete $project->{bangfirstfail};
392 delete $project->{bangmessagesent};
394 my $projhook = $project->_has_notifyhook;
395 if (defined($projhook) && $projhook ne "") {
396 $project->{notifyhook} = $projhook;
397 } else {
398 delete $project->{notifyhook};
400 $project->{README} = get_readme_desc($project->{README}) if exists($project->{README});
401 $project->{READMEDATA} = get_readme_len($project->{READMEDATA}) if exists($project->{READMEDATA});
402 my @tags = get_ctag_counts($project, 1);
403 $project->{tags} = \@tags if @tags;
404 my $projconfig = read_config_file_hash($project->{path}."/config");
405 if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) {
406 my $ahp = $projconfig->{"core.hookspath"};
407 my $rahp = realpath($ahp);
408 my $lhp = $project->{path}."/hooks";
409 my $rlhp = realpath($lhp);
410 my $ghp = $Girocco::Config::reporoot."/_global/hooks";
411 my $rghp = realpath($ghp);
412 $project->{has_local_hooks} = 1 if
413 defined($rahp) && defined($rlhp) && $rahp eq $rlhp;
414 $project->{has_global_hooks} = 1 if
415 defined($rahp) && defined($rghp) && $rahp eq $rghp;
416 $project->{hookspath} = $ahp unless $ahp eq $lhp || $ahp eq $ghp;
418 $project;
421 sub clean_addrlist {
422 my %seen = ();
423 my @newlist = ();
424 foreach (split(/[,\s]+/, $_[0])) {
425 next unless $_;
426 $seen{lc($_)} = 1, push(@newlist, $_) unless $seen{lc($_)};
428 return join(($_[1]||","), @newlist);
431 sub valid_addrlist {
432 my $cleaned = clean_addrlist(join(" ", @_));
433 return 1 if $cleaned eq "";
434 valid_email_multi($cleaned) && length($cleaned) <= 512;
437 sub validate_users {
438 my ($userlist, $force, $nodie) = @_;
439 my @newusers = ();
440 my $badlist = 0;
441 my %seenuser = ();
442 my $mobok = $Girocco::Config::mob && $Girocco::Config::mob eq "mob";
443 my %users = map({($$_[1] => $_)} get_all_users);
444 foreach (split(/[\s,]+/, $userlist)) {
445 if (exists($users{$_}) || $_ eq "everyone" || ($mobok && $_ eq "mob")) {
446 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
447 next;
449 if (Girocco::User::does_exist($_, 1)) {
450 if ($force) {
451 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
452 } else {
453 $badlist = 1;
454 warn "refusing to allow questionable user \"$_\" without --force\n" unless $nodie && $quiet;
456 next;
458 $badlist = 1;
459 warn "invalid user: \"$_\"\n" unless $nodie && $quiet
461 die if $badlist && !$nodie;
462 return @newusers;
465 sub is_default_desc {
466 # "Unnamed repository; edit this file 'description' to name the repository."
467 # "Unnamed repository; edit this file to name it for gitweb."
468 local $_ = shift;
469 return 0 unless defined($_);
470 /Unnamed\s+repository;/i && /\s+edit\s+this\s+file\s+/i && /\s+to\s+name\s+/i;
473 sub valid_desc {
474 my $test = shift;
475 chomp $test;
476 return 0 if $test =~ /[\r\n]/;
477 $test =~ s/\s\s+/ /g;
478 $test =~ s/^\s+//;
479 $test =~ s/\s+$//;
480 return $test ne '';
483 sub clean_desc {
484 my $desc = shift;
485 defined($desc) or $desc = '';
486 chomp $desc;
487 $desc = to_utf8($desc, 1);
488 $desc =~ s/\s\s+/ /g;
489 $desc =~ s/^\s+//;
490 $desc =~ s/\s+$//;
491 return $desc;
494 sub parse_options {
495 Girocco::CLIUtil::_parse_options(
496 sub {
497 warn((($_[0]eq'?')?"unrecognized":"missing argument for")." option \"$_[1]\"\n")
498 unless $quiet;
499 die_usage;
500 }, @_);
503 sub cmd_list {
504 my %sortsub = (
505 lcname => sub {lc($$a[1]) cmp lc($$b[1])},
506 name => sub {$$a[1] cmp $$b[1]},
507 gid => sub {$$a[3] <=> $$b[3]},
508 owner => sub {lc($$a[4]) cmp lc($$b[4]) || lc($$a[1]) cmp lc($$b[1])},
509 no => sub {$$a[0] <=> $$b[0]},
511 my $sortopt = 'lcname';
512 my ($verbose, $owner);
513 parse_options(":sort" => \$sortopt, verbose => \$verbose, owner => \$owner);
514 my $regex;
515 if (@ARGV) {
516 my $val = shift @ARGV;
517 $regex = qr($val) or die "bad regex \"$val\"\n";
519 !@ARGV && exists($sortsub{$sortopt}) or die_usage;
520 my $sortsub = $sortsub{$sortopt};
521 my $grepsub = defined($regex) ? ($owner ? sub {$$_[4] =~ /$regex/} : sub {$$_[1] =~ /$regex/}) : sub {1};
522 my @projects = sort($sortsub grep {&$grepsub} get_all_projects);
523 if ($verbose) {
524 print map(sprintf("%s\n", join(":", (@$_)[1..5])), @projects);
525 } else {
526 print map(sprintf("%s: %s\n", $$_[1], $$_[5] =~ /^:/ ? "<mirror>" : $$_[5]), @projects);
528 return 0;
531 sub cmd_create {
532 my ($force, $noalternates, $orphanok, $optp, $nopasswd, $noowner, $defaults, $ispush, $pushusers,
533 $ismirror, $desc, $fullmirror, $homepage);
534 parse_options(
535 force => \$force, "no-alternates" => \$noalternates, orphan => \$orphanok, p => \$optp,
536 "no-password" => \$nopasswd, "no-owner" => \$noowner, defaults => \$defaults,
537 "push" => \$ispush, ":push" => \$pushusers, ":mirror" => \$ismirror, ":desc" => \$desc,
538 ":description" => \$desc, "full-mirror" => \$fullmirror, ":homepage" => \$homepage);
539 @ARGV == 1 or die_usage;
540 !defined($pushusers) || defined($ispush) or $ispush = 1;
541 defined($ismirror) && $ismirror =~ /^\s*$/ and die "--mirror url must not be empty\n";
542 die "--mirror and --push are mutually exclusive options\n" if $ismirror && $ispush;
543 die "--full-mirror requires use of --mirror=<url> option\n" if $fullmirror && !$ismirror;
544 !$defaults || defined($ispush) || defined($ismirror) or $ispush = 1;
545 !$defaults || defined($nopasswd) or $nopasswd = 1;
546 !$defaults || defined($noowner) or $noowner = 1;
547 !defined($ispush) || defined($pushusers) or $pushusers = "";
548 my $projname = $ARGV[0];
549 $projname =~ s/\.git$//i;
550 Girocco::Project::does_exist($projname, 1) and die "Project already exists: \"$projname\"\n";
551 if (!Girocco::Project::valid_name($projname, $orphanok, $optp)) {
552 warn "Refusing to create orphan project without --orphan\n"
553 if !$quiet && !$orphanok && Girocco::Project::valid_name($projname, 1, 1);
554 warn "Required orphan parent directory does not exist (use -p): ",
555 $Girocco::Config::reporoot.'/'.Girocco::Project::get_forkee_name($projname), "\n"
556 if !$quiet && $orphanok && Girocco::Project::valid_name($projname, 1, 1);
557 die "Invalid project name: \"$projname\"\n";
559 my ($forkee, $project) = ($projname =~ m#^(.*/)?([^/]+)$#);
560 my $newtype = $forkee ? 'fork' : 'project';
561 if (length($project) > 64) {
562 die "The $newtype name is longer than 64 characters. Do you really need that much?\n"
563 unless $force;
564 warn "Allowing $newtype name longer than 64 characters with --force\n" unless $quiet;
566 unless ($Girocco::Config::push || $Girocco::Config::mirror) {
567 die "Project creation disabled (no mirrors or push projects allowed)\n" unless $force;
568 warn "Continuing with --force even though both push and mirror projects are disabled\n" unless $quiet;
570 print "Enter settings for new project \"$projname\"\n" unless $defaults;
571 my %settings = ();
572 $settings{noalternates} = $noalternates;
573 if ($nopasswd) {
574 $settings{crypt} = "unknown";
575 } else {
576 my $np1 = prompt_noecho_nl_or_die("Admin password for project $projname (echo is off)");
577 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
578 my $np2 = prompt_noecho_nl_or_die("Retype admin password for project $projname");
579 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
580 "the admin passwords you have entered do not match each other.\n";
581 $settings{crypt} = scrypt_sha1($np1);
583 my $owner = "";
584 unless ($noowner) {{
585 $owner = prompt_or_die("Owner/email name for project $projname");
586 unless (valid_email($owner)) {
587 unless ($force) {
588 warn "Your email sure looks weird...?\n";
589 redo;
591 warn "Allowing invalid email with --force\n" unless $quiet;
593 if (length($owner) > 96) {
594 unless ($force) {
595 warn "Your email is longer than 96 characters. Do you really need that much?\n";
596 redo;
598 warn "Allowing email longer than 96 characters with --force\n" unless $quiet;
601 $settings{email} = $owner;
602 my $baseurl = "";
603 my $checkmirror = sub {
604 my $checkurl = shift;
605 unless (valid_repo_url($checkurl)) {
606 unless ($force) {
607 warn "Invalid mirror URL: \"$checkurl\"\n";
608 return undef;
610 warn "Allowing invalid mirror URL with --force\n" unless $quiet;
612 if ($Girocco::Config::restrict_mirror_hosts) {
613 my $mh = extract_url_hostname($checkurl);
614 unless (is_dns_hostname($mh)) {
615 unless ($force) {
616 warn "Invalid non-DNS mirror URL: \"$checkurl\"\n";
617 return undef;
619 warn "Allowing non-DNS mirror URL with --force\n" unless $quiet;
621 if (is_our_hostname($mh)) {
622 unless ($force) {
623 warn "Invalid same-host mirror URL: \"$checkurl\"\n";
624 return undef;
626 warn "Allowing same-host mirror URL with --force\n" unless $quiet;
629 return $checkurl;
631 if ($ispush || $ismirror) {
632 !$ispush || $force || $Girocco::Config::push or
633 die "Push projects are disabled, create a mirror (or use --force)\n";
634 !$ismirror || $force || $Girocco::Config::mirror or
635 die "Mirror projects are disabled, create a push project (or use --force)\n";
636 if ($ismirror) {
637 &$checkmirror($ismirror) or die "Invalid --mirror URL\n";
638 $baseurl = $ismirror;
639 $settings{url} = $baseurl;
640 $settings{cleanmirror} = $fullmirror ? 0 : 1;
641 } else {
642 my @newusers = ();
643 if ($pushusers !~ /^[\s,]*$/) {
644 eval {@newusers = validate_users($pushusers, $force); 1;} or
645 die "Invalid --push user list\n";
647 $settings{users} = \@newusers;
649 } elsif ($force || $Girocco::Config::mirror) {{
650 if ($force || $Girocco::Config::push) {
651 $baseurl = prompt_or_die("URL to mirror from (leave blank for push project)", "");
652 } else {{
653 $baseurl = prompt_or_die("URL to mirror from");
654 unless ($baseurl ne "") {
655 warn "Push projects are disabled, you must enter a mirror URL (or use --force)\n";
656 redo;
659 if ($baseurl ne "") {
660 &$checkmirror($baseurl) or redo;
661 $settings{url} = $baseurl;
662 $settings{cleanmirror} =
663 ynprompt_or_die("Mirror only heads, tags and notes (Y/n)", "Yes");
666 my $mirror = ($baseurl eq "") ? 0 : 1;
667 my $checkdesc = sub {
668 my $d = shift;
669 if (length($d) > 1024) {
670 unless ($force) {
671 warn "Short description length greater than 1024 characters!\n";
672 return undef;
674 warn "Allowing short description length greater than 1024 characters\n" unless $quiet;
676 return $d;
678 if (defined($desc)) {
679 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
680 $desc eq "" || &$checkdesc($desc) or
681 die "Invalid --desc description\n";
682 } elsif (!$defaults) {{
683 $desc = prompt_or_die("Short description", "");
684 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
685 $desc eq "" || &$checkdesc($desc) or redo;
686 $desc = undef if $desc eq "";
688 defined($desc) or $desc = $mirror ? "Mirror of $baseurl" : "Push project $projname";
689 $settings{desc} = $desc;
690 my $checkhp = sub {
691 my $hpurl = shift;
692 unless (valid_web_url($hpurl)) {
693 unless ($force) {
694 warn "Invalid home page URL: \"$hpurl\"\n";
695 return undef;
697 warn "Allowing invalid home page URL with --force\n" unless $quiet;
699 return $hpurl;
701 if (defined($homepage)) {
702 $homepage =~ s/^\s+//; $homepage =~ s/\s+$//;
703 $homepage eq "" || &$checkhp($homepage) or
704 die "Invalid --homepage URL\n";
705 } elsif (!$defaults) {{
706 $homepage = prompt_or_die("Home page URL", "");
707 $homepage =~ s/^\s+//; $homepage =~ s/\s+$//;
708 $homepage eq "" || &$checkhp($homepage) or redo;
709 $homepage = undef if $homepage eq "";
711 $settings{hp} = $homepage;
712 my $jsonurl = "";
713 if (!$defaults) {{
714 $jsonurl = prompt_or_die("JSON notify POST URL", "");
715 if ($jsonurl ne "" && !valid_web_url($jsonurl)) {
716 unless ($force) {
717 warn "Invalid JSON notify POST URL: \$jsonurl\"\n";
718 redo;
720 warn "Allowing invalid JSON notify POST URL with --force\n" unless $quiet;
723 $settings{notifyjson} = $jsonurl;
724 my $commitaddrs = "";
725 if (!$defaults) {{
726 $commitaddrs = clean_addrlist(prompt_or_die("Commit notify email addr(s)", ""));
727 if ($commitaddrs ne "" && !valid_addrlist($commitaddrs)) {
728 unless ($force) {
729 warn"invalid commit notify email address list (use --force to accept): \"$commitaddrs\"\n";
730 redo;
732 warn "using invalid commit notify email address list with --force\n" unless $quiet;
735 $settings{notifymail} = $commitaddrs;
736 $settings{reverseorder} = 1;
737 $settings{reverseorder} = ynprompt_or_die("Oldest-to-newest commit order in emails", "Yes")
738 if !$defaults && $commitaddrs ne "";
739 $settings{summaryonly} = ynprompt_or_die("Summary only (no diff) in emails", "No")
740 if !$defaults && $commitaddrs ne "";
741 my $tagaddrs = "";
742 if (!$defaults) {{
743 $tagaddrs = clean_addrlist(prompt_or_die("Tag notify email addr(s)", ""));
744 if ($tagaddrs ne "" && !valid_addrlist($tagaddrs)) {
745 unless ($force) {
746 warn"invalid tag notify email address list (use --force to accept): \"$tagaddrs\"\n";
747 redo;
749 warn "using invalid tag notify email address list with --force\n" unless $quiet;
752 $settings{notifytag} = $tagaddrs;
753 if (!$mirror && !$ispush) {{
754 my @newusers = ();
756 my $userlist = prompt_or_die("Push users", join(",", @newusers));
757 eval {@newusers = validate_users($userlist, $force); 1;} or redo;
759 $settings{users} = \@newusers;
761 my $newproj = Girocco::Project->ghost($projname, $mirror, $orphanok, $optp)
762 or die "Girocco::Project->ghost call failed\n";
763 my ($k, $v);
764 $newproj->{$k} = $v while ($k, $v) = each(%settings);
765 my $killowner = sub {
766 system($Girocco::Config::git_bin, '--git-dir='.$newproj->{path},
767 'config', '--unset', "gitweb.owner");
769 if ($mirror) {
770 $newproj->premirror or die "Girocco::Project->premirror failed\n";
771 !$noowner or &$killowner;
772 $newproj->clone or die "Girocco::Project->clone failed\n";
773 warn "Project $projname created and cloning successfully initiated.\n"
774 unless $quiet;
775 } else {
776 $newproj->conjure or die "Girocco::Project->conjure failed\n";
777 !$noowner or &$killowner;
778 warn "New push project fork is empty due to use of --no-alternates\n"
779 if !$quiet && $projname =~ m,/, && $noalternates;
780 warn "Project $projname successfully created.\n" unless $quiet;
782 return 0;
785 sub git_config {
786 my $gd = shift;
787 system($Girocco::Config::git_bin, "--git-dir=$gd", 'config', @_) == 0
788 or die "\"git --git-dir='$gd' config ".join(" ", @_)."\" failed.\n";
791 sub cmd_adopt {
792 my ($force, $type, $nousers, $dryrun, $noowner, $owner, $users, $verbose);
793 parse_options(force => \$force, ":type" => \$type, "no-users" => \$nousers, "dry-run" => \$dryrun,
794 "no-owner" => \$noowner,":owner" => \$owner, quiet => \$quiet, q =>\$quiet, verbose => \$verbose);
795 @ARGV or die "Please give project name on command line.\n";
796 my $projname = shift @ARGV;
797 (!$noowner || !defined($owner)) && (!$nousers || !@ARGV) or die_usage;
798 !defined($type) || $type eq "mirror" || $type eq "push" or die_usage;
799 defined($type) or $type = "";
800 my $projdir;
801 if ($dryrun && $projname =~ m,^/[^.\s/\\:], && is_git_dir(realpath($projname))) {
802 $projdir = realpath($projname);
803 $projname = $projdir;
804 $projname =~ s/\.git$//i;
805 $projname =~ s,/+$,,;
806 $projname =~ s,^.*/,,;
807 $projname ne "" or $projname = $projdir;
808 } else {
809 $projname =~ s/\.git$//i;
810 $projname ne "" or die "Invalid project name \"\".\n";
811 unless (Girocco::Project::does_exist($projname, 1)) {
812 Girocco::Project::valid_name($projname, 1, 1)
813 or die "Invalid project name \"$projname\".\n";
814 die "No such project to adopt: $projname\n";
816 defined(Girocco::Project->load($projname))
817 and die "Project already known (no need to adopt): $projname\n";
818 $projdir = $Girocco::Config::reporoot . "/" . $projname . ".git";
819 is_git_dir($projdir) or die "Not a git directory: \"$projdir\"\n";
821 my $config = read_config_file($projdir . "/config");
822 my %config = ();
823 %config = map {($$_[0], defined($$_[1])?$$_[1]:"true")} @$config if defined($config);
824 git_bool($config{"core.bare"}) or die "Not a bare git repository: \"$projdir\"\n";
825 defined(read_HEAD_symref($projdir)) or die "Project with non-symbolic HEAD ref: \"$projdir\"\n";
826 @ARGV and $users = [validate_users(join(" ", @ARGV), $force, 1)];
827 my $desc = "";
828 if (-e "$projdir/description") {
829 open my $fd, '<', "$projdir/description" or die "Cannot open \"$projdir/description\": $!\n";
831 local $/;
832 $desc = <$fd>;
834 close $fd;
835 defined $desc or $desc = "";
836 chomp $desc;
837 $desc = to_utf8($desc, 1);
838 is_default_desc($desc) and $desc = "";
839 if ($desc ne "" && !valid_desc($desc)) {
840 die "invalid 'description' file contents (use --force to accept): \"$desc\"\n"
841 unless $force;
842 warn "using invalid 'description' file contents with --force\n" unless $quiet;
844 $desc = clean_desc($desc);
845 if (length($desc) > 1024) {
846 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
847 unless $force;
848 warn "using longer than 1024 char description with --force\n" unless $quiet;
851 my $readme = "";
852 my $origreadme = "";
853 my $readmedata = "";
854 my $origreadmedata = "";
855 my $readmetype = Girocco::Project::_normalize_rmtype($config{"girocco.readmetype"},1);
856 if (-e "$projdir/README.html") {
857 open my $fd, '<', "$projdir/README.html" or die "Cannot open \"$projdir/README.html\": $!\n";
859 local $/;
860 $readme = <$fd>;
862 close $fd;
863 defined $readme or $readme = "";
864 $readme = to_utf8($readme, 1);
865 $readme =~ s/\r\n?/\n/gs;
866 $readme =~ s/^\s+//s;
867 $readme =~ s/\s+$//s;
868 $readme eq "" or $readme .= "\n";
869 $origreadme = $readme;
870 if (-e "$projdir/README.dat") {
871 open my $fd2, '<', "$projdir/README.dat" or die "Cannot open \"$projdir/README.dat\": $!\n";
873 local $/;
874 $readmedata = <$fd2>;
876 close $fd2;
877 defined $readmedata or $readmedata = "";
878 $readmedata = to_utf8($readmedata, 1);
879 $readmedata =~ s/\r\n?/\n/gs;
880 $readmedata =~ s/^\s+//s;
881 $readmedata =~ s/\s+$//s;
882 $readmedata eq "" or $readmedata .= "\n";
883 $origreadmedata = $readmedata;
885 !$readmetype && length($readme) && !length($readmedata) and do {
886 # the old HTML format
887 $readmetype = 'HTML';
888 $readmedata = $readme;
890 if (length($readmedata) > 8192) {
891 die "readme greater than 8192 chars is too long (use --force to override)\n"
892 unless $force;
893 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
896 my $dummy = {READMEDATA => $readmedata, rmtype => $readmetype, name => $projname};
897 my ($cnt, $err) = Girocco::Project::_lint_readme($dummy, 0);
898 if ($cnt) {
899 my $msg = "README: $cnt error";
900 $msg .= "s" unless $cnt == 1;
901 print STDERR "$msg\n", "-" x length($msg), "\n", $err
902 unless $force && $quiet;
903 exit(255) unless $force && $readmetype eq 'HTML';
904 warn "$projname: using invalid raw HTML with --force\n" unless $quiet;
905 } else {
906 $readme = $dummy->{README};
910 $readmetype or $readmetype = Girocco::Project::_normalize_rmtype(""); # use default type
911 # Inspect any remotes now
912 # Yes, Virginia, remote urls can be multi-valued
913 my %remotes = ();
914 foreach (@$config) {
915 my ($k,$v) = @$_;
916 next unless $k =~ /^remote\.([^\/].*?)\.([^.]+)$/; # remote name cannot start with "/"
917 my ($name, $subkey) = ($1, $2);
918 $remotes{$name}->{skip} = git_bool($v,1), next if $subkey eq "skipdefaultupdate" || $subkey eq "skipfetchall";
919 $remotes{$name}->{mirror} = git_bool($v,1), next if $subkey eq "mirror"; # we might want this
920 $remotes{$name}->{vcs} = $v, next if defined($v) && $v !~ /^\s*$/ && $subkey eq "vcs";
921 push(@{$remotes{$name}->{$subkey}}, $v), next if defined($v) && $v !~ /^\s*$/ &&
922 ($subkey eq "url" || $subkey eq "fetch" || $subkey eq "push" || $subkey eq "pushurl");
924 # remotes.default is the default remote group to fetch for "git remote update" otherwise --all
925 # the remote names in a group are separated by runs of [ \t\n] characters
926 # remote names "", ".", ".." and any name starting with "/" are invalid
927 # a remote with no url or vcs setting is not considered valid
928 my @check = ();
929 my $usingall = 0;
930 if (exists($config{"remotes.default"})) {
931 foreach (split(/[ \t\n]+/, $config{"remotes.default"})) {
932 next unless exists($remotes{$_});
933 my $rmt = $remotes{$_};
934 next if !exists($rmt->{url}) && !$rmt->{vcs};
935 push(@check, $_);
937 } else {
938 $usingall = 1;
939 my %seenrmt = ();
940 foreach (@$config) {
941 my ($k,$v) = @$_;
942 next unless $k =~ /^remote\.([^\/].*?)\.[^.]+$/;
943 next if $seenrmt{$1};
944 $seenrmt{$1} = 1;
945 next unless exists($remotes{$1});
946 my $rmt = $remotes{$1};
947 next if $rmt->{skip} || (!exists($rmt->{url}) && !$rmt->{vcs});
948 push(@check, $1);
951 my @needskip = (); # remotes that need skipDefaultUpdate set to true
952 my $foundvcs = 0;
953 my $foundfetch = 0;
954 my $foundfetchwithmirror = 0;
955 foreach (@check) {
956 my $rmt = $remotes{$_};
957 push(@needskip, $_) if $usingall && !exists($rmt->{fetch});
958 next unless exists($rmt->{fetch});
959 ++$foundfetch;
960 ++$foundfetchwithmirror if $rmt->{mirror};
961 ++$foundvcs if $rmt->{vcs} || (exists($rmt->{url}) && $rmt->{url}->[0] =~ /^[a-zA-Z0-9][a-zA-Z0-9+.-]*::/);
963 # if we have $foundvcs then we need to explicitly set fetch.prune to false
964 # if we have $foundfetch > 1 then we need to explicitly set fetch.prune to false
965 my $neednoprune = !exists($config{"fetch.prune"}) && ($foundvcs || $foundfetch > 1);
966 my $baseurl = "";
967 my $needfakeorigin = 0; # if true we need to set remote.origin.skipDefaultUpdate = true
968 # if remote "origin" exists we always pick up its first url or use ""
969 if (exists($remotes{origin})) {
970 my $rmt = $remotes{origin};
971 $baseurl = exists($rmt->{url}) ? $rmt->{url}->[0] : "";
972 $needfakeorigin = !exists($rmt->{url}) && !$rmt->{vcs} && !$rmt->{skip};
973 } else {
974 $needfakeorigin = 1;
975 # get the first url of the @check remotes
976 foreach (@check) {
977 my $rmt = $remotes{$_};
978 next unless exists($rmt->{url});
979 next unless defined($rmt->{url}->[0]) && $rmt->{url}->[0] ne "";
980 $baseurl = $rmt->{url}->[0];
981 last;
984 my $makemirror = $type eq "mirror" || ($type eq "" && $foundfetch);
986 # If we have $foundfetch we want to make a mirror but complain if
987 # we $foundfetchwithmirror as well unless we have --type=mirror.
988 # Warn if we have --type=push and $foundfetch and !$foundfetchwithmirror.
989 # Warn if we need to set fetch.prune=false when making a mirror
990 # Warn if we need to create remote.origin.skipDefaultUpdate when making a mirror
991 # Complain if @needskip AND !$usingall (warn with --force but don't set skip)
992 # Warn if $usingall and any @needskip (and set them) if making a mirror
993 # Warn if making a mirror and $baseurl eq ""
994 # Warn if we have --type=mirror and !$foundfetch
996 if ($makemirror) {
997 warn "No base URL to mirror from for adopted \"$projname\"\n" unless $quiet || $baseurl ne "";
998 warn "Adopting mirror \"$projname\" without any fetch remotes\n" unless $quiet || $foundfetch;
999 if ($foundfetchwithmirror) {
1000 warn "Refusing to adopt mirror \"$projname\" with active remote.<name>.mirror=true remote(s)\n".
1001 "(Use --type=mirror to override)\n"
1002 unless $type eq "mirror";
1003 exit(255) unless $type eq "mirror" || $dryrun;
1004 warn "Adopting mirror \"$projname\" with active remote.<name>.mirror=true remotes\n"
1005 unless $quiet || $type ne "mirror";
1007 warn "Setting explicit fetch.prune=false for adoption of mirror \"$projname\"\n"
1008 if !$quiet && $neednoprune;
1009 warn "Setting remote.origin.skipDefaultUpdate=true for adoption of mirror \"$projname\"\n"
1010 if !$quiet && $needfakeorigin;
1011 if (!$usingall && @needskip) {
1012 warn "Refusing to adopt mirror empty fetch remote(s) (override with --force)\n"
1013 unless $force;
1014 exit(255) unless $force || $dryrun;
1015 warn "Adopting mirror with empty fetch remote(s) with --force\n"
1016 unless $quiet || !$force;
1018 warn "Will set skipDefaultUpdate=true on non-fetch remote(s)\n" if !$quiet && $usingall && @needskip;
1019 warn "Adopting mirror with base URL \"$baseurl\"\n" unless $quiet || $baseurl eq "";
1020 } else {
1021 warn "Adopting push \"$projname\" but active non-mirror remotes are present\n"
1022 if !$quiet && $foundfetch && !$foundfetchwithmirror;
1025 if (!$noowner && !defined($owner)) {
1026 # Select the owner
1027 $owner = $config{"gitweb.owner"};
1028 if (!defined($owner) || $owner eq "") {
1029 $owner = $Girocco::Config::admin;
1030 warn "Using owner \"$owner\" for adopted project\n" unless $quiet;
1033 if (!$nousers && !$makemirror && !defined($users)) {
1034 # select user list for push project
1035 my $findowner = $owner;
1036 defined($findowner) or $findowner = $config{"gitweb.owner"};
1037 $findowner = lc($findowner) if defined($findowner);
1038 my @owner_users = ();
1039 @owner_users = map {$findowner eq lc($$_[4]) ? $$_[1] : ()} get_all_users
1040 if defined($findowner) && $findowner ne "";
1041 defined($findowner) or $findowner = "";
1042 if (@owner_users <= 1) {
1043 $users = \@owner_users;
1044 warn "No users found that match owner \"$findowner\"\n" unless @owner_users || $quiet;
1045 } else {
1046 $users = [];
1047 warn "Found ".scalar(@owner_users)." users for owner \"$findowner\" (" .
1048 join(" ", @owner_users) . ") not setting any\n" unless $quiet;
1051 defined($users) or $users = [];
1053 # Warn if we preserve an existing receive.denyNonFastForwards or receive.denyDeleteCurrent setting
1054 # Complain if core.logallrefupdates or logs subdir exists and contains any files (allow with --force
1055 # and warn about preserving the setting)
1057 warn "Preserving existing receive.denyNonFastForwards=true\n"
1058 if !$quiet && git_bool($config{"receive.denynonfastforwards"});
1059 warn "Preserving existing receive.denyDeleteCurrent=$config{'receive.denydeletecurrent'}\n"
1060 if !$quiet && exists($config{"receive.denydeletecurrent"}) &&
1061 $config{"receive.denydeletecurrent"} ne "warn";
1063 my $reflogfiles = Girocco::Project::_contains_files("$projdir/logs");
1064 my $reflogactive = git_bool($config{"core.logallrefupdates"});
1065 if ($reflogactive || $reflogfiles) {
1066 warn "Refusing to adopt \"$projname\" with active ref logs without --force\n" if $reflogfiles && !$force;
1067 warn "Refusing to adopt \"$projname\" with core.logAllRefUpdates=true without --force\n" if $reflogactive && !$force;
1068 exit(255) unless $force || $dryrun;
1069 warn "Adopting \"$projname\" with active ref logs with --force\n" unless $quiet || ($reflogfiles && !$force);
1070 warn "Adopting \"$projname\" with core.logAllRefUpdates=true with --force\n" unless $quiet || ($reflogactive && !$force);
1073 return 0 if $dryrun && !$verbose;
1075 my $newproj = eval {Girocco::Project->ghost($projname, $makemirror, 1, $dryrun)};
1076 defined($newproj) or die "Girocco::Project::ghost failed: $@\n";
1077 $newproj->{desc} = $desc;
1078 $newproj->{README} = $readme;
1079 $newproj->{READMEDATA} = $readmedata;
1080 $newproj->{rmtype} = $readmetype;
1081 $newproj->{url} = $baseurl if $makemirror || exists($config{"gitweb.baseurl"});
1082 $newproj->{email} = $owner if defined($owner);
1083 $newproj->{users} = $users;
1084 $newproj->{crypt} = "unknown";
1085 $newproj->{reverseorder} = 1 unless exists($config{"hooks.reverseorder"});
1086 $newproj->{summaryonly} = 1 unless exists($config{"hooks.summaryonly"});
1087 my $dummy = bless {}, "Girocco::Project";
1088 $dummy->{path} = "$projdir";
1089 $dummy->{configfilehash} = \%config;
1090 $dummy->_properties_load;
1091 delete $dummy->{origurl};
1092 foreach my $k (keys(%$dummy)) {
1093 $newproj->{$k} = $dummy->{$k}
1094 if exists($dummy->{$k}) && !exists($newproj->{$k});
1097 if ($verbose) {
1098 use Data::Dumper;
1099 my %info = %$newproj;
1100 $info{README} = get_readme_desc($info{README}) if exists($info{README});
1101 my $d = Data::Dumper->new([\%info], ['*'.$newproj->{name}]);
1102 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
1103 print $d->Dump([\%info], ['*'.$newproj->{name}]);
1105 return 0 if $dryrun;
1107 # Make any changes as needed for @needskip, $neednoprune and $needfakeorigin
1108 if ($makemirror) {
1109 git_config($projdir, "fetch.prune", "false") if $neednoprune;
1110 git_config($projdir, "remote.origin.skipDefaultUpdate", "true") if $needfakeorigin;
1111 if ($usingall && @needskip) {
1112 git_config($projdir, "remote.$_.skipDefaultUpdate", "true") foreach @needskip;
1116 # Write out any README.dat/README.html changes before the actual Adoption
1117 # Otherwise they will get stepped on. The Girocco::Project::adopt function
1118 # does not know how to validate README.html during adoption like the above code does.
1119 if ($readmedata ne $origreadmedata) {
1120 open my $fd, '>', "$projdir/README.dat" or die "Cannot write \"$projdir/README.dat\": $!\n";
1121 print $fd $readmedata or die "Error writing \"$projdir/README.dat\": $!\n";
1122 close $fd or die "Error closing \"$projdir/README.dat\": $!\n";
1124 if ($readme ne $origreadme || ! -e "$projdir/README.html") {
1125 open my $fd, '>', "$projdir/README.html" or die "Cannot write \"$projdir/README.html\": $!\n";
1126 print $fd $readme or die "Error writing \"$projdir/README.html\": $!\n";
1127 close $fd or die "Error closing \"$projdir/README.html\": $!\n";
1129 git_config($projdir, "girocco.rmtype", $readmetype);
1131 # Perform the actual adoption
1132 $newproj->adopt or die "Girocco::Project::adopt failed\n";
1134 # Perhaps restore core.logAllRefUpdates, receive.denyNonFastForwards and receive.denyDeleteCurrent
1135 git_config($projdir, "receive.denyNonFastForwards", "true")
1136 if git_bool($config{"receive.denynonfastforwards"});
1137 git_config($projdir, "receive.denyDeleteCurrent", $config{"receive.denydeletecurrent"})
1138 if exists($config{"receive.denydeletecurrent"}) &&
1139 $config{"receive.denydeletecurrent"} ne "warn";
1140 git_config($projdir, "core.logAllRefUpdates", "true")
1141 if $reflogactive;
1143 # Success
1144 if ($makemirror) {
1145 warn "Mirror project \"$projname\" successfully adopted.\n" unless $quiet;
1146 } else {
1147 warn "Push project \"$projname\" successfully adopted.\n" unless $quiet;
1149 return 0;
1152 sub cmd_remove {
1153 my ($force, $reallydel, $keepforks);
1154 parse_options(force => \$force, "really-delete" => \$reallydel,
1155 "keep-forks" => \$keepforks, quiet => \$quiet, q =>\$quiet);
1156 @ARGV or die "Please give project name on command line.\n";
1157 @ARGV == 1 or die_usage;
1158 my $project = get_project($ARGV[0]); # for safety only names accepted here
1159 my $projname = $project->{name};
1160 my $isempty = !$project->{mirror} && $project->is_empty;
1161 if (!$project->{mirror} && !$isempty && $reallydel) {
1162 die "refusing to remove and delete non-empty push project without --force: $projname\n" unless $force;
1163 warn "allowing removal and deletion of non-empty push project with --force\n" unless $quiet;
1165 my $altwarn;
1166 my $removenogc;
1167 if ($project->has_forks) {
1168 die "refusing to remove project with forks (use --keep-forks): $projname\n" unless $keepforks;
1169 warn "allowing removal of forked project while preserving its forks with --keep-forks\n" unless $quiet;
1170 # Run pseudo GC on that repository so that objects don't get lost within forks
1171 my $basedir = $Girocco::Config::basedir;
1172 my $projdir = $project->{path};
1173 warn "We have to run pseudo GC on the repo so that the forks don't lose data. Hang on...\n" unless $quiet;
1174 my $nogcrunning = sub {
1175 die "Error: GC appears to be currently running on $projname\n"
1176 if -e "$projdir/gc.pid" || -e "$projdir/.gc_in_progress";
1178 &$nogcrunning;
1179 $removenogc = ! -e "$projdir/.nogc";
1180 recreate_file("$projdir/.nogc") if $removenogc;
1181 die "unable to create \"$projdir/.nogc\"\n" unless -e "$projdir/.nogc";
1182 delete $ENV{show_progress};
1183 $ENV{'show_progress'} = 1 unless $quiet;
1184 sleep 2; # *cough*
1185 &$nogcrunning;
1186 system("$basedir/toolbox/perform-pre-gc-linking.sh", "--include-packs", $projname) == 0
1187 or die "Running pseudo GC on project $projname failed\n";
1188 $altwarn = 1;
1190 my $archived;
1191 if (!$project->{mirror} && !$isempty && !$reallydel) {
1192 $archived = $project->archive_and_delete;
1193 unlink("$archived/.nogc") if $removenogc && defined($archived) && $archived ne "";
1194 } else {
1195 $project->delete;
1197 warn "Project '$projname' removed from $Girocco::Config::name" .
1198 ($archived ? ", backup in '$archived'" : "") .".\n" unless $quiet;
1199 warn "Retained forks may now have unwanted objects/info/alternates lines\n" if $altwarn && !$quiet;
1200 return 0;
1203 sub cmd_prune {
1204 my ($force, $dryrun);
1205 parse_options(force => \$force, "dry-run" => \$dryrun, "quiet" => \$quiet);
1206 ($force && !$dryrun) || (!$force && $dryrun) or die_usage;
1207 my @projs = @ARGV;
1208 my %allprojs = map({($$_[0] => $_)} Girocco::Project::get_full_list_extended());
1209 my @allprojs = sort({lc($a) cmp lc($b) || $a cmp $b} keys(%allprojs));
1210 my %seen = ();
1211 @projs or @projs = @allprojs;
1212 my $bd = $Girocco::Config::reporoot.'/';
1213 my @remove = ();
1214 foreach (@projs) {
1215 !$seen{$_} && $allprojs{$_} && ${$allprojs{$_}}[2] >= 65536 or next;
1216 $seen{$_} = 1;
1217 /^[a-zA-Z0-9]/ or next;
1218 my $pd = $bd . $_ . '.git';
1219 if (! -e $pd) {
1220 warn "$_: no such directory: $pd\n" unless $quiet;
1221 push(@remove, $_);
1222 } elsif (! -d _) {
1223 warn "$_: exists but not directory: $pd\n" unless $quiet;
1224 push(@remove, $_);
1227 warn "\n" if @remove && !$quiet;
1228 if ($dryrun) {
1229 return 0 unless @remove;
1230 my $msg = "Would remove ".scalar(@remove). " project entr";
1231 $msg .= (@remove == 1 ? "y" : "ies");
1232 $msg .= ":\n";
1233 $msg .= join("", map("\t$_\n", @remove));
1234 print $msg unless $quiet;
1235 return 1;
1237 my $msg = "Removed ".scalar(@remove). " project entr";
1238 $msg .= (@remove == 1 ? "y" : "ies");
1239 $msg .= ":\n";
1240 $msg .= join("", map("\t$_\n", @remove));
1241 my %remove = map({$_ => 1} @remove);
1242 filedb_atomic_edit(jailed_file('/etc/group'), sub {
1243 my ($name,undef,$gid) = split /:/;
1244 $gid =~ /^\d+$/ && $gid >= 65536 or return $_;
1245 $name =~ /^[a-zA-Z0-9]/ or return $_;
1246 !exists($remove{$name}) and return $_;
1248 print $msg unless $quiet;
1249 return 0;
1252 sub cmd_show {
1253 use Data::Dumper;
1254 @ARGV <= 1 or die_usage;
1255 my $project = get_clean_project_gently($ARGV[0]);
1256 defined($project) or die_usage;
1257 my %info = %$project;
1258 my $d = Data::Dumper->new([\%info], ['*'.$project->{name}]);
1259 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
1260 print $d->Dump([\%info], ['*'.$project->{name}]);
1261 return 0;
1264 sub cmd_verify {
1265 my $dirfp = 0;
1266 parse_options("quiet" => \$quiet, "dir" => \$dirfp, "directory" => \$dirfp, "git-dir" =>\$dirfp);
1267 @ARGV <= 1 or die_usage;
1268 my $project = undef;
1269 my $pname = undef;
1270 eval {
1271 $project = get_project_harder(@ARGV >= 1 ? $ARGV[0] : ".");
1273 @ARGV > 0 || reftype($project) eq 'HASH' or die_usage;
1274 my $parg = @ARGV ? $ARGV[0] : $$project{name};
1275 $pname = $project->{name} if reftype($project) eq 'HASH';
1276 defined($pname) && $pname ne "" or $project = undef;
1277 !$@ && reftype($project) ne 'HASH' and $@ = "No such project: \"$parg\"\n";
1278 warn $@ if $@ && !$quiet;
1279 exit 1 if $@;
1280 $dirfp && defined($project->{path}) && $project->{path} ne "" and
1281 $pname = $project->{path};
1282 printf "%s\n", $pname;
1283 return 0;
1286 sub get_worktrees {
1287 my $gd = shift;
1288 return () unless -d "$gd/worktrees";
1289 opendir my $dh, "$gd/worktrees" or return ();
1290 my @dirs = grep { !/^\.\.?$/ && -d "$gd/worktrees/$_"} readdir($dh);
1291 closedir $dh;
1292 my @wt = ();
1293 foreach (@dirs) {
1294 open my $hd, '<', "$gd/worktrees/$_/HEAD" or next;
1295 my $hh = <$hd>;
1296 close $hd;
1297 defined($hh) or next;
1298 chomp($hh);
1299 if ($hh =~ /^[0-9a-f]{40,}$/) {
1300 push(@wt, [$_, $hh]);
1301 } elsif ($hh =~ m{^ref:\s?(refs/heads/.+)$}) {
1302 push(@wt, [$_, $1]);
1305 return @wt;
1308 sub cmd_worktree {
1309 eval '$Girocco::Config::var_have_git_250' or die "worktree requires Git 2.5.0 or later\n";
1310 my $force;
1311 parse_options("force" => \$force, quiet => \$quiet, q =>\$quiet);
1312 my $project = get_project_harder_gently($ARGV[0]);
1313 defined($project) or die_usage;
1314 shift @ARGV if @ARGV;
1315 @ARGV or push(@ARGV, "list");
1316 my $gd = $project->{path};
1317 defined($gd) && -d $gd or die "Project \"$$project{name}\" does not actually exist\n";
1318 if ($project->{mirror}) {
1319 die "Cannot use worktree command on mirror project\n" unless $force;
1320 warn "Continuing with --force even though project is a mirror\n" unless $quiet;
1322 my $symref = undef;
1323 my $mthash = undef;
1324 my $wantdwimadd = 0;
1325 my $hb = $project->{HEAD};
1326 defined($hb) or $hb = "";
1327 $hb !~ /^\[/ or $hb = "";
1328 if ($ARGV[0] eq "add" && (@ARGV == 2 && $ARGV[1] !~ /^-/ ||
1329 @ARGV == 3 && $ARGV[1] !~ /^-/ && $hb && $ARGV[2] eq $hb)) {{
1330 $wantdwimadd = -1;
1331 # Only "add" subcommand has special handling
1332 my $isbare = get_git_chomp("--git-dir=".$gd, "rev-parse", "--is-bare-repository");
1333 defined($isbare) && $isbare eq "true" or last; # only for bare repos
1334 $symref = get_git_chomp("--git-dir=".$gd, "symbolic-ref", "-q", "HEAD");
1335 defined($symref) && $symref =~ m{^refs/heads/.} or last; # only if symref HEAD
1336 !grep({$$_[1] eq $symref} get_worktrees($gd)) or last; # and not checked out
1337 if (get_git_chomp("--git-dir=".$gd, "rev-parse", "--verify", "-q", $symref)) {
1338 # easy case, branch already exists, just add its name to arg list
1339 push(@ARGV, substr($symref, 11)) unless @ARGV == 3;
1340 $symref = undef;
1341 $wantdwimadd = 1;
1342 } else {
1343 # nasty workaround for broken worktree command
1344 my $mttree = get_git_chomp("--git-dir=".$gd, "mktree");
1345 defined($mttree) && $mttree =~ /^[0-9a-f]{40,}$/ or last;
1346 my $now = time();
1347 my $cmt = "tree $mttree\nauthor - <-> $now +0000\ncommitter - <-> $now +0000\n\n";
1348 my ($st, $rslt) = capture_command(1, $cmt, $Girocco::Config::git_bin,
1349 "--git-dir=".$gd, "hash-object", "-t", "commit", "-w", "--stdin");
1350 defined($st) && $st == 0 && defined($rslt) or last;
1351 chomp($rslt);
1352 $rslt =~ /^[0-9a-f]{40,}$/ or last;
1353 $mthash = $rslt;
1354 pop(@ARGV) if @ARGV == 3;
1355 push(@ARGV, $mthash); # requires ugly fixup afterwards
1356 $wantdwimadd = 2;
1359 warn $project->{name}, ": cannot DWIM worktree add (check `worktree list` output)\n"
1360 if $wantdwimadd == -1 && !$quiet;
1361 my $saveHOME = $ENV{HOME};
1362 if (defined($origHOME) && $origHOME ne "" && $origHOME =~ m{^(/.+)$} && -d $1) {
1363 $ENV{HOME} = $1;
1365 my $ec = system($Girocco::Config::git_bin, "--git-dir=".$gd, "worktree", @ARGV);
1366 $ENV{HOME} = $saveHOME;
1367 if (defined($ec) && $ec == 0 && defined($symref) && defined($mthash)) {
1368 # ugly fixup time
1369 foreach (map($$_[0], grep({$$_[1] eq $mthash} get_worktrees($gd)))) {
1370 open my $hf, '>', "$gd/worktrees/$_/HEAD" or next;
1371 print $hf "ref: $symref\n";
1372 close $hf;
1375 defined($ec) && $ec != -1 or return 1;
1376 return ($ec >> 8);
1379 sub cmd_urls {
1380 my $pushonly;
1381 parse_options("push" => \$pushonly);
1382 my @projs = @ARGV;
1383 @ARGV <= 1 or die_usage;
1384 my $project = get_project_harder_gently($ARGV[0]);
1385 defined($project) or die_usage;
1386 my $suffix = "/".$project->{name}.".git";
1387 @Gitweb::Config::git_base_url_list = ();
1388 @Gitweb::Config::git_base_push_urls = ();
1389 @Gitweb::Config::git_base_mirror_urls = ();
1391 package Gitweb::Config;
1392 do $Girocco::Config::basedir."/gitweb/gitweb_config.perl";
1393 !$! or die "could not read gitweb_config.perl: $!\n";
1394 !$@ or die "could not parse gitweb_config.perl: $@\n";
1396 my @fetch_urls = ();
1397 my @push_urls = ();
1398 my $add_url = sub {
1399 my $array = shift;
1400 foreach (@_) {
1401 if (ref($_)) {
1402 ref($_) eq 'ARRAY' or die "expected ARRAY ref";
1403 my $u = $$_[0];
1404 defined($u) && $u ne "" and
1405 push(@$array, $u.$suffix);
1406 } elsif (defined($_) && $_ ne "") {
1407 push(@$array, $_.$suffix);
1411 my $uniq = sub {
1412 my %items = ();
1413 $items{$_} = 1 foreach @_;
1414 sort(keys(%items));
1416 &$add_url(\@fetch_urls, @Gitweb::Config::git_base_url_list);
1417 if ($project->{mirror}) {
1418 &$add_url(\@fetch_urls, @Gitweb::Config::git_base_mirror_urls);
1419 } else {
1420 &$add_url(\@push_urls, @Gitweb::Config::git_base_push_urls);
1422 my @urls = ();
1423 if ($pushonly) {
1424 push(@urls, &$uniq(@push_urls));
1425 } else {
1426 push(@urls, &$uniq(@fetch_urls, @push_urls));
1428 print map "$_\n", @urls;
1429 return 0;
1432 sub cmd_listheads {
1433 @ARGV <= 1 or die_usage;
1434 my $project = get_project_harder_gently($ARGV[0]);
1435 defined($project) or die_usage;
1436 my @heads = sort({lc($a) cmp lc($b)} $project->get_heads);
1437 my $cur = $project->{HEAD};
1438 defined($cur) or $cur = '';
1439 my $curmark = '*';
1440 my $headhash = get_git("--git-dir=$project->{path}", 'rev-parse', '--quiet', '--verify', 'HEAD');
1441 defined($headhash) or $headhash = '';
1442 chomp $headhash;
1443 $headhash or $curmark = '!';
1444 foreach (@heads) {
1445 my $mark = $_ eq $cur ? $curmark : ' ';
1446 print "$mark $_\n";
1448 return 0;
1451 sub cmd_listtags {
1452 my $vcnt = 0;
1453 parse_options("verbose" => \$vcnt, "v" => \$vcnt);
1454 @ARGV <= 1 or die_usage;
1455 my $project = get_project_harder_gently($ARGV[0]);
1456 defined($project) or die_usage;
1457 if ($vcnt) {
1458 print map("$$_[0]\t$$_[1]\n", get_ctag_counts($project));
1459 } else {
1460 print map("$_\n", $project->get_ctag_names);
1462 return 0;
1465 sub cmd_deltags {
1466 my $ic = 0;
1467 parse_options("ignore-case" => \$ic, "i" => \$ic);
1468 @ARGV >= 2 or die_usage;
1469 my $project = get_project_harder(shift @ARGV);
1470 my %curtags;
1471 if ($ic) {
1472 push(@{$curtags{lc($_)}}, $_) foreach $project->get_ctag_names;
1473 } else {
1474 push(@{$curtags{$_}}, $_) foreach $project->get_ctag_names;
1476 my @deltags = ();
1477 my %seentag = ();
1478 my $ctags = join(" ", @ARGV);
1479 $ctags = lc($ctags) if $ic;
1480 foreach (split(/[\s,]+/, $ctags)) {
1481 next unless exists($curtags{$_});
1482 $seentag{$_}=1, push(@deltags, $_) unless $seentag{$_};
1484 if (!@deltags) {
1485 warn $project->{name}, ": skipping removal of only non-existent tags\n" unless $quiet;
1486 } else {
1487 # Avoid touching anything other than the ctags
1488 foreach my $tg (@deltags) {
1489 $project->delete_ctag($_) foreach @{$curtags{$tg}};
1491 $project->_set_changed;
1492 $project->_set_forkchange;
1493 warn $project->{name}, ": specified tags have been removed\n" unless $quiet;
1495 return 0;
1498 sub cmd_addtags {
1499 @ARGV >= 2 or die_usage;
1500 my $project = get_project_harder(shift @ARGV);
1501 my $ctags = join(" ", @ARGV);
1502 $ctags =~ /[^, a-zA-Z0-9:.+#_-]/ and
1503 die "Content tag(s) \"$ctags\" contain(s) evil character(s).\n";
1504 my $oldmask = umask();
1505 umask($oldmask & ~0060);
1506 my $changed = 0;
1507 foreach (split(/[\s,]+/, $ctags)) {
1508 ++$changed if $project->add_ctag($_, 1);
1510 if ($changed) {
1511 $project->_set_changed;
1512 $project->_set_forkchange;
1514 umask($oldmask);
1515 my $cnt = ($changed == 1) ? "1 content tag has" : $changed . " content tags have";
1516 warn $project->{name}, ": $cnt been added/updated\n" unless $quiet;
1517 return 0;
1520 sub _get_random_val {
1521 my $p = shift;
1522 my $md5;
1524 no warnings;
1525 $md5 = md5_hex(time . $$ . rand() . join(':',%$p));
1527 $md5;
1530 sub cmd_chpass {
1531 my $force = 0;
1532 parse_options("force" => \$force);
1533 my $random = undef;
1534 pop(@ARGV), $random=lc($ARGV[1]) if @ARGV==2 && $ARGV[1] =~ /^(?:random|unknown)$/i;
1535 @ARGV == 1 or die_usage;
1536 my $project = get_project_harder($ARGV[0]);
1537 die "refusing to change locked password of project \"$ARGV[0]\" without --force\n"
1538 if $project->is_password_locked;
1539 my ($newpw, $rmsg);
1540 if ($random) {
1541 if ($random eq "random") {
1542 die "refusing to set random password without --force\n" unless $force;
1543 $rmsg = "set to random value";
1544 $newpw = _get_random_val($project);
1545 } else {
1546 die "refusing to set password hash to '$random' without --force\n" unless $force;
1547 $rmsg = "hash set to '$random'";
1548 $newpw = $random;
1550 } else {
1551 $rmsg = "updated";
1552 if (-t STDIN) {
1553 print "Changing admin password for project $ARGV[0]\n";
1554 my $np1 = prompt_noecho_nl_or_die("New password for project $ARGV[0] (echo is off)");
1555 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1556 my $np2 = prompt_noecho_nl_or_die("Retype new password for project $ARGV[0]");
1557 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
1558 "the admin passwords you have entered do not match each other.\n";
1559 $newpw = $np1;
1560 } else {
1561 $newpw = <STDIN>;
1562 defined($newpw) or die "missing new password on STDIN\n";
1563 chomp($newpw);
1566 $newpw ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1567 my $old = $project->{crypt};
1568 $project->{crypt} = (defined($random) && $random ne "random") ? $newpw : scrypt_sha1($newpw);
1569 if (defined($old) && $old eq $project->{crypt}) {
1570 warn $project->{name}, ": skipping update of password hash to same value\n" unless $quiet;
1571 } else {
1572 # Avoid touching anything other than the password hash
1573 $project->_group_update;
1574 warn $project->{name}, ": admin password $rmsg (new hash stored)\n" unless $quiet;
1576 return 0;
1579 sub cmd_checkpw {
1580 @ARGV <= 1 or die_usage;
1581 my $project = get_project_harder_gently($ARGV[0]);
1582 defined($project) or die_usage;
1583 my $pwhash = $project->{crypt};
1584 defined($pwhash) or $pwhash = "";
1585 if ($pwhash eq "") {
1586 warn $project->{name}, ": no password required\n" unless $quiet;
1587 return 0;
1589 if ($project->is_password_locked) {
1590 warn $project->{name}, ": password is locked\n" unless $quiet;
1591 exit 1;
1593 my $checkpw;
1594 if (-t STDIN) {
1595 $checkpw = prompt_noecho_nl_or_die("Admin password for project $$project{name} (echo is off)");
1596 $checkpw ne "" or warn "checking for empty password as hash (very unlikely)\n" unless $quiet;
1597 } else {
1598 $checkpw = <STDIN>;
1599 defined($checkpw) or die "missing admin password on STDIN\n";
1600 chomp($checkpw);
1602 unless (Girocco::CLIUtil::check_passwd_match($pwhash, $checkpw)) {
1603 warn "password check failure\n" unless $quiet;
1604 exit 1;
1606 warn "admin password match\n" unless $quiet;
1607 return 0;
1610 sub cmd_gc {
1611 my ($force, $auto, $redelta, $recompress, $aggressive);
1612 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, auto => \$auto,
1613 redelta => \$redelta, "no-reuse-delta" => \$redelta, aggressive => \$force,
1614 recompress => \$recompress, "no-reuse-object" => $recompress,
1615 aggressive => \$aggressive);
1616 $aggressive and $force = $redelta = 1;
1617 $force && $auto and die "--force and --auto are mutually exclusive options\n";
1618 @ARGV <= 1 or die_usage;
1619 my $project = get_project_harder_gently($ARGV[0]);
1620 defined($project) or die "Please give project name on command line.\n";
1621 delete $ENV{show_progress};
1622 delete $ENV{force_gc};
1623 $quiet or $ENV{"show_progress"} = 1;
1624 $force and $ENV{"force_gc"} = 1;
1625 if (!$auto && !$force && ! -e $project->{path}."/.needsgc") {
1626 open NEEDSGC, '>', $project->{path}."/.needsgc" and close NEEDSGC;
1628 my @args = ($Girocco::Config::basedir . "/jobd/gc.sh", $project->{name});
1629 $redelta && !$recompress and push(@args, "-f");
1630 $recompress and push(@args, "-F");
1631 my $lastgc = $project->{lastgc};
1632 system({$args[0]} @args) != 0 and return 1;
1633 # Do it again Sam, but only if lastgc was set, gc.sh succeeded and now it's not set
1634 if ($lastgc) {
1635 my $newlastgc = get_git("--git-dir=$project->{path}", 'config', '--get', 'gitweb.lastgc');
1636 if (!$newlastgc) {
1637 system({$args[0]} @args) != 0 and return 1;
1640 return 0;
1643 sub cmd_update {
1644 my ($force, $summary);
1645 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, summary => \$summary);
1646 $quiet && $summary and die "--quiet and --summary are mutually exclusive options\n";
1647 @ARGV <= 1 or die_usage;
1648 my $project = get_project_harder_gently($ARGV[0]);
1649 defined($project) or die "Please give project name on command line.\n";
1650 $project->{mirror} or die "Project \"$$project{name}\" is a push project, not a mirror project.\n";
1651 delete $ENV{show_progress};
1652 delete $ENV{force_update};
1653 if ($quiet) {
1654 $ENV{"show_progress"} = 0;
1655 } else {
1656 $ENV{"show_progress"} = ($summary ? 1 : 2);
1658 $force and $ENV{"force_update"} = 1;
1659 system($Girocco::Config::basedir . "/jobd/update.sh", $project->{name}) != 0 and return 1;
1660 return 0;
1663 sub cmd_remirror {
1664 my $force = 0;
1665 parse_options(force => \$force, quiet => \$quiet, q => \$quiet);
1666 @ARGV or die "Please give project name on command line.\n";
1667 @ARGV == 1 or die_usage;
1668 my $project = get_project_harder($ARGV[0]);
1669 $project->{mirror} or die "Project \"$ARGV[0]\" is a push project, not a mirror project.\n";
1670 if ($project->{clone_in_progress} && !$project->{clone_failed}) {
1671 warn "Project \"$ARGV[0]\" already seems to have a clone underway at this moment.\n" unless $quiet && $force;
1672 exit(255) unless $force;
1673 yes_to_continue_or_die("Are you sure you want to force a remirror");
1675 unlink($project->_clonefail_path);
1676 unlink($project->_clonelog_path);
1677 recreate_file($project->_clonep_path);
1678 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or
1679 die "cannot connect to taskd.socket: $!\n";
1680 select((select($sock),$|=1)[0]);
1681 $sock->print("clone ".$project->{name}."\n");
1682 # Just ignore reply, we are going to succeed anyway and the I/O
1683 # would apparently get quite hairy.
1684 $sock->flush();
1685 sleep 2; # *cough*
1686 $sock->close();
1687 warn "Project \"$ARGV[0]\" remirror initiated.\n" unless $quiet;
1688 return 0;
1691 sub cmd_setowner {
1692 my $force = 0;
1693 parse_options("force" => \$force);
1694 @ARGV == 2 || (@ARGV <= 1 && !$force && !$setopt) or die_usage;
1695 my $project = get_project_harder_gently($ARGV[0]);
1696 defined($project) or die_usage;
1697 if (@ARGV == 2 && !valid_email($ARGV[1])) {
1698 die "invalid owner/email (use --force to accept): \"$ARGV[1]\"\n"
1699 unless $force;
1700 warn "using invalid owner/email with --force\n" unless $quiet;
1702 if (@ARGV == 2 && length($ARGV[1]) > 96) {
1703 die "owner/email longer than 96 chars (use --force to accept): \"$ARGV[1]\"\n"
1704 unless $force;
1705 warn "using longer than 96 char owner/email with --force\n" unless $quiet;
1707 my $old = $project->{email};
1708 if (@ARGV <= 1) {
1709 print "$old\n" if defined($old);
1710 return 0;
1712 if (defined($old) && $old eq $ARGV[1]) {
1713 warn $project->{name}, ": skipping update of owner/email to same value\n" unless $quiet;
1714 } else {
1715 # Avoid touching anything other than "gitweb.owner"
1716 $project->_property_fput("email", $ARGV[1]);
1717 $project->_update_index;
1718 $project->_set_changed;
1719 warn $project->{name}, ": owner/email updated to \"$ARGV[1]\"\n" unless $quiet;
1721 return 0;
1724 sub cmd_setdesc {
1725 my $force = 0;
1726 parse_options("force" => \$force);
1727 @ARGV >= 2 || (@ARGV <= 1 && !$force && !$setopt) or die_usage;
1728 my $project = get_project_harder_gently($ARGV[0]);
1729 defined($project) or die_usage;
1730 shift(@ARGV) if @ARGV;
1731 if (@ARGV && !valid_desc(join(" ", @ARGV))) {
1732 die "invalid description (use --force to accept): \"".join(" ", @ARGV)."\"\n"
1733 unless $force;
1734 warn "using invalid description with --force\n" unless $quiet;
1736 my $desc = clean_desc(join(" ", @ARGV));
1737 if (@ARGV && length($desc) > 1024) {
1738 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
1739 unless $force;
1740 warn "using longer than 1024 char description with --force\n" unless $quiet;
1742 my $old = $project->{desc};
1743 if (!@ARGV) {
1744 print "$old\n" if defined($old);
1745 return 0;
1747 if (defined($old) && $old eq $desc) {
1748 warn $project->{name}, ": skipping update of description to same value\n" unless $quiet;
1749 } else {
1750 # Avoid touching anything other than description file
1751 $project->_property_fput("desc", $desc);
1752 $project->_set_changed;
1753 warn $project->{name}, ": description updated to \"$desc\"\n" unless $quiet;
1755 return 0;
1758 sub cmd_setreadme {
1759 my ($force, $readmetype) = (0, undef);
1760 parse_options(force => \$force, ":type" => \$readmetype, ":format" => \$readmetype);
1761 @ARGV == 1 && defined($readmetype) and push(@ARGV, undef);
1762 @ARGV == 2 || (@ARGV <= 1 && !$force && !defined($readmetype) && !$setopt) or die_usage;
1763 defined($readmetype) and $readmetype = Girocco::Project::_normalize_rmtype($readmetype,1);
1764 defined($readmetype) && !$readmetype and die_usage;
1765 my $project = get_project_harder_gently($ARGV[0]);
1766 defined($project) or die_usage;
1767 my $old = $project->{READMEDATA};
1768 if (@ARGV <= 1) {
1769 chomp $old if defined($old);
1770 print "$old\n" if defined($old) && $old ne "";
1771 return 0;
1773 $readmetype or $readmetype = $project->{rmtype};
1774 my ($new, $raw, $newname);
1775 $newname = '';
1776 if (!defined($ARGV[1])) {
1777 $new = $old;
1778 $newname = "original README data";
1779 $readmetype ne $project->{rmtype} && $new ne "" and $raw = 1;
1780 } elsif ($ARGV[1] eq "-") {
1781 local $/;
1782 $new = <STDIN>;
1783 $raw = 1;
1784 $newname = "contents of <STDIN>";
1785 } elsif (lc($ARGV[1]) eq "automatic" || lc($ARGV[1]) eq "auto") {
1786 $new = "";
1787 } elsif (lc($ARGV[1]) eq "suppressed" || lc($ARGV[1]) eq "suppress") {
1788 $new = "<!-- suppress -->";
1789 } else {
1790 my $fn = $ARGV[1];
1791 $fn =~ s/^\@//;
1792 die "missing filename for README\n" unless $fn ne "";
1793 die "no such file: \"$fn\"\n" unless -f $fn && -r $fn;
1794 open F, '<', $fn or die "cannot open \"$fn\" for reading: $!\n";
1795 local $/;
1796 $new = <F>;
1797 close F;
1798 $raw = 1;
1799 $newname = "contents of \"$fn\"";
1801 defined($new) or $new = '';
1802 my $origrmtype = $project->{rmtype};
1803 $project->{rmtype} = $readmetype;
1804 $project->{READMEDATA} = to_utf8($new, 1);
1805 $project->_cleanup_readme;
1806 if (length($project->{READMEDATA}) > 8192) {
1807 die "readme greater than 8192 chars is too long (use --force to override)\n"
1808 unless $force;
1809 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
1811 if ($raw) {
1812 my ($cnt, $err) = $project->_lint_readme(0);
1813 if ($cnt) {
1814 my $msg = "README: $cnt error";
1815 $msg .= "s" unless $cnt == 1;
1816 print STDERR "$msg\n", "-" x length($msg), "\n", $err
1817 unless $force && $quiet;
1818 exit(255) unless $force && $project->{rmtype} eq 'HTML';
1819 warn $project->{name} . ": using invalid raw HTML with --force\n" unless $quiet;
1820 $project->{README} = $project->{READMEDATA};
1823 if (defined($old) && $old eq $project->{READMEDATA} && $readmetype eq $origrmtype && !$force) {
1824 warn $project->{name}, ": skipping update of README to same value\n" unless $quiet;
1825 } else {
1826 # Avoid touching anything other than README.html file
1827 $project->_property_fput("READMEDATA", $project->{READMEDATA}, 1);
1828 $project->_property_fput("README", $project->{README});
1829 $project->_property_fput("rmtype", $readmetype) if $readmetype ne $origrmtype;
1830 $project->_set_changed;
1831 my $desc = get_readme_desc($project->{README});
1832 if ($newname) {
1833 $newname .= " ($desc)";
1834 } else {
1835 $newname = $desc;
1837 warn $project->{name}, ": README $readmetype format updated to $newname\n" unless $quiet;
1839 return 0;
1842 sub valid_head {
1843 my ($proj, $newhead) = @_;
1844 my %okheads = map({($_ => 1)} $proj->get_heads);
1845 exists($okheads{$newhead});
1848 sub cmd_sethead {
1849 my $force = 0;
1850 parse_options(force => \$force);
1851 @ARGV == 2 || (@ARGV <= 1 && !$setopt) or die_usage;
1852 my $project = get_project_harder_gently($ARGV[0]);
1853 defined($project) or die_usage;
1854 if (@ARGV == 2 && !valid_head($project, $ARGV[1])) {
1855 die "invalid head (try \"@{[basename($0)]} listheads $ARGV[0]\"): \"$ARGV[1]\"\n" unless $force;
1856 valid_branch_name($ARGV[1]) or
1857 die "grossly invalid new HEAD: $ARGV[1]\n";
1858 warn "Continuing with --force even though requested branch does not exist\n" unless $quiet;
1860 my $old = $project->{HEAD};
1861 if (@ARGV <= 1) {
1862 print "$old\n" if defined($old);
1863 return 0;
1865 if (defined($old) && $old eq $ARGV[1]) {
1866 warn $project->{name}, ": skipping update of HEAD symref to same value\n" unless $quiet;
1867 } else {
1868 # Avoid touching anything other than the HEAD symref
1869 $project->set_HEAD($ARGV[1]);
1870 $project->_set_changed;
1871 warn $project->{name}, ": HEAD symref updated to \"refs/heads/$ARGV[1]\"\n" unless $quiet;
1873 return 0;
1876 sub cmd_sethooks {
1877 my $force = 0;
1878 parse_options(force => \$force);
1879 @ARGV == 2 || (@ARGV <= 1 && !$force && !$setopt) or die_usage;
1880 my $project = get_project_harder_gently($ARGV[0]);
1881 defined($project) or die_usage;
1882 my $projconfig = read_config_file_hash($project->{path}."/config");
1883 my $ghp = $Girocco::Config::reporoot."/_global/hooks";
1884 my $rghp = realpath($ghp);
1885 my $lhp = $project->{path}."/hooks";
1886 my $rlhp = realpath($lhp);
1887 my $ahp = "";
1888 my $rahp = undef;
1889 if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) {
1890 $ahp = $projconfig->{"core.hookspath"};
1891 $rahp = realpath($ahp);
1893 if (@ARGV <= 1) {
1894 if (defined($rahp) && $rahp ne "") {
1895 if ($rahp eq $rghp) {
1896 my $nc = ($ahp eq $ghp ? "" : " non-canonical");
1897 printf "%s \t(global%s)\n", $ahp, $nc;
1898 } elsif ($rahp eq $rlhp) {
1899 my $nc = ($ahp eq $lhp ? "" : " non-canonical");
1900 printf "%s \t(local%s)\n", $ahp, $nc;
1901 } elsif ($rahp ne $ahp) {
1902 print "$ahp \t($rahp)\n";
1903 } else {
1904 print "$ahp\n";
1906 } elsif ($ahp ne "") {
1907 print "$ahp \t(non-existent)\n";
1909 return 0;
1911 my $shp = $ARGV[1];
1912 if (lc($shp) eq "global") {
1913 $shp = $ghp;
1914 } elsif (lc($shp) eq "local") {
1915 $shp = $lhp;
1916 } elsif (substr($shp, 0, 2) eq "~/") {
1917 $shp = $ENV{"HOME"}.substr($shp,1);
1918 } elsif ($shp =~ m,^~([a-zA-Z_][a-zA-Z_0-9]*)((?:/.*)?)$,) {
1919 my $sfx = $2;
1920 my $hd = (getpwnam($1))[7];
1921 $shp = $hd . $sfx if defined($hd) && $hd ne "" && $hd ne "/" && -d $hd;
1923 $shp ne "" && -d $shp or die "no such directory: $ARGV[1]\n";
1924 my $rshp = realpath($shp);
1925 defined($rshp) && $rshp ne "" or die "could not realpath: $ARGV[1]\n";
1926 $rshp =~ m,^/[^/], or die "invalid hookspath: $rshp\n";
1927 die "refusing to switch from current non-global hookspath without --force\n"
1928 if !$force && defined($rahp) && $rahp ne "" && $rahp ne $rghp && $rshp ne $rahp;
1929 if (!$force && defined($rahp) && $rahp ne "") {
1930 if ($rshp eq $rahp && ($ahp eq $ghp || $ahp eq $lhp)) {
1931 warn $project->{name}, ": skipping update of hookspath to same effective value\n" unless $quiet;
1932 return 0;
1935 $rshp = $ghp if $rshp eq $rghp;
1936 $rshp = $lhp if $rshp eq $rlhp;
1937 if ($rshp eq $ahp) {
1938 warn $project->{name}, ": skipping update of hookspath to same value\n" unless $quiet;
1939 return 0;
1941 die "refusing to set neither local nor global hookspath without --force\n"
1942 if !$force && $rshp ne $ghp && $rshp ne $lhp;
1943 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1944 'config', "core.hookspath", $rshp);
1945 my $newval = '"'.$rshp.'"';
1946 $newval = "global" if $rshp eq $ghp;
1947 $newval = "local" if $rshp eq $lhp;
1948 warn $project->{name}, ": hookspath set to $newval\n" unless $quiet;
1949 return 0;
1952 our %boolfields;
1953 BEGIN {
1954 %boolfields = (
1955 cleanmirror => 1,
1956 reverseorder => 0,
1957 summaryonly => 0,
1958 statusupdates => 1,
1962 sub cmd_setbool {
1963 my $force = 0;
1964 parse_options("force" => \$force);
1965 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1966 my $project = get_project_harder($ARGV[0]);
1967 if (!exists($boolfields{$ARGV[1]})) {
1968 die "invalid boolean field name: \"$ARGV[1]\" -- try \"help\"\n";
1970 if (@ARGV == 3 && $boolfields{$ARGV[1]} && !$project->{mirror}) {
1971 die "invalid boolean field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
1972 unless $force;
1973 warn "using mirror field on non-mirror with --force\n" unless $quiet;
1975 if (@ARGV == 3 && !valid_bool($ARGV[2])) {
1976 die "invalid boolean value: \"$ARGV[2]\"\n";
1978 my $bool = clean_bool($ARGV[2]);
1979 my $old = $project->{$ARGV[1]};
1980 if (@ARGV == 2) {
1981 print "$old\n" if defined($old);
1982 return 0;
1984 if (defined($old) && $old eq $bool) {
1985 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
1986 } else {
1987 # Avoid touching anything other than $ARGV[1] field
1988 $project->_property_fput($ARGV[1], $bool);
1989 warn $project->{name}, ": $ARGV[1] updated to $bool\n" unless $quiet;
1991 return 0;
1994 sub cmd_setjsontype {
1995 @ARGV == 2 || (@ARGV <= 1 && !$setopt) or die_usage;
1996 my $project = get_project_harder_gently($ARGV[0]);
1997 defined($project) or die_usage;
1998 my $jsontype;
1999 if (@ARGV == 2) {
2000 my $jt = lc($ARGV[1]);
2001 index($jt, "/") >= 0 or $jt = "application/".$jt;
2002 $jt eq 'application/x-www-form-urlencoded' ||
2003 $jt eq 'application/json' or
2004 die "invalid jsontype value: \"$ARGV[1]\"\n";
2005 $jsontype = $jt;
2007 my $old = $project->{jsontype};
2008 if (@ARGV <= 1) {
2009 print "$old\n" if defined($old);
2010 return 0;
2012 if (defined($old) && $old eq $jsontype) {
2013 warn $project->{name}, ": skipping update of jsontype to same value\n" unless $quiet;
2014 } else {
2015 # Avoid touching anything other than jsontype field
2016 $project->_property_fput('jsontype', $jsontype);
2017 warn $project->{name}, ": jsontype updated to $jsontype\n" unless $quiet;
2019 return 0;
2022 sub cmd_setjsonsecret {
2023 @ARGV == 2 || (@ARGV <= 1 && !$setopt) or die_usage;
2024 my $project = get_project_harder_gently($ARGV[0]);
2025 defined($project) or die_usage;
2026 my $jsonsecret;
2027 if (@ARGV == 2) {
2028 my $js = $ARGV[1];
2029 $js =~ s/^\s+//; $js =~ s/\s+$//;
2030 $jsonsecret = $js;
2032 my $old = $project->{jsonsecret};
2033 if (@ARGV <= 1) {
2034 print "$old\n" if defined($old);
2035 return 0;
2037 if (defined($old) && $old eq $jsonsecret) {
2038 warn $project->{name}, ": skipping update of jsonsecret to same value\n" unless $quiet;
2039 } else {
2040 # Avoid touching anything other than jsonsecret field
2041 $project->_property_fput('jsonsecret', $jsonsecret);
2042 warn $project->{name}, ": jsonsecret updated to \"$jsonsecret\"\n" unless $quiet;
2044 return 0;
2047 sub cmd_setautogchack {
2048 @ARGV == 2 || (@ARGV <= 1 && !$setopt) or die_usage;
2049 my $project = get_project_harder_gently($ARGV[0]);
2050 defined($project) or die_usage;
2051 my $aghok = $Girocco::Config::autogchack &&
2052 ($project->{mirror} || $Girocco::Config::autogchack ne "mirror");
2053 my $old = defined($project->{autogchack}) ? clean_bool($project->{autogchack}) : "unset";
2054 if (@ARGV == 1) {
2055 print "$old\n" if $aghok;
2056 return 0;
2058 my $bool;
2059 if (lc($ARGV[1]) eq "unset") {
2060 $bool = "unset";
2061 } else {
2062 valid_bool($ARGV[1]) or die "invalid boolean value: \"$ARGV[1]\"\n";
2063 $bool = clean_bool($ARGV[1]);
2065 if (!$aghok) {
2066 die "\$Girocco::Config::autogchack is false\n" unless $Girocco::Config::autogchack;
2067 die "\$Girocco::Config::autogchack is only enabled for mirrors\n";
2069 if ($old eq $bool) {
2070 warn $project->{name}, ": autogchack value unchanged\n" unless $quiet;
2071 } else {
2072 if ($bool eq "unset") {
2073 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
2074 'config', '--unset', "girocco.autogchack");
2075 } else {
2076 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
2077 'config', '--bool', "girocco.autogchack", $bool);
2080 return system($Girocco::Config::basedir . "/jobd/maintain-auto-gc-hack.sh", $project->{name}) == 0
2081 ? 0 : 1;
2084 sub valid_url {
2085 my ($url, $type) = @_;
2086 $type ne 'baseurl' and return valid_web_url($url);
2087 valid_repo_url($url) or return 0;
2088 if ($Girocco::Config::restrict_mirror_hosts) {
2089 my $mh = extract_url_hostname($url);
2090 is_dns_hostname($mh) or return 0;
2091 !is_our_hostname($mh) or return 0;
2093 return 1;
2096 our %urlfields;
2097 BEGIN {
2098 %urlfields = (
2099 baseurl => ["url" , 1],
2100 homepage => ["hp" , 0],
2101 notifyjson => ["notifyjson", 0],
2105 sub cmd_seturl {
2106 my $force = 0;
2107 parse_options("force" => \$force);
2108 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
2109 my $project = get_project_harder($ARGV[0]);
2110 if (!exists($urlfields{$ARGV[1]})) {
2111 die "invalid URL field name: \"$ARGV[1]\" -- try \"help\"\n";
2113 if (@ARGV == 3 && ${$urlfields{$ARGV[1]}}[1] && !$project->{mirror}) {
2114 die "invalid URL field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
2115 unless $force;
2116 warn "using mirror field on non-mirror with --force\n" unless $quiet;
2118 if (@ARGV == 3 && !valid_url($ARGV[2], $ARGV[1])) {
2119 die "invalid URL (use --force to accept): \"$ARGV[2]\"\n"
2120 unless $force;
2121 warn "using invalid URL with --force\n" unless $quiet;
2123 my $old = $project->{${$urlfields{$ARGV[1]}}[0]};
2124 if (@ARGV == 2) {
2125 print "$old\n" if defined($old);
2126 return 0;
2128 if (defined($old) && $old eq $ARGV[2]) {
2129 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
2130 } else {
2131 # Avoid touching anything other than $ARGV[1]'s field
2132 $project->_property_fput(${$urlfields{$ARGV[1]}}[0], $ARGV[2]);
2133 if ($ARGV[1] eq "baseurl") {
2134 $project->{url} = $ARGV[2];
2135 $project->_set_bangagain;
2137 $project->_set_changed unless $ARGV[1] eq "notifyjson";
2138 warn $project->{name}, ": $ARGV[1] updated to $ARGV[2]\n" unless $quiet;
2140 return 0;
2143 our %msgsfields;
2144 BEGIN {
2145 %msgsfields = (
2146 notifymail => 1,
2147 notifytag => 1,
2151 sub cmd_setmsgs {
2152 my $force = 0;
2153 parse_options("force" => \$force);
2154 @ARGV >= 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
2155 my $project = get_project_harder(shift @ARGV);
2156 my $field = shift @ARGV;
2157 if (!exists($msgsfields{$field})) {
2158 die "invalid msgs field name: \"$field\" -- try \"help\"\n";
2160 if (@ARGV && !valid_addrlist(@ARGV)) {
2161 die "invalid email address list (use --force to accept): \"".join(" ",@ARGV)."\"\n"
2162 unless $force;
2163 warn "using invalid email address list with --force\n" unless $quiet;
2165 my $old = $project->{$field};
2166 if (!@ARGV) {
2167 printf "%s\n", clean_addrlist($old, " ") if defined($old);
2168 return 0;
2170 my $newlist = clean_addrlist(join(" ",@ARGV));
2171 if (defined($old) && $old eq $newlist) {
2172 warn $project->{name}, ": skipping update of $field to same value\n" unless $quiet;
2173 } else {
2174 # Avoid touching anything other than $field's field
2175 $project->_property_fput($field, $newlist);
2176 warn $project->{name}, ": $field updated to \"$newlist\"\n" unless $quiet;
2178 return 0;
2181 sub cmd_setusers {
2182 my $force = 0;
2183 parse_options("force" => \$force);
2184 @ARGV >= 2 || (@ARGV <= 1 && !$force && !$setopt) or die_usage;
2185 my $project = get_project_harder_gently($ARGV[0]);
2186 defined($project) or die_usage;
2187 shift(@ARGV) if @ARGV;
2188 my $projname = $project->{name};
2189 !@ARGV || !$project->{mirror} or die "cannot set users list for mirror project: \"$projname\"\n";
2190 my @newusers = ();
2191 if (@ARGV) {
2192 eval {@newusers = validate_users(join(" ", @ARGV), $force); 1;} or exit 255;
2193 die "refusing to set empty users list without --force\n" unless @newusers || $force;
2195 return 0 if !@ARGV && $project->{mirror};
2196 my $oldusers = $project->{users};
2197 if ($oldusers && ref($oldusers) eq "ARRAY") {
2198 $oldusers = join("\n", @$oldusers);
2199 } else {
2200 $oldusers = "";
2202 if (!@ARGV) {
2203 print "$oldusers\n" if $oldusers ne "";
2204 return 0;
2206 if ($oldusers eq join("\n", @newusers)) {
2207 warn "$projname: skipping update of users list to same value\n" unless $quiet;
2208 } else {
2209 # Avoid touching anything other than the users list
2210 $project->{users} = \@newusers;
2211 $project->_update_users;
2212 warn "$projname: users list updated to \"@{[join(',',@newusers)]}\"\n" unless $quiet;
2214 return 0;
2217 our %fieldnames;
2218 BEGIN {
2219 %fieldnames = (
2220 owner => [\&cmd_setowner, 0],
2221 desc => [\&cmd_setdesc, 0],
2222 description => [\&cmd_setdesc, 0],
2223 readme => [\&cmd_setreadme, 0],
2224 head => [\&cmd_sethead, 0],
2225 HEAD => [\&cmd_sethead, 0],
2226 hooks => [\&cmd_sethooks, 0],
2227 hookspath => [\&cmd_sethooks, 0],
2228 cleanmirror => [\&cmd_setbool, 1],
2229 reverseorder => [\&cmd_setbool, 1],
2230 summaryonly => [\&cmd_setbool, 1],
2231 statusupdates => [\&cmd_setbool, 1],
2232 autogchack => [\&cmd_setautogchack, 0],
2233 baseurl => [\&cmd_seturl, 1],
2234 homepage => [\&cmd_seturl, 1],
2235 notifyjson => [\&cmd_seturl, 1],
2236 jsontype => [\&cmd_setjsontype, 0],
2237 jsonsecret => [\&cmd_setjsonsecret, 0],
2238 notifymail => [\&cmd_setmsgs, 1],
2239 notifytag => [\&cmd_setmsgs, 1],
2240 users => [\&cmd_setusers, 0],
2244 sub do_getset {
2245 $setopt = shift;
2246 my @newargs = ();
2247 push(@newargs, shift) if @_ && $_[0] eq '--force';
2248 my $field = $_[1];
2249 (($setopt && @_ >= 3) || @_ == 2) && exists($fieldnames{$field}) or die_usage;
2250 push(@newargs, shift);
2251 shift unless ${$fieldnames{$field}}[1];
2252 push(@newargs, @_);
2253 diename(($setopt ? "set " : "get ") . $field);
2254 @ARGV = @newargs;
2255 &{${$fieldnames{$field}}[0]}(@ARGV);
2258 sub cmd_get {
2259 do_getset(0, @_);
2262 sub cmd_set {
2263 do_getset(1, @_);
2266 our %commands;
2267 BEGIN {
2268 %commands = (
2269 list => \&cmd_list,
2270 create => \&cmd_create,
2271 adopt => \&cmd_adopt,
2272 remove => \&cmd_remove,
2273 trash => \&cmd_remove,
2274 delete => \&cmd_remove,
2275 prune => \&cmd_prune,
2276 show => \&cmd_show,
2277 verify => \&cmd_verify,
2278 worktree => \&cmd_worktree,
2279 worktrees => \&cmd_worktree,
2280 urls => \&cmd_urls,
2281 listheads => \&cmd_listheads,
2282 listtags => \&cmd_listtags,
2283 listctags => \&cmd_listtags,
2284 deltags => \&cmd_deltags,
2285 delctags => \&cmd_deltags,
2286 addtags => \&cmd_addtags,
2287 addctags => \&cmd_addtags,
2288 chpass => \&cmd_chpass,
2289 checkpw => \&cmd_checkpw,
2290 gc => \&cmd_gc,
2291 update => \&cmd_update,
2292 remirror => \&cmd_remirror,
2293 setowner => \&cmd_setowner,
2294 setdesc => \&cmd_setdesc,
2295 setdescription => \&cmd_setdesc,
2296 setreadme => \&cmd_setreadme,
2297 sethead => \&cmd_sethead,
2298 sethooks => \&cmd_sethooks,
2299 sethookspath => \&cmd_sethooks,
2300 setbool => \&cmd_setbool,
2301 setboolean => \&cmd_setbool,
2302 setflag => \&cmd_setbool,
2303 setautogchack => \&cmd_setautogchack,
2304 seturl => \&cmd_seturl,
2305 setjsontype => \&cmd_setjsontype,
2306 setjsonsecret => \&cmd_setjsonsecret,
2307 setmsgs => \&cmd_setmsgs,
2308 setusers => \&cmd_setusers,
2309 get => \&cmd_get,
2310 set => \&cmd_set,
2313 our %nopager;
2314 BEGIN { %nopager = (
2315 # 1 => pager never allowed
2316 # -1 => pager defaults to off instead of on
2317 create => 1,
2318 adopt => -1,
2319 remove => -1,
2320 trash => -1,
2321 delete => -1,
2322 prune => -1,
2323 deltags => -1,
2324 delctags => -1,
2325 addtags => -1,
2326 addctags => -1,
2327 chpass => 1,
2328 checkpw => 1,
2329 gc => -1,
2330 update => -1,
2331 remirror => -1,
2332 setowner => -1,
2333 setdesc => -1,
2334 setdescription => -1,
2335 setreadme => -1,
2336 sethead => -1,
2337 sethooks => -1,
2338 sethookspath => -1,
2339 setbool => -1,
2340 setboolean => -1,
2341 setflag => -1,
2342 setautogchack => -1,
2343 seturl => -1,
2344 setjsontype => -1,
2345 setjsonsecret => -1,
2346 setmsgs => -1,
2347 setusers => -1,
2348 set => -1,
2349 urls => -1,
2350 verify => 1,
2351 worktree => 1,
2352 worktrees => 1,
2355 sub dohelp {
2356 my $cmd = shift;
2357 my $bn = basename($0);
2358 setup_pager_stdout($usepager);
2359 printf "%s version %s\n\n", $bn, $VERSION;
2360 if (defined($cmd) && $cmd ne '') {
2361 $cmd =~ s/^set(?=[a-zA-Z])//i;
2362 my $cmdhelp = '';
2363 my ($lastmt, $incmd);
2364 foreach (split('\n', sprintf($help, $bn))) {
2365 $lastmt || $incmd or $lastmt = /^\s*$/, next;
2366 $incmd = 1 if $lastmt && /^\s*(?:\[?set\]?)?$cmd\s/;
2367 last if $incmd && /^\s*$/;
2368 $incmd and $cmdhelp .= $_ . "\n";
2369 $lastmt = /^\s*$/;
2371 print $cmdhelp and exit 0 if $cmdhelp;
2373 printf $help, $bn;
2374 exit 0;
2377 sub main {
2378 local *ARGV = \@_;
2380 shift, $quiet=1, redo if @ARGV && $ARGV[0] =~ /^(?:-q|--quiet)$/i;
2381 shift, $usepager=1, redo if @ARGV && $ARGV[0] =~ /^(?:-p|--pager|--paginate)$/i;
2382 shift, $usepager=0, redo if @ARGV && $ARGV[0] =~ /^(?:--no-pager|--no-paginate)$/i;
2384 dohelp($ARGV[1]) if !@ARGV || @ARGV && $ARGV[0] =~ /^(?:-h|-?-help|help)$/i;
2385 my $command = shift;
2386 diename($command);
2387 $setopt = 1;
2388 if (!exists($commands{$command}) && exists($commands{"set".$command})) {
2389 $setopt = 0;
2390 $command = "set" . $command;
2392 exists($commands{$command}) or die "Unknown command \"$command\" -- try \"help\"\n";
2393 dohelp($command) if @ARGV && ($ARGV[0] =~ /^(?:-h|-?-help)$/i ||
2394 $ARGV[0] =~ /^help$/i && !Girocco::Project::does_exist("help",1));
2395 $nopager{$command} && $nopager{$command} > 0 and $usepager = 0;
2396 my $pgdfltoff = $nopager{$command} && $nopager{$command} < 0 ? 1 : 0;
2397 setup_pager_stdout($usepager, $pgdfltoff);
2398 &{$commands{$command}}(@ARGV);