hooks: add pre-auto-gc hook
[girocco/readme.git] / toolbox / projtool.pl
blob8ea151e4315d465e90f0d044ac4d5960fd1a99f6
1 #!/usr/bin/perl
3 # projtool.pl - command line Girocco project maintenance tool
4 # Copyright (C) 2016,2017 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'}
14 use File::Basename;
15 use Digest::MD5 qw(md5_hex);
16 use IO::Socket;
17 use Cwd qw(realpath);
18 use lib "__BASEDIR__";
19 use Girocco::Config;
20 use Girocco::Util;
21 use Girocco::HashUtil;
22 use Girocco::CLIUtil;
23 use Girocco::Project;
24 use Girocco::User;
26 exit(&main(@ARGV)||0);
28 our $help;
29 BEGIN {$help = <<'HELP'}
30 Usage: %s [--quiet] <command> <options>
32 list [--verbose] [--sort=lcname|name|owner|gid|no] [--owner] [<regexp>]
33 list all projects (default is --sort=lcname)
34 limit to project names matching <regex> if given
35 match <regex> against owner instead of project name with --owner
37 create [--force] [--no-alternates] [--orphan] [<option>...] <project>
38 create new project <project> (prompted)
39 <option> can be:
40 --no-alternates skip setup of objects/info/alternates
41 --orphan allow creation of subproject w/o a parent
42 -p use mkdir -p during --orphan creation
43 --no-password set password crypt to invalid value "unknown"
44 --no-owner leave the gitweb.owner config unset
45 --mirror=<url> create a mirror from <url>
46 --full-mirror mirror all refs
47 --push[=<list>] create a push project
48 --desc=<string> specify project description w/o prompt
49 --defaults do no interactive prompting at all
50 Using --no-password skips the prompts for password, using
51 --no-owner skips the prompt for owner and using --mirror=<url>
52 or --push[=<list>] skips the prompts for mirror URL and
53 heads-only and push users. With --defaults if neither
54 --mirror=<url> nor --push[=<list>] is given then --push will
55 be implied. Using --desc=<string> will force a specific
56 description (including an empty string) and skip the prompt for
57 it. Otherwise a non-empty default description will always be
58 supplied in lieu of an empty or omitted description.
60 adopt [--force] [--type=mirror|push] [<option>...] <project> [<users>]
61 adopt project <project>
62 type of project is guessed if --type=<type> omitted
63 <users> is same as <newuserslist> for setusers command
64 <option> can be:
65 --dry-run do all the checks but don't perform adoption
66 --verbose show project info dump (useful with --dry-run)
67 --no-users no push users at all (<users> must be omitted)
68 --no-owner leave the gitweb.owner config totally unchanged
69 --owner=<val> set the gitweb.owner config to <val>
70 Both --no-owner and --owner=<val> may NOT be given, with neither
71 take owner from preexisting gitweb.owner else use admin setting.
72 For mirrors <users> is ignored otherwise if no <users> and no
73 --no-users option the push users list will consist of the single
74 user name matching the owner or empty if none or more than one.
75 With --dry-run <project> can be an absolute path to a git dir.
77 remove [--force] [--really-delete] [--keep-forks] <project>
78 remove project <project>
79 do not move to _recyclebin with --really-delete (just rm -rf)
80 remove projects with forks (by keeping forks) using --keep-forks
82 show <project>
83 show project <project>
85 listheads <project>
86 list all available heads for <project> and indicate current head
88 listtags [--verbose] <project>
89 list all ctags on project <project>
91 deltags <project> [-i] <tagstodel>
92 remove any ctags on project <project> present in <tagstodel>
93 <tagstodel> is space or comma separated list of tags to remove
94 with -i match against <tagstodel> without regard to letter case
96 addtags <project> <tagstoadd>
97 add ctags to project <project>
98 <tagstoadd> is space or comma separated list of tags to add
100 chpass [--force] <project> [random | unknown]
101 change project <project> password (prompted)
102 with "random" set to random password
103 with "unknown" set password hash to invalid value "unknown"
105 checkpw <project>
106 check project <project> password for a match (prompted)
108 gc [--force | --auto] [--redelta] <project>
109 run the gc.sh script on project <project>
110 with --auto let the gc.sh script decide what to do
111 with --force cause a full gc to take place (force_gc=1)
112 with neither --auto nor --force do a mini or if needed a full gc
113 (in other words just touch .needsgc and run gc.sh)
114 with --redelta a full gc will use pack-objects --no-reuse-delta
115 unless the global --quiet option is given show_progress=1 is used
117 remirror [--force] <project>
118 initiate a remirror of project <project>
120 [set]owner [--force] <project> <newowner>
121 set project <project> owner to <newowner>
122 without "set" and only 1 arg, just show current project owner
124 [set]desc [--force] <project> <newdesc>
125 set project <project> description to <newdesc>
126 without "set" and only 1 arg, just show current project desc
128 [set]readme [--force] <project> <newsetting>
129 set project <project> readme to <newsetting>
130 <newsetting> is automatic|suppressed|-|[@]filename
131 without "set" and only 2 args, just show current readme setting
133 [set]head <project> <newhead>
134 set project <project> HEAD symbolic ref to <newhead>
135 without "set" and only 1 arg, just show current project HEAD
137 [set]bool [--force] <project> <flagname> <boolvalue>
138 set project <project> boolean <flagname> to <boolvalue>
139 <flagname> is cleanmirror|reverseorder|summaryonly|statusupdtaes
140 without "set" and only 2 args, just show current flag value
142 [set]url [--force] <project> <urlname> <newurlvalue>
143 set project <project> url <urlname> to <newurlvalue>
144 <urlname> is baseurl|homepage|notifyjson
145 without "set" and only 2 args, just show current url value
147 [set]msgs [--force] <project> <msgsname> <eaddrlist>
148 set project <project> msgs <msgsname> to <addrlist>
149 <msgsname> is notifymail|notifytag
150 <eaddrlist> is space or comma separated list of email addresses
151 without "set" and only 2 args, just show current msgs value
153 [set]users [--force] <project> <newuserslist>
154 set push project <project> users list to <newuserslist>
155 <newuserslist> is space or comma separated list of user names
156 without "set" and only 1 arg, just show current users list
158 get <project> <fieldname>
159 show project <project> field <fieldname>
160 <fieldname> is owner|desc|readme|head|users
161 or <flagname>|<urlname>|<msgsname>
163 set [--force] <project> <fieldname> <newfieldvalue>
164 set project <project> field <fieldname> to <newfieldvalue>
165 <fieldname> same as for get
166 <newfieldvalue> same as for corresponding set... command
167 HELP
169 our $quiet;
170 our $setopt;
171 sub die_usage {
172 my $sub = shift || diename;
173 if ($sub) {
174 die "Invalid arguments to $sub command -- try \"help\"\n";
175 } else {
176 die "Invalid arguments -- try \"help\"\n";
180 sub get_readme_desc {
181 my $rm = shift;
182 defined($rm) or $rm = '';
183 if (length($rm)) {
184 my $test = $rm;
185 $test =~ s/<!--(?:[^-]|(?:-(?!-)))*-->//gs;
186 $test =~ s/\s+//s;
187 return $test eq '' ? "suppressed" : "length " . length($rm);
188 } else {
189 return "automatic";
193 sub get_ctag_counts {
194 my $project = shift;
195 my $compact = shift;
196 my @ctags = ();
197 foreach ($project->get_ctag_names) {
198 my $val = 0;
199 my $ct;
200 if (open $ct, '<', $project->{path}."/ctags/$_") {
201 my $count = <$ct>;
202 close $ct;
203 defined $count or $count = '';
204 chomp $count;
205 $val = $count =~ /^[1-9]\d*$/ ? $count : 1;
207 if ($compact) {
208 if ($val == 1) {
209 push(@ctags, $_);
210 } elsif ($val > 1) {
211 push(@ctags, $_."(".$val.")");
213 } else {
214 push(@ctags, [$_, $val]) if $val;
217 @ctags;
220 sub get_clean_project {
221 my $project = get_project(@_);
222 delete $project->{loaded};
223 delete $project->{base_path};
224 delete $project->{ccrypt};
225 /^orig/i || !defined($project->{$_}) and delete $project->{$_} foreach keys %$project;
226 $project->{owner} = $project->{email}; delete $project->{email};
227 $project->{homepage} = $project->{hp}; delete $project->{hp};
228 $project->{baseurl} = $project->{url}; delete $project->{url};
229 my $owner = $project->{owner};
230 if ($owner) {
231 $owner = lc($owner);
232 my @owner_users = map {$owner eq lc($$_[4]) ? $$_[1] : ()} get_all_users;
233 $project->{owner_users} = \@owner_users if @owner_users;
235 my $projname = $project->{name};
236 my @forks = grep {$$_[1] =~ m,^$projname/,} get_all_projects;
237 $project->{has_forks} = 1 if @forks;
238 $project->{has_alternates} = 1 if $project->has_alternates;
239 my @bundles = $project->bundles;
240 delete $project->{bundles};
241 $project->{bundles} = \@bundles if @bundles;
242 $project->{mirror} = 0 unless $project->{mirror};
243 $project->{is_empty} = 1 if $project->is_empty;
244 delete $project->{showpush} unless $project->{showpush};
245 delete $project->{users} if $project->{mirror};
246 delete $project->{baseurl} unless $project->{mirror};
247 delete $project->{banged} unless $project->{mirror};
248 delete $project->{lastrefresh} unless $project->{mirror};
249 delete $project->{cleanmirror} unless $project->{mirror};
250 delete $project->{statusupdates} unless $project->{mirror};
251 delete $project->{lastparentgc} unless $projname =~ m,/,;
252 unless ($project->{banged}) {
253 delete $project->{bangcount};
254 delete $project->{bangfirstfail};
255 delete $project->{bangmessagesent};
257 $project->{README} = get_readme_desc($project->{README}) if exists($project->{README});
258 my @tags = get_ctag_counts($project, 1);
259 $project->{tags} = \@tags if @tags;
260 $project;
263 sub clean_addrlist {
264 my %seen = ();
265 my @newlist = ();
266 foreach (split(/[,\s]+/, $_[0])) {
267 next unless $_;
268 $seen{lc($_)} = 1, push(@newlist, $_) unless $seen{lc($_)};
270 return join(($_[1]||","), @newlist);
273 sub valid_addrlist {
274 my $cleaned = clean_addrlist(join(" ", @_));
275 return 1 if $cleaned eq "";
276 valid_email_multi($cleaned) && length($cleaned) <= 512;
279 sub validate_users {
280 my ($userlist, $force, $nodie, $quiet) = @_;
281 my @newusers = ();
282 my $badlist = 0;
283 my %seenuser = ();
284 my $mobok = $Girocco::Config::mob && $Girocco::Config::mob eq "mob";
285 my %users = map({($$_[1] => $_)} get_all_users);
286 foreach (split(/[\s,]+/, $userlist)) {
287 if (exists($users{$_}) || $_ eq "everyone" || ($mobok && $_ eq "mob")) {
288 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
289 next;
291 if (Girocco::User::does_exist($_, 1)) {
292 if ($force) {
293 $seenuser{$_}=1, push(@newusers, $_) unless $seenuser{$_};
294 } else {
295 $badlist = 1;
296 warn "refusing to allow questionable user \"$_\" without --force\n" unless $nodie && $quiet;
298 next;
300 $badlist = 1;
301 warn "invalid user: \"$_\"\n" unless $nodie && $quiet
303 die if $badlist && !$nodie;
304 return @newusers;
307 sub is_default_desc {
308 # "Unnamed repository; edit this file 'description' to name the repository."
309 # "Unnamed repository; edit this file to name it for gitweb."
310 local $_ = shift;
311 return 0 unless defined($_);
312 /Unnamed\s+repository;/i && /\s+edit\s+this\s+file\s+/i && /\s+to\s+name\s+/i;
315 sub valid_desc {
316 my $test = shift;
317 chomp $test;
318 return 0 if $test =~ /[\r\n]/;
319 $test =~ s/\s\s+/ /g;
320 $test =~ s/^\s+//;
321 $test =~ s/\s+$//;
322 return $test ne '';
325 sub clean_desc {
326 my $desc = shift;
327 defined($desc) or $desc = '';
328 chomp $desc;
329 $desc = to_utf8($desc, 1);
330 $desc =~ s/\s\s+/ /g;
331 $desc =~ s/^\s+//;
332 $desc =~ s/\s+$//;
333 return $desc;
336 sub parse_options {
337 Girocco::CLIUtil::_parse_options(
338 sub {
339 warn((($_[0]eq'?')?"unrecognized":"missing argument for")." option \"$_[1]\"\n")
340 unless $quiet;
341 die_usage;
342 }, @_);
345 sub cmd_list {
346 my %sortsub = (
347 lcname => sub {lc($$a[1]) cmp lc($$b[1])},
348 name => sub {$$a[1] cmp $$b[1]},
349 gid => sub {$$a[3] <=> $$b[3]},
350 owner => sub {lc($$a[4]) cmp lc($$b[4]) || lc($$a[1]) cmp lc($$b[1])},
351 no => sub {$$a[0] <=> $$b[0]},
353 my $sortopt = 'lcname';
354 my ($verbose, $owner);
355 parse_options(":sort" => \$sortopt, verbose => \$verbose, owner => \$owner);
356 my $regex;
357 if (@ARGV) {
358 my $val = shift @ARGV;
359 $regex = qr($val) or die "bad regex \"$val\"\n";
361 !@ARGV && exists($sortsub{$sortopt}) or die_usage;
362 my $sortsub = $sortsub{$sortopt};
363 my $grepsub = defined($regex) ? ($owner ? sub {$$_[4] =~ /$regex/} : sub {$$_[1] =~ /$regex/}) : sub {1};
364 my @projects = sort($sortsub grep {&$grepsub} get_all_projects);
365 if ($verbose) {
366 print map(sprintf("%s\n", join(":", (@$_)[1..5])), @projects);
367 } else {
368 print map(sprintf("%s: %s\n", $$_[1], $$_[5] =~ /^:/ ? "<mirror>" : $$_[5]), @projects);
370 return 0;
373 sub cmd_create {
374 my ($force, $noalternates, $orphanok, $optp, $nopasswd, $noowner, $defaults, $ispush, $pushusers,
375 $ismirror, $desc, $fullmirror);
376 parse_options(
377 force => \$force, "no-alternates" => \$noalternates, orphan => \$orphanok, p => \$optp,
378 "no-password" => \$nopasswd, "no-owner" => \$noowner, defaults => \$defaults,
379 "push" => \$ispush, ":push" => \$pushusers, ":mirror" => \$ismirror, ":desc" => \$desc,
380 "full-mirror" => \$fullmirror);
381 @ARGV == 1 or die_usage;
382 !defined($pushusers) || defined($ispush) or $ispush = 1;
383 defined($ismirror) && $ismirror =~ /^\s*$/ and die "--mirror url must not be empty\n";
384 die "--mirror and --push are mutually exclusive options\n" if $ismirror && $ispush;
385 die "--full-mirror requires use of --mirror=<url> option\n" if $fullmirror && !$ismirror;
386 !$defaults || defined($ispush) || defined($ismirror) or $ispush = 1;
387 !$defaults || defined($nopasswd) or $nopasswd = 1;
388 !$defaults || defined($noowner) or $noowner = 1;
389 !defined($ispush) || defined($pushusers) or $pushusers = "";
390 my $projname = $ARGV[0];
391 $projname =~ s/\.git$//i;
392 Girocco::Project::does_exist($projname, 1) and die "Project already exists: \"$projname\"\n";
393 if (!Girocco::Project::valid_name($projname, $orphanok, $optp)) {
394 warn "Refusing to create orphan project without --orphan\n"
395 if !$quiet && !$orphanok && Girocco::Project::valid_name($projname, 1, 1);
396 warn "Required orphan parent directory does not exist (use -p): ",
397 $Girocco::Config::reporoot.'/'.Girocco::Project::get_forkee_name($projname), "\n"
398 if !$quiet && $orphanok && Girocco::Project::valid_name($projname, 1, 1);
399 die "Invalid project name: \"$projname\"\n";
401 my ($forkee, $project) = ($projname =~ m#^(.*/)?([^/]+)$#);
402 my $newtype = $forkee ? 'fork' : 'project';
403 if (length($project) > 64) {
404 die "The $newtype name is longer than 64 characters. Do you really need that much?\n"
405 unless $force;
406 warn "Allowing $newtype name longer than 64 characters with --force\n" unless $quiet;
408 unless ($Girocco::Config::push || $Girocco::Config::mirror) {
409 die "Project creation disabled (no mirrors or push projects allowed)\n" unless $force;
410 warn "Continuing with --force even though both push and mirror projects are disabled\n" unless $quiet;
412 print "Enter settings for new project \"$projname\"\n" unless $defaults;
413 my %settings = ();
414 $settings{noalternates} = $noalternates;
415 if ($nopasswd) {
416 $settings{crypt} = "unknown";
417 } else {
418 my $np1 = prompt_noecho_nl_or_die("Admin password for project $projname (echo is off)");
419 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
420 my $np2 = prompt_noecho_nl_or_die("Retype admin password for project $projname");
421 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
422 "the admin passwords you have entered do not match each other.\n";
423 $settings{crypt} = scrypt_sha1($np1);
425 my $owner = "";
426 unless ($noowner) {
427 $owner = prompt_or_die("Owner/email name for project $projname");
428 unless (valid_email($owner)) {
429 unless ($force) {
430 warn "Your email sure looks weird...?\n";
431 redo;
433 warn "Allowing invalid email with --force\n" unless $quiet;
435 if (length($owner) > 96) {
436 unless ($force) {
437 warn "Your email is longer than 96 characters. Do you really need that much?\n";
438 redo;
440 warn "Allowing email longer than 96 characters with --force\n" unless $quiet;
443 $settings{email} = $owner;
444 my $baseurl = "";
445 my $checkmirror = sub {
446 my $checkurl = shift;
447 unless (valid_repo_url($checkurl)) {
448 unless ($force) {
449 warn "Invalid mirror URL: \"$checkurl\"\n";
450 return undef;
452 warn "Allowing invalid mirror URL with --force\n" unless $quiet;
454 if ($Girocco::Config::restrict_mirror_hosts) {
455 my $mh = extract_url_hostname($checkurl);
456 unless (is_dns_hostname($mh)) {
457 unless ($force) {
458 warn "Invalid non-DNS mirror URL: \"$checkurl\"\n";
459 undef;
461 warn "Allowing non-DNS mirror URL with --force\n" unless $quiet;
463 if (is_our_hostname($mh)) {
464 unless ($force) {
465 warn "Invalid same-host mirror URL: \"$checkurl\"\n";
466 return undef;
468 warn "Allowing same-host mirror URL with --force\n" unless $quiet;
471 return $checkurl;
473 if ($ispush || $ismirror) {
474 !$ispush || $force || $Girocco::Config::push or
475 die "Push projects are disabled, create a mirror (or use --force)\n";
476 !$ismirror || $force || $Girocco::Config::mirror or
477 die "Mirror projects are disabled, create a push project (or use --force)\n";
478 if ($ismirror) {
479 &$checkmirror($ismirror) or die "Invalid --mirror URL\n";
480 $baseurl = $ismirror;
481 $settings{url} = $baseurl;
482 $settings{cleanmirror} = $fullmirror ? 0 : 1;
483 } else {
484 my @newusers = ();
485 if ($pushusers !~ /^[\s,]*$/) {
486 eval {@newusers = validate_users($pushusers, $force); 1;} or
487 die "Invalid --push user list\n";
489 $settings{users} = \@newusers;
491 } elsif ($force || $Girocco::Config::mirror) {{
492 if ($force || $Girocco::Config::push) {
493 $baseurl = prompt_or_die("URL to mirror from (leave blank for push project)", "");
494 } else {{
495 $baseurl = prompt_or_die("URL to mirror from");
496 unless ($baseurl ne "") {
497 warn "Push projects are disabled, you must enter a mirror URL (or use --force)\n";
498 redo;
501 if ($baseurl ne "") {
502 &$checkmirror($baseurl) or redo;
503 $settings{url} = $baseurl;
504 $settings{cleanmirror} =
505 ynprompt_or_die("Mirror only heads, tags and notes (Y/n)", "Yes");
508 my $mirror = ($baseurl eq "") ? 0 : 1;
509 my $checkdesc = sub {
510 my $d = shift;
511 if (length($d) > 1024) {
512 unless ($force) {
513 warn "Short description length greater than 1024 characters!\n";
514 return undef;
516 warn "Allowing short description length greater than 1024 characters\n" unless $quiet;
518 return $d;
520 if (defined($desc)) {
521 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
522 $desc eq "" || &$checkdesc($desc) or
523 die "Invalid --desc description\n";
524 } elsif (!$defaults) {
525 $desc = prompt_or_die("Short description", "");
526 $desc =~ s/^\s+//; $desc =~ s/\s+$//;
527 $desc eq "" || &$checkdesc($desc) or redo;
528 $desc = undef if $desc eq "";
530 defined($desc) or $desc = $mirror ? "Mirror of $baseurl" : "Push project $projname";
531 $settings{desc} = $desc;
532 my $homepage = "";
533 if (!$defaults) {
534 $homepage = prompt_or_die("Home page URL", "");
535 if ($homepage ne "" && !valid_web_url($homepage)) {
536 unless ($force) {
537 warn "Invalid home page URL: \$homepage\"\n";
538 redo;
540 warn "Allowing invalid home page URL with --force\n" unless $quiet;
543 $settings{hp} = $homepage;
544 my $jsonurl = "";
545 if (!$defaults) {
546 $jsonurl = prompt_or_die("JSON notify POST URL", "");
547 if ($jsonurl ne "" && !valid_web_url($jsonurl)) {
548 unless ($force) {
549 warn "Invalid JSON notify POST URL: \$jsonurl\"\n";
550 redo;
552 warn "Allowing invalid JSON notify POST URL with --force\n" unless $quiet;
555 $settings{notifyjson} = $jsonurl;
556 my $commitaddrs = "";
557 if (!$defaults) {
558 $commitaddrs = clean_addrlist(prompt_or_die("Commit notify email addr(s)", ""));
559 if ($commitaddrs ne "" && !valid_addrlist($commitaddrs)) {
560 unless ($force) {
561 warn"invalid commit notify email address list (use --force to accept): \"$commitaddrs\"\n";
562 redo;
564 warn "using invalid commit notify email address list with --force\n" unless $quiet;
567 $settings{notifymail} = $commitaddrs;
568 $settings{reverseorder} = 1;
569 $settings{reverseorder} = ynprompt_or_die("Oldest-to-newest commit order in emails", "Yes")
570 if !$defaults && $commitaddrs ne "";
571 $settings{summaryonly} = ynprompt_or_die("Summary only (no diff) in emails", "No")
572 if !$defaults && $commitaddrs ne "";
573 my $tagaddrs = "";
574 if (!$defaults) {
575 $tagaddrs = clean_addrlist(prompt_or_die("Tag notify email addr(s)", ""));
576 if ($tagaddrs ne "" && !valid_addrlist($tagaddrs)) {
577 unless ($force) {
578 warn"invalid tag notify email address list (use --force to accept): \"$tagaddrs\"\n";
579 redo;
581 warn "using invalid tag notify email address list with --force\n" unless $quiet;
584 $settings{notifytag} = $tagaddrs;
585 if (!$mirror && !$ispush) {
586 my @newusers = ();
588 my $userlist = prompt_or_die("Push users", join(",", @newusers));
589 eval {@newusers = validate_users($userlist, $force); 1;} or redo;
591 $settings{users} = \@newusers;
593 my $newproj = Girocco::Project->ghost($projname, $mirror, $orphanok, $optp)
594 or die "Girocco::Project->ghost call failed\n";
595 my ($k, $v);
596 $newproj->{$k} = $v while ($k, $v) = each(%settings);
597 my $killowner = sub {
598 system($Girocco::Config::git_bin, '--git-dir='.$newproj->{path},
599 'config', '--unset', "gitweb.owner");
601 if ($mirror) {
602 $newproj->premirror or die "Girocco::Project->premirror failed\n";
603 !$noowner or &$killowner;
604 $newproj->clone or die "Girocco::Project->clone failed\n";
605 warn "Project $projname created and cloning successfully initiated.\n"
606 unless $quiet;
607 } else {
608 $newproj->conjure or die "Girocco::Project->conjure failed\n";
609 !$noowner or &$killowner;
610 warn "New push project fork is empty due to use of --no-alternates\n"
611 if !$quiet && $projname =~ m,/, && $noalternates;
612 warn "Project $projname successfully created.\n" unless $quiet;
614 return 0;
617 sub git_config {
618 my $gd = shift;
619 system($Girocco::Config::git_bin, "--git-dir=$gd", 'config', @_) == 0
620 or die "\"git --git-dir='$gd' config ".join(" ", @_)."\" failed.\n";
623 sub cmd_adopt {
624 my ($force, $type, $nousers, $dryrun, $noowner, $owner, $users, $verbose);
625 parse_options(force => \$force, ":type" => \$type, "no-users" => \$nousers, "dry-run" => \$dryrun,
626 "no-owner" => \$noowner,":owner" => \$owner, quiet => \$quiet, q =>\$quiet, verbose => \$verbose);
627 @ARGV or die "Please give project name on command line.\n";
628 my $projname = shift @ARGV;
629 (!$noowner || !defined($owner)) && (!$nousers || !@ARGV) or die_usage;
630 !defined($type) || $type eq "mirror" || $type eq "push" or die_usage;
631 defined($type) or $type = "";
632 my $projdir;
633 if ($dryrun && $projname =~ m,^/[^.\s/\\:], && is_git_dir(realpath($projname))) {
634 $projdir = realpath($projname);
635 $projname = $projdir;
636 $projname =~ s/\.git$//i;
637 $projname =~ s,/+$,,;
638 $projname =~ s,^.*/,,;
639 $projname ne "" or $projname = $projdir;
640 } else {
641 $projname =~ s/\.git$//i;
642 $projname ne "" or die "Invalid project name \"\".\n";
643 unless (Girocco::Project::does_exist($projname, 1)) {
644 Girocco::Project::valid_name($projname, 1, 1)
645 or die "Invalid project name \"$projname\".\n";
646 die "No such project to adopt: $projname\n";
648 defined(girocco::Project->load($projname))
649 and die "Project already known (no need to adopt): $projname\n";
650 $projdir = $Girocco::Config::reporoot . "/" . $projname . ".git";
651 is_git_dir($projdir) or die "Not a git directory: \"$projdir\"\n";
653 my $config = read_config_file($projdir . "/config");
654 my %config = ();
655 %config = map {($$_[0], defined($$_[1])?$$_[1]:"true")} @$config if defined($config);
656 git_bool($config{"core.bare"}) or die "Not a bare git repository: \"$projdir\"\n";
657 defined(read_HEAD_symref($projdir)) or die "Project with non-symbolic HEAD ref: \"$projdir\"\n";
658 @ARGV and $users = [validate_users(join(" ", @ARGV), $force, 1, $quiet)];
659 my $desc = "";
660 if (-e "$projdir/description") {
661 open my $fd, '<', "$projdir/description" or die "Cannot open \"$projdir/description\": $!\n";
663 local $/;
664 $desc = <$fd>;
666 close $fd;
667 defined $desc or $desc = "";
668 chomp $desc;
669 $desc = to_utf8($desc, 1);
670 is_default_desc($desc) and $desc = "";
671 if ($desc ne "" && !valid_desc($desc)) {
672 die "invalid 'description' file contents (use --force to accept): \"$desc\"\n"
673 unless $force;
674 warn "using invalid 'description' file contents with --force\n" unless $quiet;
676 $desc = clean_desc($desc);
677 if (length($desc) > 1024) {
678 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
679 unless $force;
680 warn "using longer than 1024 char description with --force\n" unless $quiet;
683 my $readme = "";
684 if (-e "$projdir/README.html") {
685 open my $fd, '<', "$projdir/README.html" or die "Cannot open \"$projdir/README.html\": $!\n";
687 local $/;
688 $readme = <$fd>;
690 close $fd;
691 defined $readme or $readme = "";
692 $readme = to_utf8($readme, 1);
693 $readme =~ s/\r\n?/\n/gs;
694 $readme =~ s/^\s+//s;
695 $readme =~ s/\s+$//s;
696 $readme eq "" or $readme .= "\n";
697 if (length($readme) > 8192) {
698 die "readme greater than 8192 chars is too long (use --force to override)\n"
699 unless $force;
700 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
702 my $rd = get_readme_desc($readme);
703 if ($rd ne "automatic" && $rd ne "suppressed") {
704 my $xmllint = qx(command -v xmllint); chomp $xmllint;
705 if (-f $xmllint && -x $xmllint) {
706 my $dummy = {README => $readme};
707 my ($cnt, $err) = Girocco::Project::_lint_readme($dummy, 0);
708 if ($cnt) {
709 my $msg = "xmllint: $cnt error";
710 $msg .= "s" unless $cnt == 1;
711 print STDERR "$msg\n", "-" x length($msg), "\n", $err
712 unless $force && $quiet;
713 exit(255) unless $force;
714 warn "$projname: using invalid raw HTML with --force\n" unless $quiet;
716 } else {
717 die "xmllint not available, refusing to use raw HTML without --force\n"
718 unless $force;
719 warn "xmllint not available using unchecked raw HTML with --force\n" unless $quiet;
723 # Inspect any remotes now
724 # Yes, Virginia, remote urls can be multi-valued
725 my %remotes = ();
726 foreach (@$config) {
727 my ($k,$v) = @$_;
728 next unless $k =~ /^remote\.([^\/].*?)\.([^.]+)$/; # remote name cannot start with "/"
729 my ($name, $subkey) = ($1, $2);
730 $remotes{$name}->{skip} = git_bool($v,1), next if $subkey eq "skipdefaultupdate" || $subkey eq "skipfetchall";
731 $remotes{$name}->{mirror} = git_bool($v,1), next if $subkey eq "mirror"; # we might want this
732 $remotes{$name}->{vcs} = $v, next if defined($v) && $v !~ /^\s*$/ && $subkey eq "vcs";
733 push(@{$remotes{$name}->{$subkey}}, $v), next if defined($v) && $v !~ /^\s*$/ &&
734 ($subkey eq "url" || $subkey eq "fetch" || $subkey eq "push" || $subkey eq "pushurl");
736 # remotes.default is the default remote group to fetch for "git remote update" otherwise --all
737 # the remote names in a group are separated by runs of [ \t\n] characters
738 # remote names "", ".", ".." and any name starting with "/" are invalid
739 # a remote with no url or vcs setting is not considered valid
740 my @check = ();
741 my $usingall = 0;
742 if (exists($config{"remotes.default"})) {
743 foreach (split(/[ \t\n]+/, $config{"remotes.default"})) {
744 next unless exists($remotes{$_});
745 my $rmt = $remotes{$_};
746 next if !exists($rmt->{url}) && !$rmt->{vcs};
747 push(@check, $_);
749 } else {
750 $usingall = 1;
751 my %seenrmt = ();
752 foreach (@$config) {
753 my ($k,$v) = @$_;
754 next unless $k =~ /^remote\.([^\/].*?)\.[^.]+$/;
755 next if $seenrmt{$1};
756 $seenrmt{$1} = 1;
757 next unless exists($remotes{$1});
758 my $rmt = $remotes{$1};
759 next if $rmt->{skip} || (!exists($rmt->{url}) && !$rmt->{vcs});
760 push(@check, $1);
763 my @needskip = (); # remotes that need skipDefaultUpdate set to true
764 my $foundvcs = 0;
765 my $foundfetch = 0;
766 my $foundfetchwithmirror = 0;
767 foreach (@check) {
768 my $rmt = $remotes{$_};
769 push(@needskip, $_) if $usingall && !exists($rmt->{fetch});
770 next unless exists($rmt->{fetch});
771 ++$foundfetch;
772 ++$foundfetchwithmirror if $rmt->{mirror};
773 ++$foundvcs if $rmt->{vcs} || (exists($rmt->{url}) && $rmt->{url}->[0] =~ /^[a-zA-Z0-9][a-zA-Z0-9+.-]*::/);
775 # if we have $foundvcs then we need to explicitly set fetch.prune to false
776 # if we have $foundfetch > 1 then we need to explicitly set fetch.prune to false
777 my $neednoprune = !exists($config{"fetch.prune"}) && ($foundvcs || $foundfetch > 1);
778 my $baseurl = "";
779 my $needfakeorigin = 0; # if true we need to set remote.origin.skipDefaultUpdate = true
780 # if remote "origin" exists we always pick up its first url or use ""
781 if (exists($remotes{origin})) {
782 my $rmt = $remotes{origin};
783 $baseurl = exists($rmt->{url}) ? $rmt->{url}->[0] : "";
784 $needfakeorigin = !exists($rmt->{url}) && !$rmt->{vcs} && !$rmt->{skip};
785 } else {
786 $needfakeorigin = 1;
787 # get the first url of the @check remotes
788 foreach (@check) {
789 my $rmt = $remotes{$_};
790 next unless exists($rmt->{url});
791 next unless defined($rmt->{url}->[0]) && $rmt->{url}->[0] ne "";
792 $baseurl = $rmt->{url}->[0];
793 last;
796 my $makemirror = $type eq "mirror" || ($type eq "" && $foundfetch);
798 # If we have $foundfetch we want to make a mirror but complain if
799 # we $foundfetchwithmirror as well unless we have --type=mirror.
800 # Warn if we have --type=push and $foundfetch and !$foundfetchwithmirror.
801 # Warn if we need to set fetch.prune=false when making a mirror
802 # Warn if we need to create remote.origin.skipDefaultUpdate when making a mirror
803 # Complain if @needskip AND !$usingall (warn with --force but don't set skip)
804 # Warn if $usingall and any @needskip (and set them) if making a mirror
805 # Warn if making a mirror and $baseurl eq ""
806 # Warn if we have --type=mirror and !$foundfetch
808 if ($makemirror) {
809 warn "No base URL to mirror from for adopted \"$projname\"\n" unless $quiet || $baseurl ne "";
810 warn "Adopting mirror \"$projname\" without any fetch remotes\n" unless $quiet || $foundfetch;
811 if ($foundfetchwithmirror) {
812 warn "Refusing to adopt mirror \"$projname\" with active remote.<name>.mirror=true remote(s)\n".
813 "(Use --type=mirror to override)\n"
814 unless $type eq "mirror";
815 exit(255) unless $type eq "mirror" || $dryrun;
816 warn "Adopting mirror \"$projname\" with active remote.<name>.mirror=true remotes\n"
817 unless $quiet || $type ne "mirror";
819 warn "Setting explicit fetch.prune=false for adoption of mirror \"$projname\"\n"
820 if !$quiet && $neednoprune;
821 warn "Setting remote.origin.skipDefaultUpdate=true for adoption of mirror \"$projname\"\n"
822 if !$quiet && $needfakeorigin;
823 if (!$usingall && @needskip) {
824 warn "Refusing to adopt mirror empty fetch remote(s) (override with --force)\n"
825 unless $force;
826 exit(255) unless $force || $dryrun;
827 warn "Adopting mirror with empty fetch remote(s) with --force\n"
828 unless $quiet || !$force;
830 warn "Will set skipDefaultUpdate=true on non-fetch remote(s)\n" if !$quiet && $usingall && @needskip;
831 warn "Adopting mirror with base URL \"$baseurl\"\n" unless $quiet || $baseurl eq "";
832 } else {
833 warn "Adopting push \"$projname\" but active non-mirror remotes are present\n"
834 if !$quiet && $foundfetch && !$foundfetchwithmirror;
837 if (!$noowner && !defined($owner)) {
838 # Select the owner
839 $owner = $config{"gitweb.owner"};
840 if (!defined($owner) || $owner eq "") {
841 $owner = $Girocco::Config::admin;
842 warn "Using owner \"$owner\" for adopted project\n" unless $quiet;
845 if (!$nousers && !$makemirror && !defined($users)) {
846 # select user list for push project
847 my $findowner = $owner;
848 defined($findowner) or $findowner = $config{"gitweb.owner"};
849 $findowner = lc($findowner) if defined($findowner);
850 my @owner_users = ();
851 @owner_users = map {$findowner eq lc($$_[4]) ? $$_[1] : ()} get_all_users
852 if defined($findowner) && $findowner ne "";
853 if (@owner_users <= 1) {
854 $users = \@owner_users;
855 warn "No users found that match owner \"$findowner\"\n" unless @owner_users || $quiet;
856 } else {
857 $users = [];
858 warn "Found ".scalar(@owner_users)." users for owner \"$findowner\" (" .
859 join(" ", @owner_users) . ") not setting any\n" unless $quiet;
862 defined($users) or $users = [];
864 # Warn if we preserve an existing receive.denyNonFastForwards or receive.denyDeleteCurrent setting
865 # Complain if core.logallrefupdates or logs subdir exists and contains any files (allow with --force
866 # and warn about preserving the setting)
868 warn "Preserving existing receive.denyNonFastForwards=true\n"
869 if !$quiet && git_bool($config{"receive.denynonfastforwards"});
870 warn "Preserving existing receive.denyDeleteCurrent=$config{'receive.denydeletecurrent'}\n"
871 if !$quiet && exists($config{"receive.denydeletecurrent"}) &&
872 $config{"receive.denydeletecurrent"} ne "warn";
874 my $reflogfiles = Girocco::Project::_contains_files("$projdir/logs");
875 my $reflogactive = git_bool($config{"core.logallrefupdates"});
876 if ($reflogactive || $reflogfiles) {
877 warn "Refusing to adopt \"$projname\" with active ref logs without --force\n" if $reflogfiles && !$force;
878 warn "Refusing to adopt \"$projname\" with core.logAllRefUpdates=true without --force\n" if $reflogactive && !$force;
879 exit(255) unless $force || $dryrun;
880 warn "Adopting \"$projname\" with active ref logs with --force\n" unless $quiet || ($reflogfiles && !$force);
881 warn "Adopting \"$projname\" with core.logAllRefUpdates=true with --force\n" unless $quiet || ($reflogactive && !$force);
884 return 0 if $dryrun && !$verbose;
886 my $newproj = eval {Girocco::Project->ghost($projname, $makemirror, 1, $dryrun)};
887 defined($newproj) or die "Girocco::Project::ghost failed: $@\n";
888 $newproj->{desc} = $desc;
889 $newproj->{README} = $readme;
890 $newproj->{url} = $baseurl if $makemirror || exists($config{"gitweb.baseurl"});
891 $newproj->{email} = $owner if defined($owner);
892 $newproj->{users} = $users;
893 $newproj->{crypt} = "unknown";
894 $newproj->{reverseorder} = 1 unless exists($config{"hooks.reverseorder"});
895 $newproj->{summaryonly} = 1 unless exists($config{"hooks.summaryonly"});
896 my $dummy = bless {}, "Girocco::Project";
897 $dummy->{path} = "$projdir";
898 $dummy->{configfilehash} = \%config;
899 $dummy->_properties_load;
900 delete $dummy->{origurl};
901 foreach my $k (keys(%$dummy)) {
902 $newproj->{$k} = $dummy->{$k}
903 if exists($dummy->{$k}) && !exists($newproj->{$k});
906 if ($verbose) {
907 use Data::Dumper;
908 my %info = %$newproj;
909 $info{README} = get_readme_desc($info{README}) if exists($info{README});
910 my $d = Data::Dumper->new([\%info], ['*'.$newproj->{name}]);
911 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
912 print $d->Dump([\%info], ['*'.$newproj->{name}]);
914 return 0 if $dryrun;
916 # Make any changes as needed for @needskip, $neednoprune and $needfakeorigin
917 if ($makemirror) {
918 git_config($projdir, "fetch.prune", "false") if $neednoprune;
919 git_config($projdir, "remote.origin.skipDefaultUpdate", "true") if $needfakeorigin;
920 if ($usingall && @needskip) {
921 git_config($projdir, "remote.$_.skipDefaultUpdate", "true") foreach @needskip;
925 # Perform the actual adoption
926 $newproj->adopt or die "Girocco::Project::adopt failed\n";
928 # Perhaps restore core.logAllRefUpdates, receive.denyNonFastForwards and receive.denyDeleteCurrent
929 git_config($projdir, "receive.denyNonFastForwards", "true")
930 if git_bool($config{"receive.denynonfastforwards"});
931 git_config($projdir, "receive.denyDeleteCurrent", $config{"receive.denydeletecurrent"})
932 if exists($config{"receive.denydeletecurrent"}) &&
933 $config{"receive.denydeletecurrent"} ne "warn";
934 git_config($projdir, "core.logAllRefUpdates", "true")
935 if $reflogactive;
937 # Success
938 if ($makemirror) {
939 warn "Mirror project \"$projname\" successfully adopted.\n" unless $quiet;
940 } else {
941 warn "Push project \"$projname\" successfully adopted.\n" unless $quiet;
943 return 0;
946 sub cmd_remove {
947 my ($force, $reallydel, $keepforks);
948 parse_options(force => \$force, "really-delete" => \$reallydel,
949 "keep-forks" => \$keepforks, quiet => \$quiet, q =>\$quiet);
950 @ARGV or die "Please give project name on command line.\n";
951 @ARGV == 1 or die_usage;
952 my $project = get_project($ARGV[0]);
953 my $projname = $project->{name};
954 my $isempty = !$project->{mirror} && $project->is_empty;
955 if (!$project->{mirror} && !$isempty && $reallydel) {
956 die "refusing to remove and delete non-empty push project without --force: $projname\n" unless $force;
957 warn "allowing removal and deletion of non-empty push project with --force\n" unless $quiet;
959 my $altwarn;
960 my $removenogc;
961 if ($project->has_forks) {
962 die "refusing to remove project with forks (use --keep-forks): $projname\n" unless $keepforks;
963 warn "allowing removal of forked project while preserving its forks with --keep-forks\n" unless $quiet;
964 # Run pseudo GC on that repository so that objects don't get lost within forks
965 my $basedir = $Girocco::Config::basedir;
966 my $projdir = $project->{path};
967 warn "We have to run pseudo GC on the repo so that the forks don't lose data. Hang on...\n" unless $quiet;
968 my $nogcrunning = sub {
969 die "Error: GC appears to be currently running on $projname\n"
970 if -e "$projdir/gc.pid" || -e "$projdir/.gc_in_progress";
972 &$nogcrunning;
973 $removenogc = ! -e "$projdir/.nogc";
974 recreate_file("$projdir/.nogc") if $removenogc;
975 die "unable to create \"$projdir/.nogc\"\n" unless -e "$projdir/.nogc";
976 delete $ENV{show_progress};
977 $ENV{'show_progress'} = 1 unless $quiet;
978 sleep 2; # *cough*
979 &$nogcrunning;
980 system("$basedir/toolbox/perform-pre-gc-linking.sh", "--include-packs", $projname) == 0
981 or die "Running pseudo GC on project $projname failed\n";
982 $altwarn = 1;
984 my $archived;
985 if (!$project->{mirror} && !$isempty && !$reallydel) {
986 $archived = $project->archive_and_delete;
987 unlink("$archived/.nogc") if $removenogc && defined($archived) && $archived ne "";
988 } else {
989 $project->delete;
991 warn "Project '$projname' removed from $Girocco::Config::name" .
992 ($archived ? ", backup in '$archived'" : "") .".\n" unless $quiet;
993 warn "Retained forks may now have unwanted objects/info/alternates lines\n" if $altwarn && !$quiet;
994 return 0;
997 sub cmd_show {
998 use Data::Dumper;
999 @ARGV == 1 or die_usage;
1000 my $project = get_clean_project($ARGV[0]);
1001 my %info = %$project;
1002 my $d = Data::Dumper->new([\%info], ['*'.$project->{name}]);
1003 $d->Sortkeys(sub {[sort({lc($a) cmp lc($b)} keys %{$_[0]})]});
1004 print $d->Dump([\%info], ['*'.$project->{name}]);
1005 return 0;
1008 sub cmd_listheads {
1009 @ARGV == 1 or die_usage;
1010 my $project = get_project($ARGV[0]);
1011 my @heads = sort({lc($a) cmp lc($b)} $project->get_heads);
1012 my $cur = $project->{HEAD};
1013 defined($cur) or $cur = '';
1014 my $curmark = '*';
1015 my $headhash = get_git("--git-dir=$project->{path}", 'rev-parse', '--quiet', '--verify', 'HEAD');
1016 defined($headhash) or $headhash = '';
1017 chomp $headhash;
1018 $headhash or $curmark = '!';
1019 foreach (@heads) {
1020 my $mark = $_ eq $cur ? $curmark : ' ';
1021 print "$mark $_\n";
1023 return 0;
1026 sub cmd_listtags {
1027 my $vcnt = 0;
1028 shift(@ARGV), $vcnt=1 if @ARGV && ($ARGV[0] eq '--verbose' || $ARGV[0] eq '-v');
1029 @ARGV == 1 or die_usage;
1030 my $project = get_project($ARGV[0]);
1031 if ($vcnt) {
1032 print map("$$_[0]\t$$_[1]\n", get_ctag_counts($project));
1033 } else {
1034 print map("$_\n", $project->get_ctag_names);
1036 return 0;
1039 sub cmd_deltags {
1040 my $ic = 0;
1041 shift(@ARGV), $ic=1 if @ARGV && $ARGV[0] =~ /^(?:--?ignore-case|-i)$/i;
1042 @ARGV >= 2 or die_usage;
1043 my $project = get_project(shift @ARGV);
1044 my %curtags;
1045 if ($ic) {
1046 push(@{$curtags{lc($_)}}, $_) foreach $project->get_ctag_names;
1047 } else {
1048 push(@{$curtags{$_}}, $_) foreach $project->get_ctag_names;
1050 my @deltags = ();
1051 my %seentag = ();
1052 my $ctags = join(" ", @ARGV);
1053 $ctags = lc($ctags) if $ic;
1054 foreach (split(/[\s,]+/, $ctags)) {
1055 next unless exists($curtags{$_});
1056 $seentag{$_}=1, push(@deltags, $_) unless $seentag{$_};
1058 if (!@deltags) {
1059 warn $project->{name}, ": skipping removal of only non-existent tags\n" unless $quiet;
1060 } else {
1061 # Avoid touching anything other than the ctags
1062 foreach my $tg (@deltags) {
1063 $project->delete_ctag($_) foreach @{$curtags{$tg}};
1065 $project->_set_changed;
1066 $project->_set_forkchange;
1067 warn $project->{name}, ": specified tags have been removed\n" unless $quiet;
1069 return 0;
1072 sub cmd_addtags {
1073 @ARGV >= 2 or die_usage;
1074 my $project = get_project(shift @ARGV);
1075 my $ctags = join(" ", @ARGV);
1076 $ctags =~ /[^, a-zA-Z0-9:.+#_-]/ and
1077 die "Content tag(s) \"$ctags\" contain(s) evil character(s).\n";
1078 my $oldmask = umask();
1079 umask($oldmask & ~0060);
1080 my $changed = 0;
1081 foreach (split(/[\s,]+/, $ctags)) {
1082 ++$changed if $project->add_ctag($_, 1);
1084 if ($changed) {
1085 $project->_set_changed;
1086 $project->_set_forkchange;
1088 umask($oldmask);
1089 my $cnt = ($changed == 1) ? "1 content tag has" : $changed . " content tags have";
1090 warn $project->{name}, ": $cnt been added/updated\n" unless $quiet;
1091 return 0;
1094 sub _get_random_val {
1095 my $p = shift;
1096 my $md5;
1098 no warnings;
1099 $md5 = md5_hex(time . $$ . rand() . join(':',%$p));
1101 $md5;
1104 sub cmd_chpass {
1105 my $force = 0;
1106 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1107 my $random = undef;
1108 pop(@ARGV), $random=lc($ARGV[1]) if @ARGV==2 && $ARGV[1] =~ /^(?:random|unknown)$/i;
1109 @ARGV == 1 or die_usage;
1110 my $project = get_project($ARGV[0]);
1111 die "refusing to change locked password of project \"$ARGV[0]\" without --force\n"
1112 if $project->is_password_locked;
1113 my ($newpw, $rmsg);
1114 if ($random) {
1115 if ($random eq "random") {
1116 die "refusing to set random password without --force\n" unless $force;
1117 $rmsg = "set to random value";
1118 $newpw = _get_random_val($project);
1119 } else {
1120 die "refusing to set password hash to '$random' without --force\n" unless $force;
1121 $rmsg = "hash set to '$random'";
1122 $newpw = $random;
1124 } else {
1125 $rmsg = "updated";
1126 if (-t STDIN) {
1127 print "Changing admin password for project $ARGV[0]\n";
1128 my $np1 = prompt_noecho_nl_or_die("New password for project $ARGV[0] (echo is off)");
1129 $np1 ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1130 my $np2 = prompt_noecho_nl_or_die("Retype new password for project $ARGV[0]");
1131 $np1 eq $np2 or die "Our high-paid security consultants have determined that\n" .
1132 "the admin passwords you have entered do not match each other.\n";
1133 $newpw = $np1;
1134 } else {
1135 $newpw = <STDIN>;
1136 defined($newpw) or die "missing new password on STDIN\n";
1137 chomp($newpw);
1140 $newpw ne "" or die "empty passwords are not permitted (brokenness will ensue)\n";
1141 my $old = $project->{crypt};
1142 $project->{crypt} = (defined($random) && $random ne "random") ? $newpw : scrypt_sha1($newpw);
1143 if (defined($old) && $old eq $project->{crypt}) {
1144 warn $project->{name}, ": skipping update of password hash to same value\n" unless $quiet;
1145 } else {
1146 # Avoid touching anything other than the password hash
1147 $project->_group_update;
1148 warn $project->{name}, ": admin password $rmsg (new hash stored)\n" unless $quiet;
1150 return 0;
1153 sub cmd_checkpw {
1154 @ARGV == 1 or die_usage;
1155 my $project = get_project($ARGV[0]);
1156 my $pwhash = $project->{crypt};
1157 defined($pwhash) or $pwhash = "";
1158 if ($pwhash eq "") {
1159 warn $project->{name}, ": no password required\n" unless $quiet;
1160 return 0;
1162 if ($project->is_password_locked) {
1163 warn $project->{name}, ": password is locked\n" unless $quiet;
1164 exit 1;
1166 my $checkpw;
1167 if (-t STDIN) {
1168 $checkpw = prompt_noecho_nl_or_die("Admin password for project $ARGV[0] (echo is off)");
1169 $checkpw ne "" or warn "checking for empty password as hash (very unlikely)\n" unless $quiet;
1170 } else {
1171 $checkpw = <STDIN>;
1172 defined($checkpw) or die "missing admin password on STDIN\n";
1173 chomp($checkpw);
1175 unless (Girocco::CLIUtil::check_passwd_match($pwhash, $checkpw)) {
1176 warn "password check failure\n" unless $quiet;
1177 exit 1;
1179 warn "admin password match\n" unless $quiet;
1180 return 0;
1183 sub cmd_gc {
1184 my ($force, $auto, $redelta);
1185 parse_options(force => \$force, quiet => \$quiet, q => \$quiet, auto => \$auto,
1186 redelta => \$redelta, "no-reuse-delta" => \$redelta, aggressive => \$force);
1187 $force && $auto and die "--force and --auto are mutually exclusive options\n";
1188 @ARGV or die "Please give project name on command line.\n";
1189 @ARGV == 1 or die_usage;
1190 my $project = get_project($ARGV[0]);
1191 $quiet or $ENV{"show_progress"} = 1;
1192 $force and $ENV{"force_gc"} = 1;
1193 if (!$auto && !$force && ! -e $project->{path}."/.needsgc") {
1194 open NEEDSGC, '>', $project->{path}."/.needsgc" and close NEEDSGC;
1196 my @args = ($Girocco::Config::basedir . "/jobd/gc.sh", $project->{name});
1197 $redelta and push(@args, "-f");
1198 my $lastgc = $project->{lastgc};
1199 system({$args[0]} @args) != 0 and return 1;
1200 # Do it again Sam, but only if lastgc was set, gc.sh succeeded and now it's not set
1201 if ($lastgc) {
1202 my $newlastgc = get_git("--git-dir=$project->{path}", 'config', '--get', 'gitweb.lastgc');
1203 if (!$newlastgc) {
1204 system({$args[0]} @args) != 0 and return 1;
1207 return 0;
1210 sub cmd_remirror {
1211 my $force = 0;
1212 parse_options(force => \$force, quiet => \$quiet, q => \$quiet);
1213 @ARGV or die "Please give project name on command line.\n";
1214 @ARGV == 1 or die_usage;
1215 my $project = get_project($ARGV[0]);
1216 $project->{mirror} or die "Project \"$ARGV[0]\" is a push project, not a mirror project.\n";
1217 if ($project->{clone_in_progress} && !$project->{clone_failed}) {
1218 warn "Project \"$ARGV[0]\" already seems to have a clone underway at this moment.\n" unless $quiet && $force;
1219 exit(255) unless $force;
1220 yes_to_continue_or_die("Are you sure you want to force a remirror");
1222 unlink($project->_clonefail_path);
1223 unlink($project->_clonelog_path);
1224 recreate_file($project->_clonep_path);
1225 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or
1226 die "cannot connect to taskd.socket: $!\n";
1227 select((select($sock),$|=1)[0]);
1228 $sock->print("clone ".$project->{name}."\n");
1229 # Just ignore reply, we are going to succeed anyway and the I/O
1230 # would apparently get quite hairy.
1231 $sock->flush();
1232 sleep 2; # *cough*
1233 $sock->close();
1234 warn "Project \"$ARGV[0]\" remirror initiated.\n" unless $quiet;
1235 return 0;
1238 sub cmd_setowner {
1239 my $force = 0;
1240 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1241 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1242 my $project = get_project($ARGV[0]);
1243 if (@ARGV == 2 && !valid_email($ARGV[1])) {
1244 die "invalid owner/email (use --force to accept): \"$ARGV[1]\"\n"
1245 unless $force;
1246 warn "using invalid owner/email with --force\n" unless $quiet;
1248 if (@ARGV == 2 && length($ARGV[1]) > 96) {
1249 die "owner/email longer than 96 chars (use --force to accept): \"$ARGV[1]\"\n"
1250 unless $force;
1251 warn "using longer than 96 char owner/email with --force\n" unless $quiet;
1253 my $old = $project->{email};
1254 if (@ARGV == 1) {
1255 print "$old\n" if defined($old);
1256 return 0;
1258 if (defined($old) && $old eq $ARGV[1]) {
1259 warn $project->{name}, ": skipping update of owner/email to same value\n" unless $quiet;
1260 } else {
1261 # Avoid touching anything other than "gitweb.owner"
1262 $project->_property_fput("email", $ARGV[1]);
1263 $project->_update_index;
1264 $project->_set_changed;
1265 warn $project->{name}, ": owner/email updated to \"$ARGV[1]\"\n" unless $quiet;
1267 return 0;
1270 sub cmd_setdesc {
1271 my $force = 0;
1272 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1273 @ARGV >= 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1274 my $project = get_project(shift @ARGV);
1275 if (@ARGV && !valid_desc(join(" ", @ARGV))) {
1276 die "invalid description (use --force to accept): \"".join(" ", @ARGV)."\"\n"
1277 unless $force;
1278 warn "using invalid description with --force\n" unless $quiet;
1280 my $desc = clean_desc(join(" ", @ARGV));
1281 if (@ARGV && length($desc) > 1024) {
1282 die "description longer than 1024 chars (use --force to accept): \"$desc\"\n"
1283 unless $force;
1284 warn "using longer than 1024 char description with --force\n" unless $quiet;
1286 my $old = $project->{desc};
1287 if (!@ARGV) {
1288 print "$old\n" if defined($old);
1289 return 0;
1291 if (defined($old) && $old eq $desc) {
1292 warn $project->{name}, ": skipping update of description to same value\n" unless $quiet;
1293 } else {
1294 # Avoid touching anything other than description file
1295 $project->_property_fput("desc", $desc);
1296 $project->_set_changed;
1297 warn $project->{name}, ": description updated to \"$desc\"\n" unless $quiet;
1299 return 0;
1302 sub cmd_setreadme {
1303 my $force = 0;
1304 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1305 @ARGV == 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1306 my $project = get_project($ARGV[0]);
1307 my $old = $project->{README};
1308 if (@ARGV == 1) {
1309 chomp $old if defined($old);
1310 print "$old\n" if defined($old) && $old ne "";
1311 return 0;
1313 my ($new, $raw, $newname);
1314 $newname = '';
1315 if ($ARGV[1] eq "-") {
1316 local $/;
1317 $new = <STDIN>;
1318 $raw = 1;
1319 $newname = "contents of <STDIN>";
1320 } elsif (lc($ARGV[1]) eq "automatic" || lc($ARGV[1]) eq "auto") {
1321 $new = "";
1322 } elsif (lc($ARGV[1]) eq "suppressed" || lc($ARGV[1]) eq "suppress") {
1323 $new = "<!-- suppress -->";
1324 } else {
1325 my $fn = $ARGV[1];
1326 $fn =~ s/^\@//;
1327 die "missing filename for README\n" unless $fn ne "";
1328 die "no such file: \"$fn\"\n" unless -f $fn && -r $fn;
1329 open F, '<', $fn or die "cannot open \"$fn\" for reading: $!\n";
1330 local $/;
1331 $new = <F>;
1332 close F;
1333 $raw = 1;
1334 $newname = "contents of \"$fn\"";
1336 defined($new) or $new = '';
1337 $project->{README} = to_utf8($new, 1);
1338 $project->_cleanup_readme;
1339 if (length($project->{README}) > 8192) {
1340 die "readme greater than 8192 chars is too long (use --force to override)\n"
1341 unless $force;
1342 warn "using readme greater than 8192 chars with --force\n" unless $quiet;
1344 if ($raw) {
1345 my $rd = get_readme_desc($project->{README});
1346 if ($rd ne "automatic" && $rd ne "suppressed") {
1347 my $xmllint = qx(command -v xmllint); chomp $xmllint;
1348 if (-f $xmllint && -x $xmllint) {
1349 my ($cnt, $err) = $project->_lint_readme(0);
1350 if ($cnt) {
1351 my $msg = "xmllint: $cnt error";
1352 $msg .= "s" unless $cnt == 1;
1353 print STDERR "$msg\n", "-" x length($msg), "\n", $err
1354 unless $force && $quiet;
1355 exit(255) unless $force;
1356 warn $project->{name} . ": using invalid raw HTML with --force\n" unless $quiet;
1358 } else {
1359 die "xmllint not available, refusing to use raw HTML without --force\n"
1360 unless $force;
1361 warn "xmllint not available using unchecked raw HTML with --force\n" unless $quiet;
1365 if (defined($old) && $old eq $project->{README}) {
1366 warn $project->{name}, ": skipping update of README to same value\n" unless $quiet;
1367 } else {
1368 # Avoid touching anything other than README.html file
1369 $project->_property_fput("README", $project->{README});
1370 $project->_set_changed;
1371 my $desc = get_readme_desc($project->{README});
1372 if ($newname) {
1373 $newname .= " ($desc)";
1374 } else {
1375 $newname = $desc;
1377 warn $project->{name}, ": README updated to $newname\n" unless $quiet;
1379 return 0;
1382 sub valid_head {
1383 my ($proj, $newhead) = @_;
1384 my %okheads = map({($_ => 1)} $proj->get_heads);
1385 exists($okheads{$newhead});
1388 sub cmd_sethead {
1389 @ARGV == 2 || (@ARGV == 1 && !$setopt) or die_usage;
1390 my $project = get_project($ARGV[0]);
1391 if (@ARGV == 2 && !valid_head($project, $ARGV[1])) {
1392 die "invalid head (try \"@{[basename($0)]} listheads $ARGV[0]\"): \"$ARGV[1]\"\n";
1394 my $old = $project->{HEAD};
1395 if (@ARGV == 1) {
1396 print "$old\n" if defined($old);
1397 return 0;
1399 if (defined($old) && $old eq $ARGV[1]) {
1400 warn $project->{name}, ": skipping update of HEAD symref to same value\n" unless $quiet;
1401 } else {
1402 # Avoid touching anything other than the HEAD symref
1403 $project->set_HEAD($ARGV[1]);
1404 warn $project->{name}, ": HEAD symref updated to \"refs/heads/$ARGV[1]\"\n" unless $quiet;
1406 return 0;
1409 our %boolfields;
1410 BEGIN {
1411 %boolfields = (
1412 cleanmirror => 1,
1413 reverseorder => 0,
1414 summaryonly => 0,
1415 statusupdates => 1,
1419 sub cmd_setbool {
1420 my $force = 0;
1421 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1422 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1423 my $project = get_project($ARGV[0]);
1424 if (!exists($boolfields{$ARGV[1]})) {
1425 die "invalid boolean field name: \"$ARGV[1]\" -- try \"help\"\n";
1427 if (@ARGV == 3 && $boolfields{$ARGV[1]} && !$project->{mirror}) {
1428 die "invalid boolean field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
1429 unless $force;
1430 warn "using mirror field on non-mirror with --force\n" unless $quiet;
1432 if (@ARGV == 3 && !valid_bool($ARGV[2])) {
1433 die "invalid boolean value: \"$ARGV[2]\"\n";
1435 my $bool = clean_bool($ARGV[2]);
1436 my $old = $project->{$ARGV[1]};
1437 if (@ARGV == 2) {
1438 print "$old\n" if defined($old);
1439 return 0;
1441 if (defined($old) && $old eq $bool) {
1442 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
1443 } else {
1444 # Avoid touching anything other than $ARGV[1] field
1445 $project->_property_fput($ARGV[1], $bool);
1446 warn $project->{name}, ": $ARGV[1] updated to $bool\n" unless $quiet;
1448 return 0;
1451 sub valid_url {
1452 my ($url, $type) = @_;
1453 $type ne 'baseurl' and return valid_web_url($url);
1454 valid_repo_url($url) or return 0;
1455 if ($Girocco::Config::restrict_mirror_hosts) {
1456 my $mh = extract_url_hostname($url);
1457 is_dns_hostname($mh) or return 0;
1458 !is_our_hostname($mh) or return 0;
1460 return 1;
1463 our %urlfields;
1464 BEGIN {
1465 %urlfields = (
1466 baseurl => ["url" , 1],
1467 homepage => ["hp" , 0],
1468 notifyjson => ["notifyjson", 0],
1472 sub cmd_seturl {
1473 my $force = 0;
1474 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1475 @ARGV == 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1476 my $project = get_project($ARGV[0]);
1477 if (!exists($urlfields{$ARGV[1]})) {
1478 die "invalid URL field name: \"$ARGV[1]\" -- try \"help\"\n";
1480 if (@ARGV == 3 && ${$urlfields{$ARGV[1]}}[1] && !$project->{mirror}) {
1481 die "invalid URL field for non-mirror (use --force to accept): \"$ARGV[1]\"\n"
1482 unless $force;
1483 warn "using mirror field on non-mirror with --force\n" unless $quiet;
1485 if (@ARGV == 3 && !valid_url($ARGV[2], $ARGV[1])) {
1486 die "invalid URL (use --force to accept): \"$ARGV[2]\"\n"
1487 unless $force;
1488 warn "using invalid URL with --force\n" unless $quiet;
1490 my $old = $project->{${$urlfields{$ARGV[1]}}[0]};
1491 if (@ARGV == 2) {
1492 print "$old\n" if defined($old);
1493 return 0;
1495 if (defined($old) && $old eq $ARGV[2]) {
1496 warn $project->{name}, ": skipping update of $ARGV[1] to same value\n" unless $quiet;
1497 } else {
1498 # Avoid touching anything other than $ARGV[1]'s field
1499 $project->_property_fput(${$urlfields{$ARGV[1]}}[0], $ARGV[2]);
1500 if ($ARGV[1] eq "baseurl") {
1501 $project->{url} = $ARGV[2];
1502 $project->_set_bangagain;
1504 $project->_set_changed unless $ARGV[1] eq "notifyjson";
1505 warn $project->{name}, ": $ARGV[1] updated to $ARGV[2]\n" unless $quiet;
1507 return 0;
1510 our %msgsfields;
1511 BEGIN {
1512 %msgsfields = (
1513 notifymail => 1,
1514 notifytag => 1,
1518 sub cmd_setmsgs {
1519 my $force = 0;
1520 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1521 @ARGV >= 3 || (@ARGV == 2 && !$force && !$setopt) or die_usage;
1522 my $project = get_project(shift @ARGV);
1523 my $field = shift @ARGV;
1524 if (!exists($msgsfields{$field})) {
1525 die "invalid msgs field name: \"$field\" -- try \"help\"\n";
1527 if (@ARGV && !valid_addrlist(@ARGV)) {
1528 die "invalid email address list (use --force to accept): \"".join(" ",@ARGV)."\"\n"
1529 unless $force;
1530 warn "using invalid email address list with --force\n" unless $quiet;
1532 my $old = $project->{$field};
1533 if (!@ARGV) {
1534 printf "%s\n", clean_addrlist($old, " ") if defined($old);
1535 return 0;
1537 my $newlist = clean_addrlist(join(" ",@ARGV));
1538 if (defined($old) && $old eq $newlist) {
1539 warn $project->{name}, ": skipping update of $field to same value\n" unless $quiet;
1540 } else {
1541 # Avoid touching anything other than $field's field
1542 $project->_property_fput($field, $newlist);
1543 warn $project->{name}, ": $field updated to \"$newlist\"\n" unless $quiet;
1545 return 0;
1548 sub cmd_setusers {
1549 my $force = 0;
1550 shift(@ARGV), $force=1 if @ARGV && $ARGV[0] eq '--force';
1551 @ARGV >= 2 || (@ARGV == 1 && !$force && !$setopt) or die_usage;
1552 my $project = get_project(shift @ARGV);
1553 my $projname = $project->{name};
1554 !@ARGV || !$project->{mirror} or die "cannot set users list for mirror project: \"$projname\"\n";
1555 my @newusers = ();
1556 if (@ARGV) {
1557 eval {@newusers = validate_users(join(" ", @ARGV), $force); 1;} or exit 255;
1558 die "refusing to set empty users list without --force\n" unless @newusers || $force;
1560 return 0 if !@ARGV && $project->{mirror};
1561 my $oldusers = $project->{users};
1562 if ($oldusers && ref($oldusers) eq "ARRAY") {
1563 $oldusers = join("\n", @$oldusers);
1564 } else {
1565 $oldusers = "";
1567 if (!@ARGV) {
1568 print "$oldusers\n" if $oldusers ne "";
1569 return 0;
1571 if ($oldusers eq join("\n", @newusers)) {
1572 warn "$projname: skipping update of users list to same value\n" unless $quiet;
1573 } else {
1574 # Avoid touching anything other than the users list
1575 $project->{users} = \@newusers;
1576 $project->_update_users;
1577 warn "$projname: users list updated to \"@{[join(',',@newusers)]}\"\n" unless $quiet;
1579 return 0;
1582 our %fieldnames;
1583 BEGIN {
1584 %fieldnames = (
1585 owner => [\&cmd_setowner, 0],
1586 desc => [\&cmd_setdesc, 0],
1587 description => [\&cmd_setdesc, 0],
1588 readme => [\&cmd_setreadme, 0],
1589 head => [\&cmd_sethead, 0],
1590 HEAD => [\&cmd_sethead, 0],
1591 cleanmirror => [\&cmd_setbool, 1],
1592 reverseorder => [\&cmd_setbool, 1],
1593 summaryonly => [\&cmd_setbool, 1],
1594 statusupdates => [\&cmd_setbool, 1],
1595 baseurl => [\&cmd_seturl, 1],
1596 homepage => [\&cmd_seturl, 1],
1597 notifyjson => [\&cmd_seturl, 1],
1598 notifymail => [\&cmd_setmsgs, 1],
1599 notifytag => [\&cmd_setmsgs, 1],
1600 users => [\&cmd_setusers, 0],
1604 sub do_getset {
1605 $setopt = shift;
1606 my @newargs = ();
1607 push(@newargs, shift) if @_ && $_[0] eq '--force';
1608 my $field = $_[1];
1609 (($setopt && @_ >= 3) || @_ == 2) && exists($fieldnames{$field}) or die_usage;
1610 push(@newargs, shift);
1611 shift unless ${$fieldnames{$field}}[1];
1612 push(@newargs, @_);
1613 diename(($setopt ? "set " : "get ") . $field);
1614 @ARGV = @newargs;
1615 &{${$fieldnames{$field}}[0]}(@ARGV);
1618 sub cmd_get {
1619 do_getset(0, @_);
1622 sub cmd_set {
1623 do_getset(1, @_);
1626 our %commands;
1627 BEGIN {
1628 %commands = (
1629 list => \&cmd_list,
1630 create => \&cmd_create,
1631 adopt => \&cmd_adopt,
1632 remove => \&cmd_remove,
1633 trash => \&cmd_remove,
1634 delete => \&cmd_remove,
1635 show => \&cmd_show,
1636 listheads => \&cmd_listheads,
1637 listtags => \&cmd_listtags,
1638 listctags => \&cmd_listtags,
1639 deltags => \&cmd_deltags,
1640 delctags => \&cmd_deltags,
1641 addtags => \&cmd_addtags,
1642 addctags => \&cmd_addtags,
1643 chpass => \&cmd_chpass,
1644 checkpw => \&cmd_checkpw,
1645 gc => \&cmd_gc,
1646 remirror => \&cmd_remirror,
1647 setowner => \&cmd_setowner,
1648 setdesc => \&cmd_setdesc,
1649 setdescription => \&cmd_setdesc,
1650 setreadme => \&cmd_setreadme,
1651 sethead => \&cmd_sethead,
1652 setbool => \&cmd_setbool,
1653 setflag => \&cmd_setbool,
1654 seturl => \&cmd_seturl,
1655 setmsgs => \&cmd_setmsgs,
1656 setusers => \&cmd_setusers,
1657 get => \&cmd_get,
1658 set => \&cmd_set,
1662 sub dohelp {
1663 my $bn = basename($0);
1664 printf "%s version %s\n\n", $bn, $VERSION;
1665 printf $help, $bn;
1666 exit 0;
1669 sub main {
1670 local *ARGV = \@_;
1671 shift, $quiet=1 if @ARGV && $ARGV[0] =~ /^(?:-q|--quiet)$/i;
1672 dohelp if !@ARGV || @ARGV && $ARGV[0] =~ /^(?:-h|-?-help|help)$/i;
1673 my $command = shift;
1674 diename($command);
1675 $setopt = 1;
1676 if (!exists($commands{$command}) && exists($commands{"set".$command})) {
1677 $setopt = 0;
1678 $command = "set" . $command;
1680 exists($commands{$command}) or die "Unknown command \"$command\" -- try \"help\"\n";
1681 dohelp if @ARGV && $ARGV[0] =~ /^(?:-h|-?-help|help)$/i && !Girocco::Project::does_exist("help",1);
1682 &{$commands{$command}}(@ARGV);