-
Notifications
You must be signed in to change notification settings - Fork 0
/
p4-fstat
executable file
·259 lines (204 loc) · 6.97 KB
/
p4-fstat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env perl
# p4-fstat --- pretty-printer for various p4 file operations
# Author: Noah Friedman <friedman@splode.com>
# Created: 2016-08-18
# Public domain
# $Id: p4-fstat,v 1.9 2018/01/17 22:06:27 friedman Exp $
# Commentary:
# This is easier to read and workspace paths are more useful.
# Code:
use strict;
use warnings qw(all);
use FindBin;
use lib "$FindBin::Bin/../lib/perl";
use lib "$ENV{HOME}/lib/perl";
use Getopt::Long;
use Pod::Usage;
use POSIX qw(strftime);
use NF::P4cmd qw(:direct);
use NF::FileUtil qw(:dir);
my %opt = ( localpath => 0,
);;
my @flags;
my @filter;
# Fields we request from server
# headTime is the time of the changelist
# headModTime is the modification time of the file, which might be older.
# revtime is only set for shelved files; it's the modtime of the file in the shelf.
my @fieldsT = (qw(depotFile clientFile path haveRev shelved fileSize
headAction headChange headRev headType headModTime headTime
action change workRev type revtime));
my @fieldsP = (qw(headAction headChange headRev)); # fields/row we print
my @fieldsO = (qw( action change workRev)); # or these if file open
# Fields which are right-justified
my @fieldRJ = (undef, 0, 1, 1); # first undef is for clientFile/depotFile
my $p4info;
my $strftime_fmt = "%Y-%m-%d";
sub parse_options
{
my $help = -1;
p4_process_cmdline_options ($_[0]);
local *ARGV = \@{$_[0]}; # modify our local arglist, not real ARGV.
my $pthrough = sub { push @flags, join ("", '-', @_) };
my $parser = Getopt::Long::Parser->new;
$parser->configure (qw(bundling autoabbrev no_ignore_case));
my $succ = $parser->getoptions
( "debug" => \$NF::P4cmd::DEBUG,
"h|?|help+" => \$help,
"l|local-path+" => \$opt{localpath},
"v|verbose+" => \$opt{verbose},
'x=s' => \$opt{cmd_args},
# Note that we use -e, not -c, for speed and least surprise since -c
# also matches any *later* change, a behavior which is not consistent
# with most other commands. If you really want fstat -c, use after --.
'c|change=i' => sub { push @flags, "-e", $_[1] },
'm|max=i' => sub { push @flags, "-m", $_[1] },
'F|filter=s@' => \@filter,
'O=s@' => $pthrough,
'R=s@' => $pthrough,
'S=s@' => $pthrough,
"H|have" => \$opt{have},
"o|opened" => \$opt{opened},
"e|exists" => sub { push @filter,
(qw(^headAction=delete
^headAction=move/delete
^headAction=purge
^headAction=archive
));
},
"z|size|filesize+" => \$opt{filesize},
"p=s" => \$ENV{OVERRIDE_P4PORT},
"u=s" => \$ENV{OVERRIDE_P4USER},
);
pod2usage (-exitstatus => 1, -verbose => 0) unless $succ;
pod2usage (-exitstatus => 0, -verbose => $help) if $help >= 0;
if ($opt{opened})
{
# This won't catch files open for add w/out -e changeno, which means
# they've already been shelved. But without it, this command can be
# very slow. So if --have is also specified, use it.
if ($opt{have}) { push @flags, "-Ro" }
else { push @filter, "action=*"; }
}
elsif ($opt{have}) { push @flags, "-Rh"; }
if ($opt{filesize})
{
push @flags, "-Ol";
push @fieldsP, 'fileSize';
push @fieldsO, 'fileSize';
$fieldRJ[4] = 1;
}
if ($opt{verbose})
{
$strftime_fmt = "%Y-%m-%d %H:%M:%S %z" if $opt{verbose} > 1;
my $mtime = $opt{verbose} > 2 ? 'headModTime' : 'headTime';
push @fieldsP, 'headType', $mtime;
push @fieldsO, 'type', $mtime;
}
}
sub p4cmd
{
my @data = p4 (@_);
return unless @data;
die $data[0]->{data} if $data[0]->{code} eq 'error' && @data == 1 ;
return @data;
}
sub p4info
{
unless (defined $p4info)
{
my @data = p4cmd (qw(info));
$p4info = $data[0];
}
return $p4info;
}
# Modifies arg in-place
sub prune_local
{
return if substr ($_[0], 0, 2) eq '//';
my $level = $opt{localpath};
my $cwd = p4info()->{clientCwd};
my $cwdlen = length ($cwd);
my $len = length $_[0];
if ($level >= 2 && $len > $cwdlen)
{
if (substr ($_[0], 0, $cwdlen) eq $cwd)
{
substr ($_[0], 0, $cwdlen+1) = "";
}
else
{
$_[0] = make_relative_directory_path ($_[0], $cwd);
}
}
return;
}
sub fmtsize
{
return '' unless defined $_[0] && $_[0] ne '0';
my $size = shift;
my $fmtsize = 1024;
my @suffix = ('', qw(K M G T P E));
my $idx = 0;
while ($size > $fmtsize)
{
$size /= $fmtsize;
$idx++;
}
my $fmtstr = $size < 100 && $idx != 0 ? "%.1f%s" : "%d%s" ;
return sprintf ($fmtstr, $size, $suffix[$idx]);
}
sub main
{
parse_options (\@_);
unless (@_)
{
push @_, sprintf ("//%s/...", p4info()->{clientName});
push @flags, "-Rh" unless @flags || @filter;
}
push @flags, "-F", join (" & ", @filter) if @filter;
push @flags, "-T", join (",", @fieldsT);
my @cmd = ('fstat');
unshift @cmd, ('-x', $opt{cmd_args}) if defined $opt{cmd_args};
my @data = p4cmd (@cmd, @flags, @_);
my $xFile = $opt{localpath} ? 'clientFile' : 'depotFile';
my @w;
my @toprint;
for my $r (@data)
{
# Skip entries that only contain 'desc' when using '-e changelist'
next unless exists $r->{depotFile};
if (exists $r->{action})
{ substr ($r->{change}, 0, 0, '*') }
else { substr ($r->{headChange}, 0, 0, ' ') }
map { substr ($r->{$_}, 0, 0, '#') if exists $r->{$_};
} (qw(headRev workRev));
if ($opt{filesize} && exists $r->{fileSize})
{ $r->{fileSize} = fmtsize ($r->{fileSize}) if $opt{filesize} < 2 }
else
{ $r->{fileSize} = "" } # common for deleted files
map { $r->{$_} = strftime ($strftime_fmt, localtime ($r->{$_}))
if exists $r->{$_};
} (qw(headModTime headTime revtime));
# We might not have clientFile if we have no current client or path
# is not mapped.
my @row = exists $r->{$xFile} ? $r->{$xFile} : $r->{depotFile};
prune_local ($row[0]) if $opt{localpath};
if (exists $r->{action}) { map { push @row, ($r->{$_} || "") } @fieldsO }
else { push @row, @{$r}{@fieldsP} }
for (my $i = 0; $i < @row; $i++)
{
my $len = length ($row[$i]);
$w[$i] = $len if !defined $w[$i] || $len > $w[$i];
}
push @toprint, \@row;
}
my @rj = @fieldRJ;
my $fmt = join (" ", map { join ("",
(shift @rj ? "%" : "%-"),
$_, "s")
} @w) . "\n";
map { printf $fmt, @$_ } @toprint;
}
main (@ARGV);
# eof