3 # This script copies modified files from a source CVS tree to a destination
4 # tree. Modified files are detected by comparing their modification dates
5 # with the CVS Entries file.
7 # Modified files are copied in their entirety to the destination tree
8 # (no diffing is done). Files are only copied of the CVS version of the
9 # file is the same in both trees. If the destination file is modified
10 # already, it is backed up and replaced.
12 # To use this on your tree/platform, do the following:
14 # 1. Fix the !perl line, if necessary.
15 # 2. Fix $dirsep to be the directory separator on your platform.
16 # 3. Uncomment the appropriate $dst_linebreaks file specify what linebreaks
17 # you want for the copied files. This variable defines the *destination* linebreaks
18 # that you want your changes to be converted to.
19 # For example, if you have a linux volume
20 # mounted (via SAMBA perhaps) to your windows box where you've made changes to
21 # source files, you'd want $dst_linebreaks to be set for unix. This ensures that
22 # linebreaks are converted to the appropriate OS linebreak scheme for your *target* tree.
23 # 4. Set $src_tree and $dest_tree to point to the directories you want
24 # to sync up. These don't have to point to the root of the tree,
25 # but should be equivalent directories in the two trees.
28 # Simon Fraser <sfraser@netscape.com>
33 # change for your platform ('\' == windows, ':' == mac, '/' == unix)
36 # Set this to the native OS of the *destination* tree
37 # $dst_linebreaks = pack("cc", 13); # Mac
38 #$dst_linebreaks = pack("cc", 13, 10); # DOS
39 $dst_linebreaks = pack("cc", 10); # UNIX
41 #change for your src and dest trees
42 $src_tree = "x:\\mozilla\\xpfe\\components";
43 $dest_tree = "h:\\builds\\mozilla\\xpfe\\components";
46 #//--------------------------------------------------------------------------------------------------
47 #// _copyFile. Copy file from src to dest, converting linebreaks if necessary
48 #//--------------------------------------------------------------------------------------------------
49 sub _copyFile
($;$;$;$)
51 my($srcdir, $destdir, $file, $backup) = @_;
53 my($srcfile) = $srcdir.$dirsep.$file;
54 my($dstfile) = $destdir.$dirsep.$file;
60 while (-f
$dstfile."-".$counter)
65 rename($dstfile, $dstfile."-".$counter) or die "Failed to rename file\n";
68 print "Copying $file over to dest\n";
70 my($newdest) = $dstfile."_temp";
72 open(SRCFILE
, "< $srcfile") or die "Can't open source file $srcfile\n";
73 open(NEWDEST
, "> $newdest") or die "Can't open dest file $newdest\n";
78 print NEWDEST
$_.$dst_linebreaks;
85 unlink($dstfile) or die "Failed to remove $dstfile\n";
87 rename($newdest, $dstfile) or die "Failed to rename $newdest\n";
91 #//--------------------------------------------------------------------------------------------------
92 #// _readCVSInfo. Suck in the CVS info from the Entries file
93 #//--------------------------------------------------------------------------------------------------
99 my($entries_file_name) = $cvsDir.$dirsep."CVS".$dirsep."Entries";
101 # print "Reading $entries_file_name\n";
102 open(ENTRIES
, $entries_file_name) || die "Could not open file $entries_file_name";
106 # Read in the path if available
107 while (defined ($line = <ENTRIES
>))
111 #parse out the line. Format is:
112 # files: /filename/version/date/options/tag
113 # dirs: D/dirname////
115 # because we might be reading an entries file from another platform, with
116 # different linebreaks, be anal about cleaning up $line.
118 if ($line =~ /^?\/(.+)\
/(.+)\/(.+)\
/(.*)\/(.*)?
$/)
127 "version" => $version,
129 "options" => $options,
133 # print $filename." ".$version." ".$date." ".$options." ".$tag."\n";
134 $cvs_entries{$filename} = \
%cvs_file;
144 #//--------------------------------------------------------------------------------------------------
145 #// _fileIsModified. compare mod date with CVS entries to see if a file is modified
146 #//--------------------------------------------------------------------------------------------------
147 sub _fileIsModified
($;$;$)
149 my($entries, $dir, $file) = @_;
151 my($abs_file) = $dir.$dirsep.$file;
153 if (exists($entries->{$file}))
155 my($date) = $entries->{$file}->{"date"};
157 # stat the file to get its date
158 my($file_data) = stat($abs_file) || die "Could not stat $file\n";
159 my($mod_string) = scalar(gmtime($file_data->mtime));
161 return ($mod_string ne $date);
170 #//--------------------------------------------------------------------------------------------------
171 #// _processFile. args: entries hash, dir, filename
172 #//--------------------------------------------------------------------------------------------------
174 sub _processFile
($;$;$;$;$)
176 my($src_entries, $dest_entries, $srcdir, $destdir, $file) = @_;
178 my($abs_file) = $srcdir.$dirsep.$file;
180 if (exists($src_entries->{$file}))
182 my($file_entry) = $src_entries->{$file};
183 my($version) = $file_entry->{"version"};
185 if (_fileIsModified
($src_entries, $srcdir, $file))
187 my($rel_file) = $abs_file;
188 $rel_file =~ s/^$src_tree//;
190 # print "¥ÊFile $rel_file is modified\n";
192 # check CVS version in dest
193 my($dest_entry) = $dest_entries->{$file};
196 my($dest_version) = $dest_entry->{"version"};
197 my($versions_match) = ($version == $dest_version);
198 my($dest_modified) = _fileIsModified
($dest_entries, $destdir, $file);
202 # ok, we can copy the file over now, backing up dest if it is modified
203 _copyFile
($srcdir, $destdir, $file, $dest_modified);
207 print "File $rel_file is version $version in the src tree, but $dest_version in dest. This file will not be copied.\n";
212 print "No CVS entry found in destination tree for $rel_file\n";
218 print "No entry for file $file\n";
223 #//--------------------------------------------------------------------------------------------------
224 #// _traverseDir. Traverse one dir, recurse for each found dir.
225 #//--------------------------------------------------------------------------------------------------
227 sub _traverseDir
($;$)
229 my($srcdir, $destdir) = @_;
231 opendir(DIR
, $srcdir) or die "Cannot open dir $srcdir\n";
232 my @files = readdir(DIR
);
235 # suck in the CVS info for this dir, if there is a CVS dir
236 unless (-e
$srcdir.$dirsep."CVS".$dirsep."Entries" && -e
$destdir.$dirsep."CVS".$dirsep."Entries") {
237 print "$srcdir is not a CVS directory in both source and dest\n";
241 print " Doing $srcdir\n";
243 my(%src_entries) = _readCVSInfo
($srcdir);
244 my(%dest_entries) = _readCVSInfo
($destdir);
247 foreach $file (@files)
249 my $filepath = $srcdir.$dirsep.$file;
253 if ($file ne "CVS") # ignore 'CVS' dirs
255 # print "Going into $filepath\n";
256 _traverseDir
($filepath, $destdir.$dirsep.$file);
262 _processFile
(\
%src_entries, \
%dest_entries, $srcdir, $destdir, $file);
267 #//--------------------------------------------------------------------------------------------------
269 #//--------------------------------------------------------------------------------------------------
270 sub MigrateChanges
($;$)
272 my($srcdir, $destdir) = @_;
274 # Check that src and dest exist
276 die "Source directory $srcdir does not exist\n";
280 die "Dest directory $destdir does not exist\n";
283 print "---------------------------------------------------------\n";
284 print "Migrating changes from\n $srcdir\nto\n $destdir\n";
285 print "---------------------------------------------------------\n";
286 _traverseDir
($srcdir, $destdir);
287 print "---------------------------------------------------------\n";
291 MigrateChanges
($src_tree, $dest_tree);