/
git-to-cvs
executable file
·406 lines (358 loc) · 10.4 KB
/
git-to-cvs
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
#!/usr/bin/perl -w
#
# Apply git commits to a CVS tree.
#
# Copyright 2005, 2006 Alexandre Julliard
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
#
# Usage: git-to-cvs [branch]
#
# Optional parameters:
# branch: where to get the git commits from, default is 'master'
#
# This script needs to be run from the root of the checked-out CVS tree,
# with CVS/.git (or GIT_DIR) pointing to the git repository.
#
use Unicode::String qw(latin1 utf8);
use File::Find;
my %tags = ();
# patterns to skip in .gitignore because they are ignored by default in CVS
my %ignore_patterns =
(
"*.[oa]" => 1,
"*.o" => 1,
"*.a" => 1,
"*.orig" => 1,
"*.rej" => 1,
"*.so" => 1,
"*~" => 1,
"tags" => 1,
"TAGS" => 1
);
my $branch = $ARGV[0] || "master";
my $last_commit = "last-cvs-commit/$branch";
# run a shell command and die on error
sub shell(@)
{
my @args = @_;
system(@args) == 0 or die "system @args failed: $?";
}
# run a shell command and return the first line of output
sub shell_output(@)
{
my @args = @_;
open OUT, "-|" or exec @args or die join(' ', "exec", @args);
my $ret = <OUT>;
chomp $ret;
close OUT or die join(' ', "broken pipe", @args);
return $ret;
}
# add a dir and all its parents
sub create_dir($)
{
my $dir = shift;
my $base = ".";
foreach $d (split "/", $dir)
{
last unless -d "$base/$d";
shell "cvs", "-Q", "add", "-m", "", "$base/$d" unless -d "$base/$d/CVS";
$base .= "/$d";
}
}
# this is the heuristic that git-diff uses
sub is_file_binary($)
{
my $file = shift;
my $data;
open F, $file or return 0;
sysread F, $data, 8000;
close F;
return ($data =~ /\0/);
}
# cvs add a bunch of files
sub cvs_add_files($@)
{
my $is_binary = shift;
my @files = @_;
# first create dirs if needed
foreach my $file (@files) { create_dir( $file ); }
my @cmdline = ("cvs", "-Q", "add", "-m", "");
push @cmdline, "-kb" if $is_binary;
shell @cmdline, @files;
}
# cvs rm a bunch of files
sub cvs_rm_files(@)
{
my @files = @_;
shell "cvs", "-Q", "rm", "-f", @files;
}
# set a CVS tag on all files
sub cvs_tag($)
{
my $tag = shift;
my $cvstag = $tag;
$cvstag =~ s/\./_/g;
$cvstag =~ s/^wine/Wine/;
print "Tagging $tag as $cvstag\n\n";
shell "cvs", "-Q", "tag", "-c", $cvstag;
}
# get the list of directories touched by the specified files
sub get_file_dirs(@)
{
my %dirs = ();
foreach my $file (@_)
{
if ($file =~ /^(.*)\//) { $dirs{$1} = 1; }
else { $dirs{"."} = 1; }
}
return sort keys %dirs;
}
# check if directory names match, including wildcards
sub match_dirs($$)
{
my @d1 = split "/", $_[0];
my @d2 = split "/", $_[1];
return 0 if ($#d1 != $#d2);
for (my $i = 0; $i <= $#d1; $i++)
{
return 0 if ($d1[$i] ne $d2[$i]) && ($d2[$i] ne "*");
}
return 1;
}
# read in a single .gitignore file
sub read_gitignore($$)
{
my ($basedir, $subdir) = @_;
my @patterns = ();
if (open GITIGNORE, "<$basedir/.gitignore")
{
while (<GITIGNORE>)
{
chomp;
next if (/^$/);
next if (/^#/);
if (/^(.*)\/([^\/]*)$/)
{
my ($dir, $file) = ($1, $2);
push @patterns, $file if (match_dirs($subdir, $dir) != 0);
}
else
{
push @patterns, $_;
}
}
close GITIGNORE;
}
return @patterns;
}
# read all the .gitignore files that affect the specified directory
sub read_all_gitignores($)
{
my $dir = shift;
my @elems = split("/", $dir);
my @patterns = ();
shift @elems if ($elems[0] eq ".");
if (@elems)
{
# first the top-level dir
push @patterns, read_gitignore( ".", join("/",@elems) );
# then the intermediate dirs
for (my $i = 0; $i < $#elems; $i++)
{
push @patterns, read_gitignore( join("/",@elems[0..$i]), join("/",@elems[$i+1..$#elems]) );
}
}
# and last, the specified dir
push @patterns, read_gitignore( $dir, "" );
return @patterns;
}
# generate the .cvsignore in the specified directory
sub make_cvsignore($)
{
my $dir = shift;
my %patterns = ();
my $is_dir_empty = 1;
find sub { $is_dir_empty = 0 unless (-d $_ || $_ eq ".cvsignore" || $File::Find::name =~ /\/CVS\//); }, $dir;
if (!$is_dir_empty)
{
foreach $file (read_all_gitignores($dir))
{
$patterns{$file} = 1 unless defined($ignore_patterns{$file});
}
if (keys %patterns)
{
my $op = (-f "$dir/.cvsignore") ? "M" : "A";
open CVSIGNORE, ">$dir/.cvsignore" or die "cannot create $dir/.cvsignore";
print CVSIGNORE join("\n", sort keys %patterns) . "\n";
close CVSIGNORE;
if ($op eq "A") { cvs_add_files( 0, "$dir/.cvsignore" ); }
return $op;
}
}
if (-f "$dir/.cvsignore")
{
cvs_rm_files( "$dir/.cvsignore" );
return "D";
}
return "";
}
# perform the cvs commit once the updates/adds/deletes have been done
sub cvs_commit($@)
{
my $commit = shift;
my @files = @_;
my $author;
my @log = ();
# convert log message according to Wine CVS conventions
open COMMIT, "-|" or exec "git", "cat-file", "commit", $commit or die "cannot run git cat-file";
while (<COMMIT>)
{
chomp;
if (/^author (.*<.*>) \d+ [-+]\d+$/)
{
$author = $1;
next;
}
last if /^$/;
}
while (<COMMIT>)
{
chomp;
push @log, $_;
}
close COMMIT;
open LOGFILE, ">.logfile";
print LOGFILE utf8("$author\n")->latin1;
print LOGFILE utf8(join("\n",@log), "\n")->latin1;
close LOGFILE;
print "branch $branch commit $commit\n";
print "$author\n";
print join("\n",@log), "\n";
print "\n";
shell "cvs", "-Q", "commit", "-F", ".logfile", @files;
unlink ".logfile";
shell "git", "update-ref", $last_commit, $commit;
if (defined $tags{$commit}) { cvs_tag($tags{$commit}); }
}
# apply the specified git commit to the CVS tree
sub apply_git_commit($)
{
my $commit = shift;
my @added = ();
my @added_bin = ();
my @added_text = ();
my @modified = ();
my @deleted = ();
my @gitignores = ();
my @gitignores_deleted = ();
$commit = shell_output "git", "rev-parse", "$commit^\{commit\}";
open COMMIT, "-|" or exec "git", "diff-tree", "--name-status", "-r", $last_commit, $commit or die "cannot run git diff-tree";
while (<COMMIT>)
{
chomp;
if (/^([AMD])\t(.*)$/)
{
my ($op, $file) = ($1, $2);
if ($file =~ /\.gitignore$/)
{
if ($op eq "D") { push @gitignores_deleted, $file; }
else { push @gitignores, $file; }
}
elsif ($op eq "A") { push @added, $file; }
elsif ($op eq "M") { push @modified, $file; }
elsif ($op eq "D") { push @deleted, $file; }
}
}
close COMMIT;
# get the modified files
shell "git", "read-tree", "--reset", $commit;
shell "git", "checkout-index", "-f", "-u", "--", @added, @modified, @gitignores if (@added || @modified || @gitignores);
unlink @gitignores_deleted if (@gitignores_deleted);
foreach my $file (@added)
{
if (is_file_binary($file))
{
print "A $file (binary)\n";
push @added_bin, $file;
}
else
{
print "A $file\n";
push @added_text, $file;
}
}
foreach my $file (@modified) { print "M $file\n"; }
foreach my $file (@deleted) { print "D $file\n"; }
cvs_add_files( 0, @added_text ) if (@added_text);
cvs_add_files( 1, @added_bin ) if (@added_bin);
cvs_rm_files(@deleted) if (@deleted);
foreach my $dir (get_file_dirs(@added, @modified, @deleted, @gitignores, @gitignores_deleted))
{
my $op = make_cvsignore($dir);
next unless $op;
if ($op eq "A") { push @added, "$dir/.cvsignore"; }
elsif ($op eq "M") { push @modified, "$dir/.cvsignore"; }
elsif ($op eq "D") { push @deleted, "$dir/.cvsignore"; }
}
cvs_commit( $commit, @added, @deleted, @modified );
# prune empty dirs
shell "cvs", "-Q", "update", "-P" if (@deleted);
}
# apply all commits from the specified one up to current top of branch
sub apply_commits()
{
my @commits = ();
my $base = shell_output "git", "merge-base", $last_commit, $branch;
my $commit = shell_output "git", "rev-parse", "$last_commit^\{commit\}";
my $tip = shell_output "git", "rev-parse", "$branch^\{commit\}";
die "$last_commit is not a parent of $branch" unless ($base eq $commit);
return if ($tip eq $base);
if (! -f $ENV{"GIT_INDEX_FILE"})
{
# read the tree of the first commit and make sure we are up to date
shell "git", "read-tree", "--reset", $last_commit;
shell "git", "update-index", "--refresh";
shell "git", "checkout-index", "-q", "-f", "-u", "-a";
}
open LIST, "-|" or exec "git", "rev-list", "$base..$tip" or die "cannot run git rev-list";
while (<LIST>)
{
chomp;
die "invalid commit $_" unless /^[0-9a-f]{40}$/;
unshift @commits, $_;
}
close LIST;
return unless (@commits);
read_tags();
foreach my $commit (@commits)
{
apply_git_commit($commit);
}
printf "Applied %d commits.\n", $#commits+1;
}
# build a list of all commits that are pointed to by a tag
sub read_tags()
{
open LIST, "-|" or exec "git", "for-each-ref", "--format=%(object) %(refname)", "refs/tags/wine*" or die "cannot run git for-each-ref";
while (<LIST>)
{
next unless /^([0-9a-f]{40}) refs\/tags\/(.*)$/;
$tags{$1} = $2;
}
close LIST;
}
# if we have a .git symlink in CVS/ use that as GIT_DIR
$ENV{"GIT_DIR"} = "CVS/.git" if -d "CVS/.git";
$ENV{"GIT_WORK_TREE"} = ".";
# use a tmp index file to avoid touching the main one
$ENV{"GIT_INDEX_FILE"} = "CVS/.git-index";
# sanity checks
-d "CVS" or die "Not in a CVS repository";
-d $ENV{"GIT_DIR"} or die "Invalid git dir $ENV{GIT_DIR}";
-w "." or die "No write access to current directory";
apply_commits();