projtool.pl: allow existing projects to be specified by path
[girocco/readme.git] / toolbox / projtool.pl
blob09bfb589f5760209b2ab2ea125a59d464889c88c
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.3'}
14 use File::Basename;
15 use Digest::MD5 qw(md5_hex);
16 use IO::Socket;
17 use Cwd qw(realpath);
18 use POSIX qw(strftime);
19 use lib "__BASEDIR__";
20 use Girocco::Config;
21 use Girocco::Util;
22 use Girocco::HashUtil;
23 use Girocco::CLIUtil;
24 use Girocco::Project;
25 use Girocco::User;
27 exit(&main(@ARGV)||0);
29 our $help;
30 BEGIN {my @a; /^(.*)$/s && push(@a, $1) foreach @ARGV; @ARGV=@a;}
31 BEGIN {$help = <<'HELP'}
32 Usage: %s [<global option>...] <command> <options>
34 global options:
35 -q | --quiet suppress warning messages
36 -p | --pager force output to be paginated
37 --no-pager never paginate output
39 Note that as a convenience, where an existing <project> is required as
40 an argument, a path to the project may be given instead of the name in
41 most places. The "remove" and "prune" commands only accept names.
42 Since a matching project name takes precedence over a path, to force
43 interpretation as a path, start the path with "/" or "./" or "../".
44 Giving "." will find the project matching the current working directory.
46 help [<command>]
47 show full help or just for <command> if given
49 list [--verbose] [--sort=lcname|name|owner|gid|no] [--owner] [<regexp>]
50 list all projects (default is --sort=lcname)
51 limit to project names matching <regex> if given
52 match <regex> against owner instead of project name with --owner
54 create [--force] [--no-alternates] [--orphan] [<option>...] <project>
55 create new project <project> (prompted)
56 <option> can be:
57 --no-alternates skip setup of objects/info/alternates
58 --orphan allow creation of subproject w/o a parent
59 -p use mkdir -p during --orphan creation
60 --no-password set password crypt to invalid value "unknown"
61 --no-owner leave the gitweb.owner config unset
62 --mirror=<url> create a mirror from <url>
63 --full-mirror mirror all refs
64 --push[=<list>] create a push project
65 --desc=<string> specify project description w/o prompt
66 --homepage=<url> specify project homepage URL w/o prompt
67 --defaults do no interactive prompting at all
68 Using --no-password skips the prompts for password, using
69 --no-owner skips the prompt for owner and using --mirror=<url>
70 or --push[=<list>] skips the prompts for mirror URL and
71 heads-only and push users. With --defaults if neither
72 --mirror=<url> nor --push[=<list>] is given then --push will
73 be implied. Using --desc=<string> will force a specific
74 description (including an empty string) and skip the prompt for
75 it. Otherwise a non-empty default description will always be
76 supplied in lieu of an empty or omitted description.
78 adopt [--force] [--type=mirror|push] [<option>...] <project> [<users>]
79 adopt project <project>
80 type of project is guessed if --type=<type> omitted
81 <users> is same as <newuserslist> for setusers command
82 <option> can be:
83 --dry-run do all the checks but don't perform adoption
84 --verbose show project info dump (useful with --dry-run)
85 --no-users no push users at all (<users> must be omitted)
86 --no-owner leave the gitweb.owner config totally unchanged
87 --owner=<val> set the gitweb.owner config to <val>
88 Both --no-owner and --owner=<val> may NOT be given, with neither
89 take owner from preexisting gitweb.owner else use admin setting.
90 For mirrors <users> is ignored otherwise if no <users> and no
91 --no-users option the push users list will consist of the single
92 user name matching the owner or empty if none or more than one.
93 With --dry-run <project> can be an absolute path to a git dir.
95 remove [--force] [--really-delete] [--keep-forks] <project>
96 remove project <project>
97 do not move to _recyclebin with --really-delete (just rm -rf)
98 remove projects with forks (by keeping forks) using --keep-forks
100 prune [--quiet] (--force | --dry-run) [<project>...]
101 check to see if any projects (default is all projects) are
102 missing the associated project directory on disk.
103 Requires either --force or --dry-run option to operate.
104 With --dry-run only show what would be done.
105 With --prune actually remove any extraneous project(s).
106 With --dry-run exit code is non-zero if any action needed.
107 With --quiet, suppress output message, if any.
109 show <project>
110 show project <project>
112 urls [--push] <project>
113 show available fetch/push URLs for <project>
114 Note that this does NOT include non-Git protocol URLs such
115 as any home page or any upstream URL for a mirror project --
116 those are all accessible via the "show" command.
117 The URLs shown are those that would be shown by gitweb.cgi.
118 With --push only show push urls (mirrors have no push urls)
120 listheads <project>
121 list all available heads for <project> and indicate current head
123 listtags [--verbose] <project>
124 list all ctags on project <project>
125 with --verbose include tag counts
127 deltags <project> [-i] <tagstodel>
128 remove any ctags on project <project> present in <tagstodel>
129 <tagstodel> is space or comma separated list of tags to remove
130 with -i match against <tagstodel> without regard to letter case
132 addtags <project> <tagstoadd>
133 add ctags to project <project>
134 <tagstoadd> is space or comma separated list of tags to add
136 chpass [--force] <project> [random | unknown]
137 change project <project> password (prompted)
138 with "random" set to random password
139 with "unknown" set password hash to invalid value "unknown"
141 checkpw <project>
142 check project <project> password for a match (prompted)
144 gc [--force | --auto] [--redelta | --recompress] <project>
145 run the gc.sh script on project <project>
146 with --auto let the gc.sh script decide what to do
147 with --force cause a full gc to take place (force_gc=1)
148 with neither --auto nor --force do a mini or if needed a full gc
149 (in other words just touch .needsgc and run gc.sh)
150 with --redelta a full gc will use pack-objects --no-reuse-delta
151 with --recompress a full gc uses pack-objects --no-reuse-object
152 (--no-reuse-delta and --no-reuse-object are accepted as aliases)
153 with --aggressive activate the --force and --redelta options
154 unless the global --quiet option is given show_progress=1 is used
156 update [--force] [--quiet | --summary] <project>
157 run the update.sh script on project <project>
158 with --force cause a fetch to always take place (force_update=1)
159 with --quiet only show errors (show_progress is left unset)
160 with --summary show progress and ref summary (show_progress=1)
161 with neither --quiet nor --summary show it all (show_progress=2)
163 remirror [--force] <project>
164 initiate a remirror of project <project>
166 [set]owner [--force] <project> <newowner>
167 set project <project> owner to <newowner>
168 without "set" and only 1 arg, just show current project owner
170 [set]desc [--force] <project> <newdesc>
171 set project <project> description to <newdesc>
172 without "set" and only 1 arg, just show current project desc
174 [set]readme [--force] [--format=<readmetype>] <project> [<newsetting>]
175 set project <project> readme to <newsetting>
176 <readmetype> is markdown|plain|html (default is no change)
177 <newsetting> is automatic|suppressed|-|[@]filename
178 with "set" <readmetype> and/or <newsetting> is required
179 without "set" and only 2 args, just show current readme setting
181 [set]head <project> <newhead>
182 set project <project> HEAD symbolic ref to <newhead>
183 without "set" and only 1 arg, just show current project HEAD
185 [set]bool [--force] <project> <flagname> <boolvalue>
186 set project <project> boolean <flagname> to <boolvalue>
187 <flagname> is cleanmirror|reverseorder|summaryonly|statusupdtaes
188 without "set" and only 2 args, just show current flag value
190 [set]hooks [--force] <project> local | global | <path>
191 set project <project> hookspath to local, global or <path>
192 without "set" and only 1 arg, just show current hookspath
194 [set]autogchack <project> <boolvalue> | unset
195 set project <project> autogchack to <boolvalue> or "unset" it
196 without "set" just show current autogchack setting if enabled
197 with "set" autogchack must be enabled in Config.pm for the
198 type of project and maintain-auto-gc-hack.sh is always run
200 [set]url [--force] <project> <urlname> <newurlvalue>
201 set project <project> url <urlname> to <newurlvalue>
202 <urlname> is baseurl|homepage|notifyjson
203 without "set" and only 2 args, just show current url value
205 [set]msgs [--force] <project> <msgsname> <eaddrlist>
206 set project <project> msgs <msgsname> to <addrlist>
207 <msgsname> is notifymail|notifytag
208 <eaddrlist> is space or comma separated list of email addresses
209 without "set" and only 2 args, just show current msgs value
211 [set]users [--force] <project> <newuserslist>
212 set push project <project> users list to <newuserslist>
213 <newuserslist> is space or comma separated list of user names
214 without "set" and only 1 arg, just show current users list
216 [set]jsontype <project> <newjsontype>
217 set project <project> JSON Content-Type to <newjsontype>
218 <newjsontype> is x-www-form-urlencoded or json
219 without "set" and only 1 arg, just show current jsontype
221 [set]jsonsecret <project> <newjsonsecret>
222 set project <project> JSON secret to <newjsonsecret>
223 <newjsonsecret> is a string (empty string disables signatures)
224 without "set" and only 1 arg, just show current jsonsecret
226 get <project> <fieldname>
227 show project <project> field <fieldname>
228 <fieldname> is owner|desc|readme|head|hooks|users|jsontype
229 or jsonsecret|<flagname>|autogchack|<urlname>|<msgsname>
231 set [--force] <project> <fieldname> <newfieldvalue>
232 set project <project> field <fieldname> to <newfieldvalue>
233 <fieldname> same as for get
234 <newfieldvalue> same as for corresponding set... command
235 HELP
237 our $quiet;
238 our $usepager;
239 our $setopt;
240 sub die_usage {
241 my $sub = shift || diename;
242 if ($sub) {
243 die "Invalid arguments to $sub command -- try \"help\"\n";
244 } else {
245 die "Invalid arguments -- try \"help\"\n";
249 sub get_readme_len {
250 my $rm = shift;
251 defined($rm) or $rm = '';
252 return "length " . length($rm);
255 sub get_readme_desc {
256 my $rm = shift;
257 defined($rm) or $rm = '';
258 if (length($rm)) {
259 my $test = $rm;
260 $test =~ s/<!--(?:[^-]|(?:-(?!-)))*-->//gs;
261 $test =~ s/\s+//s;
262 return $test eq '' ? "suppressed" : "length " . length($rm);
263 } else {
264 return "automatic";
268 sub get_ctag_counts {
269 my $project = shift;
270 my $compact = shift;
271 my @ctags = ();
272 foreach ($project->get_ctag_names) {
273 my $val = 0;
274 my $ct;
275 if (open $ct, '<', $project->{path}."/ctags/$_") {
276 my $count = <$ct>;
277 close $ct;
278 defined $count or $count = '';
279 chomp $count;
280 $val = $count =~ /^[1-9]\d*$/ ? $count : 1;
282 if ($compact) {
283 if ($val == 1) {
284 push(@ctags, $_);
285 } elsif ($val > 1) {
286 push(@ctags, $_."(".$val.")");
288 } else {
289 push(@ctags, [$_, $val]) if $val;
292 @ctags;
295 sub get_clean_project {
296 my $project = get_project_harder(@_);
297 delete $project->{loaded};
298 delete $project->{base_path};
299 delete $project->{ccrypt};
300 /^orig/i || !defined($project->{$_}) and delete $project->{$_} foreach keys %$project;
301 $project->{owner} = $project->{email}; delete $project->{email};
302 $project->{homepage} = $project->{hp}; delete $project->{hp};
303 $project->{baseurl} = $project->{url}; delete $project->{url};
304 if (defined($project->{path}) && $project->{path} ne "") {
305 my $rp = realpath($project->{path});
306 defined($rp) && $rp ne "" and $project->{realpath} = $rp;
307 if (-f "$rp/objects/info/packs") {
308 my $ipt = (stat _)[9];
309 defined($ipt) and $project->{infopackstime} =
310 strftime("%Y-%m-%d %H:%M:%S %z", localtime($ipt));
313 my $owner = $project->{owner};
314 if ($owner) {
315 $owner = lc($owner);
316 my @owner_users = map {$owner eq lc($$_[4]) ? $$_[1] : ()} get_all_users;
317 $project->{owner_users} = \@owner_users if @owner_users;
319 my $projname = $project->{name};
320 my @forks = grep {$$_[1] =~ m,^$projname/,} get_all_projects;
321 $project->{has_forks} = 1 if @forks;
322 $project->{has_alternates} = 1 if $project->has_alternates;
323 my @bundles = $project->bundles;
324 for (my $i = 0; $i < @bundles; ++$i) {
325 my $secs = $bundles[$i]->[0];
326 $bundles[$i]->[0] = strftime("%Y-%m-%d %H:%M:%S %z", localtime($secs));
327 my $sz = $bundles[$i]->[2];
328 1 while $sz =~ s/(?<=\d)(\d{3})(?:,|$)/,$1/g;
329 $bundles[$i]->[2] = $sz;
331 delete $project->{bundles};
332 $project->{bundles} = \@bundles if @bundles;
333 $project->{mirror} = 0 unless $project->{mirror};
334 $project->{is_empty} = 1 if $project->is_empty;
335 delete $project->{showpush} unless $project->{showpush};
336 delete $project->{users} if $project->{mirror};
337 delete $project->{baseurl} unless $project->{mirror};
338 delete $project->{banged} unless $project->{mirror};
339 delete $project->{lastrefresh} unless $project->{mirror};
340 delete $project->{cleanmirror} unless $project->{mirror};
341 delete $project->{statusupdates} unless $project->{mirror};
342 delete $project->{lastparentgc} unless $projname =~ m,/,;
343 unless ($project->{banged}) {
344 delete $project->{bangcount};
345 delete $project->{bangfirstfail};
346 delete $project->{bangmessagesent};
348 my $projhook = $project->_has_notifyhook;
349 if (defined($projhook) && $projhook ne "") {
350 $project->{notifyhook} = $projhook;
351 } else {
352 delete $project->{notifyhook};
354 $project->{README} = get_readme_desc($project->{README}) if exists($project->{README});
355 $project->{READMEDATA} = get_readme_len($project->{READMEDATA}) if exists($project->{READMEDATA});
356 my @tags = get_ctag_counts($project, 1);
357 $project->{tags} = \@tags if @tags;
358 my $projconfig = read_config_file_hash($project->{path}."/config");
359 if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) {
360 my $ahp = $projconfig->{"core.hookspath"};
361 my $rahp = realpath($ahp);
362 my $lhp = $project->{path}."/hooks";
363 my $rlhp = realpath($lhp);
364 my $ghp = $Girocco::Config::reporoot."/_global/hooks";
365 my $rghp = realpath($ghp);
366 $project->{has_local_hooks} = 1 if
367 defined($rahp) && defined($rlhp) && $rahp eq $rlhp;
368 $project->{has_global_hooks} = 1 if
369 defined($rahp) && defined($rghp) && $rahp eq $rghp;
370 $project->{hookspath} = $ahp unless $ahp eq $lhp || $ahp eq $ghp;
372 $project;
375 sub clean_addrlist {
376 my %seen = ();
377 my @newlist = ();
378 foreach (split(/[,\s]+/, $_[0])) {
379 next unless $_;
380 $seen{lc($_)} = 1, push(@newlist, $_) unless $seen{lc($_)};
382 return join(($_[1]||","), @newlist);
385 sub valid_addrlist {
386 my $cleaned = clean_addrlist(join(" ", @_));
387 return 1 if $cleaned eq "";
388 valid_email_multi($cleaned) && length($cleaned) <= 512;
391 sub validate_users {
392 my ($userlist, $force, $nodie) = @_;
393 my @newusers = ();
394 my $badlist = 0;
395 my %seenuser = ();
396 my $mobok = $Girocco::Config::mob && $Girocco::Config::mob eq "mob";
397 my %users = map({($$_[1] => $_)} get_all_users);
398 foreach (split(/[\s,]+/, $userlist)) {
399 if (exists($users{$_}) || $_ eq "everyone" || ($mobok && $_ eq "mob")) {
400 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
401 next;
403 if (Girocco::User::does_exist($_, 1)) {
404 if ($force) {
405 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
406 } else {
407 $badlist = 1;
408 warn "refusing to allow questionable user \"$_\" without --force\n" unless $nodie && $quiet;
410 next;
412 $badlist = 1;
413 warn "invalid user: \"$_\"\n" unless $nodie && $quiet
415 die if $badlist && !$nodie;
416 return @newusers;
419 sub is_default_desc {
420 # "Unnamed repository; edit this file 'description' to name the repository."
421 # "Unnamed repository; edit this file to name it for gitweb."
422 local $_ = shift;
423 return 0 unless defined($_);
424 /Unnamed\s+repository;/i && /\s+edit\s+this\s+file\s+/i && /\s+to\s+name\s+/i;
427 sub valid_desc {
428 my $test = shift;
429 chomp $test;
430 return 0 if $test =~ /[\r\n]/;
431 $test =~ s/\s\s+/ /g;
432 $test =~ s/^\s+//;
433 $test =~ s/\s+$//;
434 return $test ne '';
437 sub clean_desc {
438 my $desc = shift;
439 defined($desc) or $desc = '';
440 chomp $desc;
441 $desc = to_utf8($desc, 1);
442 $desc =~ s/\s\s+/ /g;
443 $desc =~ s/^\s+//;
444 $desc =~ s/\s+$//;
445 return $desc;
448 sub parse_options {
449 Girocco::CLIUtil::_parse_options(
450 sub {
451 warn((($_[0]eq'?')?"unrecognized":"missing argument for")." option \"$_[1]\"\n")
452 unless $quiet;
453 die_usage;
454 }, @_);
457 sub cmd_list {
458 my %sortsub = (
459 lcname => sub {lc($$a[1]) cmp lc($$b[1])},
460 name => sub {$$a[1] cmp $$b[1]},
461 gid => sub {$$a[3] <=> $$b[3]},
462 owner => sub {lc($$a[4]) cmp lc($$b[4]) || lc($$a[1]) cmp lc($$b[1])},
463 no => sub {$$a[0] <=> $$b[0]},
465 my $sortopt = 'lcname';
466 my ($verbose, $owner);
467 parse_options(":sort" => \$sortopt, verbose => \$verbose, owner => \$owner);
468 my $regex;
469 if (@ARGV) {
470 my $val = shift @ARGV;
471 $regex = qr($val) or die "bad regex \"$val\"\n";
473 !@ARGV && exists($sortsub{$sortopt}) or die_usage;
474 my $sortsub = $sortsub{$sortopt};
475 my $grepsub = defined($regex) ? ($owner ? sub {$$_[4] =~ /$regex/} : sub {$$_[1] =~ /$regex/}) : sub {1};
476 my @projects = sort($sortsub grep {&$grepsub} get_all_projects);
477 if ($verbose) {
478 print map(sprintf("%s\n", join(":", (@$_)[1..5])), @projects);
479 } else {
480 print map(sprintf("%s: %s\n", $$_[1], $$_[5] =~ /^:/ ? "<mirror>" : $$_[5]), @projects);
482 return 0;
485 sub cmd_create {
486 my ($force, $noalternates, $orphanok, $optp, $nopasswd, $noowner, $defaults, $ispush, $pushusers,
487 $ismirror, $desc, $fullmirror, $homepage);
488 parse_options(
489 force => \$force, "no-alternates" => \$noalternates, orphan => \$orphanok, p => \$optp,
490 "no-password" => \$nopasswd, "no-owner" => \$noowner, defaults => \$defaults,
491 "push" => \$ispush, ":push" => \$pushusers, ":mirror" => \$ismirror, ":desc" => \$desc,
492 ":description" => \$desc, "full-mirror" => \$fullmirror, ":homepage" => \$homepage);
493 @ARGV == 1 or die_usage;
494 !defined($pushusers) || defined($ispush) or $ispush = 1;
495 defined($ismirror) && $ismirror =~ /^\s*$/ and die "--mirror url must not be empty\n";
496 die "--mirror and --push are mutually exclusive options\n" if $ismirror && $ispush;
497 die "--full-mirror requires use of --mirror=<url> option\n" if $fullmirror && !$ismirror;
498 !$defaults || defined($ispush) || defined($ismirror) or $ispush = 1;
499 !$defaults || defined($nopasswd) or $nopasswd = 1;
500 !$defaults || defined($noowner) or $noowner = 1;
501 !defined($ispush) || defined($pushusers) or $pushusers = "";
502 my $projname = $ARGV[0];
503 $projname =~ s/\.git$//i;
504 Girocco::Project::does_exist($projname, 1) and die "Project already exists: \"$projname\"\n";
505 if (!Girocco::Project::valid_name($projname, $orphanok, $optp)) {
506 warn "Refusing to create orphan project without --orphan\n"
507 if !$quiet && !$orphanok && Girocco::Project::valid_name($projname, 1, 1);
508 warn "Required orphan parent directory does not exist (use -p): ",
509 $Girocco::Config::reporoot.'/'.Girocco::Project::get_forkee_name($projname), "\n"
510 if !$quiet && $orphanok && Girocco::Project::valid_name($projname, 1, 1);
511 die "Invalid project name: \"$projname\"\n";
513 my ($forkee, $project) = ($projname =~ m#^(.*/)?([^/]+)$#);
514 my $newtype = $forkee ? 'fork' : 'project';
515 if (length($project) > 64) {
516 die "The $newtype name is longer than 64 characters. Do you really need that much?\n"
517 unless $force;
518 warn "Allowing $newtype name longer than 64 characters with --force\n" unless $quiet;
520 unless ($Girocco::Config::push || $Girocco::Config::mirror) {
521 die "Project creation disabled (no mirrors or push projects allowed)\n" unless $force;
522 warn "Continuing with --force even though both push and mirror projects are disabled\n" unless $quiet;
524 print "Enter settings for new project \"$projname\"\n" unless $defaults;
525 my %settings = ();
526 $settings{noalternates} = $noalternates;
527 if ($nopasswd) {
528 $settings{crypt} = "unknown";
529 } else {
530 my $np1 = prompt_noecho_nl_or_die("Admin password for project $projname (echo is off)");
531 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
532 my $np2 = prompt_noecho_nl_or_die("Retype admin password for project $projname");
533 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
534 "the admin passwords you have entered do not match each other.\n";
535 $settings{crypt} = scrypt_sha1($np1);
537 my $owner = "";
538 unless ($noowner) {
539 $owner = prompt_or_die("Owner/email name for project $projname");
540 unless (valid_email($owner)) {
541 unless ($force) {
542 warn "Your email sure looks weird...?\n";
543 redo;
545 warn "Allowing invalid email with --force\n" unless $quiet;
547 if (length($owner) > 96) {
548 unless ($force) {
549 warn "Your email is longer than 96 characters. Do you really need that much?\n";
550 redo;
552 warn "Allowing email longer than 96 characters with --force\n" unless $quiet;
555 $settings{email} = $owner;
556 my $baseurl = "";
557 my $checkmirror = sub {
558 my $checkurl = shift;
559 unless (valid_repo_url($checkurl)) {
560 unless ($force) {
561 warn "Invalid mirror URL: \"$checkurl\"\n";
562 return undef;
564 warn "Allowing invalid mirror URL with --force\n" unless $quiet;
566 if ($Girocco::Config::restrict_mirror_hosts) {
567 my $mh = extract_url_hostname($checkurl);
568 unless (is_dns_hostname($mh)) {
569 unless ($force) {
570 warn "Invalid non-DNS mirror URL: \"$checkurl\"\n";
571 return undef;
573 warn "Allowing non-DNS mirror URL with --force\n" unless $quiet;
575 if (is_our_hostname($mh)) {
576 unless ($force) {
577 warn "Invalid same-host mirror URL: \"$checkurl\"\n";
578 return undef;
580 warn "Allowing same-host mirror URL with --force\n" unless $quiet;
583 return $checkurl;
585 if ($ispush || $ismirror) {
586 !$ispush || $force || $Girocco::Config::push or
587 die "Push projects are disabled, create a mirror (or use --force)\n";
588 !$ismirror || $force || $Girocco::Config::mirror or
589 die "Mirror projects are disabled, create a push project (or use --force)\n";
590 if ($ismirror) {
591 &$checkmirror($ismirror) or die "Invalid --mirror URL\n";
592 $baseurl = $ismirror;
593 $settings{url} = $baseurl;
594 $settings{cleanmirror} = $fullmirror ? 0 : 1;
595 } else {
596 my @newusers = ();
597 if ($pushusers !~ /^[\s,]*$/) {
598 eval {@newusers = validate_users($pushusers, $force); 1;} or
599 die "Invalid --push user list\n";
601 $settings{users} = \@newusers;
603 } elsif ($force || $Girocco::Config::mirror) {{
604 if ($force || $Girocco::Config::push) {
605 $baseurl = prompt_or_die("URL to mirror from (leave blank for push project)", "");
606 } else {{
607 $baseurl = prompt_or_die("URL to mirror from");
608 unless ($baseurl ne "") {
609 warn "Push projects are disabled, you must enter a mirror URL (or use --force)\n";
610 redo;
613 if ($baseurl ne "") {
614 &$checkmirror($baseurl) or redo;
615 $settings{url} = $baseurl;
616 $settings{cleanmirror} =
617 ynprompt_or_die("Mirror only heads, tags and notes (Y/n)", "Yes");
620 my $mirror = ($baseurl eq "") ? 0 : 1;
621 my $checkdesc = sub {
622 my $d = shift;
623 if (length($d) > 1024) {
624 unless ($force) {
625 warn "Short description length greater than 1024 characters!\n";
626 return undef;
628 warn "Allowing short description length greater than 1024 characters\n" unless $quiet;
630 return $d;
632 if (defined($desc)) {
633 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
634 $desc eq "" || &$checkdesc($desc) or
635 die "Invalid --desc description\n";
636 } elsif (!$defaults) {
637 $desc = prompt_or_die("Short description", "");
638 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
639 $desc eq "" || &$checkdesc($desc) or redo;
640 $desc = undef if $desc eq "";
642 defined($desc) or $desc = $mirror ? "Mirror of $baseurl" : "Push project $projname";
643 $settings{desc} = $desc;
644 my $checkhp = sub {
645 my $hpurl = shift;
646 unless (valid_web_url($hpurl)) {
647 unless ($force) {
648 warn "Invalid home page URL: \"$hpurl\"\n";
649 return undef;
651 warn "Allowing invalid home page URL with --force\n" unless $quiet;
653 return $hpurl;
655 if (defined($homepage)) {
656 $homepage =~ s/^\s+//; $homepage =~ s/\s+$//;
657 $homepage eq "" || &$checkhp($homepage) or
658 die "Invalid --homepage URL\n";
659 } elsif (!$defaults) {
660 $homepage = prompt_or_die("Home page URL", "");
661 $homepage =~ s/^\s+//; $homepage =~ s/\s+$//;
662 $homepage eq "" || &$checkhp($homepage) or redo;
663 $homepage = undef if $homepage eq "";
665 $settings{hp} = $homepage;
666 my $jsonurl = "";
667 if (!$defaults) {
668 $jsonurl = prompt_or_die("JSON notify POST URL", "");
669 if ($jsonurl ne "" && !valid_web_url($jsonurl)) {
670 unless ($force) {
671 warn "Invalid JSON notify POST URL: \$jsonurl\"\n";
672 redo;
674 warn "Allowing invalid JSON notify POST URL with --force\n" unless $quiet;
677 $settings{notifyjson} = $jsonurl;
678 my $commitaddrs = "";
679 if (!$defaults) {
680 $commitaddrs = clean_addrlist(prompt_or_die("Commit notify email addr(s)", ""));
681 if ($commitaddrs ne "" && !valid_addrlist($commitaddrs)) {
682 unless ($force) {
683 warn"invalid commit notify email address list (use --force to accept): \"$commitaddrs\"\n";
684 redo;
686 warn "using invalid commit notify email address list with --force\n" unless $quiet;
689 $settings{notifymail} = $commitaddrs;
690 $settings{reverseorder} = 1;
691 $settings{reverseorder} = ynprompt_or_die("Oldest-to-newest commit order in emails", "Yes")
692 if !$defaults && $commitaddrs ne "";
693 $settings{summaryonly} = ynprompt_or_die("Summary only (no diff) in emails", "No")
694 if !$defaults && $commitaddrs ne "";
695 my $tagaddrs = "";
696 if (!$defaults) {
697 $tagaddrs = clean_addrlist(prompt_or_die("Tag notify email addr(s)", ""));
698 if ($tagaddrs ne "" && !valid_addrlist($tagaddrs)) {
699 unless ($force) {
700 warn"invalid tag notify email address list (use --force to accept): \"$tagaddrs\"\n";
701 redo;
703 warn "using invalid tag notify email address list with --force\n" unless $quiet;
706 $settings{notifytag} = $tagaddrs;
707 if (!$mirror && !$ispush) {
708 my @newusers = ();
710 my $userlist = prompt_or_die("Push users", join(",", @newusers));
711 eval {@newusers = validate_users($userlist, $force); 1;} or redo;
713 $settings{users} = \@newusers;
715 my $newproj = Girocco::Project->ghost($projname, $mirror, $orphanok, $optp)
716 or die "Girocco::Project->ghost call failed\n";
717 my ($k, $v);
718 $newproj->{$k} = $v while ($k, $v) = each(%settings);
719 my $killowner = sub {
720 system($Girocco::Config::git_bin, '--git-dir='.$newproj->{path},
721 'config', '--unset', "gitweb.owner");
723 if ($mirror) {
724 $newproj->premirror or die "Girocco::Project->premirror failed\n";
725 !$noowner or &$killowner;
726 $newproj->clone or die "Girocco::Project->clone failed\n";
727 warn "Project $projname created and cloning successfully initiated.\n"
728 unless $quiet;
729 } else {
730 $newproj->conjure or die "Girocco::Project->conjure failed\n";
731 !$noowner or &$killowner;
732 warn "New push project fork is empty due to use of --no-alternates\n"
733 if !$quiet && $projname =~ m,/, && $noalternates;
734 warn "Project $projname successfully created.\n" unless $quiet;
736 return 0;
739 sub git_config {
740 my $gd = shift;
741 system($Girocco::Config::git_bin, "--git-dir=$gd", 'config', @_) == 0
742 or die "\"git --git-dir='$gd' config ".join(" ", @_)."\" failed.\n";
745 sub cmd_adopt {
746 my ($force, $type, $nousers, $dryrun, $noowner, $owner, $users, $verbose);
747 parse_options(force => \$force, ":type" => \$type, "no-users" => \$nousers, "dry-run" => \$dryrun,
748 "no-owner" => \$noowner,":owner" => \$owner, quiet => \$quiet, q =>\$quiet, verbose => \$verbose);
749 @ARGV or die "Please give project name on command line.\n";
750 my $projname = shift @ARGV;
751 (!$noowner || !defined($owner)) && (!$nousers || !@ARGV) or die_usage;
752 !defined($type) || $type eq "mirror" || $type eq "push" or die_usage;
753 defined($type) or $type = "";
754 my $projdir;
755 if ($dryrun && $projname =~ m,^/[^.\s/\\:], && is_git_dir(realpath($projname))) {
756 $projdir = realpath($projname);
757 $projname = $projdir;
758 $projname =~ s/\.git$//i;
759 $projname =~ s,/+$,,;
760 $projname =~ s,^.*/,,;
761 $projname ne "" or $projname = $projdir;
762 } else {
763 $projname =~ s/\.git$//i;
764 $projname ne "" or die "Invalid project name \"\".\n";
765 unless (Girocco::Project::does_exist($projname, 1)) {
766 Girocco::Project::valid_name($projname, 1, 1)
767 or die "Invalid project name \"$projname\".\n";
768 die "No such project to adopt: $projname\n";
770 defined(Girocco::Project->load($projname))
771 and die "Project already known (no need to adopt): $projname\n";
772 $projdir = $Girocco::Config::reporoot . "/" . $projname . ".git";
773 is_git_dir($projdir) or die "Not a git directory: \"$projdir\"\n";
775 my $config = read_config_file($projdir . "/config");
776 my %config = ();
777 %config = map {($$_[0], defined($$_[1])?$$_[1]:"true")} @$config if defined($config);
778 git_bool($config{"core.bare"}) or die "Not a bare git repository: \"$projdir\"\n";
779 defined(read_HEAD_symref($projdir)) or die "Project with non-symbolic HEAD ref: \"$projdir\"\n";
780 @ARGV and $users = [validate_users(join(" ", @ARGV), $force, 1)];
781 my $desc = "";
782 if (-e "$projdir/description") {
783 open my $fd, '<', "$projdir/description" or die "Cannot open \"$projdir/description\": $!\n";
785 local $/;
786 $desc = <$fd>;
788 close $fd;
789 defined $desc or $desc = "";
790 chomp $desc;
791 $desc = to_utf8($desc, 1);
792 is_default_desc($desc) and $desc = "";
793 if ($desc ne "" && !valid_desc($desc)) {
794 die "invalid 'description' file contents (use --force to accept): \"$desc\"\n"
795 unless $force;
796 warn "using invalid 'description' file contents with --force\n" unless $quiet;
798 $desc = clean_desc($desc);
799 if (length($desc) > 1024) {
800 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
801 unless $force;
802 warn "using longer than 1024 char description with --force\n" unless $quiet;
805 my $readme = "";
806 my $origreadme = "";
807 my $readmedata = "";
808 my $origreadmedata = "";
809 my $readmetype = Girocco::Project::_normalize_rmtype($config{"girocco.readmetype"},1);
810 if (-e "$projdir/README.html") {
811 open my $fd, '<', "$projdir/README.html" or die "Cannot open \"$projdir/README.html\": $!\n";
813 local $/;
814 $readme = <$fd>;
816 close $fd;
817 defined $readme or $readme = "";
818 $readme = to_utf8($readme, 1);
819 $readme =~ s/\r\n?/\n/gs;
820 $readme =~ s/^\s+//s;
821 $readme =~ s/\s+$//s;
822 $readme eq "" or $readme .= "\n";
823 $origreadme = $readme;
824 if (-e "$projdir/README.dat") {
825 open my $fd2, '<', "$projdir/README.dat" or die "Cannot open \"$projdir/README.dat\": $!\n";
827 local $/;
828 $readmedata = <$fd2>;
830 close $fd2;
831 defined $readmedata or $readmedata = "";
832 $readmedata = to_utf8($readmedata, 1);
833 $readmedata =~ s/\r\n?/\n/gs;
834 $readmedata =~ s/^\s+//s;
835 $readmedata =~ s/\s+$//s;
836 $readmedata eq "" or $readmedata .= "\n";
837 $origreadmedata = $readmedata;
839 !$readmetype && length($readme) && !length($readmedata) and do {
840 # the old HTML format
841 $readmetype = 'HTML';
842 $readmedata = $readme;
844 if (length($readmedata) > 8192) {
845 die "readme greater than 8192 chars is too long (use --force to override)\n"
846 unless $force;
847 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
850 my $dummy = {READMEDATA => $readmedata, rmtype => $readmetype, name => $projname};
851 my ($cnt, $err) = Girocco::Project::_lint_readme($dummy, 0);
852 if ($cnt) {
853 my $msg = "README: $cnt error";
854 $msg .= "s" unless $cnt == 1;
855 print STDERR "$msg\n", "-" x length($msg), "\n", $err
856 unless $force && $quiet;
857 exit(255) unless $force && $readmetype eq 'HTML';
858 warn "$projname: using invalid raw HTML with --force\n" unless $quiet;
859 } else {
860 $readme = $dummy->{README};
864 $readmetype or $readmetype = Girocco::Project::_normalize_rmtype(""); # use default type
865 # Inspect any remotes now
866 # Yes, Virginia, remote urls can be multi-valued
867 my %remotes = ();
868 foreach (@$config) {
869 my ($k,$v) = @$_;
870 next unless $k =~ /^remote\.([^\/].*?)\.([^.]+)$/; # remote name cannot start with "/"
871 my ($name, $subkey) = ($1, $2);
872 $remotes{$name}->{skip} = git_bool($v,1), next if $subkey eq "skipdefaultupdate" || $subkey eq "skipfetchall";
873 $remotes{$name}->{mirror} = git_bool($v,1), next if $subkey eq "mirror"; # we might want this
874 $remotes{$name}->{vcs} = $v, next if defined($v) && $v !~ /^\s*$/ && $subkey eq "vcs";
875 push(@{$remotes{$name}->{$subkey}}, $v), next if defined($v) && $v !~ /^\s*$/ &&
876 ($subkey eq "url" || $subkey eq "fetch" || $subkey eq "push" || $subkey eq "pushurl");
878 # remotes.default is the default remote group to fetch for "git remote update" otherwise --all
879 # the remote names in a group are separated by runs of [ \t\n] characters
880 # remote names "", ".", ".." and any name starting with "/" are invalid
881 # a remote with no url or vcs setting is not considered valid
882 my @check = ();
883 my $usingall = 0;
884 if (exists($config{"remotes.default"})) {
885 foreach (split(/[ \t\n]+/, $config{"remotes.default"})) {
886 next unless exists($remotes{$_});
887 my $rmt = $remotes{$_};
888 next if !exists($rmt->{url}) && !$rmt->{vcs};
889 push(@check, $_);
891 } else {
892 $usingall = 1;
893 my %seenrmt = ();
894 foreach (@$config) {
895 my ($k,$v) = @$_;
896 next unless $k =~ /^remote\.([^\/].*?)\.[^.]+$/;
897 next if $seenrmt{$1};
898 $seenrmt{$1} = 1;
899 next unless exists($remotes{$1});
900 my $rmt = $remotes{$1};
901 next if $rmt->{skip} || (!exists($rmt->{url}) && !$rmt->{vcs});
902 push(@check, $1);
905 my @needskip = (); # remotes that need skipDefaultUpdate set to true
906 my $foundvcs = 0;
907 my $foundfetch = 0;
908 my $foundfetchwithmirror = 0;
909 foreach (@check) {
910 my $rmt = $remotes{$_};
911 push(@needskip, $_) if $usingall && !exists($rmt->{fetch});
912 next unless exists($rmt->{fetch});
913 ++$foundfetch;
914 ++$foundfetchwithmirror if $rmt->{mirror};
915 ++$foundvcs if $rmt->{vcs} || (exists($rmt->{url}) && $rmt->{url}->[0] =~ /^[a-zA-Z0-9][a-zA-Z0-9+.-]*::/);
917 # if we have $foundvcs then we need to explicitly set fetch.prune to false
918 # if we have $foundfetch > 1 then we need to explicitly set fetch.prune to false
919 my $neednoprune = !exists($config{"fetch.prune"}) && ($foundvcs || $foundfetch > 1);
920 my $baseurl = "";
921 my $needfakeorigin = 0; # if true we need to set remote.origin.skipDefaultUpdate = true
922 # if remote "origin" exists we always pick up its first url or use ""
923 if (exists($remotes{origin})) {
924 my $rmt = $remotes{origin};
925 $baseurl = exists($rmt->{url}) ? $rmt->{url}->[0] : "";
926 $needfakeorigin = !exists($rmt->{url}) && !$rmt->{vcs} && !$rmt->{skip};
927 } else {
928 $needfakeorigin = 1;
929 # get the first url of the @check remotes
930 foreach (@check) {
931 my $rmt = $remotes{$_};
932 next unless exists($rmt->{url});
933 next unless defined($rmt->{url}->[0]) && $rmt->{url}->[0] ne "";
934 $baseurl = $rmt->{url}->[0];
935 last;
938 my $makemirror = $type eq "mirror" || ($type eq "" && $foundfetch);
940 # If we have $foundfetch we want to make a mirror but complain if
941 # we $foundfetchwithmirror as well unless we have --type=mirror.
942 # Warn if we have --type=push and $foundfetch and !$foundfetchwithmirror.
943 # Warn if we need to set fetch.prune=false when making a mirror
944 # Warn if we need to create remote.origin.skipDefaultUpdate when making a mirror
945 # Complain if @needskip AND !$usingall (warn with --force but don't set skip)
946 # Warn if $usingall and any @needskip (and set them) if making a mirror
947 # Warn if making a mirror and $baseurl eq ""
948 # Warn if we have --type=mirror and !$foundfetch
950 if ($makemirror) {
951 warn "No base URL to mirror from for adopted \"$projname\"\n" unless $quiet || $baseurl ne "";
952 warn "Adopting mirror \"$projname\" without any fetch remotes\n" unless $quiet || $foundfetch;
953 if ($foundfetchwithmirror) {
954 warn "Refusing to adopt mirror \"$projname\" with active remote.<name>.mirror=true remote(s)\n".
955 "(Use --type=mirror to override)\n"
956 unless $type eq "mirror";
957 exit(255) unless $type eq "mirror" || $dryrun;
958 warn "Adopting mirror \"$projname\" with active remote.<name>.mirror=true remotes\n"
959 unless $quiet || $type ne "mirror";
961 warn "Setting explicit fetch.prune=false for adoption of mirror \"$projname\"\n"
962 if !$quiet && $neednoprune;
963 warn "Setting remote.origin.skipDefaultUpdate=true for adoption of mirror \"$projname\"\n"
964 if !$quiet && $needfakeorigin;
965 if (!$usingall && @needskip) {
966 warn "Refusing to adopt mirror empty fetch remote(s) (override with --force)\n"
967 unless $force;
968 exit(255) unless $force || $dryrun;
969 warn "Adopting mirror with empty fetch remote(s) with --force\n"
970 unless $quiet || !$force;
972 warn "Will set skipDefaultUpdate=true on non-fetch remote(s)\n" if !$quiet && $usingall && @needskip;
973 warn "Adopting mirror with base URL \"$baseurl\"\n" unless $quiet || $baseurl eq "";
974 } else {
975 warn "Adopting push \"$projname\" but active non-mirror remotes are present\n"
976 if !$quiet && $foundfetch && !$foundfetchwithmirror;
979 if (!$noowner && !defined($owner)) {
980 # Select the owner
981 $owner = $config{"gitweb.owner"};
982 if (!defined($owner) || $owner eq "") {
983 $owner = $Girocco::Config::admin;
984 warn "Using owner \"$owner\" for adopted project\n" unless $quiet;
987 if (!$nousers && !$makemirror && !defined($users)) {
988 # select user list for push project
989 my $findowner = $owner;
990 defined($findowner) or $findowner = $config{"gitweb.owner"};
991 $findowner = lc($findowner) if defined($findowner);
992 my @owner_users = ();
993 @owner_users = map {$findowner eq lc($$_[4]) ? $$_[1] : ()} get_all_users
994 if defined($findowner) && $findowner ne "";
995 defined($findowner) or $findowner = "";
996 if (@owner_users <= 1) {
997 $users = \@owner_users;
998 warn "No users found that match owner \"$findowner\"\n" unless @owner_users || $quiet;
999 } else {
1000 $users = [];
1001 warn "Found ".scalar(@owner_users)." users for owner \"$findowner\" (" .
1002 join(" ", @owner_users) . ") not setting any\n" unless $quiet;
1005 defined($users) or $users = [];
1007 # Warn if we preserve an existing receive.denyNonFastForwards or receive.denyDeleteCurrent setting
1008 # Complain if core.logallrefupdates or logs subdir exists and contains any files (allow with --force
1009 # and warn about preserving the setting)
1011 warn "Preserving existing receive.denyNonFastForwards=true\n"
1012 if !$quiet && git_bool($config{"receive.denynonfastforwards"});
1013 warn "Preserving existing receive.denyDeleteCurrent=$config{'receive.denydeletecurrent'}\n"
1014 if !$quiet && exists($config{"receive.denydeletecurrent"}) &&
1015 $config{"receive.denydeletecurrent"} ne "warn";
1017 my $reflogfiles = Girocco::Project::_contains_files("$projdir/logs");
1018 my $reflogactive = git_bool($config{"core.logallrefupdates"});
1019 if ($reflogactive || $reflogfiles) {
1020 warn "Refusing to adopt \"$projname\" with active ref logs without --force\n" if $reflogfiles && !$force;
1021 warn "Refusing to adopt \"$projname\" with core.logAllRefUpdates=true without --force\n" if $reflogactive && !$force;
1022 exit(255) unless $force || $dryrun;
1023 warn "Adopting \"$projname\" with active ref logs with --force\n" unless $quiet || ($reflogfiles && !$force);
1024 warn "Adopting \"$projname\" with core.logAllRefUpdates=true with --force\n" unless $quiet || ($reflogactive && !$force);
1027 return 0 if $dryrun && !$verbose;
1029 my $newproj = eval {Girocco::Project->ghost($projname, $makemirror, 1, $dryrun)};
1030 defined($newproj) or die "Girocco::Project::ghost failed: $@\n";
1031 $newproj->{desc} = $desc;
1032 $newproj->{README} = $readme;
1033 $newproj->{READMEDATA} = $readmedata;
1034 $newproj->{rmtype} = $readmetype;
1035 $newproj->{url} = $baseurl if $makemirror || exists($config{"gitweb.baseurl"});
1036 $newproj->{email} = $owner if defined($owner);
1037 $newproj->{users} = $users;
1038 $newproj->{crypt} = "unknown";
1039 $newproj->{reverseorder} = 1 unless exists($config{"hooks.reverseorder"});
1040 $newproj->{summaryonly} = 1 unless exists($config{"hooks.summaryonly"});
1041 my $dummy = bless {}, "Girocco::Project";
1042 $dummy->{path} = "$projdir";
1043 $dummy->{configfilehash} = \%config;
1044 $dummy->_properties_load;
1045 delete $dummy->{origurl};
1046 foreach my $k (keys(%$dummy)) {
1047 $newproj->{$k} = $dummy->{$k}
1048 if exists($dummy->{$k}) && !exists($newproj->{$k});
1051 if ($verbose) {
1052 use Data::Dumper;
1053 my %info = %$newproj;
1054 $info{README} = get_readme_desc($info{README}) if exists($info{README});
1055 my $d = Data::Dumper->new([\%info], ['*'.$newproj->{name}]);
1056 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
1057 print $d->Dump([\%info], ['*'.$newproj->{name}]);
1059 return 0 if $dryrun;
1061 # Make any changes as needed for @needskip, $neednoprune and $needfakeorigin
1062 if ($makemirror) {
1063 git_config($projdir, "fetch.prune", "false") if $neednoprune;
1064 git_config($projdir, "remote.origin.skipDefaultUpdate", "true") if $needfakeorigin;
1065 if ($usingall && @needskip) {
1066 git_config($projdir, "remote.$_.skipDefaultUpdate", "true") foreach @needskip;
1070 # Write out any README.dat/README.html changes before the actual Adoption
1071 # Otherwise they will get stepped on. The Girocco::Project::adopt function
1072 # does not know how to validate README.html during adoption like the above code does.
1073 if ($readmedata ne $origreadmedata) {
1074 open my $fd, '>', "$projdir/README.dat" or die "Cannot write \"$projdir/README.dat\": $!\n";
1075 print $fd $readmedata or die "Error writing \"$projdir/README.dat\": $!\n";
1076 close $fd or die "Error closing \"$projdir/README.dat\": $!\n";
1078 if ($readme ne $origreadme || ! -e "$projdir/README.html") {
1079 open my $fd, '>', "$projdir/README.html" or die "Cannot write \"$projdir/README.html\": $!\n";
1080 print $fd $readme or die "Error writing \"$projdir/README.html\": $!\n";
1081 close $fd or die "Error closing \"$projdir/README.html\": $!\n";
1083 git_config($projdir, "girocco.rmtype", $readmetype);
1085 # Perform the actual adoption
1086 $newproj->adopt or die "Girocco::Project::adopt failed\n";
1088 # Perhaps restore core.logAllRefUpdates, receive.denyNonFastForwards and receive.denyDeleteCurrent
1089 git_config($projdir, "receive.denyNonFastForwards", "true")
1090 if git_bool($config{"receive.denynonfastforwards"});
1091 git_config($projdir, "receive.denyDeleteCurrent", $config{"receive.denydeletecurrent"})
1092 if exists($config{"receive.denydeletecurrent"}) &&
1093 $config{"receive.denydeletecurrent"} ne "warn";
1094 git_config($projdir, "core.logAllRefUpdates", "true")
1095 if $reflogactive;
1097 # Success
1098 if ($makemirror) {
1099 warn "Mirror project \"$projname\" successfully adopted.\n" unless $quiet;
1100 } else {
1101 warn "Push project \"$projname\" successfully adopted.\n" unless $quiet;
1103 return 0;
1106 sub cmd_remove {
1107 my ($force, $reallydel, $keepforks);
1108 parse_options(force => \$force, "really-delete" => \$reallydel,
1109 "keep-forks" => \$keepforks, quiet => \$quiet, q =>\$quiet);
1110 @ARGV or die "Please give project name on command line.\n";
1111 @ARGV == 1 or die_usage;
1112 my $project = get_project($ARGV[0]); # for safety only names accepted here
1113 my $projname = $project->{name};
1114 my $isempty = !$project->{mirror} && $project->is_empty;
1115 if (!$project->{mirror} && !$isempty && $reallydel) {
1116 die "refusing to remove and delete non-empty push project without --force: $projname\n" unless $force;
1117 warn "allowing removal and deletion of non-empty push project with --force\n" unless $quiet;
1119 my $altwarn;
1120 my $removenogc;
1121 if ($project->has_forks) {
1122 die "refusing to remove project with forks (use --keep-forks): $projname\n" unless $keepforks;
1123 warn "allowing removal of forked project while preserving its forks with --keep-forks\n" unless $quiet;
1124 # Run pseudo GC on that repository so that objects don't get lost within forks
1125 my $basedir = $Girocco::Config::basedir;
1126 my $projdir = $project->{path};
1127 warn "We have to run pseudo GC on the repo so that the forks don't lose data. Hang on...\n" unless $quiet;
1128 my $nogcrunning = sub {
1129 die "Error: GC appears to be currently running on $projname\n"
1130 if -e "$projdir/gc.pid" || -e "$projdir/.gc_in_progress";
1132 &$nogcrunning;
1133 $removenogc = ! -e "$projdir/.nogc";
1134 recreate_file("$projdir/.nogc") if $removenogc;
1135 die "unable to create \"$projdir/.nogc\"\n" unless -e "$projdir/.nogc";
1136 delete $ENV{show_progress};
1137 $ENV{'show_progress'} = 1 unless $quiet;
1138 sleep 2; # *cough*
1139 &$nogcrunning;
1140 system("$basedir/toolbox/perform-pre-gc-linking.sh", "--include-packs", $projname) == 0
1141 or die "Running pseudo GC on project $projname failed\n";
1142 $altwarn = 1;
1144 my $archived;
1145 if (!$project->{mirror} && !$isempty && !$reallydel) {
1146 $archived = $project->archive_and_delete;
1147 unlink("$archived/.nogc") if $removenogc && defined($archived) && $archived ne "";
1148 } else {
1149 $project->delete;
1151 warn "Project '$projname' removed from $Girocco::Config::name" .
1152 ($archived ? ", backup in '$archived'" : "") .".\n" unless $quiet;
1153 warn "Retained forks may now have unwanted objects/info/alternates lines\n" if $altwarn && !$quiet;
1154 return 0;
1157 sub cmd_prune {
1158 my ($force, $dryrun);
1159 parse_options(force => \$force, "dry-run" => \$dryrun, "quiet" => \$quiet);
1160 ($force && !$dryrun) || (!$force && $dryrun) or die_usage;
1161 my @projs = @ARGV;
1162 my %allprojs = map({($$_[0] => $_)} Girocco::Project::get_full_list_extended());
1163 my @allprojs = sort({lc($a) cmp lc($b) || $a cmp $b} keys(%allprojs));
1164 my %seen = ();
1165 @projs or @projs = @allprojs;
1166 my $bd = $Girocco::Config::reporoot.'/';
1167 my @remove = ();
1168 foreach (@projs) {
1169 !$seen{$_} && $allprojs{$_} && ${$allprojs{$_}}[2] >= 65536 or next;
1170 $seen{$_} = 1;
1171 /^[a-zA-Z0-9]/ or next;
1172 my $pd = $bd . $_ . '.git';
1173 if (! -e $pd) {
1174 warn "$_: no such directory: $pd\n" unless $quiet;
1175 push(@remove, $_);
1176 } elsif (! -d _) {
1177 warn "$_: exists but not directory: $pd\n" unless $quiet;
1178 push(@remove, $_);
1181 warn "\n" if @remove && !$quiet;
1182 if ($dryrun) {
1183 return 0 unless @remove;
1184 my $msg = "Would remove ".scalar(@remove). " project entr";
1185 $msg .= (@remove == 1 ? "y" : "ies");
1186 $msg .= ":\n";
1187 $msg .= join("", map("\t$_\n", @remove));
1188 print $msg unless $quiet;
1189 return 1;
1191 my $msg = "Removed ".scalar(@remove). " project entr";
1192 $msg .= (@remove == 1 ? "y" : "ies");
1193 $msg .= ":\n";
1194 $msg .= join("", map("\t$_\n", @remove));
1195 my %remove = map({$_ => 1} @remove);
1196 filedb_atomic_edit(jailed_file('/etc/group'), sub {
1197 my ($name,undef,$gid) = split /:/;
1198 $gid =~ /^\d+$/ && $gid >= 65536 or return $_;
1199 $name =~ /^[a-zA-Z0-9]/ or return $_;
1200 !exists($remove{$name}) and return $_;
1202 print $msg unless $quiet;
1203 return 0;
1206 sub cmd_show {
1207 use Data::Dumper;
1208 @ARGV == 1 or die_usage;
1209 my $project = get_clean_project($ARGV[0]);
1210 my %info = %$project;
1211 my $d = Data::Dumper->new([\%info], ['*'.$project->{name}]);
1212 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
1213 print $d->Dump([\%info], ['*'.$project->{name}]);
1214 return 0;
1217 sub cmd_urls {
1218 my $pushonly;
1219 parse_options("push" => \$pushonly);
1220 my @projs = @ARGV;
1221 @ARGV == 1 or die_usage;
1222 my $project = get_project_harder($ARGV[0]);
1223 my $suffix = "/".$project->{name}.".git";
1224 @Gitweb::Config::git_base_url_list = ();
1225 @Gitweb::Config::git_base_push_urls = ();
1226 @Gitweb::Config::git_base_mirror_urls = ();
1228 package Gitweb::Config;
1229 do $Girocco::Config::basedir."/gitweb/gitweb_config.perl";
1230 !$! or die "could not read gitweb_config.perl: $!\n";
1231 !$@ or die "could not parse gitweb_config.perl: $@\n";
1233 my @fetch_urls = ();
1234 my @push_urls = ();
1235 my $add_url = sub {
1236 my $array = shift;
1237 foreach (@_) {
1238 if (ref($_)) {
1239 ref($_) eq 'ARRAY' or die "expected ARRAY ref";
1240 my $u = $$_[0];
1241 defined($u) && $u ne "" and
1242 push(@$array, $u.$suffix);
1243 } elsif (defined($_) && $_ ne "") {
1244 push(@$array, $_.$suffix);
1248 my $uniq = sub {
1249 my %items = ();
1250 $items{$_} = 1 foreach @_;
1251 sort(keys(%items));
1253 &$add_url(\@fetch_urls, @Gitweb::Config::git_base_url_list);
1254 if ($project->{mirror}) {
1255 &$add_url(\@fetch_urls, @Gitweb::Config::git_base_mirror_urls);
1256 } else {
1257 &$add_url(\@push_urls, @Gitweb::Config::git_base_push_urls);
1259 my @urls = ();
1260 if ($pushonly) {
1261 push(@urls, &$uniq(@push_urls));
1262 } else {
1263 push(@urls, &$uniq(@fetch_urls, @push_urls));
1265 print map "$_\n", @urls;
1266 return 0;
1269 sub cmd_listheads {
1270 @ARGV == 1 or die_usage;
1271 my $project = get_project_harder($ARGV[0]);
1272 my @heads = sort({lc($a) cmp lc($b)} $project->get_heads);
1273 my $cur = $project->{HEAD};
1274 defined($cur) or $cur = '';
1275 my $curmark = '*';
1276 my $headhash = get_git("--git-dir=$project->{path}", 'rev-parse', '--quiet', '--verify', 'HEAD');
1277 defined($headhash) or $headhash = '';
1278 chomp $headhash;
1279 $headhash or $curmark = '!';
1280 foreach (@heads) {
1281 my $mark = $_ eq $cur ? $curmark : ' ';
1282 print "$mark $_\n";
1284 return 0;
1287 sub cmd_listtags {
1288 my $vcnt = 0;
1289 shift(@ARGV), $vcnt=1 if @ARGV && ($ARGV[0] eq '--verbose' || $ARGV[0] eq '-v');
1290 @ARGV == 1 or die_usage;
1291 my $project = get_project_harder($ARGV[0]);
1292 if ($vcnt) {
1293 print map("$$_[0]\t$$_[1]\n", get_ctag_counts($project));
1294 } else {
1295 print map("$_\n", $project->get_ctag_names);
1297 return 0;
1300 sub cmd_deltags {
1301 my $ic = 0;
1302 shift(@ARGV), $ic=1 if @ARGV && $ARGV[0] =~ /^(?:--?ignore-case|-i)$/i;
1303 @ARGV >= 2 or die_usage;
1304 my $project = get_project_harder(shift @ARGV);
1305 my %curtags;
1306 if ($ic) {
1307 push(@{$curtags{lc($_)}}, $_) foreach $project->get_ctag_names;
1308 } else {
1309 push(@{$curtags{$_}}, $_) foreach $project->get_ctag_names;
1311 my @deltags = ();
1312 my %seentag = ();
1313 my $ctags = join(" ", @ARGV);
1314 $ctags = lc($ctags) if $ic;
1315 foreach (split(/[\s,]+/, $ctags)) {
1316 next unless exists($curtags{$_});
1317 $seentag{$_}=1, push(@deltags, $_) unless $seentag{$_};
1319 if (!@deltags) {
1320 warn $project->{name}, ": skipping removal of only non-existent tags\n" unless $quiet;
1321 } else {
1322 # Avoid touching anything other than the ctags
1323 foreach my $tg (@deltags) {
1324 $project->delete_ctag($_) foreach @{$curtags{$tg}};
1326 $project->_set_changed;
1327 $project->_set_forkchange;
1328 warn $project->{name}, ": specified tags have been removed\n" unless $quiet;
1330 return 0;
1333 sub cmd_addtags {
1334 @ARGV >= 2 or die_usage;
1335 my $project = get_project_harder(shift @ARGV);
1336 my $ctags = join(" ", @ARGV);
1337 $ctags =~ /[^, a-zA-Z0-9:.+#_-]/ and
1338 die "Content tag(s) \"$ctags\" contain(s) evil character(s).\n";
1339 my $oldmask = umask();
1340 umask($oldmask & ~0060);
1341 my $changed = 0;
1342 foreach (split(/[\s,]+/, $ctags)) {
1343 ++$changed if $project->add_ctag($_, 1);
1345 if ($changed) {
1346 $project->_set_changed;
1347 $project->_set_forkchange;
1349 umask($oldmask);
1350 my $cnt = ($changed == 1) ? "1 content tag has" : $changed . " content tags have";
1351 warn $project->{name}, ": $cnt been added/updated\n" unless $quiet;
1352 return 0;
1355 sub _get_random_val {
1356 my $p = shift;
1357 my $md5;
1359 no warnings;
1360 $md5 = md5_hex(time . $$ . rand() . join(':',%$p));
1362 $md5;
1365 sub cmd_chpass {
1366 my $force = 0;
1367 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1368 my $random = undef;
1369 pop(@ARGV), $random=lc($ARGV[1]) if @ARGV==2 && $ARGV[1] =~ /^(?:random|unknown)$/i;
1370 @ARGV == 1 or die_usage;
1371 my $project = get_project_harder($ARGV[0]);
1372 die "refusing to change locked password of project \"$ARGV[0]\" without --force\n"
1373 if $project->is_password_locked;
1374 my ($newpw, $rmsg);
1375 if ($random) {
1376 if ($random eq "random") {
1377 die "refusing to set random password without --force\n" unless $force;
1378 $rmsg = "set to random value";
1379 $newpw = _get_random_val($project);
1380 } else {
1381 die "refusing to set password hash to '$random' without --force\n" unless $force;
1382 $rmsg = "hash set to '$random'";
1383 $newpw = $random;
1385 } else {
1386 $rmsg = "updated";
1387 if (-t STDIN) {
1388 print "Changing admin password for project $ARGV[0]\n";
1389 my $np1 = prompt_noecho_nl_or_die("New password for project $ARGV[0] (echo is off)");
1390 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1391 my $np2 = prompt_noecho_nl_or_die("Retype new password for project $ARGV[0]");
1392 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
1393 "the admin passwords you have entered do not match each other.\n";
1394 $newpw = $np1;
1395 } else {
1396 $newpw = <STDIN>;
1397 defined($newpw) or die "missing new password on STDIN\n";
1398 chomp($newpw);
1401 $newpw ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1402 my $old = $project->{crypt};
1403 $project->{crypt} = (defined($random) && $random ne "random") ? $newpw : scrypt_sha1($newpw);
1404 if (defined($old) && $old eq $project->{crypt}) {
1405 warn $project->{name}, ": skipping update of password hash to same value\n" unless $quiet;
1406 } else {
1407 # Avoid touching anything other than the password hash
1408 $project->_group_update;
1409 warn $project->{name}, ": admin password $rmsg (new hash stored)\n" unless $quiet;
1411 return 0;
1414 sub cmd_checkpw {
1415 @ARGV == 1 or die_usage;
1416 my $project = get_project_harder($ARGV[0]);
1417 my $pwhash = $project->{crypt};
1418 defined($pwhash) or $pwhash = "";
1419 if ($pwhash eq "") {
1420 warn $project->{name}, ": no password required\n" unless $quiet;
1421 return 0;
1423 if ($project->is_password_locked) {
1424 warn $project->{name}, ": password is locked\n" unless $quiet;
1425 exit 1;
1427 my $checkpw;
1428 if (-t STDIN) {
1429 $checkpw = prompt_noecho_nl_or_die("Admin password for project $ARGV[0] (echo is off)");
1430 $checkpw ne "" or warn "checking for empty password as hash (very unlikely)\n" unless $quiet;
1431 } else {
1432 $checkpw = <STDIN>;
1433 defined($checkpw) or die "missing admin password on STDIN\n";
1434 chomp($checkpw);
1436 unless (Girocco::CLIUtil::check_passwd_match($pwhash, $checkpw)) {
1437 warn "password check failure\n" unless $quiet;
1438 exit 1;
1440 warn "admin password match\n" unless $quiet;
1441 return 0;
1444 sub cmd_gc {
1445 my ($force, $auto, $redelta, $recompress, $aggressive);
1446 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, auto => \$auto,
1447 redelta => \$redelta, "no-reuse-delta" => \$redelta, aggressive => \$force,
1448 recompress => \$recompress, "no-reuse-object" => $recompress,
1449 aggressive => \$aggressive);
1450 $aggressive and $force = $redelta = 1;
1451 $force && $auto and die "--force and --auto are mutually exclusive options\n";
1452 @ARGV or die "Please give project name on command line.\n";
1453 @ARGV == 1 or die_usage;
1454 my $project = get_project_harder($ARGV[0]);
1455 delete $ENV{show_progress};
1456 delete $ENV{force_gc};
1457 $quiet or $ENV{"show_progress"} = 1;
1458 $force and $ENV{"force_gc"} = 1;
1459 if (!$auto && !$force && ! -e $project->{path}."/.needsgc") {
1460 open NEEDSGC, '>', $project->{path}."/.needsgc" and close NEEDSGC;
1462 my @args = ($Girocco::Config::basedir . "/jobd/gc.sh", $project->{name});
1463 $redelta && !$recompress and push(@args, "-f");
1464 $recompress and push(@args, "-F");
1465 my $lastgc = $project->{lastgc};
1466 system({$args[0]} @args) != 0 and return 1;
1467 # Do it again Sam, but only if lastgc was set, gc.sh succeeded and now it's not set
1468 if ($lastgc) {
1469 my $newlastgc = get_git("--git-dir=$project->{path}", 'config', '--get', 'gitweb.lastgc');
1470 if (!$newlastgc) {
1471 system({$args[0]} @args) != 0 and return 1;
1474 return 0;
1477 sub cmd_update {
1478 my ($force, $summary);
1479 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, summary => \$summary);
1480 $quiet && $summary and die "--quiet and --summary are mutually exclusive options\n";
1481 @ARGV or die "Please give project name on command line.\n";
1482 @ARGV == 1 or die_usage;
1483 my $project = get_project_harder($ARGV[0]);
1484 $project->{mirror} or die "Project \"$ARGV[0]\" is a push project, not a mirror project.\n";
1485 delete $ENV{show_progress};
1486 delete $ENV{force_update};
1487 if ($quiet) {
1488 $ENV{"show_progress"} = 0;
1489 } else {
1490 $ENV{"show_progress"} = ($summary ? 1 : 2);
1492 $force and $ENV{"force_update"} = 1;
1493 system($Girocco::Config::basedir . "/jobd/update.sh", $project->{name}) != 0 and return 1;
1494 return 0;
1497 sub cmd_remirror {
1498 my $force = 0;
1499 parse_options(force => \$force, quiet => \$quiet, q => \$quiet);
1500 @ARGV or die "Please give project name on command line.\n";
1501 @ARGV == 1 or die_usage;
1502 my $project = get_project_harder($ARGV[0]);
1503 $project->{mirror} or die "Project \"$ARGV[0]\" is a push project, not a mirror project.\n";
1504 if ($project->{clone_in_progress} && !$project->{clone_failed}) {
1505 warn "Project \"$ARGV[0]\" already seems to have a clone underway at this moment.\n" unless $quiet && $force;
1506 exit(255) unless $force;
1507 yes_to_continue_or_die("Are you sure you want to force a remirror");
1509 unlink($project->_clonefail_path);
1510 unlink($project->_clonelog_path);
1511 recreate_file($project->_clonep_path);
1512 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or
1513 die "cannot connect to taskd.socket: $!\n";
1514 select((select($sock),$|=1)[0]);
1515 $sock->print("clone ".$project->{name}."\n");
1516 # Just ignore reply, we are going to succeed anyway and the I/O
1517 # would apparently get quite hairy.
1518 $sock->flush();
1519 sleep 2; # *cough*
1520 $sock->close();
1521 warn "Project \"$ARGV[0]\" remirror initiated.\n" unless $quiet;
1522 return 0;
1525 sub cmd_setowner {
1526 my $force = 0;
1527 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1528 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1529 my $project = get_project_harder($ARGV[0]);
1530 if (@ARGV == 2 && !valid_email($ARGV[1])) {
1531 die "invalid owner/email (use --force to accept): \"$ARGV[1]\"\n"
1532 unless $force;
1533 warn "using invalid owner/email with --force\n" unless $quiet;
1535 if (@ARGV == 2 && length($ARGV[1]) > 96) {
1536 die "owner/email longer than 96 chars (use --force to accept): \"$ARGV[1]\"\n"
1537 unless $force;
1538 warn "using longer than 96 char owner/email with --force\n" unless $quiet;
1540 my $old = $project->{email};
1541 if (@ARGV == 1) {
1542 print "$old\n" if defined($old);
1543 return 0;
1545 if (defined($old) && $old eq $ARGV[1]) {
1546 warn $project->{name}, ": skipping update of owner/email to same value\n" unless $quiet;
1547 } else {
1548 # Avoid touching anything other than "gitweb.owner"
1549 $project->_property_fput("email", $ARGV[1]);
1550 $project->_update_index;
1551 $project->_set_changed;
1552 warn $project->{name}, ": owner/email updated to \"$ARGV[1]\"\n" unless $quiet;
1554 return 0;
1557 sub cmd_setdesc {
1558 my $force = 0;
1559 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1560 @ARGV >= 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1561 my $project = get_project_harder(shift @ARGV);
1562 if (@ARGV && !valid_desc(join(" ", @ARGV))) {
1563 die "invalid description (use --force to accept): \"".join(" ", @ARGV)."\"\n"
1564 unless $force;
1565 warn "using invalid description with --force\n" unless $quiet;
1567 my $desc = clean_desc(join(" ", @ARGV));
1568 if (@ARGV && length($desc) > 1024) {
1569 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
1570 unless $force;
1571 warn "using longer than 1024 char description with --force\n" unless $quiet;
1573 my $old = $project->{desc};
1574 if (!@ARGV) {
1575 print "$old\n" if defined($old);
1576 return 0;
1578 if (defined($old) && $old eq $desc) {
1579 warn $project->{name}, ": skipping update of description to same value\n" unless $quiet;
1580 } else {
1581 # Avoid touching anything other than description file
1582 $project->_property_fput("desc", $desc);
1583 $project->_set_changed;
1584 warn $project->{name}, ": description updated to \"$desc\"\n" unless $quiet;
1586 return 0;
1589 sub cmd_setreadme {
1590 my ($force, $readmetype) = (0, undef);
1591 parse_options(force => \$force, ":type" => \$readmetype, ":format" => \$readmetype);
1592 @ARGV == 1 && defined($readmetype) and push(@ARGV, undef);
1593 @ARGV == 2 || (@ARGV == 1 && !$force && !defined($readmetype) && !$setopt) or die_usage;
1594 defined($readmetype) and $readmetype = Girocco::Project::_normalize_rmtype($readmetype,1);
1595 defined($readmetype) && !$readmetype and die_usage;
1596 my $project = get_project_harder($ARGV[0]);
1597 my $old = $project->{READMEDATA};
1598 if (@ARGV == 1) {
1599 chomp $old if defined($old);
1600 print "$old\n" if defined($old) && $old ne "";
1601 return 0;
1603 $readmetype or $readmetype = $project->{rmtype};
1604 my ($new, $raw, $newname);
1605 $newname = '';
1606 if (!defined($ARGV[1])) {
1607 $new = $old;
1608 $newname = "original README data";
1609 $readmetype ne $project->{rmtype} && $new ne "" and $raw = 1;
1610 } elsif ($ARGV[1] eq "-") {
1611 local $/;
1612 $new = <STDIN>;
1613 $raw = 1;
1614 $newname = "contents of <STDIN>";
1615 } elsif (lc($ARGV[1]) eq "automatic" || lc($ARGV[1]) eq "auto") {
1616 $new = "";
1617 } elsif (lc($ARGV[1]) eq "suppressed" || lc($ARGV[1]) eq "suppress") {
1618 $new = "<!-- suppress -->";
1619 } else {
1620 my $fn = $ARGV[1];
1621 $fn =~ s/^\@//;
1622 die "missing filename for README\n" unless $fn ne "";
1623 die "no such file: \"$fn\"\n" unless -f $fn && -r $fn;
1624 open F, '<', $fn or die "cannot open \"$fn\" for reading: $!\n";
1625 local $/;
1626 $new = <F>;
1627 close F;
1628 $raw = 1;
1629 $newname = "contents of \"$fn\"";
1631 defined($new) or $new = '';
1632 my $origrmtype = $project->{rmtype};
1633 $project->{rmtype} = $readmetype;
1634 $project->{READMEDATA} = to_utf8($new, 1);
1635 $project->_cleanup_readme;
1636 if (length($project->{READMEDATA}) > 8192) {
1637 die "readme greater than 8192 chars is too long (use --force to override)\n"
1638 unless $force;
1639 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
1641 if ($raw) {
1642 my ($cnt, $err) = $project->_lint_readme(0);
1643 if ($cnt) {
1644 my $msg = "README: $cnt error";
1645 $msg .= "s" unless $cnt == 1;
1646 print STDERR "$msg\n", "-" x length($msg), "\n", $err
1647 unless $force && $quiet;
1648 exit(255) unless $force && $project->{rmtype} eq 'HTML';
1649 warn $project->{name} . ": using invalid raw HTML with --force\n" unless $quiet;
1650 $project->{README} = $project->{READMEDATA};
1653 if (defined($old) && $old eq $project->{READMEDATA} && $readmetype eq $origrmtype && !$force) {
1654 warn $project->{name}, ": skipping update of README to same value\n" unless $quiet;
1655 } else {
1656 # Avoid touching anything other than README.html file
1657 $project->_property_fput("READMEDATA", $project->{READMEDATA}, 1);
1658 $project->_property_fput("README", $project->{README});
1659 $project->_property_fput("rmtype", $readmetype) if $readmetype ne $origrmtype;
1660 $project->_set_changed;
1661 my $desc = get_readme_desc($project->{README});
1662 if ($newname) {
1663 $newname .= " ($desc)";
1664 } else {
1665 $newname = $desc;
1667 warn $project->{name}, ": README $readmetype format updated to $newname\n" unless $quiet;
1669 return 0;
1672 sub valid_head {
1673 my ($proj, $newhead) = @_;
1674 my %okheads = map({($_ => 1)} $proj->get_heads);
1675 exists($okheads{$newhead});
1678 sub cmd_sethead {
1679 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1680 my $project = get_project_harder($ARGV[0]);
1681 if (@ARGV == 2 && !valid_head($project, $ARGV[1])) {
1682 die "invalid head (try \"@{[basename($0)]} listheads $ARGV[0]\"): \"$ARGV[1]\"\n";
1684 my $old = $project->{HEAD};
1685 if (@ARGV == 1) {
1686 print "$old\n" if defined($old);
1687 return 0;
1689 if (defined($old) && $old eq $ARGV[1]) {
1690 warn $project->{name}, ": skipping update of HEAD symref to same value\n" unless $quiet;
1691 } else {
1692 # Avoid touching anything other than the HEAD symref
1693 $project->set_HEAD($ARGV[1]);
1694 $project->_set_changed;
1695 warn $project->{name}, ": HEAD symref updated to \"refs/heads/$ARGV[1]\"\n" unless $quiet;
1697 return 0;
1700 sub cmd_sethooks {
1701 my $force = 0;
1702 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1703 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1704 my $project = get_project_harder($ARGV[0]);
1705 my $projconfig = read_config_file_hash($project->{path}."/config");
1706 my $ghp = $Girocco::Config::reporoot."/_global/hooks";
1707 my $rghp = realpath($ghp);
1708 my $lhp = $project->{path}."/hooks";
1709 my $rlhp = realpath($lhp);
1710 my $ahp = "";
1711 my $rahp = undef;
1712 if (defined($projconfig) && defined($projconfig->{"core.hookspath"})) {
1713 $ahp = $projconfig->{"core.hookspath"};
1714 $rahp = realpath($ahp);
1716 if (@ARGV == 1) {
1717 if (defined($rahp) && $rahp ne "") {
1718 if ($rahp eq $rghp) {
1719 my $nc = ($ahp eq $ghp ? "" : " non-canonical");
1720 printf "%s \t(global%s)\n", $ahp, $nc;
1721 } elsif ($rahp eq $rlhp) {
1722 my $nc = ($ahp eq $lhp ? "" : " non-canonical");
1723 printf "%s \t(local%s)\n", $ahp, $nc;
1724 } elsif ($rahp ne $ahp) {
1725 print "$ahp \t($rahp)\n";
1726 } else {
1727 print "$ahp\n";
1729 } elsif ($ahp ne "") {
1730 print "$ahp \t(non-existent)\n";
1732 return 0;
1734 my $shp = $ARGV[1];
1735 if (lc($shp) eq "global") {
1736 $shp = $ghp;
1737 } elsif (lc($shp) eq "local") {
1738 $shp = $lhp;
1739 } elsif (substr($shp, 0, 2) eq "~/") {
1740 $shp = $ENV{"HOME"}.substr($shp,1);
1741 } elsif ($shp =~ m,^~([a-zA-Z_][a-zA-Z_0-9]*)((?:/.*)?)$,) {
1742 my $sfx = $2;
1743 my $hd = (getpwnam($1))[7];
1744 $shp = $hd . $sfx if defined($hd) && $hd ne "" && $hd ne "/" && -d $hd;
1746 $shp ne "" && -d $shp or die "no such directory: $ARGV[1]\n";
1747 my $rshp = realpath($shp);
1748 defined($rshp) && $rshp ne "" or die "could not realpath: $ARGV[1]\n";
1749 $rshp =~ m,^/[^/], or die "invalid hookspath: $rshp\n";
1750 die "refusing to switch from current non-global hookspath without --force\n"
1751 if !$force && defined($rahp) && $rahp ne "" && $rahp ne $rghp && $rshp ne $rahp;
1752 if (!$force && defined($rahp) && $rahp ne "") {
1753 if ($rshp eq $rahp && ($ahp eq $ghp || $ahp eq $lhp)) {
1754 warn $project->{name}, ": skipping update of hookspath to same effective value\n" unless $quiet;
1755 return 0;
1758 $rshp = $ghp if $rshp eq $rghp;
1759 $rshp = $lhp if $rshp eq $rlhp;
1760 if ($rshp eq $ahp) {
1761 warn $project->{name}, ": skipping update of hookspath to same value\n" unless $quiet;
1762 return 0;
1764 die "refusing to set neither local nor global hookspath without --force\n"
1765 if !$force && $rshp ne $ghp && $rshp ne $lhp;
1766 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1767 'config', "core.hookspath", $rshp);
1768 my $newval = '"'.$rshp.'"';
1769 $newval = "global" if $rshp eq $ghp;
1770 $newval = "local" if $rshp eq $lhp;
1771 warn $project->{name}, ": hookspath set to $newval\n" unless $quiet;
1772 return 0;
1775 our %boolfields;
1776 BEGIN {
1777 %boolfields = (
1778 cleanmirror => 1,
1779 reverseorder => 0,
1780 summaryonly => 0,
1781 statusupdates => 1,
1785 sub cmd_setbool {
1786 my $force = 0;
1787 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1788 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1789 my $project = get_project_harder($ARGV[0]);
1790 if (!exists($boolfields{$ARGV[1]})) {
1791 die "invalid boolean field name: \"$ARGV[1]\" -- try \"help\"\n";
1793 if (@ARGV == 3 && $boolfields{$ARGV[1]} && !$project->{mirror}) {
1794 die "invalid boolean field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
1795 unless $force;
1796 warn "using mirror field on non-mirror with --force\n" unless $quiet;
1798 if (@ARGV == 3 && !valid_bool($ARGV[2])) {
1799 die "invalid boolean value: \"$ARGV[2]\"\n";
1801 my $bool = clean_bool($ARGV[2]);
1802 my $old = $project->{$ARGV[1]};
1803 if (@ARGV == 2) {
1804 print "$old\n" if defined($old);
1805 return 0;
1807 if (defined($old) && $old eq $bool) {
1808 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
1809 } else {
1810 # Avoid touching anything other than $ARGV[1] field
1811 $project->_property_fput($ARGV[1], $bool);
1812 warn $project->{name}, ": $ARGV[1] updated to $bool\n" unless $quiet;
1814 return 0;
1817 sub cmd_setjsontype {
1818 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1819 my $project = get_project_harder($ARGV[0]);
1820 my $jsontype;
1821 if (@ARGV == 2) {
1822 my $jt = lc($ARGV[1]);
1823 index($jt, "/") >= 0 or $jt = "application/".$jt;
1824 $jt eq 'application/x-www-form-urlencoded' ||
1825 $jt eq 'application/json' or
1826 die "invalid jsontype value: \"$ARGV[1]\"\n";
1827 $jsontype = $jt;
1829 my $old = $project->{jsontype};
1830 if (@ARGV == 1) {
1831 print "$old\n" if defined($old);
1832 return 0;
1834 if (defined($old) && $old eq $jsontype) {
1835 warn $project->{name}, ": skipping update of jsontype to same value\n" unless $quiet;
1836 } else {
1837 # Avoid touching anything other than jsontype field
1838 $project->_property_fput('jsontype', $jsontype);
1839 warn $project->{name}, ": jsontype updated to $jsontype\n" unless $quiet;
1841 return 0;
1844 sub cmd_setjsonsecret {
1845 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1846 my $project = get_project_harder($ARGV[0]);
1847 my $jsonsecret;
1848 if (@ARGV == 2) {
1849 my $js = $ARGV[1];
1850 $js =~ s/^\s+//; $js =~ s/\s+$//;
1851 $jsonsecret = $js;
1853 my $old = $project->{jsonsecret};
1854 if (@ARGV == 1) {
1855 print "$old\n" if defined($old);
1856 return 0;
1858 if (defined($old) && $old eq $jsonsecret) {
1859 warn $project->{name}, ": skipping update of jsonsecret to same value\n" unless $quiet;
1860 } else {
1861 # Avoid touching anything other than jsonsecret field
1862 $project->_property_fput('jsonsecret', $jsonsecret);
1863 warn $project->{name}, ": jsonsecret updated to \"$jsonsecret\"\n" unless $quiet;
1865 return 0;
1868 sub cmd_setautogchack {
1869 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1870 my $project = get_project_harder($ARGV[0]);
1871 my $aghok = $Girocco::Config::autogchack &&
1872 ($project->{mirror} || $Girocco::Config::autogchack ne "mirror");
1873 my $old = defined($project->{autogchack}) ? clean_bool($project->{autogchack}) : "unset";
1874 if (@ARGV == 1) {
1875 print "$old\n" if $aghok;
1876 return 0;
1878 my $bool;
1879 if (lc($ARGV[1]) eq "unset") {
1880 $bool = "unset";
1881 } else {
1882 valid_bool($ARGV[1]) or die "invalid boolean value: \"$ARGV[1]\"\n";
1883 $bool = clean_bool($ARGV[1]);
1885 if (!$aghok) {
1886 die "\$Girocco::Config::autogchack is false\n" unless $Girocco::Config::autogchack;
1887 die "\$Girocco::Config::autogchack is only enabled for mirrors\n";
1889 if ($old eq $bool) {
1890 warn $project->{name}, ": autogchack value unchanged\n" unless $quiet;
1891 } else {
1892 if ($bool eq "unset") {
1893 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1894 'config', '--unset', "girocco.autogchack");
1895 } else {
1896 system($Girocco::Config::git_bin, '--git-dir='.$project->{path},
1897 'config', '--bool', "girocco.autogchack", $bool);
1900 return system($Girocco::Config::basedir . "/jobd/maintain-auto-gc-hack.sh", $project->{name}) == 0
1901 ? 0 : 1;
1904 sub valid_url {
1905 my ($url, $type) = @_;
1906 $type ne 'baseurl' and return valid_web_url($url);
1907 valid_repo_url($url) or return 0;
1908 if ($Girocco::Config::restrict_mirror_hosts) {
1909 my $mh = extract_url_hostname($url);
1910 is_dns_hostname($mh) or return 0;
1911 !is_our_hostname($mh) or return 0;
1913 return 1;
1916 our %urlfields;
1917 BEGIN {
1918 %urlfields = (
1919 baseurl => ["url" , 1],
1920 homepage => ["hp" , 0],
1921 notifyjson => ["notifyjson", 0],
1925 sub cmd_seturl {
1926 my $force = 0;
1927 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1928 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1929 my $project = get_project_harder($ARGV[0]);
1930 if (!exists($urlfields{$ARGV[1]})) {
1931 die "invalid URL field name: \"$ARGV[1]\" -- try \"help\"\n";
1933 if (@ARGV == 3 && ${$urlfields{$ARGV[1]}}[1] && !$project->{mirror}) {
1934 die "invalid URL field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
1935 unless $force;
1936 warn "using mirror field on non-mirror with --force\n" unless $quiet;
1938 if (@ARGV == 3 && !valid_url($ARGV[2], $ARGV[1])) {
1939 die "invalid URL (use --force to accept): \"$ARGV[2]\"\n"
1940 unless $force;
1941 warn "using invalid URL with --force\n" unless $quiet;
1943 my $old = $project->{${$urlfields{$ARGV[1]}}[0]};
1944 if (@ARGV == 2) {
1945 print "$old\n" if defined($old);
1946 return 0;
1948 if (defined($old) && $old eq $ARGV[2]) {
1949 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
1950 } else {
1951 # Avoid touching anything other than $ARGV[1]'s field
1952 $project->_property_fput(${$urlfields{$ARGV[1]}}[0], $ARGV[2]);
1953 if ($ARGV[1] eq "baseurl") {
1954 $project->{url} = $ARGV[2];
1955 $project->_set_bangagain;
1957 $project->_set_changed unless $ARGV[1] eq "notifyjson";
1958 warn $project->{name}, ": $ARGV[1] updated to $ARGV[2]\n" unless $quiet;
1960 return 0;
1963 our %msgsfields;
1964 BEGIN {
1965 %msgsfields = (
1966 notifymail => 1,
1967 notifytag => 1,
1971 sub cmd_setmsgs {
1972 my $force = 0;
1973 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1974 @ARGV >= 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1975 my $project = get_project_harder(shift @ARGV);
1976 my $field = shift @ARGV;
1977 if (!exists($msgsfields{$field})) {
1978 die "invalid msgs field name: \"$field\" -- try \"help\"\n";
1980 if (@ARGV && !valid_addrlist(@ARGV)) {
1981 die "invalid email address list (use --force to accept): \"".join(" ",@ARGV)."\"\n"
1982 unless $force;
1983 warn "using invalid email address list with --force\n" unless $quiet;
1985 my $old = $project->{$field};
1986 if (!@ARGV) {
1987 printf "%s\n", clean_addrlist($old, " ") if defined($old);
1988 return 0;
1990 my $newlist = clean_addrlist(join(" ",@ARGV));
1991 if (defined($old) && $old eq $newlist) {
1992 warn $project->{name}, ": skipping update of $field to same value\n" unless $quiet;
1993 } else {
1994 # Avoid touching anything other than $field's field
1995 $project->_property_fput($field, $newlist);
1996 warn $project->{name}, ": $field updated to \"$newlist\"\n" unless $quiet;
1998 return 0;
2001 sub cmd_setusers {
2002 my $force = 0;
2003 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
2004 @ARGV >= 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
2005 my $project = get_project_harder(shift @ARGV);
2006 my $projname = $project->{name};
2007 !@ARGV || !$project->{mirror} or die "cannot set users list for mirror project: \"$projname\"\n";
2008 my @newusers = ();
2009 if (@ARGV) {
2010 eval {@newusers = validate_users(join(" ", @ARGV), $force); 1;} or exit 255;
2011 die "refusing to set empty users list without --force\n" unless @newusers || $force;
2013 return 0 if !@ARGV && $project->{mirror};
2014 my $oldusers = $project->{users};
2015 if ($oldusers && ref($oldusers) eq "ARRAY") {
2016 $oldusers = join("\n", @$oldusers);
2017 } else {
2018 $oldusers = "";
2020 if (!@ARGV) {
2021 print "$oldusers\n" if $oldusers ne "";
2022 return 0;
2024 if ($oldusers eq join("\n", @newusers)) {
2025 warn "$projname: skipping update of users list to same value\n" unless $quiet;
2026 } else {
2027 # Avoid touching anything other than the users list
2028 $project->{users} = \@newusers;
2029 $project->_update_users;
2030 warn "$projname: users list updated to \"@{[join(',',@newusers)]}\"\n" unless $quiet;
2032 return 0;
2035 our %fieldnames;
2036 BEGIN {
2037 %fieldnames = (
2038 owner => [\&cmd_setowner, 0],
2039 desc => [\&cmd_setdesc, 0],
2040 description => [\&cmd_setdesc, 0],
2041 readme => [\&cmd_setreadme, 0],
2042 head => [\&cmd_sethead, 0],
2043 HEAD => [\&cmd_sethead, 0],
2044 hooks => [\&cmd_sethooks, 0],
2045 hookspath => [\&cmd_sethooks, 0],
2046 cleanmirror => [\&cmd_setbool, 1],
2047 reverseorder => [\&cmd_setbool, 1],
2048 summaryonly => [\&cmd_setbool, 1],
2049 statusupdates => [\&cmd_setbool, 1],
2050 autogchack => [\&cmd_setautogchack, 0],
2051 baseurl => [\&cmd_seturl, 1],
2052 homepage => [\&cmd_seturl, 1],
2053 notifyjson => [\&cmd_seturl, 1],
2054 jsontype => [\&cmd_setjsontype, 0],
2055 jsonsecret => [\&cmd_setjsonsecret, 0],
2056 notifymail => [\&cmd_setmsgs, 1],
2057 notifytag => [\&cmd_setmsgs, 1],
2058 users => [\&cmd_setusers, 0],
2062 sub do_getset {
2063 $setopt = shift;
2064 my @newargs = ();
2065 push(@newargs, shift) if @_ && $_[0] eq '--force';
2066 my $field = $_[1];
2067 (($setopt && @_ >= 3) || @_ == 2) && exists($fieldnames{$field}) or die_usage;
2068 push(@newargs, shift);
2069 shift unless ${$fieldnames{$field}}[1];
2070 push(@newargs, @_);
2071 diename(($setopt ? "set " : "get ") . $field);
2072 @ARGV = @newargs;
2073 &{${$fieldnames{$field}}[0]}(@ARGV);
2076 sub cmd_get {
2077 do_getset(0, @_);
2080 sub cmd_set {
2081 do_getset(1, @_);
2084 our %commands;
2085 BEGIN {
2086 %commands = (
2087 list => \&cmd_list,
2088 create => \&cmd_create,
2089 adopt => \&cmd_adopt,
2090 remove => \&cmd_remove,
2091 trash => \&cmd_remove,
2092 delete => \&cmd_remove,
2093 prune => \&cmd_prune,
2094 show => \&cmd_show,
2095 urls => \&cmd_urls,
2096 listheads => \&cmd_listheads,
2097 listtags => \&cmd_listtags,
2098 listctags => \&cmd_listtags,
2099 deltags => \&cmd_deltags,
2100 delctags => \&cmd_deltags,
2101 addtags => \&cmd_addtags,
2102 addctags => \&cmd_addtags,
2103 chpass => \&cmd_chpass,
2104 checkpw => \&cmd_checkpw,
2105 gc => \&cmd_gc,
2106 update => \&cmd_update,
2107 remirror => \&cmd_remirror,
2108 setowner => \&cmd_setowner,
2109 setdesc => \&cmd_setdesc,
2110 setdescription => \&cmd_setdesc,
2111 setreadme => \&cmd_setreadme,
2112 sethead => \&cmd_sethead,
2113 sethooks => \&cmd_sethooks,
2114 sethookspath => \&cmd_sethooks,
2115 setbool => \&cmd_setbool,
2116 setboolean => \&cmd_setbool,
2117 setflag => \&cmd_setbool,
2118 setautogchack => \&cmd_setautogchack,
2119 seturl => \&cmd_seturl,
2120 setjsontype => \&cmd_setjsontype,
2121 setjsonsecret => \&cmd_setjsonsecret,
2122 setmsgs => \&cmd_setmsgs,
2123 setusers => \&cmd_setusers,
2124 get => \&cmd_get,
2125 set => \&cmd_set,
2128 our %nopager;
2129 BEGIN { %nopager = (
2130 # 1 => pager never allowed
2131 # -1 => pager defaults to off instead of on
2132 create => 1,
2133 adopt => -1,
2134 remove => -1,
2135 trash => -1,
2136 delete => -1,
2137 prune => -1,
2138 deltags => -1,
2139 delctags => -1,
2140 addtags => -1,
2141 addctags => -1,
2142 chpass => 1,
2143 checkpw => 1,
2144 gc => -1,
2145 update => -1,
2146 remirror => -1,
2147 setowner => -1,
2148 setdesc => -1,
2149 setdescription => -1,
2150 setreadme => -1,
2151 sethead => -1,
2152 sethooks => -1,
2153 sethookspath => -1,
2154 setbool => -1,
2155 setboolean => -1,
2156 setflag => -1,
2157 setautogchack => -1,
2158 seturl => -1,
2159 setjsontype => -1,
2160 setjsonsecret => -1,
2161 setmsgs => -1,
2162 setusers => -1,
2163 set => -1,
2164 urls => -1,
2167 sub dohelp {
2168 my $cmd = shift;
2169 my $bn = basename($0);
2170 setup_pager_stdout($usepager);
2171 printf "%s version %s\n\n", $bn, $VERSION;
2172 if (defined($cmd) && $cmd ne '') {
2173 $cmd =~ s/^set(?=[a-zA-Z])//i;
2174 my $cmdhelp = '';
2175 my ($lastmt, $incmd);
2176 foreach (split('\n', sprintf($help, $bn))) {
2177 $lastmt || $incmd or $lastmt = /^\s*$/, next;
2178 $incmd = 1 if $lastmt && /^\s*(?:\[?set\]?)?$cmd\s/;
2179 last if $incmd && /^\s*$/;
2180 $incmd and $cmdhelp .= $_ . "\n";
2181 $lastmt = /^\s*$/;
2183 print $cmdhelp and exit 0 if $cmdhelp;
2185 printf $help, $bn;
2186 exit 0;
2189 sub main {
2190 local *ARGV = \@_;
2192 shift, $quiet=1, redo if @ARGV && $ARGV[0] =~ /^(?:-q|--quiet)$/i;
2193 shift, $usepager=1, redo if @ARGV && $ARGV[0] =~ /^(?:-p|--pager|--paginate)$/i;
2194 shift, $usepager=0, redo if @ARGV && $ARGV[0] =~ /^(?:--no-pager|--no-paginate)$/i;
2196 dohelp($ARGV[1]) if !@ARGV || @ARGV && $ARGV[0] =~ /^(?:-h|-?-help|help)$/i;
2197 my $command = shift;
2198 diename($command);
2199 $setopt = 1;
2200 if (!exists($commands{$command}) && exists($commands{"set".$command})) {
2201 $setopt = 0;
2202 $command = "set" . $command;
2204 exists($commands{$command}) or die "Unknown command \"$command\" -- try \"help\"\n";
2205 dohelp($command) if @ARGV && ($ARGV[0] =~ /^(?:-h|-?-help)$/i ||
2206 $ARGV[0] =~ /^help$/i && !Girocco::Project::does_exist("help",1));
2207 $nopager{$command} && $nopager{$command} > 0 and $usepager = 0;
2208 my $pgdfltoff = $nopager{$command} && $nopager{$command} < 0 ? 1 : 0;
2209 setup_pager_stdout($usepager, $pgdfltoff);
2210 &{$commands{$command}}(@ARGV);