#! /usr/bin/perl
=comment
Copyright (c) 2010, Perforce Software, Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=cut
=comment
User contributed content on the Perforce Public Depot is not supported
by Perforce, although it may be supported by its author. This applies
to all contributions even those submitted by Perforce employees.
=cut
=comment
Script to recursively print out a directory structure.
Example output:
[] ./recursive_dirs.pl leaf //depot/a
//depot/a
/a
/a
/ax
/b
/branch
/x
/1
/2
[] ./recursive_dirs.pl full //depot/a
//depot/a
//depot/a/a
//depot/a/a/a
//depot/a/ax
//depot/a/b
//depot/a/branch
//depot/a/x
//depot/a/x/1
//depot/a/x/1/2
One way to test the output is to sync a directory tree
and then compare the script output with that of the
OS tree traversal tool, like "find some_dir -type d | sort".
=cut
################################################################################
# Run lots of recursive 'p4 dirs' commands. An alternative approach would be to
# run 'p4 sizes' and process the data. 'sizes' is a cheap command.
sub dirs
{
my ($level, $type, $dir) = @_;
if( $type eq 'leaf' )
{
split /\//, $dir;
$end = $_[ $#_ ];
pop;
$base = join '/', @_;
if( $iters == 0 ) { print "$dir\n"; }
else { print " "x(length $base) . "\/$end\n"; }
}
# type eq 'full'
else { print "$dir\n"; }
$iters++;
my $cmd = "p4 dirs \"$dir/\*\" 2>\&1 | ";
open DIRS, $cmd or die "Couldn't run '$cmd'.\n";
my @d;
$_ = <DIRS>;
chomp;
# Break recursion at the end of a path.
return if / - no such file\(s\)\.$/;
push @d, $_;
map { chomp; push @d, $_ } <DIRS>;
map { dirs( $level++, $type, $_ ) } @d;
}
################################################################################
die "USAGE : recursive_dirs.pl \[full|leaf\] path\n" .
"EXAMPLE : recursive_dirs.pl full //depot/some/path\n" if @ARGV != 2;
my $type = $ARGV[ 0 ];
die "Invalid option: \"$type\". Expected \"full\" or \"leaf\".\n"
if $type != "full" or $type != "leaf";
my $path = @ARGV[ 1 ];
# Trim a trailing slash
if( $path =~ /(^.*)\/$/ ) { $path = $1; }
my $iters = 0;
dirs 0, $type, $path;