-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvn_load_dirs.in
2072 lines (1819 loc) · 66 KB
/
svn_load_dirs.in
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
# Copyright (c) 2002,2003,2004,2005,2006,2007,2009 Dolby. All rights reserved.
#
# Licensed under the Academic Free License version 3.0. See LICENSE_AFL3.txt
# or http://www.opensource.org/licenses/academic.php for a copy of the license
# text.
# $HeadURL$
# $LastChangedDate$
# $LastChangedBy$
# $LastChangedRevision$
$| = 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;
use version;
$Text::Wrap::columns = 72;
# Specify the location of the svn command.
my $svn = '@SVN_BINDIR@/svn';
# Process the command line options.
# The base URL for the portion of the repository to work in. Note
# that this does not have to be the root of the subversion repository,
# it can point to a subdirectory in the repository.
my $repos_base_url;
# The relative path from the repository base URL to work in to the
# directory to load the input directories into.
my $repos_load_rel_path;
# To specify where tags, which are simply copies of the imported
# directory, should be placed relative to the repository base URL, use
# the -t command line option. This value must contain regular
# expressions that match portions of the input directory names to
# create an unique tag for each input directory. The regular
# expressions are surrounded by a specified character to distinguish
# the regular expression from the normal directory path.
my $opt_import_tag_location;
# Do not ask for any user input. Just go ahead and do everything.
my $opt_no_user_input;
# Do not automatically set the svn:executable property based on the
# file's exe bit.
my $opt_no_auto_exe;
# Username to use for commits.
my $opt_svn_username;
# Password to use for commits.
my $opt_svn_password;
# Verbosity level.
my $opt_verbose;
# Path to already checked-out working copy.
my $opt_existing_wc_dir;
# List of filename patterns to ignore (as in .subversion/config's
# "global-ignores" option).
my $opt_glob_ignores;
# Message to use for the final commit.
my $opt_final_commit_msg;
# This is the character used to separate regular expressions occuring
# in the tag directory path from the path itself.
my $REGEX_SEP_CHAR = '@';
# This specifies a configuration file that contains a list of regular
# expressions to check against a file and the properties to set on
# matching files.
my $property_config_filename;
GetOptions('no_user_input' => \$opt_no_user_input,
'no_auto_exe' => \$opt_no_auto_exe,
'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,
'glob_ignores=s' => \$opt_glob_ignores,
'message=s' => \$opt_final_commit_msg)
or &usage;
&usage("$0: too few arguments") if @ARGV < 2;
$repos_base_url = shift;
$repos_load_rel_path = shift;
# Check that the repository base URL and the import directories do not
# contain any ..'s.
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 there are no directories listed on the command line, then the
# directories are read from standard input. In this case, the
# -no_user_input command line option must be specified.
if (!@ARGV and !$opt_no_user_input)
{
&usage("$0: must use -no_user_input if no dirs listed on command line.");
}
# The tag option cannot be used when directories are read from
# standard input because tags may collide and no user input can be
# taken to verify that the input is ok.
if (!@ARGV and $opt_import_tag_location)
{
&usage("$0: cannot use -tag_location when dirs are read from stdin.");
}
# If the tag directory is set, then the import directory cannot be '.'.
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.");
}
# Set the svn command line options that are used anytime svn connects
# to the repository.
my @svn_use_repos_cmd_opts;
&set_svn_use_repos_cmd_opts($opt_svn_username, $opt_svn_password);
# Check that the tag directories do not contain any ..'s. Also, the
# import and tag directories cannot be absolute.
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)
}
# If no glob_ignores specified, try to deduce from config file,
# or use the default below.
my $ignores_str =
'*.o *.lo *.la #*# .*.rej *.rej .*~ *~ .#* .DS_Store';
if ( defined $opt_glob_ignores)
{
$ignores_str = $opt_glob_ignores;
}
elsif ( -f "$ENV{HOME}/.subversion/config" )
{
open my $conf, "$ENV{HOME}/.subversion/config";
while (<$conf>)
{
if ( /^global-ignores\s*=\s*(.*?)\s*$/ )
{
$ignores_str = $1;
last;
}
}
}
my @glob_ignores = map
{
s/\./\\\./g; s/\*/\.\*/g; "^$_\$";
} split(/\s+/, $ignores_str);
unshift @glob_ignores, '\.svn$';
# Convert the string URL into a URI object.
$repos_base_url =~ s|/*$||;
my $repos_base_uri = URI->new($repos_base_url);
# Check that $repos_load_rel_path is not a directory here implying
# that a command line option was forgotten.
if ($repos_load_rel_path ne '.' and -d $repos_load_rel_path)
{
die "$0: import_dir '$repos_load_rel_path' is a directory.\n";
}
# The remaining command line arguments should be directories. Check
# that they all exist and that there are no duplicates.
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;
}
}
# Create the tag locations and print them for the user to review.
# Check that there are no duplicate tags.
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";
}
# Load the property configuration filename, if one was specified, into
# an array of hashes, where each hash contains a regular expression
# and a property to apply to the file if the regular expression
# matches.
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*#/;
# Split the input line into words taking into account that
# single or double quotes may define a single word with
# whitespace in it. The format for the file is
# regex control property_name property_value
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;
}
# Compile the regular expression.
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;
}
# Check that the svn base URL works by running svn log on it. Only
# get the HEAD revision log message; there's no need to waste
# bandwidth seeing all of the log messages.
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;
# The first step is to determine the root of the svn repository. Do
# this with the svn log command. Take the svn_url hostname and port
# as the initial url and append to it successive portions of the final
# path until svn log succeeds.
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";
}
# Create a temporary directory for svn to work in.
my $temp_dir = tempdir( "svn_load_dirs_XXXXXXXXXX", TMPDIR => 1 );
# Put in a signal handler to clean up any temporary directories.
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;
# Create an object that when DESTROY'ed will delete the temporary
# directory. The CLEANUP flag to tempdir should do this, but they
# call rmtree with 1 as the last argument which takes extra security
# measures that do not clean up the .svn directories.
my $temp_dir_cleanup = Temp::Delete->new;
# Determine the native end of line style for this system. Do this the
# most portable way, by writing a file with a single \n in non-binary
# mode and then reading the file in binary mode.
my $native_eol = &determine_native_eol;
# Check if all the directories exist to load the directories into the
# repository. If not, ask if they should be created. For tags, do
# not create the tag directory itself, that is done on the svn cp.
{
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;
# Assume that the last portion of the tag directory contains the
# version number and remove it from the directories to create,
# because the tag directory will be created by svn cp.
foreach my $load_tag (sort values %load_tags)
{
# Skip this tag if there is only one segment in its name.
my $index = rindex($load_tag, '/');
next if $index == -1;
# Trim off the last segment and record the result.
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";
}
}
# Either checkout a new working copy from the repository or use an
# existing working copy.
if (defined $opt_existing_wc_dir)
{
# Update an already existing working copy.
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
{
# Check out the svn repository starting at the svn URL into a
# fixed directory name.
my $checkout_dir_name = 'my_import_wc';
# Check out only the directory being imported to, otherwise the
# checkout of the entire base URL can be very huge, if it contains
# a large number of tags.
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";
}
# At this point, the current working directory is the top level
# directory of the working copy. Record the absolute path to this
# location because the script will chdir back here later on.
my $wc_import_dir_cwd = cwd;
# Set up the names for the path to the import and tag directories.
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;
}
# Now go through each source directory and copy each file from the
# source directory to the target directory. For new target files, add
# them to svn. For files that no longer exist, delete them.
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";
}
# The first hash is keyed by the old name in a rename and the
# second by the new name. The last variable contains a list of
# old and new filenames in a rename.
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;
# Get the list of files and directories in the repository
# working copy. This hash is called %del_files because
# each file or directory will be deleted from the hash
# using the list of files and directories in the source
# directory, leaving the files and directories that need
# to be deleted.
%del_files = &recursive_ls_and_hash($wc_import_dir_cwd);
# This anonymous subroutine finds all the files and
# directories in the directory to load. It notes the file
# type and for each file found, it deletes it from
# %del_files.
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);
# Fail if the destination type exists but is of a
# different type of file than the source type.
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 'd' and
$source_type ne 'f' and
$source_type ne 'l')
{
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;
}
};
# Now change into the directory containing the files to
# load. First change to the original directory where this
# script was run so that if the specified directory is a
# relative directory path, then the script can change into
# it.
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 }
grep { $_ !~ /^[._]svn$/ } @_ },
wanted => $wanted
}, '.');
# At this point %add_files contains the list of new files
# and directories to be created in the working copy tree
# and %del_files contains the files and directories that
# need to be deleted. Because there may be renames that
# have taken place, give the user the opportunity to
# rename any deleted files and directories to ones being
# added.
my @add_files = sort keys %add_files;
my @del_files = sort keys %del_files;
# Because the source code management system may keep the
# original renamed file or directory in the working copy
# until a commit, remove them from the list of deleted
# files or directories.
&filter_renamed_files(\@del_files, \%rename_from_files);
# Now change into the working copy directory in case any
# renames need to be performed.
chdir($wc_import_dir_cwd)
or die "$0: cannot chdir '$wc_import_dir_cwd': $!\n";
# Only do renames if there are both added and deleted
# files and directories.
if (@add_files and @del_files)
{
my $max = @add_files > @del_files ? @add_files : @del_files;
# Print the files that have been added and deleted.
# Find the deleted file with the longest name and use
# that for the width of the filename column. Add one
# to the filename width to let the directory /
# character be appended to a directory name.
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 or 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";
}
# Sort the added and deleted files and directories by
# the lowercase versions of their basenames instead of
# their complete path, which makes finding files that
# were moved into different directories easier to
# match up.
@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;
}
}
}
# Get the feedback from the user.
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;
# Check that the file or directory to be renamed
# has the same file type.
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;
# Because subversion cannot rename the same file
# or directory twice, which includes doing a
# rename of a file in a directory that was
# previously renamed, a commit has to be
# performed. Check if the file or directory being
# renamed now would cause such a problem and
# commit if so.
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;
}
# Check that any required directories to do the
# rename exist.
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 there are any renames that have not been committed, then do
# that now.
if (@renamed_filenames)
{
&commit_renames($load_dir,
\@renamed_filenames,
\%rename_from_files,
\%rename_to_files);
}
# At this point all renames have been performed. Now get the
# final list of files and directories in the working copy
# directory. The %add_files hash will contain the list of files
# and directories to add to the working copy and %del_files starts
# with all the files already in the working copy and gets files
# removed that are in the imported directory, which results in a
# list of files that should be deleted. %upd_files holds the list
# of files that have been updated.
my %add_files;
my %del_files = &recursive_ls_and_hash($wc_import_dir_cwd);
my %upd_files;
# This anonymous subroutine copies files from the source directory
# to the working copy directory.
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' and
$source_type ne 'l');
# Fail if the destination type exists but is of a different
# type of file than the source type.
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";
}
# Determine if the file is being added or is an update to an
# already existing file using the file's digest.
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;
# Create an array reference to hold the list of properties
# to apply to this object.
unless (defined $add_files{$source_path}{properties})
{
$add_files{$source_path}{properties} = [];
}
# Go through the list of properties for a match on this
# file or directory and if there is a match, then apply
# the property to it.
foreach my $property (@property_settings)
{
my $re = $property->{re};
if ($source_path =~ $re)
{
my $property_name = $property->{name};