show-pack-counts.pl: utility to view project pack counts
[girocco.git] / toolbox / show-pack-counts.pl
blob67f807f4ffb75c81acd7bbc591e4a8afdf193e1e
1 #!/usr/bin/perl
3 # Show all projects' pack counts (how many .pack files)
4 # Use "show-pack-counts.pl --sorted" to see in pack count order
5 # Use the --objects option to show pack object counts instead of pack counts
7 use strict;
8 use warnings;
9 use lib @basedir@;
11 use Girocco::Config;
12 use Girocco::Project;
14 my $sorted = 0;
15 my $objcounts = 0;
16 $sorted=1, shift if @ARGV && $ARGV[0] eq '--sorted';
17 $objcounts = 1, shift if @ARGV && $ARGV[0] eq '--objects';
18 $sorted=1, shift if @ARGV && $ARGV[0] eq '--sorted';
19 my @projects = Girocco::Project::get_full_list;
20 my $lpbin = $Girocco::Config::basedir . "/bin/list_packs";
21 my $prjroot = $Girocco::Config::reporoot;
22 my @lpcmd = ($lpbin, "--exclude-no-idx");
23 push(@lpcmd, $objcounts ? "--count-objects" : "--count");
25 select((select(STDOUT),$|=1)[0]) unless $sorted;
27 sub count_proj_packs {
28 my $projname = shift;
29 my $pd = $prjroot . "/" . $projname . ".git/objects/pack";
30 -d $pd or warn("no such project (anymore) $projname\n"), return undef;
31 open LPCMD, '-|', @lpcmd, $pd or
32 warn("failed to run list_packs for project $projname\n"), return undef;
33 my $pcount = <LPCMD>;
34 chomp($pcount);
35 close LPCMD;
36 $pcount =~ /^\d+$/ or
37 warn("list_packs produced invalid output for project $projname\n"), return undef;
38 return 0 + $pcount;
41 my %counts = ();
43 foreach my $proj (@projects) {
44 my $c = count_proj_packs($proj);
45 next unless defined($c);
46 if ($sorted) {
47 $counts{$proj} = $c;
48 } else {
49 printf "%s\t%s\n", $proj, $c;
53 if ($sorted) {
54 my $sortsub = sub {
55 my $order = $counts{$b} <=> $counts{$a};
56 $order or $order = lc($a) cmp lc($b);
57 $order;
60 foreach my $proj (sort($sortsub keys(%counts))) {
61 printf "%s\t%s\n", $proj, $counts{$proj};
65 exit 0;