$| = 1;
use strict;
use Carp;
use Cwd;
use Digest::MD5 2.20;
use File::Copy 2.03;
use File::Find;
use File::Path 1.0404;
use File::Temp 0.12 qw(tempdir tempfile);
use Getopt::Long 2.25;
use Text::Wrap;
use URI 1.17;
use English;
$Text::Wrap::columns = 72;
my $svn = '/usr/bin/svn';
my $repos_base_url;
my $repos_load_rel_path;
my $opt_import_tag_location;
my $opt_no_user_input;
my $opt_svn_username;
my $opt_svn_password;
my $opt_verbose;
my $opt_existing_wc_dir;
my $REGEX_SEP_CHAR = '@';
my $property_config_filename;
GetOptions('no_user_input' => \$opt_no_user_input,
'property_cfg_filename=s' => \$property_config_filename,
'svn_password=s' => \$opt_svn_password,
'svn_username=s' => \$opt_svn_username,
'tag_location=s' => \$opt_import_tag_location,
'verbose+' => \$opt_verbose,
'wc=s' => \$opt_existing_wc_dir)
or &usage;
&usage("$0: too few arguments") if @ARGV < 2;
$repos_base_url = shift;
$repos_load_rel_path = shift;
if ($repos_base_url =~ /\.{2}/)
{
die "$0: repos base URL $repos_base_url cannot contain ..'s.\n";
}
if ($repos_load_rel_path =~ /\.{2}/)
{
die "$0: repos import relative directory path $repos_load_rel_path ",
"cannot contain ..'s.\n";
}
if (!@ARGV and !$opt_no_user_input)
{
&usage("$0: must use -no_user_input if no dirs listed on command line.");
}
if (!@ARGV and $opt_import_tag_location)
{
&usage("$0: cannot use -tag_location when dirs are read from stdin.");
}
if (defined $opt_import_tag_location and $repos_load_rel_path eq '.')
{
&usage("$0: cannot set import_dir to '.' and use -t command line option.");
}
my @svn_use_repos_cmd_opts;
&set_svn_use_repos_cmd_opts($opt_svn_username, $opt_svn_password);
if (defined $opt_import_tag_location and $opt_import_tag_location =~ /\.{2}/)
{
die "$0: repos tag relative directory path $opt_import_tag_location ",
"cannot contain ..'s.\n";
}
if ($repos_load_rel_path =~ m|^/|)
{
die "$0: repos import relative directory path $repos_load_rel_path ",
"cannot start with /.\n";
}
if (defined $opt_import_tag_location and $opt_import_tag_location =~ m|^/|)
{
die "$0: repos tagrelative directory path $opt_import_tag_location ",
"cannot start with /.\n";
}
if (defined $opt_existing_wc_dir)
{
unless (-e $opt_existing_wc_dir)
{
die "$0: working copy '$opt_existing_wc_dir' does not exist.\n";
}
unless (-d _)
{
die "$0: working copy '$opt_existing_wc_dir' is not a directory.\n";
}
unless (-d "$opt_existing_wc_dir/.svn")
{
die "$0: working copy '$opt_existing_wc_dir' does not have .svn ",
"directory.\n";
}
$opt_existing_wc_dir = Cwd::abs_path($opt_existing_wc_dir)
}
$repos_base_url =~ s|/*$||;
my $repos_base_uri = URI->new($repos_base_url);
if ($repos_load_rel_path ne '.' and -d $repos_load_rel_path)
{
warn "$0: import_dir '$repos_load_rel_path' is a directory.\n";
}
if (@ARGV)
{
my %dirs;
foreach my $dir (@ARGV)
{
unless (-e $dir)
{
die "$0: directory '$dir' does not exist.\n";
}
unless (-d _)
{
die "$0: directory '$dir' is not a directory.\n";
}
if ($dirs{$dir})
{
die "$0: directory '$dir' is listed more than once on command ",
"line.\n";
}
$dirs{$dir} = 1;
}
}
my %load_tags;
if (@ARGV and defined $opt_import_tag_location)
{
my %seen_tags;
foreach my $load_dir (@ARGV)
{
my $load_tag = &get_tag_dir($load_dir);
print "Directory $load_dir will be tagged as $load_tag\n";
if ($seen_tags{$load_tag})
{
die "$0: duplicate tag generated.\n";
}
$seen_tags{$load_tag} = 1;
$load_tags{$load_dir} = $load_tag;
}
exit 0 unless &get_answer("Please examine identified tags. Are they " .
"acceptable? (Y/n) ", 'ny', 1);
print "\n";
}
my @property_settings;
if (defined $property_config_filename and length $property_config_filename)
{
open(CFG, $property_config_filename)
or die "$0: cannot open '$property_config_filename' for reading: $!\n";
my $ok = 1;
while (my $line = <CFG>)
{
next if $line =~ /^\s*$/;
next if $line =~ /^\s*
my @line = &split_line($line);
next if @line == 0;
unless (@line == 2 or @line == 4)
{
warn "$0: line $. of '$property_config_filename' has to have 2 ",
"or 4 columns.\n";
$ok = 0;
next;
}
my ($regex, $control, $property_name, $property_value) = @line;
unless ($control eq 'break' or $control eq 'cont')
{
warn "$0: line $. of '$property_config_filename' has illegal ",
"value for column 3 '$control', must be 'break' or 'cont'.\n";
$ok = 0;
next;
}
my $re;
eval { $re = qr/$regex/i };
if ($@)
{
warn "$0: line $. of '$property_config_filename' regex '$regex' ",
"does not compile:\n$@\n";
$ok = 0;
next;
}
push(@property_settings, {name => $property_name,
value => $property_value,
control => $control,
re => $re});
}
close(CFG)
or warn "$0: error in closing '$property_config_filename' for ",
"reading: $!\n";
exit 1 unless $ok;
}
print "Checking that the base URL is a Subversion repository.\n";
read_from_process($svn, 'log', '-r', 'HEAD',
@svn_use_repos_cmd_opts, $repos_base_uri);
print "\n";
my $orig_cwd = cwd;
print "Finding the root URL of the Subversion repository.\n";
my $repos_root_uri;
my $repos_root_uri_path;
my $repos_base_path_segment;
{
my $r = $repos_base_uri->clone;
my @path_segments = grep { length($_) } $r->path_segments;
my @repos_base_path_segments = @path_segments;
unshift(@path_segments, '');
$r->path('');
my @r_path_segments;
while (@path_segments)
{
$repos_root_uri_path = shift @path_segments;
push(@r_path_segments, $repos_root_uri_path);
$r->path_segments(@r_path_segments);
if (safe_read_from_pipe($svn, 'log', '-r', 'HEAD',
@svn_use_repos_cmd_opts, $r) == 0)
{
$repos_root_uri = $r;
last;
}
shift @repos_base_path_segments;
}
$repos_base_path_segment = join('/', @repos_base_path_segments);
}
if ($repos_root_uri)
{
print "Determined that the svn root URL is $repos_root_uri.\n\n";
}
else
{
die "$0: cannot determine root svn URL.\n";
}
my $temp_dir = tempdir( "svn_load_dirs_XXXXXXXXXX", TMPDIR => 1 );
sub catch_signal {
my $signal = shift;
warn "$0: caught signal $signal. Quitting now.\n";
exit 1;
}
$SIG{HUP} = \&catch_signal;
$SIG{INT} = \&catch_signal;
$SIG{TERM} = \&catch_signal;
$SIG{PIPE} = \&catch_signal;
my $temp_dir_cleanup = Temp::Delete->new;
my $native_eol = &determine_native_eol;
{
print "Finding if any directories need to be created in repository.\n";
my @dirs_to_create;
my @urls_to_create;
my %seen_dir;
my @load_tags_without_last_segment;
foreach my $load_tag (sort values %load_tags)
{
my $index = rindex($load_tag, '/');
next if $index == -1;
push(@load_tags_without_last_segment, substr($load_tag, 0, $index));
}
foreach my $dir ($repos_load_rel_path, @load_tags_without_last_segment)
{
next unless length $dir;
my $d = '';
foreach my $segment (split('/', $dir))
{
$d = length $d ? "$d/$segment" : $segment;
my $url = "$repos_base_url/$d";
unless ($seen_dir{$d})
{
$seen_dir{$d} = 1;
if (safe_read_from_pipe($svn, 'log', '-r', 'HEAD',
@svn_use_repos_cmd_opts, $url) != 0)
{
push(@dirs_to_create, $d);
push(@urls_to_create, $url);
}
}
}
}
if (@dirs_to_create)
{
print "The following directories do not exist and need to exist:\n";
foreach my $dir (@dirs_to_create)
{
print " $dir\n";
}
exit 0 unless &get_answer("You must add them now to load the " .
"directories. Continue (Y/n)? ", 'ny', 1);
my $message = "Create directories to load project into.\n\n";
foreach my $dir (@dirs_to_create)
{
if (length $repos_base_path_segment)
{
$message .= "* $repos_base_path_segment/$dir: New directory.\n";
}
else
{
$message .= "* $dir: New directory.\n";
}
}
$message = wrap('', ' ', $message);
read_from_process($svn, 'mkdir', @svn_use_repos_cmd_opts,
'-m', $message, @urls_to_create);
}
else
{
print "No directories need to be created to prepare repository.\n";
}
}
if (defined $opt_existing_wc_dir)
{
print "Not checking out anything; using existing working directory at\n";
print "$opt_existing_wc_dir\n";
chdir($opt_existing_wc_dir)
or die "$0: cannot chdir '$opt_existing_wc_dir': $!\n";
read_from_process($svn, 'update', @svn_use_repos_cmd_opts);
}
else
{
my $checkout_dir_name = 'my_import_wc';
my $checkout_url;
if ($repos_load_rel_path eq '.')
{
$checkout_url = $repos_base_url;
}
else
{
$checkout_url = "$repos_base_url/$repos_load_rel_path";
}
print "Checking out $checkout_url into $temp_dir/$checkout_dir_name\n";
chdir($temp_dir)
or die "$0: cannot chdir '$temp_dir': $!\n";
read_from_process($svn, 'checkout',
@svn_use_repos_cmd_opts,
$checkout_url, $checkout_dir_name);
chdir($checkout_dir_name)
or die "$0: cannot chdir '$checkout_dir_name': $!\n";
}
my $wc_import_dir_cwd = cwd;
my $repos_load_abs_path;
if ($repos_load_rel_path eq '.')
{
$repos_load_abs_path = length($repos_base_path_segment) ?
$repos_base_path_segment : "/";
}
else
{
$repos_load_abs_path = length($repos_base_path_segment) ?
"$repos_base_path_segment/$repos_load_rel_path" :
$repos_load_rel_path;
}
my $print_rename_message = 1;
my @load_dirs = @ARGV;
while (defined (my $load_dir = &get_next_load_dir))
{
my $load_tag = $load_tags{$load_dir};
if (defined $load_tag)
{
print "\nLoading $load_dir and will save in tag $load_tag.\n";
}
else
{
print "\nLoading $load_dir.\n";
}
my %rename_from_files;
my %rename_to_files;
my @renamed_filenames;
unless ($opt_no_user_input)
{
my $repeat_loop;
do
{
$repeat_loop = 0;
my %add_files;
my %del_files;
%del_files = &recursive_ls_and_hash($wc_import_dir_cwd);
my $wanted = sub
{
s return if $_ eq '.';
my $source_path = $_;
my $dest_path = "$wc_import_dir_cwd/$_";
my ($source_type) = &file_info($source_path);
my ($dest_type) = &file_info($dest_path);
if ($dest_type ne '0' and $source_type ne $dest_type)
{
die "$0: does not handle changing source and destination ",
"type for '$source_path'.\n";
}
if ($source_type ne 'f' and $source_type ne 'd')
{
warn "$0: skipping loading file '$source_path' of type ",
"'$source_type'.\n";
unless ($opt_no_user_input)
{
print STDERR "Press return to continue: ";
<STDIN>;
}
return;
}
unless (defined delete $del_files{$source_path})
{
$add_files{$source_path}{type} = $source_type;
}
};
chdir($orig_cwd)
or die "$0: cannot chdir '$orig_cwd': $!\n";
chdir($load_dir)
or die "$0: cannot chdir '$load_dir': $!\n";
find({no_chdir => 1,
preprocess => sub { sort { $b cmp $a } @_ },
wanted => $wanted
}, '.');
my @add_files = sort keys %add_files;
my @del_files = sort keys %del_files;
&filter_renamed_files(\@del_files, \%rename_from_files);
chdir($wc_import_dir_cwd)
or die "$0: cannot chdir '$wc_import_dir_cwd': $!\n";
if (@add_files and @del_files)
{
my $max = @add_files > @del_files ? @add_files : @del_files;
my $line_number_width = 4;
my $filename_width = 0;
foreach my $f (@del_files)
{
my $l = length($f);
$filename_width = $l if $l > $filename_width;
}
++$filename_width;
my $printf_format = "%${line_number_width}d";
if ($print_rename_message)
{
$print_rename_message = 0;
print "\n",
"The following table lists files and directories that\n",
"exist in either the Subversion repository and the\n",
"directory to be imported but not both. You now have\n",
"the opportunity to match them up as renames instead\n",
"of deletes and adds. This is a Good Thing as it'll\n",
"make the repository take less space.\n\n",
"The left column lists files and directories that\n",
"exist in the Subversion repository and do not exist\n",
"in the directory being imported. The right column\n",
"lists files and directories that exist in the\n",
"directory being imported. Match up a deleted item\n",
"from the left column with an added item from the\n",
"right column. Note the line numbers on the left\n",
"which you type into this script to have a rename\n",
"performed.\n";
}
@add_files = map { $_->[0] }
sort { $a->[1] cmp $b->[1] }
map { [$_->[0], lc($_->[1])] }
map { [$_, m @add_files;
@del_files = map { $_->[0] }
sort { $a->[1] cmp $b->[1] }
map { [$_->[0], lc($_->[1])] }
map { [$_, m @del_files;
RELIST:
for (my $i=0; $i<$max; ++$i)
{
my $add_filename = '';
my $del_filename = '';
if ($i < @add_files)
{
$add_filename = $add_files[$i];
if ($add_files{$add_filename}{type} eq 'd')
{
$add_filename .= '/';
}
}
if ($i < @del_files)
{
$del_filename = $del_files[$i];
if ($del_files{$del_filename}{type} eq 'd')
{
$del_filename .= '/';
}
}
if ($i % 22 == 0)
{
print
"\n",
" " x $line_number_width,
" ",
"Deleted", " " x ($filename_width-length("Deleted")),
" ",
"Added\n";
}
printf $printf_format, $i;
print " ", $del_filename,
"_" x ($filename_width - length($del_filename)),
" ", $add_filename, "\n";
if (($i+1) % 22 == 0)
{
unless (&get_answer("Continue printing (Y/n)? ",
'ny', 1))
{
last;
}
}
}
my $line;
my $add_filename;
my $add_index;
my $del_filename;
my $del_index;
my $got_line = 0;
do {
print "Enter two indexes for each column to rename, ",
"(R)elist, or (F)inish: ";
$line = <STDIN>;
$line = '' unless defined $line;
if ($line =~ /^R$/i )
{
goto RELIST;
}
if ($line =~ /^F$/i)
{
$got_line = 1;
}
elsif ($line =~ /^(\d+)\s+(\d+)$/)
{
print "\n";
$del_index = $1;
$add_index = $2;
if ($del_index >= @del_files)
{
print "Delete index $del_index is larger than ",
"maximum index of ", scalar @del_files - 1,
".\n";
$del_index = undef;
}
if ($add_index > @add_files)
{
print "Add index $add_index is larger than maximum ",
"index of ", scalar @add_files - 1, ".\n";
$add_index = undef;
}
$got_line = defined $del_index && defined $add_index;
if ($got_line)
{
$add_filename = $add_files[$add_index];
$del_filename = $del_files[$del_index];
if ($add_files{$add_filename}{type} ne
$del_files{$del_filename}{type})
{
print "File types for $del_filename and ",
"$add_filename differ.\n";
$got_line = undef;
}
}
}
} until ($got_line);
if ($line !~ /^F$/i)
{
print "Renaming $del_filename to $add_filename.\n";
$repeat_loop = 1;
my $do_commit_now = 0;
foreach my $rename_to_filename (keys %rename_to_files)
{
if (contained_in($del_filename,
$rename_to_filename,
$rename_to_files{$rename_to_filename}{type}))
{
$do_commit_now = 1;
last;
}
}
if ($do_commit_now)
{
print "Now committing previously run renames.\n";
&commit_renames($load_dir,
\@renamed_filenames,
\%rename_from_files,
\%rename_to_files);
}
push(@renamed_filenames, $del_filename, $add_filename);
{
my $d = $del_files{$del_filename};
$rename_from_files{$del_filename} = $d;
$rename_to_files{$add_filename} = $d;
}
my @add_segments = split('/', $add_filename);
pop(@add_segments);
my $add_dir = '';
my @add_dirs;
foreach my $segment (@add_segments)
{
$add_dir = length($add_dir) ? "$add_dir/$segment" :
$segment;
unless (-d $add_dir)
{
push(@add_dirs, $add_dir);
}
}
if (@add_dirs)
{
read_from_process($svn, 'mkdir', @add_dirs);
}
read_from_process($svn, 'mv',
$del_filename, $add_filename);
}
}
} while ($repeat_loop);
}
if (@renamed_filenames)
{
&commit_renames($load_dir,
\@renamed_filenames,
\%rename_from_files,
\%rename_to_files);
}
my %add_files;
my %del_files = &recursive_ls_and_hash($wc_import_dir_cwd);
my %upd_files;
my $wanted = sub
{
s return if $_ eq '.';
my $source_path = $_;
my $dest_path = "$wc_import_dir_cwd/$_";
my ($source_type, $source_is_exe) = &file_info($source_path);
my ($dest_type) = &file_info($dest_path);
return if ($source_type ne 'd' and $source_type ne 'f');
if ($dest_type ne '0' and $source_type ne $dest_type)
{
die "$0: does not handle changing source and destination type ",
"for '$source_path'.\n";
}
my $del_info = delete $del_files{$source_path};
if (defined $del_info)
{
if (defined (my $del_digest = $del_info->{digest}))
{
my $new_digest = &digest_hash_file($source_path);
if ($new_digest ne $del_digest)
{
print "U $source_path\n";
$upd_files{$source_path} = $del_info;
}
}
}
else
{
print "A $source_path\n";
$add_files{$source_path}{type} = $source_type;
unless (defined $add_files{$source_path}{properties})
{
$add_files{$source_path}{properties} = [];
}
foreach my $property (@property_settings)
{
my $re = $property->{re};
if ($source_path =~ $re)
{
my $property_name = $property->{name};
my $property_value = $property->{value};
if (defined $property_name and defined $property_value)
{
if ($source_type eq 'd')
{
if ($property_name eq 'svn:eol-style' or
$property_name eq 'svn:executable' or
$property_name eq 'svn:keywords' or
$property_name eq 'svn:mime-type')
{
next;
}
}
if ($source_type eq 'f')
{
if ($property_name eq 'svn:externals' or
$property_name eq 'svn:ignore')
{
next;
}
}
print "Adding to '$source_path' property ",
"'$property_name' with value ",
"'$property_value'.\n";
push(@{$add_files{$source_path}{properties}},
$property);
}
last if $property->{control} eq 'break';
}
}
}
if ($source_is_exe)
{
print "Adding to '$source_path' property 'svn:executable' with ",
"value ''.\n";
my $property = {name => 'svn:executable', value => ''};
push (@{$add_files{$source_path}{properties}},
$property);
}
if ($source_type eq 'd')
{
if ($dest_type eq '0')
{
mkdir($dest_path)
or die "$0: cannot mkdir '$dest_path': $!\n";
}
}
elsif
($source_type eq 'f') {
if ($add_files{$source_path} or $upd_files{$source_path})
{
copy($source_path, $dest_path)
or die "$0: copy '$source_path' to '$dest_path': $!\n";
}
}
else
{
die "$0: does not handle copying files of type '$source_type'.\n";
}
};
chdir($orig_cwd)
or die "$0: cannot chdir '$orig_cwd': $!\n";
chdir($load_dir)
or die "$0: cannot chdir '$load_dir': $!\n";
find({no_chdir => 1,
preprocess => sub { sort { $b cmp $a } @_ },
wanted => $wanted
}, '.');
my $repeat_loop;
do
{
$repeat_loop = 0;
my @del_files = sort {length($a) <=> length($b) || $a cmp $b}
keys %del_files;
&filter_renamed_files(\@del_files, \%rename_from_files);
foreach my $file (@del_files)
{
if ($del_files{$file}{type} eq 'd')
{
my $dir = "$file/";
my $dir_length = length($dir);
foreach my $f (@del_files)
{
next if $file eq $f;
if (length($f) >= $dir_length and
substr($f, 0, $dir_length) eq $dir)
{
print "d $f\n";
delete $del_files{$f};
$repeat_loop = 1;
}
}
last if $repeat_loop;
}
}
} while ($repeat_loop);
my @del_files = sort {length($b) <=> length($a) || $a cmp $b }
keys %del_files;
&filter_renamed_files(\@del_files, \%rename_from_files);
foreach my $file (@del_files)
{
print "D $file\n";
}
chdir($wc_import_dir_cwd)
or die "$0: cannot chdir '$wc_import_dir_cwd': $!\n";
my @diff_ignore_space_changes;
if (keys %add_files)
{
my @add_files = sort {length($a) <=> length($b) || $a cmp $b}
keys %add_files;
my $target_filename = &make_targets_file(@add_files);
read_from_process($svn, 'add', '-N', '--targets', $target_filename);
unlink($target_filename);
foreach my $add_file (@add_files)
{
foreach my $property (@{$add_files{$add_file}{properties}})
{
my $property_name = $property->{name};
my $property_value = $property->{value};
if ($property_name eq 'svn:eol-style')
{
@diff_ignore_space_changes = ('-b');
}
my ($handle, $tmpfile) = tempfile(DIR => $temp_dir);
print $handle $property_value;
close($handle);
read_from_process($svn,
'propset',
$property_name,
'--file',
$tmpfile,
$add_file);
}
}
}
if (@del_files)
{
my $target_filename = &make_targets_file(@del_files);
read_from_process($svn, 'rm', '--targets', $target_filename);
unlink($target_filename);
}
if (keys %upd_files)
{
my @upd_files = sort {length($a) <=> length($b) || $a cmp $b}
keys %upd_files;
foreach my $upd_file (@upd_files)
{
my @command = ($svn, 'propget', 'svn:eol-style', $upd_file);
my @lines = read_from_process(@command);
next unless @lines;
if (@lines > 1)
{
warn "$0: '@command' returned more than one line of output: ",
"'@lines'.\n";
next;
}
my $eol_style = $lines[0];
if ($eol_style eq 'native')
{
@diff_ignore_space_changes = ('-b');
if (&convert_file_to_native_eol($upd_file))
{
print "Native eol-style conversion modified $upd_file.\n";
}
}
}
}
my $message = wrap('', '', "Load $load_dir into $repos_load_abs_path.\n");
read_from_process($svn, 'commit',
@svn_use_repos_cmd_opts,
'-m', $message);
read_from_process($svn, 'update');
if (@del_files)
{
rmtree(\@del_files, 1, 0);
}
if (defined $load_tag)
{
my $repos_tag_abs_path = length($repos_base_path_segment) ?
"$repos_base_path_segment/$load_tag" :
$load_tag;
my $from_url = $repos_load_rel_path eq '.' ?
$repos_load_rel_path :
"$repos_base_url/$repos_load_rel_path";
my $to_url = "$repos_base_url/$load_tag";
$message = wrap("",
"",
"Tag $repos_load_abs_path as " .
"$repos_tag_abs_path.\n");
read_from_process($svn, 'cp', @svn_use_repos_cmd_opts,
'-m', $message, $from_url, $to_url);
my $checkout_dir_name = "my_tag_wc_named_$load_tag";
print "Checking out $to_url into $temp_dir/$checkout_dir_name\n";
chdir($temp_dir)
or die "$0: cannot chdir '$temp_dir': $!\n";
read_from_process($svn, 'checkout',
@svn_use_repos_cmd_opts,
$to_url, $checkout_dir_name);
chdir($checkout_dir_name)
or die "$0: cannot chdir '$checkout_dir_name': $!\n";
chdir($orig_cwd)
or die "$0: cannot chdir '$orig_cwd': $!\n";
read_from_process('diff', '-u', @diff_ignore_space_changes,
'-x', '.svn',
'-r', $load_dir, "$temp_dir/$checkout_dir_name");
}
}
exit 0;
sub usage
{
warn "@_\n" if @_;
die "usage: $0 [options] svn_url svn_import_dir [dir_v1 [dir_v2 [..]]]\n",
" svn_url is the file:// or http:// URL of the svn repository\n",
" svn_import_dir is the path relative to svn_url where to load dirs\n",
" dir_v1 .. list dirs to import otherwise read from stdin\n",
"options are\n",
" -no_user_input don't ask yes/no questions and assume yes answer\n",
" -p filename table listing properties to apply to matching files\n",
" -svn_username username to perform commits as\n",
" -svn_password password to supply to svn commit\n",
" -t tag_dir create a tag copy in tag_dir, relative to svn_url\n",
" -v increase program verbosity, multiple -v's allowed\n",
" -wc path use the already checked-out working copy at path\n",
" instead of checkout out a fresh working copy\n";
}
my $get_next_load_dir_init = 0;
my @get_next_load_dirs;
sub get_next_load_dir
{
if (@ARGV)
{
unless ($get_next_load_dir_init)
{
$get_next_load_dir_init = 1;
@get_next_load_dirs = @ARGV;
}
return shift @get_next_load_dirs;
}
if ($opt_verbose)
{
print "Waiting for next directory to import on standard input:\n";
}
my $line = <STDIN>;
print "\n" if $opt_verbose;
chomp $line;
if ($line =~ m|(\S+)\s+(\S+)|)
{
$line = $1;
set_svn_use_repos_cmd_opts($2, $opt_svn_password);
}
$line;
}
use vars qw($INCORRECT_NUMBER_OF_ARGS);
$INCORRECT_NUMBER_OF_ARGS = "passed incorrect number of arguments.\n";
my $make_targets_file_counter;
sub make_targets_file
{
unless (@_)
{
confess "$0: make_targets_file $INCORRECT_NUMBER_OF_ARGS";
}
$make_targets_file_counter = 1 unless defined $make_targets_file_counter;
my $filename = sprintf "%s/targets.%05d",
$temp_dir,
$make_targets_file_counter;
++$make_targets_file_counter;
open(TARGETS, ">$filename")
or die "$0: cannot open '$filename' for writing: $!\n";
foreach my $file (@_)
{
print TARGETS "$file\n";
}
close(TARGETS)
or die "$0: error in closing '$filename' for writing: $!\n";
$filename;
}
sub set_svn_use_repos_cmd_opts
{
unless (@_ == 2)
{
confess "$0: set_svn_use_repos_cmd_opts $INCORRECT_NUMBER_OF_ARGS";
}
my $username = shift;
my $password = shift;
@svn_use_repos_cmd_opts = ();
if (defined $username and length $username)
{
push(@svn_use_repos_cmd_opts, '--username', $username);
}
if (defined $password)
{
push(@svn_use_repos_cmd_opts, '--password', $password);
}
}
sub get_tag_dir
{
unless (@_ == 1)
{
confess "$0: get_tag_dir $INCORRECT_NUMBER_OF_ARGS";
}
my $load_dir = shift;
my $tag_location = $opt_import_tag_location;
my $load_tag = '';
while ((my $i = index($tag_location, $REGEX_SEP_CHAR)) >= 0)
{
$load_tag .= substr($tag_location, 0, $i, '');
substr($tag_location, 0, 1, '');
my $j = index($tag_location, $REGEX_SEP_CHAR);
if ($j < 0)
{
die "$0: -t value '$opt_import_tag_location' does not have ",
"matching $REGEX_SEP_CHAR.\n";
}
my $regex = substr($tag_location, 0, $j, '');
$regex = "($regex)" unless ($regex =~ /\(.+\)/);
substr($tag_location, 0, 1, '');
my @results = $load_dir =~ m/$regex/;
$load_tag .= join('', @results);
}
$load_tag .= $tag_location;
$load_tag;
}
sub file_info
{
lstat(shift) or return ('0', '');
-b _ and return ('b', '');
-c _ and return ('c', '');
-d _ and return ('d', '');
-f _ and return ('f', -x _);
-l _ and return ('l', '');
-p _ and return ('p', '');
-S _ and return ('S', '');
return '?';
}
my $openfork_available = "MSWin32" ne $OSNAME;
sub safe_read_from_pipe
{
unless (@_)
{
croak "$0: safe_read_from_pipe $INCORRECT_NUMBER_OF_ARGS";
}
if ($openfork_available)
{
print "Running @_\n";
my $pid = open(SAFE_READ, "-|");
unless (defined $pid)
{
die "$0: cannot fork: $!\n";
}
unless ($pid)
{
open(STDERR, ">&STDOUT")
or die "$0: cannot dup STDOUT: $!\n";
exec(@_)
or die "$0: cannot exec '@_': $!\n";
}
}
else
{
my @commandline = ();
my $command;
my $comment;
while ($command = shift)
{
if ("-m" eq $command)
{
my $comment = shift;
my ($handle, $tmpfile) = tempfile(DIR => $temp_dir);
print $handle $comment;
close($handle);
push(@commandline, "--file");
push(@commandline, $tmpfile);
}
else
{
$command =~ s/\"/\\\"/g;
if ($command =~ m"\s") { $command = "\"$command\""; }
if ($command eq "") { $command = "\"\""; }
if ($command =~ m"\n")
{
warn "$0: carriage return detected in command - may not work\n";
}
push(@commandline, $command);
}
}
print "Running @commandline\n";
if ( $comment ) { print $comment; }
open(SAFE_READ, "@commandline |")
or die "$0: cannot pipe to command: $!\n";
}
my @output;
while (<SAFE_READ>)
{
chomp;
push(@output, $_);
}
close(SAFE_READ);
my $result = $?;
my $exit = $result >> 8;
my $signal = $result & 127;
my $cd = $result & 128 ? "with core dump" : "";
if ($signal or $cd)
{
warn "$0: pipe from '@_' failed $cd: exit=$exit signal=$signal\n";
}
if (wantarray)
{
return ($result, @output);
}
else
{
return $result;
}
}
sub read_from_process
{
unless (@_)
{
croak "$0: read_from_process $INCORRECT_NUMBER_OF_ARGS";
}
my ($status, @output) = &safe_read_from_pipe(@_);
if ($status)
{
print STDERR "$0: @_ failed with this output:\n", join("\n", @output),
"\n";
unless ($opt_no_user_input)
{
print STDERR
"Press return to quit and clean up svn working directory: ";
<STDIN>;
}
exit 1;
}
else
{
return @output;
}
}
sub recursive_ls_and_hash
{
unless (@_ == 1)
{
croak "$0: recursive_ls_and_hash $INCORRECT_NUMBER_OF_ARGS";
}
my $dir = shift;
my $return_cwd = cwd;
chdir($dir)
or die "$0: cannot chdir '$dir': $!\n";
my %files;
my $wanted = sub
{
s return if $_ eq '.';
my ($file_type) = &file_info($_);
my $file_digest;
if ($file_type eq 'f' or ($file_type eq 'l' and stat($_) and -f _))
{
$file_digest = &digest_hash_file($_);
}
$files{$_} = {type => $file_type,
digest => $file_digest};
};
find({no_chdir => 1,
preprocess => sub { grep { $_ !~ /\.svn$/ } @_ },
wanted => $wanted
}, '.');
chdir($return_cwd)
or die "$0: cannot chdir '$return_cwd': $!\n";
%files;
}
sub commit_renames
{
unless (@_ == 4)
{
croak "$0: commit_renames $INCORRECT_NUMBER_OF_ARGS";
}
my $load_dir = shift;
my $renamed_filenames = shift;
my $rename_from_files = shift;
my $rename_to_files = shift;
my $number_renames = @$renamed_filenames/2;
my $message = "To prepare to load $load_dir into $repos_load_abs_path, " .
"perform $number_renames rename" .
($number_renames > 1 ? "s" : "") . ".\n";
$message = wrap("", "", $message) . "\n";
while (@$renamed_filenames)
{
my $from = "$repos_load_abs_path/" . shift @$renamed_filenames;
my $to = "$repos_load_abs_path/" . shift @$renamed_filenames;
$message .= wrap("", " ", "* $to: Renamed from $from.\n");
}
my $cwd = cwd;
chdir($wc_import_dir_cwd)
or die "$0: cannot chdir '$wc_import_dir_cwd': $!\n";
read_from_process($svn, 'commit', @svn_use_repos_cmd_opts, '-m', $message);
read_from_process($svn, 'update', @svn_use_repos_cmd_opts);
chdir($cwd)
or die "$0: cannot chdir '$cwd': $!\n";
my @del_files = sort {length($b) <=> length($a) || $a cmp $b }
keys %$rename_from_files;
rmtree(\@del_files, 1, 0);
undef %$rename_from_files;
undef %$rename_to_files;
}
sub contained_in
{
unless (@_ == 3)
{
croak "$0: contain_in $INCORRECT_NUMBER_OF_ARGS";
}
my $contained = shift;
my $container = shift;
my $container_type = shift;
if ($container eq $contained)
{
return 1;
}
if ($container_type eq 'd')
{
my $dirname = "$container/";
my $dirname_length = length($dirname);
if ($dirname_length <= length($contained) and
$dirname eq substr($contained, 0, $dirname_length))
{
return 1;
}
}
return 0;
}
sub filter_renamed_files
{
unless (@_ == 2)
{
croak "$0: filter_renamed_files $INCORRECT_NUMBER_OF_ARGS";
}
my $array_ref = shift;
my $hash_ref = shift;
foreach my $remove_filename (keys %$hash_ref)
{
my $remove_file_type = $hash_ref->{$remove_filename}{type};
for (my $i=0; $i<@$array_ref;)
{
if (contained_in($array_ref->[$i],
$remove_filename,
$remove_file_type))
{
splice(@$array_ref, $i, 1);
next;
}
++$i;
}
}
}
sub digest_hash_file
{
unless (@_ == 1)
{
croak "$0: digest_hash_file $INCORRECT_NUMBER_OF_ARGS";
}
my $filename = shift;
my $ctx = Digest::MD5->new;
if (open(READ, $filename))
{
binmode READ;
$ctx->addfile(*READ);
close(READ);
}
else
{
die "$0: cannot open '$filename' for reading: $!\n";
}
$ctx->digest;
}
sub get_answer
{
unless (@_ == 3)
{
croak "$0: get_answer $INCORRECT_NUMBER_OF_ARGS";
}
my $message = shift;
my $answers = shift;
my $def_ans = shift;
return $def_ans if $opt_no_user_input;
my $char;
do
{
print $message;
$char = '';
my $line = <STDIN>;
if (defined $line and length $line)
{
$char = substr($line, 0, 1);
$char = '' if $char eq "\n";
}
} until $char eq '' or $answers =~ /$char/ig;
return $def_ans if $char eq '';
return pos($answers) - 1;
}
sub determine_native_eol
{
my $filename = "$temp_dir/svn_load_dirs_eol_test.$$";
if (-e $filename)
{
unlink($filename)
or die "$0: cannot unlink '$filename': $!\n";
}
open(NL_TEST, ">$filename")
or die "$0: cannot open '$filename' for writing: $!\n";
print NL_TEST "\n";
close(NL_TEST)
or die "$0: error in closing '$filename' for writing: $!\n";
open(NL_TEST, $filename)
or die "$0: cannot open '$filename' for reading: $!\n";
binmode NL_TEST;
local $/;
undef $/;
my $eol = <NL_TEST>;
close(NL_TEST)
or die "$0: cannot close '$filename' for reading: $!\n";
unlink($filename)
or die "$0: cannot unlink '$filename': $!\n";
my $eol_length = length($eol);
unless ($eol_length)
{
die "$0: native eol length on this system is 0.\n";
}
print "Native EOL on this system is ";
for (my $i=0; $i<$eol_length; ++$i)
{
printf "\\%03o", ord(substr($eol, $i, 1));
}
print ".\n\n";
$eol;
}
sub convert_file_to_native_eol
{
unless (@_ == 1)
{
croak "$0: convert_file_to_native_eol $INCORRECT_NUMBER_OF_ARGS";
}
my $filename = shift;
open(FILE, $filename)
or die "$0: cannot open '$filename' for reading: $!\n";
binmode FILE;
local $/;
undef $/;
my $in = <FILE>;
close(FILE)
or die "$0: error in closing '$filename' for reading: $!\n";
my $out = '';
my $i = 0;
while ($i < length($in))
{
my $cc = substr($in, $i, 2);
if ($cc eq "\015\012")
{
$out .= $native_eol;
$i += 2;
next;
}
my $c = substr($cc, 0, 1);
if ($c eq "\012" or $c eq "\015")
{
$out .= $native_eol;
}
else
{
$out .= $c;
}
++$i;
}
return 0 if $in eq $out;
my $tmp_filename = ".svn/tmp/svn_load_dirs.$$";
open(FILE, ">$tmp_filename")
or die "$0: cannot open '$tmp_filename' for writing: $!\n";
binmode FILE;
print FILE $out;
close(FILE)
or die "$0: cannot close '$tmp_filename' for writing: $!\n";
rename($tmp_filename, $filename)
or die "$0: cannot rename '$tmp_filename' to '$filename': $!\n";
return 1;
}
sub split_line
{
unless (@_ == 1)
{
croak "$0: split_line $INCORRECT_NUMBER_OF_ARGS";
}
my $line = shift;
$line =~ s/^\s+//;
my $line_length = length $line;
my @words = ();
my $current_word = '';
my $in_quote = '';
my $in_protect = '';
my $in_space = '';
my $i = 0;
while ($i < $line_length)
{
my $c = substr($line, $i, 1);
++$i;
if ($in_protect)
{
if ($c eq $in_quote)
{
$current_word .= $c;
}
elsif ($c eq '"' or $c eq "'")
{
$current_word .= $c;
}
else
{
$current_word .= "$in_protect$c";
}
$in_protect = '';
}
elsif ($c eq '\\')
{
$in_protect = $c;
}
elsif ($in_quote)
{
if ($c eq $in_quote)
{
$in_quote = '';
}
else
{
$current_word .= $c;
}
}
elsif ($c eq '"' or $c eq "'")
{
$in_quote = $c;
}
elsif ($c =~ m/^\s$/)
{
unless ($in_space)
{
push(@words, $current_word);
$current_word = '';
}
}
else
{
$current_word .= $c;
}
$in_space = $c =~ m/^\s$/;
}
$current_word .= $in_protect if $in_protect;
push(@words, $current_word) if length $current_word;
@words;
}
package Temp::Delete;
sub new
{
bless {}, shift;
}
sub DESTROY
{
print "Cleaning up $temp_dir\n";
File::Path::rmtree([$temp_dir], 0, 0);
}