projtool.pl: support urls --bundle
[girocco.git] / toolbox / projtool.pl
blob51bc9da6f09da74b19bcc690841f460e2d3bae1f
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.9'}
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 | --bundle] <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)
146 With --bundle show downloadble bundle urls instead
148 listheads <project>
149 list all available heads for <project> and indicate current head
151 listtags [--verbose] <project>
152 list all ctags on project <project>
153 with --verbose include tag counts
155 deltags <project> [-i] <tagstodel>
156 remove any ctags on project <project> present in <tagstodel>
157 <tagstodel> is space or comma separated list of tags to remove
158 with -i match against <tagstodel> without regard to letter case
160 addtags <project> <tagstoadd>
161 add ctags to project <project>
162 <tagstoadd> is space or comma separated list of tags to add
164 chpass [--force] <project> [random | unknown]
165 change project <project> password (prompted)
166 with "random" set to random password
167 with "unknown" set password hash to invalid value "unknown"
169 checkpw <project>
170 check project <project> password for a match (prompted)
172 gc [--force | --auto] [--redelta | --recompress] <project>
173 run the gc.sh script on project <project>
174 with --auto let the gc.sh script decide what to do
175 with --force cause a full gc to take place (force_gc=1)
176 with neither --auto nor --force do a mini or if needed a full gc
177 (in other words just touch .needsgc and run gc.sh)
178 with --redelta a full gc will use pack-objects --no-reuse-delta
179 with --recompress a full gc uses pack-objects --no-reuse-object
180 (--no-reuse-delta and --no-reuse-object are accepted as aliases)
181 with --aggressive activate the --force and --redelta options
182 unless the global --quiet option is given show_progress=1 is used
184 update [--force] [--quiet | --summary] <project>
185 run the update.sh script on project <project>
186 with --force cause a fetch to always take place (force_update=1)
187 with --quiet only show errors (show_progress is left unset)
188 with --summary show progress and ref summary (show_progress=1)
189 with neither --quiet nor --summary show it all (show_progress=2)
191 remirror [--force] <project>
192 initiate a remirror of project <project>
194 [set]owner [--force] <project> <newowner>
195 set project <project> owner to <newowner>
196 without "set" and only 1 arg, just show current project owner
198 [set]desc [--force] <project> <newdesc>
199 set project <project> description to <newdesc>
200 without "set" and only 1 arg, just show current project desc
202 [set]readme [--force] [--format=<readmetype>] <project> [<newsetting>]
203 set project <project> readme to <newsetting>
204 <readmetype> is markdown|plain|html (default is no change)
205 <newsetting> is automatic|suppressed|-|[@]filename
206 with "set" <readmetype> and/or <newsetting> is required
207 without "set" and only 1 arg, just show current readme setting
209 [set]head [--force] <project> <newhead>
210 set project <project> HEAD symbolic ref to <newhead>
211 without "set" and only 1 arg, just show current project HEAD
213 [set]bool [--force] <project> <flagname> <boolvalue>
214 set project <project> boolean <flagname> to <boolvalue>
215 <flagname> is cleanmirror|reverseorder|summaryonly|statusupdtaes
216 without "set" and only 2 args, just show current flag value
218 [set]hooks [--force] <project> local | global | <path>
219 set project <project> hookspath to local, global or <path>
220 without "set" and only 1 arg, just show current hookspath
222 [set]autogchack <project> <boolvalue> | unset
223 set project <project> autogchack to <boolvalue> or "unset" it
224 without "set" just show current autogchack setting if enabled
225 with "set" autogchack must be enabled in Config.pm for the
226 type of project and maintain-auto-gc-hack.sh is always run
228 [set]url [--force] <project> <urlname> <newurlvalue>
229 set project <project> url <urlname> to <newurlvalue>
230 <urlname> is baseurl|homepage|notifyjson
231 without "set" and only 2 args, just show current url value
233 [set]msgs [--force] <project> <msgsname> <eaddrlist>
234 set project <project> msgs <msgsname> to <addrlist>
235 <msgsname> is notifymail|notifytag
236 <eaddrlist> is space or comma separated list of email addresses
237 without "set" and only 2 args, just show current msgs value
239 [set]users [--force] <project> <newuserslist>
240 set push project <project> users list to <newuserslist>
241 <newuserslist> is space or comma separated list of user names
242 without "set" and only 1 arg, just show current users list
244 [set]jsontype <project> <newjsontype>
245 set project <project> JSON Content-Type to <newjsontype>
246 <newjsontype> is x-www-form-urlencoded or json
247 without "set" and only 1 arg, just show current jsontype
249 [set]jsonsecret <project> <newjsonsecret>
250 set project <project> JSON secret to <newjsonsecret>
251 <newjsonsecret> is a string (empty string disables signatures)
252 without "set" and only 1 arg, just show current jsonsecret
254 get <project> <fieldname>
255 show project <project> field <fieldname>
256 <fieldname> is owner|desc|readme|head|hooks|users|jsontype
257 or jsonsecret|<flagname>|autogchack|<urlname>|<msgsname>
259 set [--force] <project> <fieldname> <newfieldvalue>
260 set project <project> field <fieldname> to <newfieldvalue>
261 <fieldname> same as for get
262 <newfieldvalue> same as for corresponding set... command
263 HELP
265 our $quiet;
266 our $usepager;
267 our $setopt;
268 sub die_usage {
269 my $sub = shift || diename;
270 if ($sub) {
271 die "Invalid arguments to $sub command -- try \"help\"\n";
272 } else {
273 die "Invalid arguments -- try \"help\"\n";
277 sub get_readme_len {
278 my $rm = shift;
279 defined($rm) or $rm = '';
280 return "length " . length($rm);
283 sub get_readme_desc {
284 my $rm = shift;
285 defined($rm) or $rm = '';
286 if (length($rm)) {
287 my $test = $rm;
288 $test =~ s/<!--(?:[^-]|(?:-(?!-)))*-->//gs;
289 $test =~ s/\s+//s;
290 return $test eq '' ? "suppressed" : "length " . length($rm);
291 } else {
292 return "automatic";
296 sub get_ctag_counts {
297 my $project = shift;
298 my $compact = shift;
299 my @ctags = ();
300 foreach ($project->get_ctag_names) {
301 my $val = 0;
302 my $ct;
303 if (open $ct, '<', $project->{path}."/ctags/$_") {
304 my $count = <$ct>;
305 close $ct;
306 defined $count or $count = '';
307 chomp $count;
308 $val = $count =~ /^[1-9]\d*$/ ? $count : 1;
310 if ($compact) {
311 if ($val == 1) {
312 push(@ctags, $_);
313 } elsif ($val > 1) {
314 push(@ctags, $_."(".$val.")");
316 } else {
317 push(@ctags, [$_, $val]) if $val;
320 @ctags;
323 sub reftype {
324 use Scalar::Util ();
325 return ref($_[0]) ? Scalar::Util::reftype($_[0]) : ''
328 sub get_project_harder_gently {
329 defined($_[0]) && return get_project_harder(@_);
330 my $proj = eval { get_project_harder(".") };
331 reftype($proj) eq 'HASH' or $proj = undef;
332 return $proj;
335 sub get_clean_project_gently {
336 defined($_[0]) && return get_clean_project(@_);
337 my $proj = eval { get_clean_project(".") };
338 reftype($proj) eq 'HASH' or $proj = undef;
339 return $proj;
342 sub get_clean_project {
343 my $project = get_project_harder(@_);
344 delete $project->{loaded};
345 delete $project->{base_path};
346 delete $project->{ccrypt};
347 /^orig/i || !defined($project->{$_}) and delete $project->{$_} foreach keys %$project;
348 $project->{owner} = $project->{email}; delete $project->{email};
349 $project->{homepage} = $project->{hp}; delete $project->{hp};
350 $project->{baseurl} = $project->{url}; delete $project->{url};
351 if (defined($project->{path}) && $project->{path} ne "") {
352 my $rp = realpath($project->{path});
353 defined($rp) && $rp ne "" and $project->{realpath} = $rp;
354 if (-f "$rp/objects/info/packs") {
355 my $ipt = (stat _)[9];
356 defined($ipt) and $project->{infopackstime} =
357 strftime("%Y-%m-%d %H:%M:%S %z", localtime($ipt));
360 my $owner = $project->{owner};
361 if ($owner) {
362 $owner = lc($owner);
363 my @owner_users = map {$owner eq lc($$_[4]) ? $$_[1] : ()} get_all_users;
364 $project->{owner_users} = \@owner_users if @owner_users;
366 my $projname = $project->{name};
367 my @forks = grep {$$_[1] =~ m,^$projname/,} get_all_projects;
368 $project->{has_forks} = 1 if @forks;
369 $project->{has_alternates} = 1 if $project->has_alternates;
370 my @bundles = $project->bundles;
371 for (my $i = 0; $i < @bundles; ++$i) {
372 my $secs = $bundles[$i]->[0];
373 $bundles[$i]->[0] = strftime("%Y-%m-%d %H:%M:%S %z", localtime($secs));
374 my $sz = $bundles[$i]->[2];
375 1 while $sz =~ s/(?<=\d)(\d{3})(?:,|$)/,$1/g;
376 $bundles[$i]->[2] = $sz;
378 delete $project->{bundles};
379 $project->{bundles} = \@bundles if @bundles;
380 $project->{mirror} = 0 unless $project->{mirror};
381 $project->{is_empty} = 1 if $project->is_empty;
382 delete $project->{showpush} unless $project->{showpush};
383 delete $project->{users} if $project->{mirror};
384 delete $project->{baseurl} unless $project->{mirror};
385 delete $project->{banged} unless $project->{mirror};
386 delete $project->{lastrefresh} unless $project->{mirror};
387 delete $project->{cleanmirror} unless $project->{mirror};
388 delete $project->{statusupdates} unless $project->{mirror};
389 delete $project->{lastparentgc} unless $projname =~ m,/,;
390 unless ($project->{banged}) {
391 delete $project->{bangcount};
392 delete $project->{bangfirstfail};
393 delete $project->{bangmessagesent};
395 my $projhook = $project->_has_notifyhook;
396 if (defined($projhook) && $projhook ne "") {
397 $project->{notifyhook} = $projhook;
398 } else {
399 delete $project->{notifyhook};
401 $project->{README} = get_readme_desc($project->{README}) if exists($project->{README});
402 $project->{READMEDATA} = get_readme_len($project->{READMEDATA}) if exists($project->{READMEDATA});
403 my @tags = get_ctag_counts($project, 1);
404 $project->{tags} = \@tags if @tags;
405 my $projconfig = read_config_file_hash($project->{path}."/config");
406 if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) {
407 my $ahp = $projconfig->{"core.hookspath"};
408 my $rahp = realpath($ahp);
409 my $lhp = $project->{path}."/hooks";
410 my $rlhp = realpath($lhp);
411 my $ghp = $Girocco::Config::reporoot."/_global/hooks";
412 my $rghp = realpath($ghp);
413 $project->{has_local_hooks} = 1 if
414 defined($rahp) && defined($rlhp) && $rahp eq $rlhp;
415 $project->{has_global_hooks} = 1 if
416 defined($rahp) && defined($rghp) && $rahp eq $rghp;
417 $project->{hookspath} = $ahp unless $ahp eq $lhp || $ahp eq $ghp;
419 $project;
422 sub clean_addrlist {
423 my %seen = ();
424 my @newlist = ();
425 foreach (split(/[,\s]+/, $_[0])) {
426 next unless $_;
427 $seen{lc($_)} = 1, push(@newlist, $_) unless $seen{lc($_)};
429 return join(($_[1]||","), @newlist);
432 sub valid_addrlist {
433 my $cleaned = clean_addrlist(join(" ", @_));
434 return 1 if $cleaned eq "";
435 valid_email_multi($cleaned) && length($cleaned) <= 512;
438 sub validate_users {
439 my ($userlist, $force, $nodie) = @_;
440 my @newusers = ();
441 my $badlist = 0;
442 my %seenuser = ();
443 my $mobok = $Girocco::Config::mob && $Girocco::Config::mob eq "mob";
444 my %users = map({($$_[1] => $_)} get_all_users);
445 foreach (split(/[\s,]+/, $userlist)) {
446 if (exists($users{$_}) || $_ eq "everyone" || ($mobok && $_ eq "mob")) {
447 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
448 next;
450 if (Girocco::User::does_exist($_, 1)) {
451 if ($force) {
452 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
453 } else {
454 $badlist = 1;
455 warn "refusing to allow questionable user \"$_\" without --force\n" unless $nodie && $quiet;
457 next;
459 $badlist = 1;
460 warn "invalid user: \"$_\"\n" unless $nodie && $quiet
462 die if $badlist && !$nodie;
463 return @newusers;
466 sub is_default_desc {
467 # "Unnamed repository; edit this file 'description' to name the repository."
468 # "Unnamed repository; edit this file to name it for gitweb."
469 local $_ = shift;
470 return 0 unless defined($_);
471 /Unnamed\s+repository;/i && /\s+edit\s+this\s+file\s+/i && /\s+to\s+name\s+/i;
474 sub valid_desc {
475 my $test = shift;
476 chomp $test;
477 return 0 if $test =~ /[\r\n]/;
478 $test =~ s/\s\s+/ /g;
479 $test =~ s/^\s+//;
480 $test =~ s/\s+$//;
481 return $test ne '';
484 sub clean_desc {
485 my $desc = shift;
486 defined($desc) or $desc = '';
487 chomp $desc;
488 $desc = to_utf8($desc, 1);
489 $desc =~ s/\s\s+/ /g;
490 $desc =~ s/^\s+//;
491 $desc =~ s/\s+$//;
492 return $desc;
495 sub parse_options {
496 Girocco::CLIUtil::_parse_options(
497 sub {
498 warn((($_[0]eq'?')?"unrecognized":"missing argument for")." option \"$_[1]\"\n")
499 unless $quiet;
500 die_usage;
501 }, @_);
504 sub cmd_list {
505 my %sortsub = (
506 lcname => sub {lc($$a[1]) cmp lc($$b[1])},
507 name => sub {$$a[1] cmp $$b[1]},
508 gid => sub {$$a[3] <=> $$b[3]},
509 owner => sub {lc($$a[4]) cmp lc($$b[4]) || lc($$a[1]) cmp lc($$b[1])},
510 no => sub {$$a[0] <=> $$b[0]},
512 my $sortopt = 'lcname';
513 my ($verbose, $owner);
514 parse_options(":sort" => \$sortopt, verbose => \$verbose, owner => \$owner);
515 my $regex;
516 if (@ARGV) {
517 my $val = shift @ARGV;
518 $regex = qr($val) or die "bad regex \"$val\"\n";
520 !@ARGV && exists($sortsub{$sortopt}) or die_usage;
521 my $sortsub = $sortsub{$sortopt};
522 my $grepsub = defined($regex) ? ($owner ? sub {$$_[4] =~ /$regex/} : sub {$$_[1] =~ /$regex/}) : sub {1};
523 my @projects = sort($sortsub grep {&$grepsub} get_all_projects);
524 if ($verbose) {
525 print map(sprintf("%s\n", join(":", (@$_)[1..5])), @projects);
526 } else {
527 print map(sprintf("%s: %s\n", $$_[1], $$_[5] =~ /^:/ ? "<mirror>" : $$_[5]), @projects);
529 return 0;
532 sub cmd_create {
533 my ($force, $noalternates, $orphanok, $optp, $nopasswd, $noowner, $defaults, $ispush, $pushusers,
534 $ismirror, $desc, $fullmirror, $homepage);
535 parse_options(
536 force => \$force, "no-alternates" => \$noalternates, orphan => \$orphanok, p => \$optp,
537 "no-password" => \$nopasswd, "no-owner" => \$noowner, defaults => \$defaults,
538 "push" => \$ispush, ":push" => \$pushusers, ":mirror" => \$ismirror, ":desc" => \$desc,
539 ":description" => \$desc, "full-mirror" => \$fullmirror, ":homepage" => \$homepage);
540 @ARGV == 1 or die_usage;
541 !defined($pushusers) || defined($ispush) or $ispush = 1;
542 defined($ismirror) && $ismirror =~ /^\s*$/ and die "--mirror url must not be empty\n";
543 die "--mirror and --push are mutually exclusive options\n" if $ismirror && $ispush;
544 die "--full-mirror requires use of --mirror=<url> option\n" if $fullmirror && !$ismirror;
545 !$defaults || defined($ispush) || defined($ismirror) or $ispush = 1;
546 !$defaults || defined($nopasswd) or $nopasswd = 1;
547 !$defaults || defined($noowner) or $noowner = 1;
548 !defined($ispush) || defined($pushusers) or $pushusers = "";
549 my $projname = $ARGV[0];
550 $projname =~ s/\.git$//i;
551 Girocco::Project::does_exist($projname, 1) and die "Project already exists: \"$projname\"\n";
552 if (!Girocco::Project::valid_name($projname, $orphanok, $optp)) {
553 warn "Refusing to create orphan project without --orphan\n"
554 if !$quiet && !$orphanok && Girocco::Project::valid_name($projname, 1, 1);
555 warn "Required orphan parent directory does not exist (use -p): ",
556 $Girocco::Config::reporoot.'/'.Girocco::Project::get_forkee_name($projname), "\n"
557 if !$quiet && $orphanok && Girocco::Project::valid_name($projname, 1, 1);
558 die "Invalid project name: \"$projname\"\n";
560 my ($forkee, $project) = ($projname =~ m#^(.*/)?([^/]+)$#);
561 my $newtype = $forkee ? 'fork' : 'project';
562 if (length($project) > 64) {
563 die "The $newtype name is longer than 64 characters. Do you really need that much?\n"
564 unless $force;
565 warn "Allowing $newtype name longer than 64 characters with --force\n" unless $quiet;
567 unless ($Girocco::Config::push || $Girocco::Config::mirror) {
568 die "Project creation disabled (no mirrors or push projects allowed)\n" unless $force;
569 warn "Continuing with --force even though both push and mirror projects are disabled\n" unless $quiet;
571 print "Enter settings for new project \"$projname\"\n" unless $defaults;
572 my %settings = ();
573 $settings{noalternates} = $noalternates;
574 if ($nopasswd) {
575 $settings{crypt} = "unknown";
576 } else {
577 my $np1 = prompt_noecho_nl_or_die("Admin password for project $projname (echo is off)");
578 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
579 my $np2 = prompt_noecho_nl_or_die("Retype admin password for project $projname");
580 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
581 "the admin passwords you have entered do not match each other.\n";
582 $settings{crypt} = scrypt_sha1($np1);
584 my $owner = "";
585 unless ($noowner) {{
586 $owner = prompt_or_die("Owner/email name for project $projname");
587 unless (valid_email($owner)) {
588 unless ($force) {
589 warn "Your email sure looks weird...?\n";
590 redo;
592 warn "Allowing invalid email with --force\n" unless $quiet;
594 if (length($owner) > 96) {
595 unless ($force) {
596 warn "Your email is longer than 96 characters. Do you really need that much?\n";
597 redo;
599 warn "Allowing email longer than 96 characters with --force\n" unless $quiet;
602 $settings{email} = $owner;
603 my $baseurl = "";
604 my $checkmirror = sub {
605 my $checkurl = shift;
606 unless (valid_repo_url($checkurl)) {
607 unless ($force) {
608 warn "Invalid mirror URL: \"$checkurl\"\n";
609 return undef;
611 warn "Allowing invalid mirror URL with --force\n" unless $quiet;
613 if ($Girocco::Config::restrict_mirror_hosts) {
614 my $mh = extract_url_hostname($checkurl);
615 unless (is_dns_hostname($mh)) {
616 unless ($force) {
617 warn "Invalid non-DNS mirror URL: \"$checkurl\"\n";
618 return undef;
620 warn "Allowing non-DNS mirror URL with --force\n" unless $quiet;
622 if (is_our_hostname($mh)) {
623 unless ($force) {
624 warn "Invalid same-host mirror URL: \"$checkurl\"\n";
625 return undef;
627 warn "Allowing same-host mirror URL with --force\n" unless $quiet;
630 return $checkurl;
632 if ($ispush || $ismirror) {
633 !$ispush || $force || $Girocco::Config::push or
634 die "Push projects are disabled, create a mirror (or use --force)\n";
635 !$ismirror || $force || $Girocco::Config::mirror or
636 die "Mirror projects are disabled, create a push project (or use --force)\n";
637 if ($ismirror) {
638 &$checkmirror($ismirror) or die "Invalid --mirror URL\n";
639 $baseurl = $ismirror;
640 $settings{url} = $baseurl;
641 $settings{cleanmirror} = $fullmirror ? 0 : 1;
642 } else {
643 my @newusers = ();
644 if ($pushusers !~ /^[\s,]*$/) {
645 eval {@newusers = validate_users($pushusers, $force); 1;} or
646 die "Invalid --push user list\n";
648 $settings{users} = \@newusers;
650 } elsif ($force || $Girocco::Config::mirror) {{
651 if ($force || $Girocco::Config::push) {
652 $baseurl = prompt_or_die("URL to mirror from (leave blank for push project)", "");
653 } else {{
654 $baseurl = prompt_or_die("URL to mirror from");
655 unless ($baseurl ne "") {
656 warn "Push projects are disabled, you must enter a mirror URL (or use --force)\n";
657 redo;
660 if ($baseurl ne "") {
661 &$checkmirror($baseurl) or redo;
662 $settings{url} = $baseurl;
663 $settings{cleanmirror} =
664 ynprompt_or_die("Mirror only heads, tags and notes (Y/n)", "Yes");
667 my $mirror = ($baseurl eq "") ? 0 : 1;
668 my $checkdesc = sub {
669 my $d = shift;
670 if (length($d) > 1024) {
671 unless ($force) {
672 warn "Short description length greater than 1024 characters!\n";
673 return undef;
675 warn "Allowing short description length greater than 1024 characters\n" unless $quiet;
677 return $d;
679 if (defined($desc)) {
680 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
681 $desc eq "" || &$checkdesc($desc) or
682 die "Invalid --desc description\n";
683 } elsif (!$defaults) {{
684 $desc = prompt_or_die("Short description", "");
685 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
686 $desc eq "" || &$checkdesc($desc) or redo;
687 $desc = undef if $desc eq "";
689 defined($desc) or $desc = $mirror ? "Mirror of $baseurl" : "Push project $projname";
690 $settings{desc} = $desc;
691 my $checkhp = sub {
692 my $hpurl = shift;
693 unless (valid_web_url($hpurl)) {
694 unless ($force) {
695 warn "Invalid home page URL: \"$hpurl\"\n";
696 return undef;
698 warn "Allowing invalid home page URL with --force\n" unless $quiet;
700 return $hpurl;
702 if (defined($homepage)) {
703 $homepage =~ s/^\s+//; $homepage =~ s/\s+$//;
704 $homepage eq "" || &$checkhp($homepage) or
705 die "Invalid --homepage URL\n";
706 } elsif (!$defaults) {{
707 $homepage = prompt_or_die("Home page URL", "");
708 $homepage =~ s/^\s+//; $homepage =~ s/\s+$//;
709 $homepage eq "" || &$checkhp($homepage) or redo;
710 $homepage = undef if $homepage eq "";
712 $settings{hp} = $homepage;
713 my $jsonurl = "";
714 if (!$defaults) {{
715 $jsonurl = prompt_or_die("JSON notify POST URL", "");
716 if ($jsonurl ne "" && !valid_web_url($jsonurl)) {
717 unless ($force) {
718 warn "Invalid JSON notify POST URL: \$jsonurl\"\n";
719 redo;
721 warn "Allowing invalid JSON notify POST URL with --force\n" unless $quiet;
724 $settings{notifyjson} = $jsonurl;
725 my $commitaddrs = "";
726 if (!$defaults) {{
727 $commitaddrs = clean_addrlist(prompt_or_die("Commit notify email addr(s)", ""));
728 if ($commitaddrs ne "" && !valid_addrlist($commitaddrs)) {
729 unless ($force) {
730 warn"invalid commit notify email address list (use --force to accept): \"$commitaddrs\"\n";
731 redo;
733 warn "using invalid commit notify email address list with --force\n" unless $quiet;
736 $settings{notifymail} = $commitaddrs;
737 $settings{reverseorder} = 1;
738 $settings{reverseorder} = ynprompt_or_die("Oldest-to-newest commit order in emails", "Yes")
739 if !$defaults && $commitaddrs ne "";
740 $settings{summaryonly} = ynprompt_or_die("Summary only (no diff) in emails", "No")
741 if !$defaults && $commitaddrs ne "";
742 my $tagaddrs = "";
743 if (!$defaults) {{
744 $tagaddrs = clean_addrlist(prompt_or_die("Tag notify email addr(s)", ""));
745 if ($tagaddrs ne "" && !valid_addrlist($tagaddrs)) {
746 unless ($force) {
747 warn"invalid tag notify email address list (use --force to accept): \"$tagaddrs\"\n";
748 redo;
750 warn "using invalid tag notify email address list with --force\n" unless $quiet;
753 $settings{notifytag} = $tagaddrs;
754 if (!$mirror && !$ispush) {{
755 my @newusers = ();
757 my $userlist = prompt_or_die("Push users", join(",", @newusers));
758 eval {@newusers = validate_users($userlist, $force); 1;} or redo;
760 $settings{users} = \@newusers;
762 my $newproj = Girocco::Project->ghost($projname, $mirror, $orphanok, $optp)
763 or die "Girocco::Project->ghost call failed\n";
764 my ($k, $v);
765 $newproj->{$k} = $v while ($k, $v) = each(%settings);
766 my $killowner = sub {
767 system($Girocco::Config::git_bin, '--git-dir='.$newproj->{path},
768 'config', '--unset', "gitweb.owner");
770 if ($mirror) {
771 $newproj->premirror or die "Girocco::Project->premirror failed\n";
772 !$noowner or &$killowner;
773 $newproj->clone or die "Girocco::Project->clone failed\n";
774 warn "Project $projname created and cloning successfully initiated.\n"
775 unless $quiet;
776 } else {
777 $newproj->conjure or die "Girocco::Project->conjure failed\n";
778 !$noowner or &$killowner;
779 warn "New push project fork is empty due to use of --no-alternates\n"
780 if !$quiet && $projname =~ m,/, && $noalternates;
781 warn "Project $projname successfully created.\n" unless $quiet;
783 return 0;
786 sub git_config {
787 my $gd = shift;
788 system($Girocco::Config::git_bin, "--git-dir=$gd", 'config', @_) == 0
789 or die "\"git --git-dir='$gd' config ".join(" ", @_)."\" failed.\n";
792 sub cmd_adopt {
793 my ($force, $type, $nousers, $dryrun, $noowner, $owner, $users, $verbose);
794 parse_options(force => \$force, ":type" => \$type, "no-users" => \$nousers, "dry-run" => \$dryrun,
795 "no-owner" => \$noowner,":owner" => \$owner, quiet => \$quiet, q =>\$quiet, verbose => \$verbose);
796 @ARGV or die "Please give project name on command line.\n";
797 my $projname = shift @ARGV;
798 (!$noowner || !defined($owner)) && (!$nousers || !@ARGV) or die_usage;
799 !defined($type) || $type eq "mirror" || $type eq "push" or die_usage;
800 defined($type) or $type = "";
801 my $projdir;
802 if ($dryrun && $projname =~ m,^/[^.\s/\\:], && is_git_dir(realpath($projname))) {
803 $projdir = realpath($projname);
804 $projname = $projdir;
805 $projname =~ s/\.git$//i;
806 $projname =~ s,/+$,,;
807 $projname =~ s,^.*/,,;
808 $projname ne "" or $projname = $projdir;
809 } else {
810 $projname =~ s/\.git$//i;
811 $projname ne "" or die "Invalid project name \"\".\n";
812 unless (Girocco::Project::does_exist($projname, 1)) {
813 Girocco::Project::valid_name($projname, 1, 1)
814 or die "Invalid project name \"$projname\".\n";
815 die "No such project to adopt: $projname\n";
817 defined(Girocco::Project->load($projname))
818 and die "Project already known (no need to adopt): $projname\n";
819 $projdir = $Girocco::Config::reporoot . "/" . $projname . ".git";
820 is_git_dir($projdir) or die "Not a git directory: \"$projdir\"\n";
822 my $config = read_config_file($projdir . "/config");
823 my %config = ();
824 %config = map {($$_[0], defined($$_[1])?$$_[1]:"true")} @$config if defined($config);
825 git_bool($config{"core.bare"}) or die "Not a bare git repository: \"$projdir\"\n";
826 defined(read_HEAD_symref($projdir)) or die "Project with non-symbolic HEAD ref: \"$projdir\"\n";
827 @ARGV and $users = [validate_users(join(" ", @ARGV), $force, 1)];
828 my $desc = "";
829 if (-e "$projdir/description") {
830 open my $fd, '<', "$projdir/description" or die "Cannot open \"$projdir/description\": $!\n";
832 local $/;
833 $desc = <$fd>;
835 close $fd;
836 defined $desc or $desc = "";
837 chomp $desc;
838 $desc = to_utf8($desc, 1);
839 is_default_desc($desc) and $desc = "";
840 if ($desc ne "" && !valid_desc($desc)) {
841 die "invalid 'description' file contents (use --force to accept): \"$desc\"\n"
842 unless $force;
843 warn "using invalid 'description' file contents with --force\n" unless $quiet;
845 $desc = clean_desc($desc);
846 if (length($desc) > 1024) {
847 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
848 unless $force;
849 warn "using longer than 1024 char description with --force\n" unless $quiet;
852 my $readme = "";
853 my $origreadme = "";
854 my $readmedata = "";
855 my $origreadmedata = "";
856 my $readmetype = Girocco::Project::_normalize_rmtype($config{"girocco.readmetype"},1);
857 if (-e "$projdir/README.html") {
858 open my $fd, '<', "$projdir/README.html" or die "Cannot open \"$projdir/README.html\": $!\n";
860 local $/;
861 $readme = <$fd>;
863 close $fd;
864 defined $readme or $readme = "";
865 $readme = to_utf8($readme, 1);
866 $readme =~ s/\r\n?/\n/gs;
867 $readme =~ s/^\s+//s;
868 $readme =~ s/\s+$//s;
869 $readme eq "" or $readme .= "\n";
870 $origreadme = $readme;
871 if (-e "$projdir/README.dat") {
872 open my $fd2, '<', "$projdir/README.dat" or die "Cannot open \"$projdir/README.dat\": $!\n";
874 local $/;
875 $readmedata = <$fd2>;
877 close $fd2;
878 defined $readmedata or $readmedata = "";
879 $readmedata = to_utf8($readmedata, 1);
880 $readmedata =~ s/\r\n?/\n/gs;
881 $readmedata =~ s/^\s+//s;
882 $readmedata =~ s/\s+$//s;
883 $readmedata eq "" or $readmedata .= "\n";
884 $origreadmedata = $readmedata;
886 !$readmetype && length($readme) && !length($readmedata) and do {
887 # the old HTML format
888 $readmetype = 'HTML';
889 $readmedata = $readme;
891 if (length($readmedata) > 8192) {
892 die "readme greater than 8192 chars is too long (use --force to override)\n"
893 unless $force;
894 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
897 my $dummy = {READMEDATA => $readmedata, rmtype => $readmetype, name => $projname};
898 my ($cnt, $err) = Girocco::Project::_lint_readme($dummy, 0);
899 if ($cnt) {
900 my $msg = "README: $cnt error";
901 $msg .= "s" unless $cnt == 1;
902 print STDERR "$msg\n", "-" x length($msg), "\n", $err
903 unless $force && $quiet;
904 exit(255) unless $force && $readmetype eq 'HTML';
905 warn "$projname: using invalid raw HTML with --force\n" unless $quiet;
906 } else {
907 $readme = $dummy->{README};
911 $readmetype or $readmetype = Girocco::Project::_normalize_rmtype(""); # use default type
912 # Inspect any remotes now
913 # Yes, Virginia, remote urls can be multi-valued
914 my %remotes = ();
915 foreach (@$config) {
916 my ($k,$v) = @$_;
917 next unless $k =~ /^remote\.([^\/].*?)\.([^.]+)$/; # remote name cannot start with "/"
918 my ($name, $subkey) = ($1, $2);
919 $remotes{$name}->{skip} = git_bool($v,1), next if $subkey eq "skipdefaultupdate" || $subkey eq "skipfetchall";
920 $remotes{$name}->{mirror} = git_bool($v,1), next if $subkey eq "mirror"; # we might want this
921 $remotes{$name}->{vcs} = $v, next if defined($v) && $v !~ /^\s*$/ && $subkey eq "vcs";
922 push(@{$remotes{$name}->{$subkey}}, $v), next if defined($v) && $v !~ /^\s*$/ &&
923 ($subkey eq "url" || $subkey eq "fetch" || $subkey eq "push" || $subkey eq "pushurl");
925 # remotes.default is the default remote group to fetch for "git remote update" otherwise --all
926 # the remote names in a group are separated by runs of [ \t\n] characters
927 # remote names "", ".", ".." and any name starting with "/" are invalid
928 # a remote with no url or vcs setting is not considered valid
929 my @check = ();
930 my $usingall = 0;
931 if (exists($config{"remotes.default"})) {
932 foreach (split(/[ \t\n]+/, $config{"remotes.default"})) {
933 next unless exists($remotes{$_});
934 my $rmt = $remotes{$_};
935 next if !exists($rmt->{url}) && !$rmt->{vcs};
936 push(@check, $_);
938 } else {
939 $usingall = 1;
940 my %seenrmt = ();
941 foreach (@$config) {
942 my ($k,$v) = @$_;
943 next unless $k =~ /^remote\.([^\/].*?)\.[^.]+$/;
944 next if $seenrmt{$1};
945 $seenrmt{$1} = 1;
946 next unless exists($remotes{$1});
947 my $rmt = $remotes{$1};
948 next if $rmt->{skip} || (!exists($rmt->{url}) && !$rmt->{vcs});
949 push(@check, $1);
952 my @needskip = (); # remotes that need skipDefaultUpdate set to true
953 my $foundvcs = 0;
954 my $foundfetch = 0;
955 my $foundfetchwithmirror = 0;
956 foreach (@check) {
957 my $rmt = $remotes{$_};
958 push(@needskip, $_) if $usingall && !exists($rmt->{fetch});
959 next unless exists($rmt->{fetch});
960 ++$foundfetch;
961 ++$foundfetchwithmirror if $rmt->{mirror};
962 ++$foundvcs if $rmt->{vcs} || (exists($rmt->{url}) && $rmt->{url}->[0] =~ /^[a-zA-Z0-9][a-zA-Z0-9+.-]*::/);
964 # if we have $foundvcs then we need to explicitly set fetch.prune to false
965 # if we have $foundfetch > 1 then we need to explicitly set fetch.prune to false
966 my $neednoprune = !exists($config{"fetch.prune"}) && ($foundvcs || $foundfetch > 1);
967 my $baseurl = "";
968 my $needfakeorigin = 0; # if true we need to set remote.origin.skipDefaultUpdate = true
969 # if remote "origin" exists we always pick up its first url or use ""
970 if (exists($remotes{origin})) {
971 my $rmt = $remotes{origin};
972 $baseurl = exists($rmt->{url}) ? $rmt->{url}->[0] : "";
973 $needfakeorigin = !exists($rmt->{url}) && !$rmt->{vcs} && !$rmt->{skip};
974 } else {
975 $needfakeorigin = 1;
976 # get the first url of the @check remotes
977 foreach (@check) {
978 my $rmt = $remotes{$_};
979 next unless exists($rmt->{url});
980 next unless defined($rmt->{url}->[0]) && $rmt->{url}->[0] ne "";
981 $baseurl = $rmt->{url}->[0];
982 last;
985 my $makemirror = $type eq "mirror" || ($type eq "" && $foundfetch);
987 # If we have $foundfetch we want to make a mirror but complain if
988 # we $foundfetchwithmirror as well unless we have --type=mirror.
989 # Warn if we have --type=push and $foundfetch and !$foundfetchwithmirror.
990 # Warn if we need to set fetch.prune=false when making a mirror
991 # Warn if we need to create remote.origin.skipDefaultUpdate when making a mirror
992 # Complain if @needskip AND !$usingall (warn with --force but don't set skip)
993 # Warn if $usingall and any @needskip (and set them) if making a mirror
994 # Warn if making a mirror and $baseurl eq ""
995 # Warn if we have --type=mirror and !$foundfetch
997 if ($makemirror) {
998 warn "No base URL to mirror from for adopted \"$projname\"\n" unless $quiet || $baseurl ne "";
999 warn "Adopting mirror \"$projname\" without any fetch remotes\n" unless $quiet || $foundfetch;
1000 if ($foundfetchwithmirror) {
1001 warn "Refusing to adopt mirror \"$projname\" with active remote.<name>.mirror=true remote(s)\n".
1002 "(Use --type=mirror to override)\n"
1003 unless $type eq "mirror";
1004 exit(255) unless $type eq "mirror" || $dryrun;
1005 warn "Adopting mirror \"$projname\" with active remote.<name>.mirror=true remotes\n"
1006 unless $quiet || $type ne "mirror";
1008 warn "Setting explicit fetch.prune=false for adoption of mirror \"$projname\"\n"
1009 if !$quiet && $neednoprune;
1010 warn "Setting remote.origin.skipDefaultUpdate=true for adoption of mirror \"$projname\"\n"
1011 if !$quiet && $needfakeorigin;
1012 if (!$usingall && @needskip) {
1013 warn "Refusing to adopt mirror empty fetch remote(s) (override with --force)\n"
1014 unless $force;
1015 exit(255) unless $force || $dryrun;
1016 warn "Adopting mirror with empty fetch remote(s) with --force\n"
1017 unless $quiet || !$force;
1019 warn "Will set skipDefaultUpdate=true on non-fetch remote(s)\n" if !$quiet && $usingall && @needskip;
1020 warn "Adopting mirror with base URL \"$baseurl\"\n" unless $quiet || $baseurl eq "";
1021 } else {
1022 warn "Adopting push \"$projname\" but active non-mirror remotes are present\n"
1023 if !$quiet && $foundfetch && !$foundfetchwithmirror;
1026 if (!$noowner && !defined($owner)) {
1027 # Select the owner
1028 $owner = $config{"gitweb.owner"};
1029 if (!defined($owner) || $owner eq "") {
1030 $owner = $Girocco::Config::admin;
1031 warn "Using owner \"$owner\" for adopted project\n" unless $quiet;
1034 if (!$nousers && !$makemirror && !defined($users)) {
1035 # select user list for push project
1036 my $findowner = $owner;
1037 defined($findowner) or $findowner = $config{"gitweb.owner"};
1038 $findowner = lc($findowner) if defined($findowner);
1039 my @owner_users = ();
1040 @owner_users = map {$findowner eq lc($$_[4]) ? $$_[1] : ()} get_all_users
1041 if defined($findowner) && $findowner ne "";
1042 defined($findowner) or $findowner = "";
1043 if (@owner_users <= 1) {
1044 $users = \@owner_users;
1045 warn "No users found that match owner \"$findowner\"\n" unless @owner_users || $quiet;
1046 } else {
1047 $users = [];
1048 warn "Found ".scalar(@owner_users)." users for owner \"$findowner\" (" .
1049 join(" ", @owner_users) . ") not setting any\n" unless $quiet;
1052 defined($users) or $users = [];
1054 # Warn if we preserve an existing receive.denyNonFastForwards or receive.denyDeleteCurrent setting
1055 # Complain if core.logallrefupdates or logs subdir exists and contains any files (allow with --force
1056 # and warn about preserving the setting)
1058 warn "Preserving existing receive.denyNonFastForwards=true\n"
1059 if !$quiet && git_bool($config{"receive.denynonfastforwards"});
1060 warn "Preserving existing receive.denyDeleteCurrent=$config{'receive.denydeletecurrent'}\n"
1061 if !$quiet && exists($config{"receive.denydeletecurrent"}) &&
1062 $config{"receive.denydeletecurrent"} ne "warn";
1064 my $reflogfiles = Girocco::Project::_contains_files("$projdir/logs");
1065 my $reflogactive = git_bool($config{"core.logallrefupdates"});
1066 if ($reflogactive || $reflogfiles) {
1067 warn "Refusing to adopt \"$projname\" with active ref logs without --force\n" if $reflogfiles && !$force;
1068 warn "Refusing to adopt \"$projname\" with core.logAllRefUpdates=true without --force\n" if $reflogactive && !$force;
1069 exit(255) unless $force || $dryrun;
1070 warn "Adopting \"$projname\" with active ref logs with --force\n" unless $quiet || ($reflogfiles && !$force);
1071 warn "Adopting \"$projname\" with core.logAllRefUpdates=true with --force\n" unless $quiet || ($reflogactive && !$force);
1074 return 0 if $dryrun && !$verbose;
1076 my $newproj = eval {Girocco::Project->ghost($projname, $makemirror, 1, $dryrun)};
1077 defined($newproj) or die "Girocco::Project::ghost failed: $@\n";
1078 $newproj->{desc} = $desc;
1079 $newproj->{README} = $readme;
1080 $newproj->{READMEDATA} = $readmedata;
1081 $newproj->{rmtype} = $readmetype;
1082 $newproj->{url} = $baseurl if $makemirror || exists($config{"gitweb.baseurl"});
1083 $newproj->{email} = $owner if defined($owner);
1084 $newproj->{users} = $users;
1085 $newproj->{crypt} = "unknown";
1086 $newproj->{reverseorder} = 1 unless exists($config{"hooks.reverseorder"});
1087 $newproj->{summaryonly} = 1 unless exists($config{"hooks.summaryonly"});
1088 my $dummy = bless {}, "Girocco::Project";
1089 $dummy->{path} = "$projdir";
1090 $dummy->{configfilehash} = \%config;
1091 $dummy->_properties_load;
1092 delete $dummy->{origurl};
1093 foreach my $k (keys(%$dummy)) {
1094 $newproj->{$k} = $dummy->{$k}
1095 if exists($dummy->{$k}) && !exists($newproj->{$k});
1098 if ($verbose) {
1099 use Data::Dumper;
1100 my %info = %$newproj;
1101 $info{README} = get_readme_desc($info{README}) if exists($info{README});
1102 my $d = Data::Dumper->new([\%info], ['*'.$newproj->{name}]);
1103 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
1104 print $d->Dump([\%info], ['*'.$newproj->{name}]);
1106 return 0 if $dryrun;
1108 # Make any changes as needed for @needskip, $neednoprune and $needfakeorigin
1109 if ($makemirror) {
1110 git_config($projdir, "fetch.prune", "false") if $neednoprune;
1111 git_config($projdir, "remote.origin.skipDefaultUpdate", "true") if $needfakeorigin;
1112 if ($usingall && @needskip) {
1113 git_config($projdir, "remote.$_.skipDefaultUpdate", "true") foreach @needskip;
1117 # Write out any README.dat/README.html changes before the actual Adoption
1118 # Otherwise they will get stepped on. The Girocco::Project::adopt function
1119 # does not know how to validate README.html during adoption like the above code does.
1120 if ($readmedata ne $origreadmedata) {
1121 open my $fd, '>', "$projdir/README.dat" or die "Cannot write \"$projdir/README.dat\": $!\n";
1122 print $fd $readmedata or die "Error writing \"$projdir/README.dat\": $!\n";
1123 close $fd or die "Error closing \"$projdir/README.dat\": $!\n";
1125 if ($readme ne $origreadme || ! -e "$projdir/README.html") {
1126 open my $fd, '>', "$projdir/README.html" or die "Cannot write \"$projdir/README.html\": $!\n";
1127 print $fd $readme or die "Error writing \"$projdir/README.html\": $!\n";
1128 close $fd or die "Error closing \"$projdir/README.html\": $!\n";
1130 git_config($projdir, "girocco.rmtype", $readmetype);
1132 # Perform the actual adoption
1133 $newproj->adopt or die "Girocco::Project::adopt failed\n";
1135 # Perhaps restore core.logAllRefUpdates, receive.denyNonFastForwards and receive.denyDeleteCurrent
1136 git_config($projdir, "receive.denyNonFastForwards", "true")
1137 if git_bool($config{"receive.denynonfastforwards"});
1138 git_config($projdir, "receive.denyDeleteCurrent", $config{"receive.denydeletecurrent"})
1139 if exists($config{"receive.denydeletecurrent"}) &&
1140 $config{"receive.denydeletecurrent"} ne "warn";
1141 git_config($projdir, "core.logAllRefUpdates", "true")
1142 if $reflogactive;
1144 # Success
1145 if ($makemirror) {
1146 warn "Mirror project \"$projname\" successfully adopted.\n" unless $quiet;
1147 } else {
1148 warn "Push project \"$projname\" successfully adopted.\n" unless $quiet;
1150 return 0;
1153 sub cmd_remove {
1154 my ($force, $reallydel, $keepforks);
1155 parse_options(force => \$force, "really-delete" => \$reallydel,
1156 "keep-forks" => \$keepforks, quiet => \$quiet, q =>\$quiet);
1157 @ARGV or die "Please give project name on command line.\n";
1158 @ARGV == 1 or die_usage;
1159 my $project = get_project($ARGV[0]); # for safety only names accepted here
1160 my $projname = $project->{name};
1161 my $isempty = !$project->{mirror} && $project->is_empty;
1162 if (!$project->{mirror} && !$isempty && $reallydel) {
1163 die "refusing to remove and delete non-empty push project without --force: $projname\n" unless $force;
1164 warn "allowing removal and deletion of non-empty push project with --force\n" unless $quiet;
1166 my $altwarn;
1167 my $removenogc;
1168 if ($project->has_forks) {
1169 die "refusing to remove project with forks (use --keep-forks): $projname\n" unless $keepforks;
1170 warn "allowing removal of forked project while preserving its forks with --keep-forks\n" unless $quiet;
1171 # Run pseudo GC on that repository so that objects don't get lost within forks
1172 my $basedir = $Girocco::Config::basedir;
1173 my $projdir = $project->{path};
1174 warn "We have to run pseudo GC on the repo so that the forks don't lose data. Hang on...\n" unless $quiet;
1175 my $nogcrunning = sub {
1176 die "Error: GC appears to be currently running on $projname\n"
1177 if -e "$projdir/gc.pid" || -e "$projdir/.gc_in_progress";
1179 &$nogcrunning;
1180 $removenogc = ! -e "$projdir/.nogc";
1181 recreate_file("$projdir/.nogc") if $removenogc;
1182 die "unable to create \"$projdir/.nogc\"\n" unless -e "$projdir/.nogc";
1183 delete $ENV{show_progress};
1184 $ENV{'show_progress'} = 1 unless $quiet;
1185 sleep 2; # *cough*
1186 &$nogcrunning;
1187 system("$basedir/toolbox/perform-pre-gc-linking.sh", "--include-packs", $projname) == 0
1188 or die "Running pseudo GC on project $projname failed\n";
1189 $altwarn = 1;
1191 my $archived;
1192 if (!$project->{mirror} && !$isempty && !$reallydel) {
1193 $archived = $project->archive_and_delete;
1194 unlink("$archived/.nogc") if $removenogc && defined($archived) && $archived ne "";
1195 } else {
1196 $project->delete;
1198 warn "Project '$projname' removed from $Girocco::Config::name" .
1199 ($archived ? ", backup in '$archived'" : "") .".\n" unless $quiet;
1200 warn "Retained forks may now have unwanted objects/info/alternates lines\n" if $altwarn && !$quiet;
1201 return 0;
1204 sub cmd_prune {
1205 my ($force, $dryrun);
1206 parse_options(force => \$force, "dry-run" => \$dryrun, "quiet" => \$quiet);
1207 ($force && !$dryrun) || (!$force && $dryrun) or die_usage;
1208 my @projs = @ARGV;
1209 my %allprojs = map({($$_[0] => $_)} Girocco::Project::get_full_list_extended());
1210 my @allprojs = sort({lc($a) cmp lc($b) || $a cmp $b} keys(%allprojs));
1211 my %seen = ();
1212 @projs or @projs = @allprojs;
1213 my $bd = $Girocco::Config::reporoot.'/';
1214 my @remove = ();
1215 foreach (@projs) {
1216 !$seen{$_} && $allprojs{$_} && ${$allprojs{$_}}[2] >= 65536 or next;
1217 $seen{$_} = 1;
1218 /^[a-zA-Z0-9]/ or next;
1219 my $pd = $bd . $_ . '.git';
1220 if (! -e $pd) {
1221 warn "$_: no such directory: $pd\n" unless $quiet;
1222 push(@remove, $_);
1223 } elsif (! -d _) {
1224 warn "$_: exists but not directory: $pd\n" unless $quiet;
1225 push(@remove, $_);
1228 warn "\n" if @remove && !$quiet;
1229 if ($dryrun) {
1230 return 0 unless @remove;
1231 my $msg = "Would remove ".scalar(@remove). " project entr";
1232 $msg .= (@remove == 1 ? "y" : "ies");
1233 $msg .= ":\n";
1234 $msg .= join("", map("\t$_\n", @remove));
1235 print $msg unless $quiet;
1236 return 1;
1238 my $msg = "Removed ".scalar(@remove). " project entr";
1239 $msg .= (@remove == 1 ? "y" : "ies");
1240 $msg .= ":\n";
1241 $msg .= join("", map("\t$_\n", @remove));
1242 my %remove = map({$_ => 1} @remove);
1243 filedb_atomic_edit(jailed_file('/etc/group'), sub {
1244 my ($name,undef,$gid) = split /:/;
1245 $gid =~ /^\d+$/ && $gid >= 65536 or return $_;
1246 $name =~ /^[a-zA-Z0-9]/ or return $_;
1247 !exists($remove{$name}) and return $_;
1249 print $msg unless $quiet;
1250 return 0;
1253 sub cmd_show {
1254 use Data::Dumper;
1255 @ARGV <= 1 or die_usage;
1256 my $project = get_clean_project_gently($ARGV[0]);
1257 defined($project) or die_usage;
1258 my %info = %$project;
1259 my $d = Data::Dumper->new([\%info], ['*'.$project->{name}]);
1260 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
1261 print $d->Dump([\%info], ['*'.$project->{name}]);
1262 return 0;
1265 sub cmd_verify {
1266 my $dirfp = 0;
1267 parse_options("quiet" => \$quiet, "dir" => \$dirfp, "directory" => \$dirfp, "git-dir" =>\$dirfp);
1268 @ARGV <= 1 or die_usage;
1269 my $project = undef;
1270 my $pname = undef;
1271 eval {
1272 $project = get_project_harder(@ARGV >= 1 ? $ARGV[0] : ".");
1274 @ARGV > 0 || reftype($project) eq 'HASH' or die_usage;
1275 my $parg = @ARGV ? $ARGV[0] : $$project{name};
1276 $pname = $project->{name} if reftype($project) eq 'HASH';
1277 defined($pname) && $pname ne "" or $project = undef;
1278 !$@ && reftype($project) ne 'HASH' and $@ = "No such project: \"$parg\"\n";
1279 warn $@ if $@ && !$quiet;
1280 exit 1 if $@;
1281 $dirfp && defined($project->{path}) && $project->{path} ne "" and
1282 $pname = $project->{path};
1283 printf "%s\n", $pname;
1284 return 0;
1287 sub get_worktrees {
1288 my $gd = shift;
1289 return () unless -d "$gd/worktrees";
1290 opendir my $dh, "$gd/worktrees" or return ();
1291 my @dirs = grep { !/^\.\.?$/ && -d "$gd/worktrees/$_"} readdir($dh);
1292 closedir $dh;
1293 my @wt = ();
1294 foreach (@dirs) {
1295 open my $hd, '<', "$gd/worktrees/$_/HEAD" or next;
1296 my $hh = <$hd>;
1297 close $hd;
1298 defined($hh) or next;
1299 chomp($hh);
1300 if ($hh =~ /^[0-9a-f]{40,}$/) {
1301 push(@wt, [$_, $hh]);
1302 } elsif ($hh =~ m{^ref:\s?(refs/heads/.+)$}) {
1303 push(@wt, [$_, $1]);
1306 return @wt;
1309 sub cmd_worktree {
1310 eval '$Girocco::Config::var_have_git_250' or die "worktree requires Git 2.5.0 or later\n";
1311 my $force;
1312 parse_options("force" => \$force, quiet => \$quiet, q =>\$quiet);
1313 my $project = get_project_harder_gently($ARGV[0]);
1314 defined($project) or die_usage;
1315 shift @ARGV if @ARGV;
1316 @ARGV or push(@ARGV, "list");
1317 my $gd = $project->{path};
1318 defined($gd) && -d $gd or die "Project \"$$project{name}\" does not actually exist\n";
1319 if ($project->{mirror}) {
1320 die "Cannot use worktree command on mirror project\n" unless $force;
1321 warn "Continuing with --force even though project is a mirror\n" unless $quiet;
1323 my $symref = undef;
1324 my $mthash = undef;
1325 my $wantdwimadd = 0;
1326 my $hb = $project->{HEAD};
1327 defined($hb) or $hb = "";
1328 $hb !~ /^\[/ or $hb = "";
1329 if ($ARGV[0] eq "add" && (@ARGV == 2 && $ARGV[1] !~ /^-/ ||
1330 @ARGV == 3 && $ARGV[1] !~ /^-/ && $hb && $ARGV[2] eq $hb)) {{
1331 $wantdwimadd = -1;
1332 # Only "add" subcommand has special handling
1333 my $isbare = get_git_chomp("--git-dir=".$gd, "rev-parse", "--is-bare-repository");
1334 defined($isbare) && $isbare eq "true" or last; # only for bare repos
1335 $symref = get_git_chomp("--git-dir=".$gd, "symbolic-ref", "-q", "HEAD");
1336 defined($symref) && $symref =~ m{^refs/heads/.} or last; # only if symref HEAD
1337 !grep({$$_[1] eq $symref} get_worktrees($gd)) or last; # and not checked out
1338 if (get_git_chomp("--git-dir=".$gd, "rev-parse", "--verify", "-q", $symref)) {
1339 # easy case, branch already exists, just add its name to arg list
1340 push(@ARGV, substr($symref, 11)) unless @ARGV == 3;
1341 $symref = undef;
1342 $wantdwimadd = 1;
1343 } else {
1344 # nasty workaround for broken worktree command
1345 my $mttree = get_git_chomp("--git-dir=".$gd, "mktree");
1346 defined($mttree) && $mttree =~ /^[0-9a-f]{40,}$/ or last;
1347 my $now = time();
1348 my $cmt = "tree $mttree\nauthor - <-> $now +0000\ncommitter - <-> $now +0000\n\n";
1349 my ($st, $rslt) = capture_command(1, $cmt, $Girocco::Config::git_bin,
1350 "--git-dir=".$gd, "hash-object", "-t", "commit", "-w", "--stdin");
1351 defined($st) && $st == 0 && defined($rslt) or last;
1352 chomp($rslt);
1353 $rslt =~ /^[0-9a-f]{40,}$/ or last;
1354 $mthash = $rslt;
1355 pop(@ARGV) if @ARGV == 3;
1356 push(@ARGV, $mthash); # requires ugly fixup afterwards
1357 $wantdwimadd = 2;
1360 warn $project->{name}, ": cannot DWIM worktree add (check `worktree list` output)\n"
1361 if $wantdwimadd == -1 && !$quiet;
1362 my $saveHOME = $ENV{HOME};
1363 if (defined($origHOME) && $origHOME ne "" && $origHOME =~ m{^(/.+)$} && -d $1) {
1364 $ENV{HOME} = $1;
1366 my $ec = system($Girocco::Config::git_bin, "--git-dir=".$gd, "worktree", @ARGV);
1367 $ENV{HOME} = $saveHOME;
1368 if (defined($ec) && $ec == 0 && defined($symref) && defined($mthash)) {
1369 # ugly fixup time
1370 foreach (map($$_[0], grep({$$_[1] eq $mthash} get_worktrees($gd)))) {
1371 open my $hf, '>', "$gd/worktrees/$_/HEAD" or next;
1372 print $hf "ref: $symref\n";
1373 close $hf;
1376 defined($ec) && $ec != -1 or return 1;
1377 return ($ec >> 8);
1380 sub cmd_urls {
1381 my ($pushonly, $bundle);
1382 parse_options("push" => \$pushonly, "bundle" => \$bundle, "bundles" => \$bundle);
1383 $pushonly && $bundle and die "--push and --bundle are incompatible\n";
1384 my @projs = @ARGV;
1385 @ARGV <= 1 or die_usage;
1386 my $project = get_project_harder_gently($ARGV[0]);
1387 defined($project) or die_usage;
1388 my $suffix = "/".$project->{name}.".git";
1389 if ($bundle) {
1390 my $bub = $Girocco::Config::httpbundleurl;
1391 defined($bub) && $bub ne "" or return 0;
1392 $bub =~ s{/$}{};
1393 $bub .= $suffix . "/";
1394 foreach ($project->bundles) {
1395 print $bub, $_->[1], "\n";
1397 return 0;
1399 @Gitweb::Config::git_base_url_list = ();
1400 @Gitweb::Config::git_base_push_urls = ();
1401 @Gitweb::Config::git_base_mirror_urls = ();
1403 package Gitweb::Config;
1404 do $Girocco::Config::basedir."/gitweb/gitweb_config.perl";
1405 !$! or die "could not read gitweb_config.perl: $!\n";
1406 !$@ or die "could not parse gitweb_config.perl: $@\n";
1408 my @fetch_urls = ();
1409 my @push_urls = ();
1410 my $add_url = sub {
1411 my $array = shift;
1412 foreach (@_) {
1413 if (ref($_)) {
1414 ref($_) eq 'ARRAY' or die "expected ARRAY ref";
1415 my $u = $$_[0];
1416 defined($u) && $u ne "" and
1417 push(@$array, $u.$suffix);
1418 } elsif (defined($_) && $_ ne "") {
1419 push(@$array, $_.$suffix);
1423 my $uniq = sub {
1424 my %items = ();
1425 $items{$_} = 1 foreach @_;
1426 sort(keys(%items));
1428 &$add_url(\@fetch_urls, @Gitweb::Config::git_base_url_list);
1429 if ($project->{mirror}) {
1430 &$add_url(\@fetch_urls, @Gitweb::Config::git_base_mirror_urls);
1431 } else {
1432 &$add_url(\@push_urls, @Gitweb::Config::git_base_push_urls);
1434 my @urls = ();
1435 if ($pushonly) {
1436 push(@urls, &$uniq(@push_urls));
1437 } else {
1438 push(@urls, &$uniq(@fetch_urls, @push_urls));
1440 print map "$_\n", @urls;
1441 return 0;
1444 sub cmd_listheads {
1445 @ARGV <= 1 or die_usage;
1446 my $project = get_project_harder_gently($ARGV[0]);
1447 defined($project) or die_usage;
1448 my @heads = sort({lc($a) cmp lc($b)} $project->get_heads);
1449 my $cur = $project->{HEAD};
1450 defined($cur) or $cur = '';
1451 my $curmark = '*';
1452 my $headhash = get_git("--git-dir=$project->{path}", 'rev-parse', '--quiet', '--verify', 'HEAD');
1453 defined($headhash) or $headhash = '';
1454 chomp $headhash;
1455 $headhash or $curmark = '!';
1456 foreach (@heads) {
1457 my $mark = $_ eq $cur ? $curmark : ' ';
1458 print "$mark $_\n";
1460 return 0;
1463 sub cmd_listtags {
1464 my $vcnt = 0;
1465 parse_options("verbose" => \$vcnt, "v" => \$vcnt);
1466 @ARGV <= 1 or die_usage;
1467 my $project = get_project_harder_gently($ARGV[0]);
1468 defined($project) or die_usage;
1469 if ($vcnt) {
1470 print map("$$_[0]\t$$_[1]\n", get_ctag_counts($project));
1471 } else {
1472 print map("$_\n", $project->get_ctag_names);
1474 return 0;
1477 sub cmd_deltags {
1478 my $ic = 0;
1479 parse_options("ignore-case" => \$ic, "i" => \$ic);
1480 @ARGV >= 2 or die_usage;
1481 my $project = get_project_harder(shift @ARGV);
1482 my %curtags;
1483 if ($ic) {
1484 push(@{$curtags{lc($_)}}, $_) foreach $project->get_ctag_names;
1485 } else {
1486 push(@{$curtags{$_}}, $_) foreach $project->get_ctag_names;
1488 my @deltags = ();
1489 my %seentag = ();
1490 my $ctags = join(" ", @ARGV);
1491 $ctags = lc($ctags) if $ic;
1492 foreach (split(/[\s,]+/, $ctags)) {
1493 next unless exists($curtags{$_});
1494 $seentag{$_}=1, push(@deltags, $_) unless $seentag{$_};
1496 if (!@deltags) {
1497 warn $project->{name}, ": skipping removal of only non-existent tags\n" unless $quiet;
1498 } else {
1499 # Avoid touching anything other than the ctags
1500 foreach my $tg (@deltags) {
1501 $project->delete_ctag($_) foreach @{$curtags{$tg}};
1503 $project->_set_changed;
1504 $project->_set_forkchange;
1505 warn $project->{name}, ": specified tags have been removed\n" unless $quiet;
1507 return 0;
1510 sub cmd_addtags {
1511 @ARGV >= 2 or die_usage;
1512 my $project = get_project_harder(shift @ARGV);
1513 my $ctags = join(" ", @ARGV);
1514 $ctags =~ /[^, a-zA-Z0-9:.+#_-]/ and
1515 die "Content tag(s) \"$ctags\" contain(s) evil character(s).\n";
1516 my $oldmask = umask();
1517 umask($oldmask & ~0060);
1518 my $changed = 0;
1519 foreach (split(/[\s,]+/, $ctags)) {
1520 ++$changed if $project->add_ctag($_, 1);
1522 if ($changed) {
1523 $project->_set_changed;
1524 $project->_set_forkchange;
1526 umask($oldmask);
1527 my $cnt = ($changed == 1) ? "1 content tag has" : $changed . " content tags have";
1528 warn $project->{name}, ": $cnt been added/updated\n" unless $quiet;
1529 return 0;
1532 sub _get_random_val {
1533 my $p = shift;
1534 my $md5;
1536 no warnings;
1537 $md5 = md5_hex(time . $$ . rand() . join(':',%$p));
1539 $md5;
1542 sub cmd_chpass {
1543 my $force = 0;
1544 parse_options("force" => \$force);
1545 my $random = undef;
1546 pop(@ARGV), $random=lc($ARGV[1]) if @ARGV==2 && $ARGV[1] =~ /^(?:random|unknown)$/i;
1547 @ARGV == 1 or die_usage;
1548 my $project = get_project_harder($ARGV[0]);
1549 die "refusing to change locked password of project \"$ARGV[0]\" without --force\n"
1550 if $project->is_password_locked;
1551 my ($newpw, $rmsg);
1552 if ($random) {
1553 if ($random eq "random") {
1554 die "refusing to set random password without --force\n" unless $force;
1555 $rmsg = "set to random value";
1556 $newpw = _get_random_val($project);
1557 } else {
1558 die "refusing to set password hash to '$random' without --force\n" unless $force;
1559 $rmsg = "hash set to '$random'";
1560 $newpw = $random;
1562 } else {
1563 $rmsg = "updated";
1564 if (-t STDIN) {
1565 print "Changing admin password for project $ARGV[0]\n";
1566 my $np1 = prompt_noecho_nl_or_die("New password for project $ARGV[0] (echo is off)");
1567 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1568 my $np2 = prompt_noecho_nl_or_die("Retype new password for project $ARGV[0]");
1569 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
1570 "the admin passwords you have entered do not match each other.\n";
1571 $newpw = $np1;
1572 } else {
1573 $newpw = <STDIN>;
1574 defined($newpw) or die "missing new password on STDIN\n";
1575 chomp($newpw);
1578 $newpw ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1579 my $old = $project->{crypt};
1580 $project->{crypt} = (defined($random) && $random ne "random") ? $newpw : scrypt_sha1($newpw);
1581 if (defined($old) && $old eq $project->{crypt}) {
1582 warn $project->{name}, ": skipping update of password hash to same value\n" unless $quiet;
1583 } else {
1584 # Avoid touching anything other than the password hash
1585 $project->_group_update;
1586 warn $project->{name}, ": admin password $rmsg (new hash stored)\n" unless $quiet;
1588 return 0;
1591 sub cmd_checkpw {
1592 @ARGV <= 1 or die_usage;
1593 my $project = get_project_harder_gently($ARGV[0]);
1594 defined($project) or die_usage;
1595 my $pwhash = $project->{crypt};
1596 defined($pwhash) or $pwhash = "";
1597 if ($pwhash eq "") {
1598 warn $project->{name}, ": no password required\n" unless $quiet;
1599 return 0;
1601 if ($project->is_password_locked) {
1602 warn $project->{name}, ": password is locked\n" unless $quiet;
1603 exit 1;
1605 my $checkpw;
1606 if (-t STDIN) {
1607 $checkpw = prompt_noecho_nl_or_die("Admin password for project $$project{name} (echo is off)");
1608 $checkpw ne "" or warn "checking for empty password as hash (very unlikely)\n" unless $quiet;
1609 } else {
1610 $checkpw = <STDIN>;
1611 defined($checkpw) or die "missing admin password on STDIN\n";
1612 chomp($checkpw);
1614 unless (Girocco::CLIUtil::check_passwd_match($pwhash, $checkpw)) {
1615 warn "password check failure\n" unless $quiet;
1616 exit 1;
1618 warn "admin password match\n" unless $quiet;
1619 return 0;
1622 sub cmd_gc {
1623 my ($force, $auto, $redelta, $recompress, $aggressive);
1624 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, auto => \$auto,
1625 redelta => \$redelta, "no-reuse-delta" => \$redelta, aggressive => \$force,
1626 recompress => \$recompress, "no-reuse-object" => $recompress,
1627 aggressive => \$aggressive);
1628 $aggressive and $force = $redelta = 1;
1629 $force && $auto and die "--force and --auto are mutually exclusive options\n";
1630 @ARGV <= 1 or die_usage;
1631 my $project = get_project_harder_gently($ARGV[0]);
1632 defined($project) or die "Please give project name on command line.\n";
1633 delete $ENV{show_progress};
1634 delete $ENV{force_gc};
1635 $quiet or $ENV{"show_progress"} = 1;
1636 $force and $ENV{"force_gc"} = 1;
1637 if (!$auto && !$force && ! -e $project->{path}."/.needsgc") {
1638 open NEEDSGC, '>', $project->{path}."/.needsgc" and close NEEDSGC;
1640 my @args = ($Girocco::Config::basedir . "/jobd/gc.sh", $project->{name});
1641 $redelta && !$recompress and push(@args, "-f");
1642 $recompress and push(@args, "-F");
1643 my $lastgc = $project->{lastgc};
1644 system({$args[0]} @args) != 0 and return 1;
1645 # Do it again Sam, but only if lastgc was set, gc.sh succeeded and now it's not set
1646 if ($lastgc) {
1647 my $newlastgc = get_git("--git-dir=$project->{path}", 'config', '--get', 'gitweb.lastgc');
1648 if (!$newlastgc) {
1649 system({$args[0]} @args) != 0 and return 1;
1652 return 0;
1655 sub cmd_update {
1656 my ($force, $summary);
1657 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, summary => \$summary);
1658 $quiet && $summary and die "--quiet and --summary are mutually exclusive options\n";
1659 @ARGV <= 1 or die_usage;
1660 my $project = get_project_harder_gently($ARGV[0]);
1661 defined($project) or die "Please give project name on command line.\n";
1662 $project->{mirror} or die "Project \"$$project{name}\" is a push project, not a mirror project.\n";
1663 delete $ENV{show_progress};
1664 delete $ENV{force_update};
1665 if ($quiet) {
1666 $ENV{"show_progress"} = 0;
1667 } else {
1668 $ENV{"show_progress"} = ($summary ? 1 : 2);
1670 $force and $ENV{"force_update"} = 1;
1671 system($Girocco::Config::basedir . "/jobd/update.sh", $project->{name}) != 0 and return 1;
1672 return 0;
1675 sub cmd_remirror {
1676 my $force = 0;
1677 parse_options(force => \$force, quiet => \$quiet, q => \$quiet);
1678 @ARGV or die "Please give project name on command line.\n";
1679 @ARGV == 1 or die_usage;
1680 my $project = get_project_harder($ARGV[0]);
1681 $project->{mirror} or die "Project \"$ARGV[0]\" is a push project, not a mirror project.\n";
1682 if ($project->{clone_in_progress} && !$project->{clone_failed}) {
1683 warn "Project \"$ARGV[0]\" already seems to have a clone underway at this moment.\n" unless $quiet && $force;
1684 exit(255) unless $force;
1685 yes_to_continue_or_die("Are you sure you want to force a remirror");
1687 unlink($project->_clonefail_path);
1688 unlink($project->_clonelog_path);
1689 recreate_file($project->_clonep_path);
1690 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or
1691 die "cannot connect to taskd.socket: $!\n";
1692 select((select($sock),$|=1)[0]);
1693 $sock->print("clone ".$project->{name}."\n");
1694 # Just ignore reply, we are going to succeed anyway and the I/O
1695 # would apparently get quite hairy.
1696 $sock->flush();
1697 sleep 2; # *cough*
1698 $sock->close();
1699 warn "Project \"$ARGV[0]\" remirror initiated.\n" unless $quiet;
1700 return 0;
1703 sub cmd_setowner {
1704 my $force = 0;
1705 parse_options("force" => \$force);
1706 @ARGV == 2 || (@ARGV <= 1 && !$force && !$setopt) or die_usage;
1707 my $project = get_project_harder_gently($ARGV[0]);
1708 defined($project) or die_usage;
1709 if (@ARGV == 2 && !valid_email($ARGV[1])) {
1710 die "invalid owner/email (use --force to accept): \"$ARGV[1]\"\n"
1711 unless $force;
1712 warn "using invalid owner/email with --force\n" unless $quiet;
1714 if (@ARGV == 2 && length($ARGV[1]) > 96) {
1715 die "owner/email longer than 96 chars (use --force to accept): \"$ARGV[1]\"\n"
1716 unless $force;
1717 warn "using longer than 96 char owner/email with --force\n" unless $quiet;
1719 my $old = $project->{email};
1720 if (@ARGV <= 1) {
1721 print "$old\n" if defined($old);
1722 return 0;
1724 if (defined($old) && $old eq $ARGV[1]) {
1725 warn $project->{name}, ": skipping update of owner/email to same value\n" unless $quiet;
1726 } else {
1727 # Avoid touching anything other than "gitweb.owner"
1728 $project->_property_fput("email", $ARGV[1]);
1729 $project->_update_index;
1730 $project->_set_changed;
1731 warn $project->{name}, ": owner/email updated to \"$ARGV[1]\"\n" unless $quiet;
1733 return 0;
1736 sub cmd_setdesc {
1737 my $force = 0;
1738 parse_options("force" => \$force);
1739 @ARGV >= 2 || (@ARGV <= 1 && !$force && !$setopt) or die_usage;
1740 my $project = get_project_harder_gently($ARGV[0]);
1741 defined($project) or die_usage;
1742 shift(@ARGV) if @ARGV;
1743 if (@ARGV && !valid_desc(join(" ", @ARGV))) {
1744 die "invalid description (use --force to accept): \"".join(" ", @ARGV)."\"\n"
1745 unless $force;
1746 warn "using invalid description with --force\n" unless $quiet;
1748 my $desc = clean_desc(join(" ", @ARGV));
1749 if (@ARGV && length($desc) > 1024) {
1750 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
1751 unless $force;
1752 warn "using longer than 1024 char description with --force\n" unless $quiet;
1754 my $old = $project->{desc};
1755 if (!@ARGV) {
1756 print "$old\n" if defined($old);
1757 return 0;
1759 if (defined($old) && $old eq $desc) {
1760 warn $project->{name}, ": skipping update of description to same value\n" unless $quiet;
1761 } else {
1762 # Avoid touching anything other than description file
1763 $project->_property_fput("desc", $desc);
1764 $project->_set_changed;
1765 warn $project->{name}, ": description updated to \"$desc\"\n" unless $quiet;
1767 return 0;
1770 sub cmd_setreadme {
1771 my ($force, $readmetype) = (0, undef);
1772 parse_options(force => \$force, ":type" => \$readmetype, ":format" => \$readmetype);
1773 @ARGV == 1 && defined($readmetype) and push(@ARGV, undef);
1774 @ARGV == 2 || (@ARGV <= 1 && !$force && !defined($readmetype) && !$setopt) or die_usage;
1775 defined($readmetype) and $readmetype = Girocco::Project::_normalize_rmtype($readmetype,1);
1776 defined($readmetype) && !$readmetype and die_usage;
1777 my $project = get_project_harder_gently($ARGV[0]);
1778 defined($project) or die_usage;
1779 my $old = $project->{READMEDATA};
1780 if (@ARGV <= 1) {
1781 chomp $old if defined($old);
1782 print "$old\n" if defined($old) && $old ne "";
1783 return 0;
1785 $readmetype or $readmetype = $project->{rmtype};
1786 my ($new, $raw, $newname);
1787 $newname = '';
1788 if (!defined($ARGV[1])) {
1789 $new = $old;
1790 $newname = "original README data";
1791 $readmetype ne $project->{rmtype} && $new ne "" and $raw = 1;
1792 } elsif ($ARGV[1] eq "-") {
1793 local $/;
1794 $new = <STDIN>;
1795 $raw = 1;
1796 $newname = "contents of <STDIN>";
1797 } elsif (lc($ARGV[1]) eq "automatic" || lc($ARGV[1]) eq "auto") {
1798 $new = "";
1799 } elsif (lc($ARGV[1]) eq "suppressed" || lc($ARGV[1]) eq "suppress") {
1800 $new = "<!-- suppress -->";
1801 } else {
1802 my $fn = $ARGV[1];
1803 $fn =~ s/^\@//;
1804 die "missing filename for README\n" unless $fn ne "";
1805 die "no such file: \"$fn\"\n" unless -f $fn && -r $fn;
1806 open F, '<', $fn or die "cannot open \"$fn\" for reading: $!\n";
1807 local $/;
1808 $new = <F>;
1809 close F;
1810 $raw = 1;
1811 $newname = "contents of \"$fn\"";
1813 defined($new) or $new = '';
1814 my $origrmtype = $project->{rmtype};
1815 $project->{rmtype} = $readmetype;
1816 $project->{READMEDATA} = to_utf8($new, 1);
1817 $project->_cleanup_readme;
1818 if (length($project->{READMEDATA}) > 8192) {
1819 die "readme greater than 8192 chars is too long (use --force to override)\n"
1820 unless $force;
1821 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
1823 if ($raw) {
1824 my ($cnt, $err) = $project->_lint_readme(0);
1825 if ($cnt) {
1826 my $msg = "README: $cnt error";
1827 $msg .= "s" unless $cnt == 1;
1828 print STDERR "$msg\n", "-" x length($msg), "\n", $err
1829 unless $force && $quiet;
1830 exit(255) unless $force && $project->{rmtype} eq 'HTML';
1831 warn $project->{name} . ": using invalid raw HTML with --force\n" unless $quiet;
1832 $project->{README} = $project->{READMEDATA};
1835 if (defined($old) && $old eq $project->{READMEDATA} && $readmetype eq $origrmtype && !$force) {
1836 warn $project->{name}, ": skipping update of README to same value\n" unless $quiet;
1837 } else {
1838 # Avoid touching anything other than README.html file
1839 $project->_property_fput("READMEDATA", $project->{READMEDATA}, 1);
1840 $project->_property_fput("README", $project->{README});
1841 $project->_property_fput("rmtype", $readmetype) if $readmetype ne $origrmtype;
1842 $project->_set_changed;
1843 my $desc = get_readme_desc($project->{README});
1844 if ($newname) {
1845 $newname .= " ($desc)";
1846 } else {
1847 $newname = $desc;
1849 warn $project->{name}, ": README $readmetype format updated to $newname\n" unless $quiet;
1851 return 0;
1854 sub valid_head {
1855 my ($proj, $newhead) = @_;
1856 my %okheads = map({($_ => 1)} $proj->get_heads);
1857 exists($okheads{$newhead});
1860 sub cmd_sethead {
1861 my $force = 0;
1862 parse_options(force => \$force);
1863 @ARGV == 2 || (@ARGV <= 1 && !$setopt) or die_usage;
1864 my $project = get_project_harder_gently($ARGV[0]);
1865 defined($project) or die_usage;
1866 if (@ARGV == 2 && !valid_head($project, $ARGV[1])) {
1867 die "invalid head (try \"@{[basename($0)]} listheads $ARGV[0]\"): \"$ARGV[1]\"\n" unless $force;
1868 valid_branch_name($ARGV[1]) or
1869 die "grossly invalid new HEAD: $ARGV[1]\n";
1870 warn "Continuing with --force even though requested branch does not exist\n" unless $quiet;
1872 my $old = $project->{HEAD};
1873 if (@ARGV <= 1) {
1874 print "$old\n" if defined($old);
1875 return 0;
1877 if (defined($old) && $old eq $ARGV[1]) {
1878 warn $project->{name}, ": skipping update of HEAD symref to same value\n" unless $quiet;
1879 } else {
1880 # Avoid touching anything other than the HEAD symref
1881 $project->set_HEAD($ARGV[1]);
1882 $project->_set_changed;
1883 warn $project->{name}, ": HEAD symref updated to \"refs/heads/$ARGV[1]\"\n" unless $quiet;
1885 return 0;
1888 sub cmd_sethooks {
1889 my $force = 0;
1890 parse_options(force => \$force);
1891 @ARGV == 2 || (@ARGV <= 1 && !$force && !$setopt) or die_usage;
1892 my $project = get_project_harder_gently($ARGV[0]);
1893 defined($project) or die_usage;
1894 my $projconfig = read_config_file_hash($project->{path}."/config");
1895 my $ghp = $Girocco::Config::reporoot."/_global/hooks";
1896 my $rghp = realpath($ghp);
1897 my $lhp = $project->{path}."/hooks";
1898 my $rlhp = realpath($lhp);
1899 my $ahp = "";
1900 my $rahp = undef;
1901 if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) {
1902 $ahp = $projconfig->{"core.hookspath"};
1903 $rahp = realpath($ahp);
1905 if (@ARGV <= 1) {
1906 if (defined($rahp) && $rahp ne "") {
1907 if ($rahp eq $rghp) {
1908 my $nc = ($ahp eq $ghp ? "" : " non-canonical");
1909 printf "%s \t(global%s)\n", $ahp, $nc;
1910 } elsif ($rahp eq $rlhp) {
1911 my $nc = ($ahp eq $lhp ? "" : " non-canonical");
1912 printf "%s \t(local%s)\n", $ahp, $nc;
1913 } elsif ($rahp ne $ahp) {
1914 print "$ahp \t($rahp)\n";
1915 } else {
1916 print "$ahp\n";
1918 } elsif ($ahp ne "") {
1919 print "$ahp \t(non-existent)\n";
1921 return 0;
1923 my $shp = $ARGV[1];
1924 if (lc($shp) eq "global") {
1925 $shp = $ghp;
1926 } elsif (lc($shp) eq "local") {
1927 $shp = $lhp;
1928 } elsif (substr($shp, 0, 2) eq "~/") {
1929 $shp = $ENV{"HOME"}.substr($shp,1);
1930 } elsif ($shp =~ m,^~([a-zA-Z_][a-zA-Z_0-9]*)((?:/.*)?)$,) {
1931 my $sfx = $2;
1932 my $hd = (getpwnam($1))[7];
1933 $shp = $hd . $sfx if defined($hd) && $hd ne "" && $hd ne "/" && -d $hd;
1935 $shp ne "" && -d $shp or die "no such directory: $ARGV[1]\n";
1936 my $rshp = realpath($shp);
1937 defined($rshp) && $rshp ne "" or die "could not realpath: $ARGV[1]\n";
1938 $rshp =~ m,^/[^/], or die "invalid hookspath: $rshp\n";
1939 die "refusing to switch from current non-global hookspath without --force\n"
1940 if !$force && defined($rahp) && $rahp ne "" && $rahp ne $rghp && $rshp ne $rahp;
1941 if (!$force && defined($rahp) && $rahp ne "") {
1942 if ($rshp eq $rahp && ($ahp eq $ghp || $ahp eq $lhp)) {
1943 warn $project->{name}, ": skipping update of hookspath to same effective value\n" unless $quiet;
1944 return 0;
1947 $rshp = $ghp if $rshp eq $rghp;
1948 $rshp = $lhp if $rshp eq $rlhp;
1949 if ($rshp eq $ahp) {
1950 warn $project->{name}, ": skipping update of hookspath to same value\n" unless $quiet;
1951 return 0;
1953 die "refusing to set neither local nor global hookspath without --force\n"
1954 if !$force && $rshp ne $ghp && $rshp ne $lhp;
1955 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1956 'config', "core.hookspath", $rshp);
1957 my $newval = '"'.$rshp.'"';
1958 $newval = "global" if $rshp eq $ghp;
1959 $newval = "local" if $rshp eq $lhp;
1960 warn $project->{name}, ": hookspath set to $newval\n" unless $quiet;
1961 return 0;
1964 our %boolfields;
1965 BEGIN {
1966 %boolfields = (
1967 cleanmirror => 1,
1968 reverseorder => 0,
1969 summaryonly => 0,
1970 statusupdates => 1,
1974 sub cmd_setbool {
1975 my $force = 0;
1976 parse_options("force" => \$force);
1977 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1978 my $project = get_project_harder($ARGV[0]);
1979 if (!exists($boolfields{$ARGV[1]})) {
1980 die "invalid boolean field name: \"$ARGV[1]\" -- try \"help\"\n";
1982 if (@ARGV == 3 && $boolfields{$ARGV[1]} && !$project->{mirror}) {
1983 die "invalid boolean field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
1984 unless $force;
1985 warn "using mirror field on non-mirror with --force\n" unless $quiet;
1987 if (@ARGV == 3 && !valid_bool($ARGV[2])) {
1988 die "invalid boolean value: \"$ARGV[2]\"\n";
1990 my $bool = clean_bool($ARGV[2]);
1991 my $old = $project->{$ARGV[1]};
1992 if (@ARGV == 2) {
1993 print "$old\n" if defined($old);
1994 return 0;
1996 if (defined($old) && $old eq $bool) {
1997 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
1998 } else {
1999 # Avoid touching anything other than $ARGV[1] field
2000 $project->_property_fput($ARGV[1], $bool);
2001 warn $project->{name}, ": $ARGV[1] updated to $bool\n" unless $quiet;
2003 return 0;
2006 sub cmd_setjsontype {
2007 @ARGV == 2 || (@ARGV <= 1 && !$setopt) or die_usage;
2008 my $project = get_project_harder_gently($ARGV[0]);
2009 defined($project) or die_usage;
2010 my $jsontype;
2011 if (@ARGV == 2) {
2012 my $jt = lc($ARGV[1]);
2013 index($jt, "/") >= 0 or $jt = "application/".$jt;
2014 $jt eq 'application/x-www-form-urlencoded' ||
2015 $jt eq 'application/json' or
2016 die "invalid jsontype value: \"$ARGV[1]\"\n";
2017 $jsontype = $jt;
2019 my $old = $project->{jsontype};
2020 if (@ARGV <= 1) {
2021 print "$old\n" if defined($old);
2022 return 0;
2024 if (defined($old) && $old eq $jsontype) {
2025 warn $project->{name}, ": skipping update of jsontype to same value\n" unless $quiet;
2026 } else {
2027 # Avoid touching anything other than jsontype field
2028 $project->_property_fput('jsontype', $jsontype);
2029 warn $project->{name}, ": jsontype updated to $jsontype\n" unless $quiet;
2031 return 0;
2034 sub cmd_setjsonsecret {
2035 @ARGV == 2 || (@ARGV <= 1 && !$setopt) or die_usage;
2036 my $project = get_project_harder_gently($ARGV[0]);
2037 defined($project) or die_usage;
2038 my $jsonsecret;
2039 if (@ARGV == 2) {
2040 my $js = $ARGV[1];
2041 $js =~ s/^\s+//; $js =~ s/\s+$//;
2042 $jsonsecret = $js;
2044 my $old = $project->{jsonsecret};
2045 if (@ARGV <= 1) {
2046 print "$old\n" if defined($old);
2047 return 0;
2049 if (defined($old) && $old eq $jsonsecret) {
2050 warn $project->{name}, ": skipping update of jsonsecret to same value\n" unless $quiet;
2051 } else {
2052 # Avoid touching anything other than jsonsecret field
2053 $project->_property_fput('jsonsecret', $jsonsecret);
2054 warn $project->{name}, ": jsonsecret updated to \"$jsonsecret\"\n" unless $quiet;
2056 return 0;
2059 sub cmd_setautogchack {
2060 @ARGV == 2 || (@ARGV <= 1 && !$setopt) or die_usage;
2061 my $project = get_project_harder_gently($ARGV[0]);
2062 defined($project) or die_usage;
2063 my $aghok = $Girocco::Config::autogchack &&
2064 ($project->{mirror} || $Girocco::Config::autogchack ne "mirror");
2065 my $old = defined($project->{autogchack}) ? clean_bool($project->{autogchack}) : "unset";
2066 if (@ARGV == 1) {
2067 print "$old\n" if $aghok;
2068 return 0;
2070 my $bool;
2071 if (lc($ARGV[1]) eq "unset") {
2072 $bool = "unset";
2073 } else {
2074 valid_bool($ARGV[1]) or die "invalid boolean value: \"$ARGV[1]\"\n";
2075 $bool = clean_bool($ARGV[1]);
2077 if (!$aghok) {
2078 die "\$Girocco::Config::autogchack is false\n" unless $Girocco::Config::autogchack;
2079 die "\$Girocco::Config::autogchack is only enabled for mirrors\n";
2081 if ($old eq $bool) {
2082 warn $project->{name}, ": autogchack value unchanged\n" unless $quiet;
2083 } else {
2084 if ($bool eq "unset") {
2085 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
2086 'config', '--unset', "girocco.autogchack");
2087 } else {
2088 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
2089 'config', '--bool', "girocco.autogchack", $bool);
2092 return system($Girocco::Config::basedir . "/jobd/maintain-auto-gc-hack.sh", $project->{name}) == 0
2093 ? 0 : 1;
2096 sub valid_url {
2097 my ($url, $type) = @_;
2098 $type ne 'baseurl' and return valid_web_url($url);
2099 valid_repo_url($url) or return 0;
2100 if ($Girocco::Config::restrict_mirror_hosts) {
2101 my $mh = extract_url_hostname($url);
2102 is_dns_hostname($mh) or return 0;
2103 !is_our_hostname($mh) or return 0;
2105 return 1;
2108 our %urlfields;
2109 BEGIN {
2110 %urlfields = (
2111 baseurl => ["url" , 1],
2112 homepage => ["hp" , 0],
2113 notifyjson => ["notifyjson", 0],
2117 sub cmd_seturl {
2118 my $force = 0;
2119 parse_options("force" => \$force);
2120 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
2121 my $project = get_project_harder($ARGV[0]);
2122 if (!exists($urlfields{$ARGV[1]})) {
2123 die "invalid URL field name: \"$ARGV[1]\" -- try \"help\"\n";
2125 if (@ARGV == 3 && ${$urlfields{$ARGV[1]}}[1] && !$project->{mirror}) {
2126 die "invalid URL field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
2127 unless $force;
2128 warn "using mirror field on non-mirror with --force\n" unless $quiet;
2130 if (@ARGV == 3 && !valid_url($ARGV[2], $ARGV[1])) {
2131 die "invalid URL (use --force to accept): \"$ARGV[2]\"\n"
2132 unless $force;
2133 warn "using invalid URL with --force\n" unless $quiet;
2135 my $old = $project->{${$urlfields{$ARGV[1]}}[0]};
2136 if (@ARGV == 2) {
2137 print "$old\n" if defined($old);
2138 return 0;
2140 if (defined($old) && $old eq $ARGV[2]) {
2141 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
2142 } else {
2143 # Avoid touching anything other than $ARGV[1]'s field
2144 $project->_property_fput(${$urlfields{$ARGV[1]}}[0], $ARGV[2]);
2145 if ($ARGV[1] eq "baseurl") {
2146 $project->{url} = $ARGV[2];
2147 $project->_set_bangagain;
2149 $project->_set_changed unless $ARGV[1] eq "notifyjson";
2150 warn $project->{name}, ": $ARGV[1] updated to $ARGV[2]\n" unless $quiet;
2152 return 0;
2155 our %msgsfields;
2156 BEGIN {
2157 %msgsfields = (
2158 notifymail => 1,
2159 notifytag => 1,
2163 sub cmd_setmsgs {
2164 my $force = 0;
2165 parse_options("force" => \$force);
2166 @ARGV >= 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
2167 my $project = get_project_harder(shift @ARGV);
2168 my $field = shift @ARGV;
2169 if (!exists($msgsfields{$field})) {
2170 die "invalid msgs field name: \"$field\" -- try \"help\"\n";
2172 if (@ARGV && !valid_addrlist(@ARGV)) {
2173 die "invalid email address list (use --force to accept): \"".join(" ",@ARGV)."\"\n"
2174 unless $force;
2175 warn "using invalid email address list with --force\n" unless $quiet;
2177 my $old = $project->{$field};
2178 if (!@ARGV) {
2179 printf "%s\n", clean_addrlist($old, " ") if defined($old);
2180 return 0;
2182 my $newlist = clean_addrlist(join(" ",@ARGV));
2183 if (defined($old) && $old eq $newlist) {
2184 warn $project->{name}, ": skipping update of $field to same value\n" unless $quiet;
2185 } else {
2186 # Avoid touching anything other than $field's field
2187 $project->_property_fput($field, $newlist);
2188 warn $project->{name}, ": $field updated to \"$newlist\"\n" unless $quiet;
2190 return 0;
2193 sub cmd_setusers {
2194 my $force = 0;
2195 parse_options("force" => \$force);
2196 @ARGV >= 2 || (@ARGV <= 1 && !$force && !$setopt) or die_usage;
2197 my $project = get_project_harder_gently($ARGV[0]);
2198 defined($project) or die_usage;
2199 shift(@ARGV) if @ARGV;
2200 my $projname = $project->{name};
2201 !@ARGV || !$project->{mirror} or die "cannot set users list for mirror project: \"$projname\"\n";
2202 my @newusers = ();
2203 if (@ARGV) {
2204 eval {@newusers = validate_users(join(" ", @ARGV), $force); 1;} or exit 255;
2205 die "refusing to set empty users list without --force\n" unless @newusers || $force;
2207 return 0 if !@ARGV && $project->{mirror};
2208 my $oldusers = $project->{users};
2209 if ($oldusers && ref($oldusers) eq "ARRAY") {
2210 $oldusers = join("\n", @$oldusers);
2211 } else {
2212 $oldusers = "";
2214 if (!@ARGV) {
2215 print "$oldusers\n" if $oldusers ne "";
2216 return 0;
2218 if ($oldusers eq join("\n", @newusers)) {
2219 warn "$projname: skipping update of users list to same value\n" unless $quiet;
2220 } else {
2221 # Avoid touching anything other than the users list
2222 $project->{users} = \@newusers;
2223 $project->_update_users;
2224 warn "$projname: users list updated to \"@{[join(',',@newusers)]}\"\n" unless $quiet;
2226 return 0;
2229 our %fieldnames;
2230 BEGIN {
2231 %fieldnames = (
2232 owner => [\&cmd_setowner, 0],
2233 desc => [\&cmd_setdesc, 0],
2234 description => [\&cmd_setdesc, 0],
2235 readme => [\&cmd_setreadme, 0],
2236 head => [\&cmd_sethead, 0],
2237 HEAD => [\&cmd_sethead, 0],
2238 hooks => [\&cmd_sethooks, 0],
2239 hookspath => [\&cmd_sethooks, 0],
2240 cleanmirror => [\&cmd_setbool, 1],
2241 reverseorder => [\&cmd_setbool, 1],
2242 summaryonly => [\&cmd_setbool, 1],
2243 statusupdates => [\&cmd_setbool, 1],
2244 autogchack => [\&cmd_setautogchack, 0],
2245 baseurl => [\&cmd_seturl, 1],
2246 homepage => [\&cmd_seturl, 1],
2247 notifyjson => [\&cmd_seturl, 1],
2248 jsontype => [\&cmd_setjsontype, 0],
2249 jsonsecret => [\&cmd_setjsonsecret, 0],
2250 notifymail => [\&cmd_setmsgs, 1],
2251 notifytag => [\&cmd_setmsgs, 1],
2252 users => [\&cmd_setusers, 0],
2256 sub do_getset {
2257 $setopt = shift;
2258 my @newargs = ();
2259 push(@newargs, shift) if @_ && $_[0] eq '--force';
2260 my $field = $_[1];
2261 (($setopt && @_ >= 3) || @_ == 2) && exists($fieldnames{$field}) or die_usage;
2262 push(@newargs, shift);
2263 shift unless ${$fieldnames{$field}}[1];
2264 push(@newargs, @_);
2265 diename(($setopt ? "set " : "get ") . $field);
2266 @ARGV = @newargs;
2267 &{${$fieldnames{$field}}[0]}(@ARGV);
2270 sub cmd_get {
2271 do_getset(0, @_);
2274 sub cmd_set {
2275 do_getset(1, @_);
2278 our %commands;
2279 BEGIN {
2280 %commands = (
2281 list => \&cmd_list,
2282 create => \&cmd_create,
2283 adopt => \&cmd_adopt,
2284 remove => \&cmd_remove,
2285 trash => \&cmd_remove,
2286 delete => \&cmd_remove,
2287 prune => \&cmd_prune,
2288 show => \&cmd_show,
2289 verify => \&cmd_verify,
2290 worktree => \&cmd_worktree,
2291 worktrees => \&cmd_worktree,
2292 urls => \&cmd_urls,
2293 listheads => \&cmd_listheads,
2294 listtags => \&cmd_listtags,
2295 listctags => \&cmd_listtags,
2296 deltags => \&cmd_deltags,
2297 delctags => \&cmd_deltags,
2298 addtags => \&cmd_addtags,
2299 addctags => \&cmd_addtags,
2300 chpass => \&cmd_chpass,
2301 checkpw => \&cmd_checkpw,
2302 gc => \&cmd_gc,
2303 update => \&cmd_update,
2304 remirror => \&cmd_remirror,
2305 setowner => \&cmd_setowner,
2306 setdesc => \&cmd_setdesc,
2307 setdescription => \&cmd_setdesc,
2308 setreadme => \&cmd_setreadme,
2309 sethead => \&cmd_sethead,
2310 sethooks => \&cmd_sethooks,
2311 sethookspath => \&cmd_sethooks,
2312 setbool => \&cmd_setbool,
2313 setboolean => \&cmd_setbool,
2314 setflag => \&cmd_setbool,
2315 setautogchack => \&cmd_setautogchack,
2316 seturl => \&cmd_seturl,
2317 setjsontype => \&cmd_setjsontype,
2318 setjsonsecret => \&cmd_setjsonsecret,
2319 setmsgs => \&cmd_setmsgs,
2320 setusers => \&cmd_setusers,
2321 get => \&cmd_get,
2322 set => \&cmd_set,
2325 our %nopager;
2326 BEGIN { %nopager = (
2327 # 1 => pager never allowed
2328 # -1 => pager defaults to off instead of on
2329 create => 1,
2330 adopt => -1,
2331 remove => -1,
2332 trash => -1,
2333 delete => -1,
2334 prune => -1,
2335 deltags => -1,
2336 delctags => -1,
2337 addtags => -1,
2338 addctags => -1,
2339 chpass => 1,
2340 checkpw => 1,
2341 gc => -1,
2342 update => -1,
2343 remirror => -1,
2344 setowner => -1,
2345 setdesc => -1,
2346 setdescription => -1,
2347 setreadme => -1,
2348 sethead => -1,
2349 sethooks => -1,
2350 sethookspath => -1,
2351 setbool => -1,
2352 setboolean => -1,
2353 setflag => -1,
2354 setautogchack => -1,
2355 seturl => -1,
2356 setjsontype => -1,
2357 setjsonsecret => -1,
2358 setmsgs => -1,
2359 setusers => -1,
2360 set => -1,
2361 urls => -1,
2362 verify => 1,
2363 worktree => 1,
2364 worktrees => 1,
2367 sub dohelp {
2368 my $cmd = shift;
2369 my $bn = basename($0);
2370 setup_pager_stdout($usepager);
2371 printf "%s version %s\n\n", $bn, $VERSION;
2372 if (defined($cmd) && $cmd ne '') {
2373 $cmd =~ s/^set(?=[a-zA-Z])//i;
2374 my $cmdhelp = '';
2375 my ($lastmt, $incmd);
2376 foreach (split('\n', sprintf($help, $bn))) {
2377 $lastmt || $incmd or $lastmt = /^\s*$/, next;
2378 $incmd = 1 if $lastmt && /^\s*(?:\[?set\]?)?$cmd\s/;
2379 last if $incmd && /^\s*$/;
2380 $incmd and $cmdhelp .= $_ . "\n";
2381 $lastmt = /^\s*$/;
2383 print $cmdhelp and exit 0 if $cmdhelp;
2385 printf $help, $bn;
2386 exit 0;
2389 sub main {
2390 local *ARGV = \@_;
2392 shift, $quiet=1, redo if @ARGV && $ARGV[0] =~ /^(?:-q|--quiet)$/i;
2393 shift, $usepager=1, redo if @ARGV && $ARGV[0] =~ /^(?:-p|--pager|--paginate)$/i;
2394 shift, $usepager=0, redo if @ARGV && $ARGV[0] =~ /^(?:--no-pager|--no-paginate)$/i;
2396 dohelp($ARGV[1]) if !@ARGV || @ARGV && $ARGV[0] =~ /^(?:-h|-?-help|help)$/i;
2397 my $command = shift;
2398 diename($command);
2399 $setopt = 1;
2400 if (!exists($commands{$command}) && exists($commands{"set".$command})) {
2401 $setopt = 0;
2402 $command = "set" . $command;
2404 exists($commands{$command}) or die "Unknown command \"$command\" -- try \"help\"\n";
2405 dohelp($command) if @ARGV && ($ARGV[0] =~ /^(?:-h|-?-help)$/i ||
2406 $ARGV[0] =~ /^help$/i && !Girocco::Project::does_exist("help",1));
2407 $nopager{$command} && $nopager{$command} > 0 and $usepager = 0;
2408 my $pgdfltoff = $nopager{$command} && $nopager{$command} < 0 ? 1 : 0;
2409 setup_pager_stdout($usepager, $pgdfltoff);
2410 &{$commands{$command}}(@ARGV);