forked from spellingmistake/cvs2git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcvs2git.pl
More file actions
executable file
·1240 lines (1095 loc) · 40.9 KB
/
cvs2git.pl
File metadata and controls
executable file
·1240 lines (1095 loc) · 40.9 KB
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
################################################################################
# #
# Copyright (C) 2011 Thomas Egerer and Torsten Hilbrich, secunet Security #
# Networks AG #
# #
# 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. See <http://www.fsf.org/copyleft/gpl.txt>. #
# #
# This program is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License #
# for more details. #
# #
################################################################################
use strict;
use warnings;
use Data::Dumper qw(Dumper);
use Date::Format;
use Date::Parse;
use Encode qw(encode decode);
use File::Basename qw(dirname);
use File::Copy qw(copy);
use File::Path qw(mkpath);
use File::Spec::Functions qw(rel2abs);
use File::stat qw(stat);
use File::Temp qw(tempfile);
use Getopt::Long;
use IO::File;
use IO::File qw();
use POSIX qw(dup2);
use IPC::Open3;
use Cwd;
use Symbol;
sub help($;$)
{
my ($err, $long) = @_;
my $ret = defined $err ? -1 : 0;
print $err if defined $err;
print <<EOF;
usage: $0 --cvsdir <cvs_dir> --gitdir <git_dir> [<options>]
Convert CVS component in directory cvs_dir and store all commits in git_dir.
--cvsdir <cvs_dir> CVS source directory to use in conversion
--gitdir <git_dir> git destination directory (must exist)
--prefix <prefix> prefix to cut off from CVS path
--maxcommits <number> stop conversion after <number> of commits
--squashdate <date> squash all commits up to <date> into a single one
--finisher <scriptlet> scriptlet to be executed after conversion process
--binfiles <regexp> treat files matching <regexp> as binary files
--ignorefiles <regexp> do not import files matching <regexp> into git repo
--maxcvserrs <number> allow at most <number> of anoncvs errors per file
--authorfile <file> load authors from <file>
--debug be verbose about what script is doing
--dry-run simulate the conversion process
--no-unknown do not allow unknown authors
--force-binary force binary conversion mode on all files
--update update a repository already converted
--help display this help text
--longhelp display more verbose help information
EOF
exit $ret unless (defined $long);
print <<EOF;
NOTES:
cvs_dir is a working directory holding the component to convert. It
must exist and should be up to date when starting the conversion
process. The option --update causes a 'cvs up' to be executed prior
to the conversion.
git_dir must exist a 'git init' must not necessarily have been run
in this directory. Commits will be appended to the current branch,
so if you're using the --update option be sure you are in the branch
which is meant to be the one updated.
The string given with --prefix concatenated with the name of the
cvs_dir is removed from the beginning of each RCS file. So with
'--cvsdir fnord --prefix /cvsroot/fnord' /cvsroot/fnord/fnord is to
be removed from a RCS file. If the string given here is not found
in the RCS file path the script terminates.
Use --maxcommits to only perform conversion of the first <number> of CVS
commits. If you use it with --squashdate you might end up with no conversion
at all if all of the first <number> of commits are before <date>.
Use --squashdate to squash all CVS commits up to and including <date> into
one commit. Below is a list of date formats known to be understood by
--squashdate:
1995:01:24T09:08:17.1823213 ISO-8601
1995-01-24T09:08:17.1823213
Wed, 16 Jun 94 07:29:35 CST Comma and day name are optional
Thu, 13 Oct 94 10:13:13 -0700
Wed, 9 Nov 1994 09:50:32 -0500 (EST) Text in () will be ignored.
21 dec 17:05 Will be parsed in the current time zone
21-dec 17:05
21/dec 17:05
21/dec/93 17:05
1999 10:02:18 "GMT"
16 Nov 94 22:28:20 PST
The options --finisher <scriptlet> causes <scriptlet> to be executed after
the conversion has been performed in <git_dir>. It can be used to perform
git repacking or running git filter-branch. It is called with cvs_dir,
git_dir and number of commits as arguments.
All files matching the <regexp> given with --binfiles are treated to be
binary, see --force-binary.
All files matching the <regexp> given with --ignorefiles are excluded
from import into the git repository.
The option --maxcvserrs can be used to override the default of four
anoncvs errors allowed per file when performing update operations as anon
user on a CVS repo. Somehow this happened a lot during tests and was
pretty annoying.
The option --authorfile can be used to specify a file from which the
author list should be obtained. Format is supposed to be the same as
with svn2git:
id = Author Name <mailing address>\n
where mailing address can be ommited.
Use --debug to see (almost) everything the script is doing during the
conversion.
The --dry-run option simulates the conversion process without actually
doing anything. You can use it with --debug to see what will be done during
the conversion.
The --no-unknown forces all authors to be known in the --authorfile. It
can be used to get a commit log with complete author information for each
author.
Use --force-binary to force all files to be treated as binary files. In
for CVS keyword substitution had to be explicitely disabled for binary
files. Most repositories contain binary files that were not added with
the -kb switch. This options treats all files as if they were binary.
EOF
exit $ret;
}
################################################################################
# generate_commit_hash - generate a commit hash from the infos of the file #
# currently processed to identify all files within this #
# commit: <epoch>_|||_<commitid>_||_<author>, #
# sounds easier than it is since CVS does not generate #
# commit IDs all the time and if it does collisions are #
# quite probable (I've seen them often) #
# in: quite obvious #
# out: even more #
################################################################################
sub generate_commit_hash($$$)
{
my ($epoch, $commitid, $author) = @_;
# for projects with commits prior to Sep 9 2001, 03:46:40 we must
# make sure that sorting succeeds when using the epoch value by
# prepending zeros so it is 10 digits long
sprintf('%010d', $epoch) . '_|||_' . $commitid . '_|||_' . $author;
}
################################################################################
# build_env_hash - build environemnt for git to accept author date and mail #
# for a commit #
# in: date - date to use for author date value #
# author - author to use with commit #
# mail - mail address of the author #
# cdate - commit date to use with commit (defaults to date if omited) #
# out: environemnt string to use with git commit #
################################################################################
sub build_env_hash($$$;$)
{
my ($date, $author, $mail, $cdate) = @_;
{
'GIT_AUTHOR_DATE' => $date,
'GIT_AUTHOR_NAME' => $author,
'GIT_AUTHOR_EMAIL' => $mail,
'GIT_COMMITTER_DATE' => "${\(defined $cdate ? $cdate : $date)}"
}
}
################################################################################
# populate_commit_hash - looks for a suitable entry in <commits> and inserts #
# the pushes file info contained in <rinfos> into this #
# commits files array; if no entry is found, a new one #
# is generated and <count> is increased. #
# in: commits - ref to commits hash #
# rinfos - ref to file info hash (will be wiped except for the filename) #
# count - ref to number of commits, gets increased for a new one #
# update - date to use with update option, means all commits prior and #
# up to this epoch value are not included in commit hash #
# out: filename #
################################################################################
sub populate_commit_hash(%$$$)
{
my ($commits, $rinfos, $count, $update) = @_;
my (@commit_tags, $commit_tag, $epoch, $filename);
if ($$rinfos->{'curr'}->{'rev'} !~ /^[0-9]+\.[0-9]+$/)
{
# skip all branch commits
return;
}
return if ($update and $update >= $$rinfos->{'curr'}->{'epoch'});
my $infos = $$rinfos;
$infos->{'curr'}->{'rev'} = 'dead' if ($infos->{'curr'}->{'state'} eq 'dead');
$infos->{'curr'}->{'commitid'} = "<unknown>" if (!exists $infos->{'curr'}->{'commitid'});
$epoch = $infos->{'curr'}->{'epoch'};
$filename = $infos->{'filename'};
# CVS commits aren't atomic in means of time (or any other)
# so we use this heuristic and accept all commits with the
# same name and commit ID (if any, thank you CVS!) and author
# within 15 seconds before and after the commit we're
# currently processing; if we have a hit we check the commit
# message to be 100% sure it's really the same commit;
for my $i (-15 .. 15)
{
my $tag = generate_commit_hash($epoch + $i, $infos->{'curr'}->{'commitid'},
$infos->{'curr'}->{'author'});
if (exists $commits->{$tag})
{
# compare commit messages
if ($commits->{$tag}->{'comment'} eq (join("\n", @{$infos->{'curr'}->{'comment'}})))
{
$commit_tag = $tag;
last;
}
}
}
if (!defined $commit_tag)
{
# we need a new kenn^w commit tag
$commit_tag = generate_commit_hash($epoch, $infos->{'curr'}->{'commitid'},
$infos->{'curr'}->{'author'});
}
if (!exists $commits->{$commit_tag})
{
chomp (my $date = ctime($epoch));
$commits->{$commit_tag} =
{
'comment' => join("\n", @{$infos->{'curr'}->{'comment'}}),
#'date' => $date,
};
print "\rProcessed CVS commit " . ++$$count;
}
my $hash =
{
'revision' => $infos->{'curr'}->{'rev'},
'filename' => $filename,
};
$hash->{'binary'} = 1 if $infos->{'binary'};
unshift @{${$commits->{$commit_tag}}{'files'}}, $hash;
if ($infos->{'curr'}->{'tags'})
{
foreach my $tag (@{$infos->{'curr'}->{'tags'}})
{
if (!defined $commits->{'tags'}->{$tag} or
defined $commits->{'tags'}->{$tag} < $epoch)
{
$commits->{'tags'}->{$tag} = $epoch
}
}
}
$$rinfos->{'filename'} = $filename;
}
################################################################################
# <helper functions for commit log parser> #
################################################################################
sub START() { return 0; }
sub INITIAL() { return 1; }
sub RCS_FILE() { return 2; }
sub SKIP_TO_TAGS() { return 3; }
sub PROCESS_TAGS() { return 4; }
sub SKIP_TO_REVISION() { return 5; }
sub SKIP_TO_INFOS() { return 6; }
sub SKIP_TO_BRANCH_INFO() { return 7; }
sub BUILD_COMMIT_LOG() { return 8; }
################################################################################
# </helper functions for commit log parser> #
################################################################################
################################################################################
# parse_commit_log - parse the commit log obtained by executing <cmd> (read in #
# chunks of 4096 bytes into an internal structure that will #
# later on be used to generate git commits from; <prefix> #
# is removed from path strings and unless <allow> is set, #
# unknown authors are complained about; results are stored #
# in $commits ref; #
# in: cmd command to execute for cvs log (e.g. a cat command) #
# commits hash ref to store results in #
# opts - hash ref with options with the followoing options being #
# used: #
# prefix prefix to remove from cvs path #
# noallow allow unknown authors #
# update date to use with update options #
# ignorefiles ignore files matching this regexp #
# authors hash map of author's names and mailing addresses #
# binary hash ref { 'all'/'regexp' } to set binary flag on all #
# files/those matching regexp #
# out: number of commits #
################################################################################
sub parse_commit_log($%%)
{
my ($cmd, $commits, $opts) = @_;
my ($state, $infos, $tags, $count, $buf, $rest, $forcebinary, $regexp, %unknown_authors);
my ($prefix, $noallow, $update, $ignorefiles, $authors, $binary) = (
$opts->{'prefix'},
$opts->{'nounknown'},
$opts->{'update'},
$opts->{'ignorefiles'},
$opts->{'authors'},
$opts->{'forcebinary'});
if (defined $binary)
{
$forcebinary = $binary->{'all'};
$regexp = $binary->{'regexp'};
}
$state = START;
$count = 0;
select(STDOUT);
$| = 1;
$cmd .= " | ";
open C, $cmd;
# this let's commit log contents stay within a page of memory;
# some commit logs I've seen were very large and blew up this
# little parser
while (read(C, $buf, 4096) or $rest)
{
$buf = "\n" if !defined $buf;
$buf = ($rest . $buf) if defined $rest;
while ($buf =~ s/(.*)\n//)
{
my $line = $1;
if ($state == INITIAL or $state == START)
{
if ($line =~ /^$/)
{
$state = RCS_FILE;
next;
}
elsif ($state == START and $line =~ /^\? /)
{
# untracked file, skip it
next;
}
else
{
die "Invalid input in state INITIAL: $line\n";
}
}
elsif ($state == RCS_FILE)
{
if ($line =~ /RCS file: (.*?),v.*/)
{
undef $infos;
undef $tags;
$infos->{'filename'} = $1;
$infos->{'filename'} =~ s|/Attic/|/|;
if ($forcebinary or defined $regexp and $infos->{'filename'} =~ /$regexp/)
{
$infos->{'binary'} = 1;
}
if (defined $prefix and 0 == $infos->{'filename'} =~ s/\Q$prefix\E//o)
{
die "prefix '$prefix' not found in '$infos->{'filename'}"
}
$state = SKIP_TO_TAGS;
next;
}
else
{
die "Invalid input in state RCS_FILE: $line\n";
}
}
elsif ($state == SKIP_TO_TAGS)
{
$state = PROCESS_TAGS if ($line =~ /^symbolic names:/);
next;
}
elsif ($state == PROCESS_TAGS)
{
if ($line =~ /^\t(.+): ([0-9.]+)/)
{
my ($tag, $rev) = ($1, $2);
# ignore branches!
unshift @{$tags->{$rev}}, $tag if $rev =~ /[0-9]+\.[0-9]+/;
}
elsif ($line =~ /^keyword substitution: b/)
{
# for now this only handles binary keyword substitution
$infos->{'binary'} = 1;
}
elsif ($line eq ('-' x 28))
{
$state = SKIP_TO_REVISION;
}
next;
}
elsif ($state == SKIP_TO_REVISION)
{
if ($line =~ /^revision (\S+)/)
{
$infos->{'curr'}->{'rev'} = $1;
$infos->{'curr'}->{'tags'} = $tags->{$1} if defined $tags->{$1};
$state = SKIP_TO_INFOS;
}
next;
}
elsif ($state == SKIP_TO_INFOS)
{
if ($line =~ /date: (\S+) (.*?);/)
{
$infos->{'curr'}->{'epoch'} = str2time("$1 $2");
}
if ($line =~ /author: (.*?);/)
{
$unknown_authors{$1} = 1 if (!defined $authors->{$1} and $noallow);
$infos->{'curr'}->{'author'} = $1;
}
$infos->{'curr'}->{'state'} = $1 if ($line =~ /state: (.*?);/);
$infos->{'curr'}->{'commitid'} = $1 if ($line =~ /commitid: (.*?);/);
$state = SKIP_TO_BRANCH_INFO;
next;
}
elsif ($state == SKIP_TO_BRANCH_INFO)
{
if ($line =~ /^branches: [0-9.]+;/)
{
$state = BUILD_COMMIT_LOG;
}
else
{
# message already belongs to commit message
unless ($line =~ /^\s*$/)
{
push @{$infos->{'curr'}->{'comment'}}, "$line"
}
$state = BUILD_COMMIT_LOG;
}
next;
}
elsif ($state == BUILD_COMMIT_LOG)
{
if ($line eq ('-' x 28))
{
$state = SKIP_TO_REVISION;
}
elsif($line eq ('=' x 77))
{
# last revision for file
$state = INITIAL;
}
else
{
# part of the commit message
unless ($line =~ /^\s*$/)
{
push @{$infos->{'curr'}->{'comment'}}, "$line"
}
next;
}
if (!defined $ignorefiles or $infos->{'filename'} !~ /$ignorefiles/)
{
populate_commit_hash($commits, \$infos, \$count, $update);
}
# clear up info hash except for the filename
delete $infos->{'curr'};
next;
}
}
$rest = $buf;
}
if ($noallow && scalar keys %unknown_authors)
{
my @unknown_authors = keys %unknown_authors;
die "\nUnknown authors found:\n\t@unknown_authors,\nPlease fix!";
}
close C;
print "\n";
select(STDOUT);
$| = 0;
$count;
}
################################################################################
# cd - change into directory - dies on failure #
# in: directory to cd in #
################################################################################
sub cd($)
{
chdir $_[0] or die "Failed to change to directory '$_[0]': $!";
}
################################################################################
# parse_authors - parse authors file/string into given hash ref. #
# in: authors - hash ref to fill with author information #
# source - where author information is take from: SCALAR ref, use string #
# directly, no ref, use string as a file name #
################################################################################
sub parse_authors($$)
{
my ($authors, $source) = @_;
my ($str, $i);
$i = 1;
if (ref $source eq "SCALAR")
{
$str = $$source;
}
elsif (ref $source eq "")
{
local $/ = undef;
open S, "<$source" or die "Unable to open file $source: $!";
$str = <S>;
close S;
}
else
{
die "Unable to handle ref '${\(ref $source)}'";
}
foreach my $author (split /\n/, $str)
{
# skip empty lines and comments
if ($author =~ /^\s*$/ or $author =~ /^\s*#/)
{
++$i;
next;
}
if ($author =~ /^(.+) = (.*?)( <(.+)>)?$/)
{
if (defined $authors->{$1})
{
die "Author name redefinition of $1: $2 vs. $authors->{$1}->[0]";
}
$authors->{$1} = [ $2, $4 ];
}
else
{
die "Malformed author information, line $i: '$author'";
}
++$i;
}
}
################################################################################
# do_command - execute command with optional redirection of stdout and stderr #
# into a file or variable #
# in: command - command to execute (must be an array ref) #
# out - where to direct stdout and stderr, must be a hash ref; #
# 'stderr'/'stdout' define where the appropriate output goes: #
# use a SCALAR ref to get the output to a variable, if value #
# is no ref, output goes to the given string interpreted as a #
# file; if any of the values is ommited output goes nowhere #
# debug - 1 == debug, 2 == dry-run #
# environ - environment to use with command #
# out: return code of the process run #
################################################################################
sub do_command($%;$$)
{
my ($cmd, $out, $debug, $environ) = @_;
my ($stderr, $stdout, $pid);
if ($debug & 1)
{
local $" = " ";
print "@{$cmd}";
if ($out->{'stderr'} and "" eq ref($out->{'stderr'}))
{
print(" 2>" . $out->{'stderr'});
}
if ($out->{'stdout'} and "" eq ref($out->{'stdout'}))
{
print(" >" . $out->{'stdout'});
}
print "\n";
}
return 0 if 2 & $debug;
if (defined $environ)
{
foreach my $name (keys %{$environ})
{
$ENV{$name} = $environ->{$name};
}
}
if ($out->{'stdout'} and "" eq (ref $out->{'stdout'}))
{
open C, ">$out->{'stdout'}";
$stdout = ">&C";
delete $out->{'stdout'};
}
if ($out->{'stderr'} and "" eq (ref $out->{'stderr'}))
{
open C, ">$out->{'stderr'}";
$stderr = ">&C";
delete $out->{'stderr'};
}
$stderr = gensym if !defined $stderr;
$cmd = (join ' ', @{$cmd});
$pid = open3(undef, $stdout, $stderr, $cmd);
waitpid($pid, 0);
if (defined $out->{'stdout'})
{
${$out->{'stdout'}} .= $_ while (<$stdout>);
}
if (defined $out->{'stderr'})
{
${$out->{'stderr'}} .= $_ while (<$stderr>);
}
close $stdout;
close $stderr;
$? >> 8;
}
################################################################################
# write_file - write given <content> into file #
# in: file to write to #
# contents to write to file #
################################################################################
sub write_file($$)
{
my ($file, $content) = @_;
open FILE, ">$file";
print FILE $content;
close FILE;
}
################################################################################
# convert_charset - convert charset from latin1 to utf-8 #
# NOTE: had to shorten this sub like this, feels good though #
# in: string to convert #
# out: converted string #
################################################################################
sub convert_charset($)
{
my $comment = $_[0];
eval
{
# Check if contents is uft8
decode('utf-8', $_[0], Encode::FB_CROAK);
};
$@ ? encode('utf-8', decode('latin1', $comment)) : $comment;
}
################################################################################
# trim_comment - make first commit line for git look good; since git is #
# superiour to CVS in many ways a commit line displayed with #
# --oneline option should not exceed the 80 chars limit; #
# this is essentially what this function does; somehow the #
# commits that I've had the honor to convert with it kinda did #
# every crap one can possible think of (me included, though I #
# have been at least consistent in the crap I did); #
# took me quite some time to finally figure out what I had #
# planned with every single line in the first place, seemed to #
# make sense by the time writing it; note to self: use comments #
# next time! #
# in: possible crappy comment #
# out: somehow not so crappy headline of the new git commit #
################################################################################
sub trim_comment($)
{
my $comment = $_[0];
my ($ret, $line);
$line = 0;
$comment = convert_charset($comment);
foreach my $s (split /\n/, $comment)
{
my ($words, $len);
# leading white spaces
$s =~ s/^\s+//;
# leading dot-like stuff once, or even twice
$s =~ s/^[-+_o*]{1,2}\s?(\w|\"|\')/$1/g;
$words = scalar(my @words = split /\s/, $s);
$len = $words ? length($words[0]) : 0;
# we accept it as valid comment iff
# - it has at least two words, or
# - the first word is > 9 characters and comment line does not end
# with a colon(oscopie)
if ((2 <= $words or ($len > 9 and $s !~ /:$/)) and !defined $ret)
{
($ret = $s) =~ s/^\s//g;
}
elsif (defined $ret)
{
# indicate that comment continues
$ret =~ s/\s*$/.../;
last;
}
++$line;
}
# fall back to single comment line
$ret = $comment if !defined $ret;
# limit lenght of line to 50 chars tops
if (length($ret) > 50)
{
$ret = substr($ret, 0, 47);
# remove any start of a german utf-8 character
$ret =~ s/\xC3$//;
$ret =~ s/\s*$/.../;
}
$ret;
}
################################################################################
# cvs2git - perform actual cvs update of the file and redirect it into git #
# repository #
# in: filename - filename of the CVS file to copy to git #
# revision - revision of the CVS file to use with update #
# gitdir - git directory to use for file #
# chmod - perform a chmod operation on the file #
# maxerr - maximum number of anoncvs errors allowed for file #
# binary - is this a binary file? #
# debug - 1 == debug, 2 == dry-run #
# out: number of commits done #
################################################################################
sub cvs2git($$$$$$$) {
my ($filename, $revision, $gitdir, $chmod, $binary, $maxerr, $debug) = @_;
my ($file, $cmd, $out, $ret, $stderr);
$file = "$gitdir/$filename";
print "mkdir ${\(dirname($file))}\n" if $debug & 1;
mkpath(dirname($file)) if !($debug & 2);
$cmd = ['cvs', 'update', ($binary ? '' : '-p'), '-r', $revision, $filename];
$out = { 'stderr' => \$stderr };
$out->{'stdout'} = $file if !$binary;
do {
print "error: '$stderr' ($maxerr)\n" if $maxerr < 4;
$ret = do_command($cmd, $out, $debug);
$maxerr--;
} while ($ret and $maxerr and $stderr =~ /anoncvs_.*?: no such system user/);
die "error: $stderr" if ($ret);
# binary files are sticky, we have to copy them
if ($binary)
{
print "cp $filename $file\n" if $debug & 1;
copy($filename, $file) if !($debug & 2);
}
if ($chmod)
{
my $stat = stat($filename);
my $mode = defined $stat ? $stat->mode & 0777 : 0644;
printf("chmod %o $file\n", $mode) if $debug & 1;
if (!($debug & 2))
{
chmod $mode, $file or die "Failed to chmod file '$file': $!";
}
}
}
################################################################################
# create_squash_commit - create a single commit from all files contained in #
# <squashed> hash ref; #
# in: squashed - hash ref with files to commit, revision, binary status, #
# start and end date, as well as number of commits squashed #
# cvsdir - CVS directory to use #
# gitdir - git directory to use for file #
# tmpfile - temporary file to use for git comment #
# maxerr - maximum number of anoncvs errors allowed for file #
# debug - 1 == debug, 2 == dry-run #
################################################################################
sub create_squash_commit(%$$$$$) {
my ($squashed, $cvsdir, $gitdir, $tmpfile, $maxerr, $debug) = @_;
my ($commitstr, $env, $filename, $binary, $only, $authors, $count);
$commitstr = <<EOF;
CVS import: Initial squash-commit
This commit squashes $squashed->{'count'} commit(s) starting on
$squashed->{'start'} ending $squashed->{'end'}
into a single commit to simplify git history.
Commits of the following authors (in order of number):
EOF
$count = 0;
# helper sub for sorting by commit number
$authors = "";
foreach $a (sort
{
$squashed->{'authors'}->{$b} <=> $squashed->{'authors'}->{$a}
} keys %{$squashed->{'authors'}})
{
$authors .= "\t$a: $squashed->{'authors'}->{$a}\n";
$only = $a;
++$count;
}
$commitstr .= "$authors\nFiles:\n";
$env = build_env_hash($squashed->{'end'},
$count == 1 ? $only : "various artists",
'hakke_007@gmx.de');
foreach my $filename (sort(keys %{$squashed->{'files'}}))
{
my ($revision, $binary) = @{$squashed->{'files'}->{$filename}};
$commitstr .= "\tadded: $filename -> $revision\n";
cvs2git($filename, $revision, $gitdir, 1, $binary, $maxerr, $debug);
}
cd($gitdir);
do_command(['git', 'add', '.'], {}, $debug);
write_file($tmpfile, $commitstr);
do_command(['git', 'commit', '-F', "$tmpfile"], {}, $debug, $env);
cd($cvsdir);
}
################################################################################
# create_regular_commit - create a regular commit from all files contained in #
# <commitobj> hash ref; #
# in: commitobj - hash ref with files to commit, revision, binary status, #
# start and end date #
# cvsdir - CVS directory to use #
# gitdir - git directory to use for file #
# tmpfile - temporary file to use for git comment #
# maxerr - maximum number of anoncvs errors allowed for file #
# debug - 1 == debug, 2 == dry-run #
################################################################################
sub create_regular_commit(%$$$$$)
{
my ($commitobj, $cvsdir, $gitdir, $tmpfile, $maxerr, $debug) = @_;
my ($headline, $comment, $commitstr, $env);
$headline = trim_comment($commitobj->{'comment'});
($comment = (" " x 4) . $commitobj->{'comment'}) =~ s/\n/$& . (" " x 4)/eg;
$commitstr = <<EOF;
$headline
CVS import: $commitobj->{'author'}, $commitobj->{'date'}
original comment:\n$comment
Files:
EOF
$env = build_env_hash($commitobj->{'date'},
$commitobj->{'author'},
$commitobj->{'mail'});
foreach my $filename (sort(keys %{$commitobj->{'added'}}))
{
my ($revision, $binary) = @{$commitobj->{'added'}->{$filename}};
cvs2git($filename, $revision, $gitdir, 1, $binary, $maxerr, $debug);
$commitstr .= "\tadded: $filename -> $revision\n"
}
foreach my $filename (sort(keys %{$commitobj->{'updated'}}))
{
my ($revision, $binary) = @{$commitobj->{'updated'}->{$filename}};
cvs2git($filename, $revision, $gitdir, 0, $binary, $maxerr, $debug);
$commitstr .= "\tupdated: $filename -> $revision\n"
}
cd($gitdir);
foreach my $filename (sort(keys %{$commitobj->{'removed'}}))
{
do_command(['git', 'rm', '-f', $filename], {}, $debug);
$commitstr .= "\tremoved: $filename\n"
}
do_command(['git', 'add', '.'], {}, $debug);
write_file($tmpfile, $commitstr);
do_command(['git', 'commit', '-F', "$tmpfile"], {}, $debug, $env);
cd($cvsdir);
}
################################################################################
# create_commits - create an actual commit from the parsed commit log hash ref #
# maxcommits (end) and squashedate are evaluated, too. #
# in: commits - hash ref of the commit log as created by parse_commit_log #
# count - number of commits processed by parse_commit_log #
# opts - hash ref with options with the followoing options being #
# used: #
# authors - hash map of author's names and mailing addresses #
# cvsdir - CVS directory to use #
# gitdir - git directory to use for file #
# end - maximum number of commits as specified with --maxcommits #
# squashdate - all CVS commits up to and including this point of time #
# - will be squashed into one single commit #
# maxerr - maximum number of anoncvs errors allowed for file #
# debug - 1 == debug, 2 == dry-run #
# out: number of commits done #
################################################################################
sub create_commits(%$%)
{
my ($commits, $count, $opts) = @_;
my (%revisions, $squashed, $i, $commitno);
my (undef, $tmpfile) = tempfile();
my ($authors, $cvsdir, $gitdir, $end, $squashdate, $maxerr, $debug) = (
$opts->{'authors'},
$opts->{'cvsdir'},
$opts->{'gitdir'},
$opts->{'maxcommits'},
$opts->{'squashdate'},
$opts->{'maxcvserrs'},
$opts->{'debug'});
$commitno = $i = 0;
if (!$debug)
{
select(STDOUT);
$| = 1;
}
if ($end)
{
print "Converting $end of the $count total commits\n";
$count = $end
}
else
{
print "Converting $count commits\n";
}
foreach my $commit (sort keys %{$commits})
{
my ($commitobj, $author, $mail, $epoch, $date, $login);
next if $commit eq 'tags';
die "no files: $commit" if 0 == (scalar @{${$commits->{$commit}}{"files"}});
($epoch, undef, $login) = (split /\Q_|||_\E/, $commit);
($author, $mail) = (defined $authors->{$login}) ?
@{$authors->{$login}} : ($login, "unknown");
$author .= " ($login)" unless $author eq $login;
chomp ($date = ctime($epoch));
if ($epoch <= $squashdate)
{
print "${\($debug ? \"\n\" : \"\r\")}Skipping commit ${\(++$i)}/$count";
if (!$squashed)
{
# intialize squashed hash
$squashed = { "start" => $date, "end" => $date, "count" => 0, };
}
++$squashed->{'count'};
$squashed->{'authors'}->{$author} = 0 if !defined $squashed->{'authors'}->{$author};
++$squashed->{'authors'}->{$author};
}
else
{
print "${\($debug ? \"\n\" : \"\r\")}Converting commit ${\(++$i)}/$count";
if ($squashed)