mob: Use 4 decimals in SI output
[sunny256-utils.git] / git-tree-size
blobe155ad49cc0aad461a0dac0e37971d832a5a20a6
1 #!/usr/bin/env perl
3 #=======================================================================
4 # git-tree-size
5 # File ID: 7364955c-334d-11e1-b42e-4302457ff287
6 # [Description]
8 # Character set: UTF-8
9 # ©opyleft 2011– Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 2 or later, see end of
11 # file for legal stuff.
12 #=======================================================================
14 use strict;
15 use warnings;
16 use Getopt::Long;
18 local $| = 1;
20 our $Debug = 0;
22 our %Opt = (
24 'debug' => 0,
25 'help' => 0,
26 'verbose' => 0,
27 'version' => 0,
31 our $progname = $0;
32 $progname =~ s/^.*\/(.*?)$/$1/;
33 our $VERSION = '0.00';
35 Getopt::Long::Configure('bundling');
36 GetOptions(
38 'debug' => \$Opt{'debug'},
39 'help|h' => \$Opt{'help'},
40 'verbose|v+' => \$Opt{'verbose'},
41 'version' => \$Opt{'version'},
43 ) || die("$progname: Option error. Use -h for help.\n");
45 $Opt{'debug'} && ($Debug = 1);
46 $Opt{'help'} && usage(0);
47 if ($Opt{'version'}) {
48 print_version();
49 exit(0);
52 my $rev = defined($ARGV[0]) ? $ARGV[0] : 'HEAD';
53 print(treesize($rev) . "\n");
55 sub treesize {
56 # {{{
57 my $commit = shift;
58 my $Retval = 0;
59 open(PipeFP, "git ls-tree --full-tree -l -r $commit | cut -f 1 |") or die("$progname: Cannot open git ls-tree pipe: $!");
60 while (<PipeFP>) {
61 if (/ blob /) {
62 s/^.*?(\d+)$/$1/;
63 $Retval += $_;
66 return($Retval);
67 # }}}
68 } # treesize()
70 sub print_version {
71 # Print program version {{{
72 print("$progname v$VERSION\n");
73 return;
74 # }}}
75 } # print_version()
77 sub usage {
78 # Send the help message to stdout {{{
79 my $Retval = shift;
81 if ($Opt{'verbose'}) {
82 print("\n");
83 print_version();
85 print(<<"END");
87 Usage: $progname [options] [tree-ish]
89 Options:
91 -h, --help
92 Show this help.
93 -v, --verbose
94 Increase level of verbosity. Can be repeated.
95 --version
96 Print version information.
97 --debug
98 Print debugging messages.
101 exit($Retval);
102 # }}}
103 } # usage()
105 sub msg {
106 # Print a status message to stderr based on verbosity level {{{
107 my ($verbose_level, $Txt) = @_;
109 if ($Opt{'verbose'} >= $verbose_level) {
110 print(STDERR "$progname: $Txt\n");
112 return;
113 # }}}
114 } # msg()
116 sub D {
117 # Print a debugging message {{{
118 $Debug || return;
119 my @call_info = caller;
120 chomp(my $Txt = shift);
121 my $File = $call_info[1];
122 $File =~ s#\\#/#g;
123 $File =~ s#^.*/(.*?)$#$1#;
124 print(STDERR "$File:$call_info[2] $$ $Txt\n");
125 return('');
126 # }}}
127 } # D()
129 __END__
131 # Plain Old Documentation (POD) {{{
133 =pod
135 =head1 NAME
139 =head1 SYNOPSIS
141 [options] [file [files [...]]]
143 =head1 DESCRIPTION
147 =head1 OPTIONS
149 =over 4
151 =item B<-h>, B<--help>
153 Print a brief help summary.
155 =item B<-v>, B<--verbose>
157 Increase level of verbosity. Can be repeated.
159 =item B<--version>
161 Print version information.
163 =item B<--debug>
165 Print debugging messages.
167 =back
169 =head1 BUGS
173 =head1 AUTHOR
175 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
177 =head1 COPYRIGHT
179 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
180 This is free software; see the file F<COPYING> for legalese stuff.
182 =head1 LICENCE
184 This program is free software: you can redistribute it and/or modify it
185 under the terms of the GNU General Public License as published by the
186 Free Software Foundation, either version 2 of the License, or (at your
187 option) any later version.
189 This program is distributed in the hope that it will be useful, but
190 WITHOUT ANY WARRANTY; without even the implied warranty of
191 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
192 See the GNU General Public License for more details.
194 You should have received a copy of the GNU General Public License along
195 with this program.
196 If not, see L<http://www.gnu.org/licenses/>.
198 =head1 SEE ALSO
200 =cut
202 # }}}
204 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :