-
Notifications
You must be signed in to change notification settings - Fork 3
/
git-vss.pl
2558 lines (1957 loc) · 72.1 KB
/
git-vss.pl
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
use utf8;
use strict;
use warnings;
use Encode::Byte;
use Encode::Unicode;
use DBI;
use DBD::SQLite;
use Getopt::Long;
use File::Spec;
use File::Temp qw(tmpnam tempdir);
use POSIX qw(mktime strftime);
use Win32::OLE;
use Win32::OLE::Const 'Microsoft SourceSafe';
use IO::Pipe;
use IPC::Open2;
$| = 1;
our $dbh;
our $is_postgres = 0;
our ($opt_init, $opt_import, $opt_connect, $git_tree, $opt_newhead, $opt_nomaps);
our ($opt_dump, $opt_load, $authors_file, $filename_file);
our ($opt_rebase, $opt_nofetch, $opt_repin, $opt_undo_checkouts,
$opt_checkout, $opt_squash, $opt_commit, $opt_master,
$opt_sanitize, $opt_mergetool);
sub usage() {
print STDERR <<END;
Usage: git-vss [-h|--help] [--root=GIT_repository] parameters...
Update Git from VSS:
[--new-head] [--checkout] [--rebase] branchname
Commit changes from Git into VSS:
--commit [--squash=title[:]] [--mergetool=cmd] branchname
Undo previous checkouts:
--undo-checkouts [branchname]
Repin commits to the specified branch:
--repin branchname commit commit...
Initialize repository:
--connect base_path
(--init|--import=path) [--no-mappings] [--no-fetch] [--master=branch]
branchname vss_repo log_path log_offset < mappings.txt
(--load|--dump) [--authors=file] [--filenames=file]
Canonify newly-added file names in the index:
--sanitize-adds
END
exit(1);
}
GetOptions(
'help|h' => \&usage,
'root|C=s' => \$git_tree,
'init' => \$opt_init,
'import=s' => \$opt_import,
'connect' => \$opt_connect,
'no-mappings' => \$opt_nomaps,
'new-head' => \$opt_newhead,
'load' => \$opt_load,
'dump' => \$opt_dump,
'authors=s' => \$authors_file,
'filenames=s' => \$filename_file,
'rebase' => \$opt_rebase,
'no-fetch' => \$opt_nofetch,
'repin' => \$opt_repin,
'undo-checkouts' => \$opt_undo_checkouts,
'checkout' => \$opt_checkout,
'commit' => \$opt_commit,
'squash=s' => \$opt_squash,
'master=s' => \$opt_master,
'sanitize-adds' => \$opt_sanitize,
'mergetool=s' => \$opt_mergetool,
) or usage();
$opt_nofetch = 0 unless $opt_init || $opt_import;
our $base_path;
our $vss_path;
our $log_path;
our $log_offset;
our $initial_head;
our $branch_name;
our $vss;
our $vss_user;
our %vss_path_mapping;
our @vss_path_list;
# Chdir to the specified folder
$git_tree ||= ".";
chdir $git_tree;
# Path to the git repository root
my $git_dir = `git rev-parse --git-dir`;
($? == 0) or die "Bad working directory\n";
chomp $git_dir;
$git_dir = File::Spec->rel2abs($git_dir);
# Path to the working tree root
my $work_dir = $git_dir;
$work_dir =~ s/[\\\/]\.git$//;
# Add the git installation dir to the path
my $git_path = `git --exec-path`;
chomp $git_path;
$git_path =~ s/\//\\/g;
$ENV{PATH} .= ';' . $git_path;
my $win_path = $ENV{COMSPEC};
if ($win_path) {
$win_path =~ s/[\/\\][^\/\\]+$//;
$ENV{PATH} .= ';' . $win_path;
}
my $checkout_file = $git_dir.'/vss-checkouts';
my $squash_msg_file = $git_dir.'/vss-message';
###########################################################
# LOG SCANNING
sub fetch(\$*) {
my ($rp, $ra) = @_;
local $_;
while (<$ra>) {
return undef unless /\n$/;
$$rp .= $_;
s/\s+$//;
return $_;
}
return undef;
}
sub scan_log($$;$$) {
my ($processed, $log_fn, $handler, $on_error) = @_;
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat($log_fn);
return -1 if ($size < $processed);
return $processed if $size == $processed;
open FH, '<:raw', $log_fn;
seek FH, $processed, 0;
my $active_proc = '';
my ($last_pos, $last_proc);
my @actions;
eval {
while (defined ($_ = fetch($active_proc, FH))) {
next if /^\s*$/;
if (/^(\$.*\S)$/) {
my $path = $1;
$path =~ s/^\$//;
defined ($_ = fetch($active_proc, FH)) or die "Incomplete block\n";
my $ver;
if (/^Version:\s+(\d+)/) {
$ver = $1;
defined ($_ = fetch($active_proc, FH)) or die "Incomplete block\n";
}
/^User:\s+(\S+)\s+Date:/ or die "Bad date line\n";
my $user = $1;
defined ($_ = fetch($active_proc, FH)) or die "Incomplete block\n";
my $cmd = $_;
my ($arg1,$arg2);
my $entry = {
path => $path,
version => $ver,
user => $user,
alterations => [],
};
if ($cmd =~ /^(.+) created$/) {
$cmd = 'Created';
$entry->{name} = $1;
} elsif ($cmd =~ /^(.+) added$/) {
$cmd = 'Added';
$entry->{name} = $1;
} elsif ($cmd =~ /^\$?(.+) deleted$/) {
$cmd = 'Deleted';
$entry->{name} = $1;
} elsif ($cmd =~ /^\$?(.+) destroyed$/) {
$cmd = 'Destroyed';
$entry->{name} = $1;
} elsif ($cmd =~ /^\$?(.+) purged$/) {
$cmd = 'Purged';
$entry->{name} = $1;
} elsif ($cmd =~ /^\$?(.+) archived$/) {
$cmd = 'Archived';
$entry->{name} = $1;
} elsif ($cmd =~ /^Restored version (\d+) to (\d+)$/) {
$cmd = 'Restored';
$entry->{name} = $2;
$entry->{obj_ver} = $1;
} elsif ($cmd =~ /^\$?(.+) recovered$/) {
$cmd = 'Recovered';
$entry->{name} = $1;
} elsif ($cmd =~ /^Labeled (.+)$/) {
$cmd = 'Labeled';
$entry->{label} = $1;
} elsif ($cmd =~ /^\$?(.+) shared from (\$.*)\/([^\/]*)$/) {
$cmd = 'Shared';
$entry->{name} = $1;
$entry->{src_path} =
($3 && lc(substr($1,0,length($3))) eq lc($3)) ? $2 : undef;
} elsif ($cmd =~ /^\$?(.+) copied from (\$.*)\/([^\/]+)$/) {
$cmd = 'Copied';
$entry->{name} = $1;
$entry->{src_path} =
($3 && lc(substr($1,0,length($3))) eq lc($3)) ? $2 : undef;
} elsif ($cmd =~ /^\$?(.+) moved to (\$.*)$/) {
$cmd = 'Moved';
$entry->{name} = $1;
$entry->{move_target} = $2;
} elsif ($cmd =~ /^(.+) renamed to (.+)$/) {
$cmd = 'Renamed';
$entry->{name} = $1;
$entry->{new_name} = $2;
} elsif ($cmd =~ /^Checked in$/) {
$cmd = 'Checked';
} elsif ($cmd =~ /^\$?(.+) branched$/) {
$cmd = 'Branched';
$entry->{name} = $1;
} else {
die "Unknown command: $cmd\n";
}
$entry->{command} = $cmd;
die "Version undefined\n" unless defined $ver;
my $comment;
defined ($_ = fetch($active_proc, FH)) or die "Incomplete block\n";
while (/\S/) {
$comment .= $_;
defined ($_ = fetch($active_proc, FH)) or die "Incomplete block\n";
}
$comment =~ s/^Comment: // if $comment;
$entry->{comment} = $comment;
my $ok = $handler ? $handler->($entry) : 1;
push @actions, $entry if $ok;
($last_pos, $last_proc) = ($processed, $active_proc);
$processed += length($active_proc);
$active_proc = '';
} else {
die "Invalid head line: $_\n";
}
}
};
if ($@) {
my $err = $@;
$active_proc = '';
$on_error->($err, $processed) if $on_error;
print STDERR $err;
}
$processed += length($active_proc);
close FH;
return wantarray ? ($processed, $last_pos, $last_proc, \@actions) : $processed;
}
###########################################################
# LOG ANALYSIS
sub matches($$) {
my ($path, $mask) = @_;
return 0 unless substr($path,0,length($mask)) eq $mask;
my $c = substr($path,length($mask),1);
return 0 if $c && $c ne '/';
return 1;
}
sub find_git_path($) {
my $vss_path = shift;
my $cpath = lc($vss_path);
for my $candidate (@vss_path_list) {
next unless matches($cpath, $candidate);
# Ignore list
return undef unless defined $vss_path_mapping{$candidate};
my $rpath = $vss_path_mapping{$candidate} . substr($vss_path, length($candidate));
$rpath =~ s/^\/+//;
return $rpath;
}
return undef;
}
sub read_log_tasks($$) {
my ($processed, $file) = @_;
my @actions;
my $handler = sub {
my ($entry) = @_;
# Move affects two paths, check the target one
if (defined $entry->{move_target}) {
die "Unsupported move into $entry->{move_target}\n"
if defined (find_git_path $entry->{move_target})
|| grep { matches($_, $entry->{move_target}); } @vss_path_list;
}
# Ignore actions that are outside of the defined mappings
defined ($entry->{git_path} = find_git_path $entry->{path})
or return 0;
if ($entry->{command} =~ /Renamed|Deleted|Recovered|Destroyed|Purged/) {
print STDERR "WARNING: $entry->{command} $entry->{path}/$entry->{name}\n";
push @{$_->{alterations}}, $entry for @actions;
}
# These commands change naming or versions, so bail out immediately
die "Unsupported command: $entry->{path} $entry->{command} $entry->{name}\n"
if $entry->{command} =~ /Moved|Restored/;
# Ignore no-op actions
return 0 if $entry->{command} =~ /Labeled|Archived|Branched|Created|Purged/;
push @actions, $entry;
return 1;
};
my ($rp, $lp, $lr, $lacts) = scan_log $processed, $file, $handler, sub {
my $msg = $_[0];
return if $msg eq "Incomplete block\n";
die $msg;
};
return $rp, $lacts;
}
###########################################################
# VSS HISTORY ANALYSIS
sub get_timestamp($) {
my $ver = shift;
return undef unless $ver;
my $date = $ver->{Date};
my $str = $date->Date('yyyy-MM-dd ').$date->Time('HH:mm:ss');
$str =~ /(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/;
return mktime($6, $5, $4, $3, $2-1, $1-1900, 0, 0, -1);
}
sub get_last_before($$) {
my ($item, $time) = @_;
return undef unless $item;
my $vset = $item->Versions(VSSFLAG_HISTIGNOREFILES);
my $enum = Win32::OLE::Enum->new($vset);
while (defined (my $ver = $enum->Next())) {
next if $ver->{Action} =~ /^Label/;
return $ver if get_timestamp $ver <= $time;
}
return undef;
}
sub get_last_by_text($$) {
my ($item, $action_text) = @_;
return undef unless $item;
my $vset = $item->Versions(VSSFLAG_HISTIGNOREFILES);
my $enum = Win32::OLE::Enum->new($vset);
while (defined (my $ver = $enum->Next())) {
next unless $ver->{Action} eq $action_text;
return $ver;
}
return undef;
}
sub get_version_at($$) {
my ($item, $idx) = @_;
return undef unless $item;
my $vset = $item->Versions(VSSFLAG_HISTIGNOREFILES);
my $entry = $vset->Item($idx);
if ($entry && $entry->{Action} =~ /^Label/) {
$entry = undef;
my $enum = Win32::OLE::Enum->new($vset);
while (defined (my $ver = $enum->Next())) {
next if $ver->{Action} =~ /^Label/;
last if $ver->{VersionNumber} < $idx;
next if $ver->{VersionNumber} > $idx;
$entry = $ver;
last;
}
}
return $entry;
}
sub alter_path($$;$) {
my ($alt_list, $path, $name) = @_;
my $ret_name = defined $name;
my $isdel = 0;
$path .= '/'.$name if $ret_name;
for my $item (@$alt_list) {
next unless lc $path eq lc($item->{path}.'/'.$item->{name});
if ($item->{command} eq 'Renamed') {
next if $isdel;
$path = $item->{path}.'/'.$item->{new_name};
} elsif ($item->{command} eq 'Deleted') {
$isdel = 1;
} elsif ($item->{command} eq 'Recovered') {
$isdel = 0;
} elsif ($item->{command} eq 'Destroyed') {
$isdel = -1;
last;
} elsif ($item->{command} eq 'Purged') {
next unless $isdel;
$isdel = -1;
last;
} else {
die "Invalid alteration: $item->{command}";
}
}
if ($ret_name) {
$path =~ /\/([^\/]+)$/ or die "Invalid path $path";
return ($1, $isdel);
} else {
return ($path, $isdel);
}
}
my $action_lookup_stmt;
# Binds a log entry to the actual VSS history
sub convert_action($) {
my ($entry) = @_;
# Account for later renames
my ($act_path, $act_del) = alter_path $entry->{alterations}, $entry->{path};
if ($act_del < 0) {
print STDERR "Skipping $entry->{command} on $entry->{path} - object destroyed.\n";
return undef;
}
my $msg_path = $act_path eq $entry->{path} ? $entry->{path} : "$entry->{path} ($act_path)";
# Find the item, and extract the named version
my $vss_item = $vss->VSSItem('$'.$act_path, $act_del)
or die "Cannot find item \$$msg_path";
my $item_version = get_version_at $vss_item, $entry->{version};
if ($entry->{command} eq 'Renamed') {
my $text = 'Renamed '.$entry->{name}.' to '.$entry->{new_name};
$item_version = get_last_by_text $vss_item, $text
unless $item_version && $item_version->{Action} eq $text;
print STDERR
"VSS BUG: Rename logged as $entry->{version}, ".
"actually $item_version->{VersionNumber}\n"
if $item_version &&
$item_version->{VersionNumber} != $entry->{version};
} elsif ($entry->{command} eq 'Copied') {
$item_version = get_version_at $vss_item, $entry->{version}-1
unless $item_version &&
$item_version->{Action} =~ /^Shared .*\/([^\/]+)$/ &&
$1 eq $entry->{name};
}
die "Cannot find version \$$msg_path:$entry->{version}\n" unless $item_version;
# Verify the action - it must match the logged one
my $item_action = $item_version->{Action};
my $item_command;
my $pin_version;
if ($item_action =~ /^Pinned.*to version (\d+)$/) {
$item_command = 'Shared';
$pin_version = $1;
} elsif ($item_action =~ /^Unpinned/) {
$item_command = 'Shared';
$entry->{unpin} = 1;
} elsif ($item_action =~ /^(\S+)/) {
$item_command = $1;
}
$item_command = 'Copied'
if $item_command eq 'Shared' && $entry->{command} eq 'Copied';
die "Command mismatch: $item_action vs $entry->{command} at \$$msg_path:$entry->{version}\n"
unless $item_command eq $entry->{command};
# Check if this action is known to have been done via git-vss
$action_lookup_stmt = $dbh->prepare(
'SELECT vss_action FROM known_actions '.
'WHERE vss_physid = ? AND vss_version = ? AND is_imported = 0'
)
unless $action_lookup_stmt;
$action_lookup_stmt->execute(
$item_version->{VSSItem}->{Physical},
$item_version->{VersionNumber}
);
if (my $row = $action_lookup_stmt->fetchrow_arrayref()) {
$row->[0] = $item_command
if $row->[0] eq 'Added' && $item_command eq 'Recovered';
($row->[0] eq $item_command)
or die "$entry->{path}:$entry->{version} - mismatch with known '$row->[0]':\n$item_action\n";
print STDERR "$entry->{path}:$entry->{version} - known, skipping.\n";
return undef;
}
# Prepare data to add this action into the table
$entry->{known_action_info} = [
$item_version->{VSSItem}->{Physical},
$item_version->{VersionNumber},
$item_command,
$entry->{git_path}
];
# Process the action in detail
if ($entry->{command} =~ /Deleted|Destroyed/) {
print STDERR "del $entry->{path}/$entry->{name}\n";
return [ 'del', $entry->{git_path}.'/'.$entry->{name}, $item_version, $entry ];
} elsif ($entry->{command} eq 'Checked') {
print STDERR "edit $entry->{path}:$entry->{version}\n";
return [ 'edit', $entry->{git_path}, $item_version, $entry ];
} elsif ($entry->{command} eq 'Renamed') {
print STDERR "rename $entry->{path}/$entry->{name} -> $entry->{new_name}\n";
return [ 'rename:'.$entry->{new_name}, $entry->{git_path}."/".$entry->{name},
$item_version, $entry ];
} else {
my $subpath = $entry->{git_path}.'/'.$entry->{name};
my ($act_name, $act_fdel) = alter_path $entry->{alterations}, $entry->{path}, $entry->{name};
if ($act_fdel < 0) {
print STDERR "Skipping $entry->{command} on $entry->{path}/$entry->{name} - object destroyed.\n";
return undef;
}
my $file_item = $item_version->{VSSItem}->Child($entry->{name}, 0)
or die "$entry->{name} not found in $act_path:$entry->{version}\n";
if ($entry->{command} eq 'Added') {
print STDERR "add $subpath:1\n";
return [ 'add', $subpath, get_version_at($file_item, 1), $entry ];
} elsif ($entry->{command} =~ /Shared|Recovered|Copied/) {
die "Sharing/recovering a folder"
if $file_item->{Type} == VSSITEM_PROJECT;
my $ver = $pin_version
? get_version_at($file_item, $pin_version)
: get_last_before($file_item, get_timestamp ($item_version));
print STDERR "share/add $subpath:$ver->{VersionNumber}\n";
return [ 'share', $subpath, $ver, $entry,
get_timestamp($item_version),
($entry->{command} eq 'Shared' ? $ver->{Username}.': ' : '') . $ver->{Comment},
$item_version->{Username} ];
} else {
die "Invalid command $entry->{command}\n";
}
}
}
###########################################################
# FILENAMES
my %canonical_dirs;
my %canonical_files;
my $get_file_stmt;
my $add_file_stmt;
my $mark_dir_stmt;
sub fetch_file_names() {
return if $add_file_stmt;
$dbh->do('LOCK TABLE file_names IN ROW EXCLUSIVE MODE') if $is_postgres;
$add_file_stmt = $dbh->prepare('INSERT INTO file_names VALUES (?, ?, ?)');
$mark_dir_stmt = $dbh->prepare('UPDATE file_names SET is_folder = 1 WHERE key_name = ?');
%canonical_dirs = ();
%canonical_files = ();
for my $line (@{$dbh->selectall_arrayref(<<QUERY)})
SELECT key_name, canonical_name, is_folder FROM file_names
QUERY
{
if ($line->[2]) {
$canonical_dirs{$line->[0]} = 1;
}
$canonical_files{$line->[0]} = $line->[1];
}
}
sub open_file_names() {
return if $add_file_stmt;
$dbh->do('LOCK TABLE file_names IN ROW EXCLUSIVE MODE') if $is_postgres;
$get_file_stmt = $dbh->prepare('SELECT canonical_name, is_folder FROM file_names WHERE key_name = ?');
$add_file_stmt = $dbh->prepare('INSERT INTO file_names VALUES (?, ?, ?)');
$mark_dir_stmt = $dbh->prepare('UPDATE file_names SET is_folder = 1 WHERE key_name = ?');
%canonical_dirs = ();
%canonical_files = ();
}
sub add_file_path($;$) {
my ($new_path, $is_dir) = @_;
my $lnew_path = lc $new_path;
if ($canonical_files{$lnew_path}) {
if ($is_dir && !$canonical_dirs{$lnew_path}) {
$canonical_dirs{$lnew_path} = 1;
$mark_dir_stmt->execute($lnew_path);
}
return $canonical_files{$lnew_path};
}
if ($get_file_stmt) {
$get_file_stmt->execute($lnew_path);
my $row = $get_file_stmt->fetchrow_arrayref();
if ($row) {
my ($name, $fldr) = @$row;
$canonical_dirs{$lnew_path} = 1 if $fldr;
return $canonical_files{$lnew_path} = $name;
}
}
my $npath;
if ($new_path =~ /^(.+)\/([^\/]+)$/) {
my ($dir, $file) = ($1, $2);
$npath = &add_file_path($dir, 1).'/'.$file;
} else {
$npath = $new_path;
}
$canonical_dirs{$lnew_path} = 1 if $is_dir;
$add_file_stmt->execute($lnew_path, $npath, $is_dir||0);
return $canonical_files{$lnew_path} = $npath;
}
sub force_file_names($$$) {
my ($dir, $oldpath, $newpath) = @_;
my @oldlist = split /\//, $oldpath;
my @newlist = split /\//, $newpath;
die "Invalid path pair: $oldpath -> $newpath\n"
unless @oldlist == @newlist && length($oldpath) == length($newpath);
while (@oldlist) {
rename $dir.'/'.$oldlist[0], $dir.'/'.$newlist[0]
unless $oldlist[0] eq $newlist[0];
$dir .= '/'.$newlist[0];
shift @oldlist;
shift @newlist;
}
}
sub sanitize_adds() {
open_file_names();
open FH, "git-diff-index --diff-filter=A HEAD |"
or die "Could not list added files\n";
while (<FH>) {
chomp;
/^:(\d+) (\d+) ([a-f0-9]+) ([a-f0-9]+) (A)\t(.*)$/
or die "Bad diff line: $_";
my ($omode, $nmode, $oblob, $nblob, $op, $name) = ($1, $2, $3, $4, $5, $6);
my $path = add_file_path $name;
unless ($path eq $name) {
print STDERR "Invalid addition $name, must be $path\n";
system 'git-update-index', '--add', '--cacheinfo', $nmode, $nblob, $path;
($? == 0) or die "Index update failed\n";
system 'git-update-index', '--force-remove', $name;
force_file_names $work_dir, $name, $path;
}
}
}
my %current_files;
sub load_tree_listing($) {
my $tree = shift;
fetch_file_names();
%current_files = ();
open TREE, "git ls-tree -r --name-only --full-name $tree |";
while (<TREE>) {
chomp;
$current_files{lc $_} = $_;
my $path = add_file_path $_;
print STDERR "Filename mismatch: $_ vs canonical $path\n"
unless $path eq $_;
}
close TREE;
}
###########################################################
# AUTHORS
my %author_table;
sub load_author_table() {
$author_table{$_->[0]} = [ $_->[1], $_->[2] ]
for @{$dbh->selectall_arrayref(<<QUERY)};
SELECT vss_user, real_name, real_email FROM vss_users
QUERY
}
###########################################################
# GIT IMPORT
sub cache_file($$) {
my ($item, $path) = @_;
unless ($item && $item->{VSSItem}) {
print STDERR " - no data, skipping\n";
return;
}
unless ($item->{VSSItem}->{Type} == VSSITEM_FILE) {
die "Trying to cache a project: $item->{VSSItem}->{Spec} as $path";
}
my $name = tmpnam();
unlink $name;
$item->{VSSItem}->Get($name, VSSFLAG_FORCEDIRNO);
unless (-e $name) {
print STDERR " - get failed, skipping\n";
return;
}
my $sha = `git-hash-object -w \"$name\"`;
chomp $sha;
unlink $name;
system 'git-update-index', '--add', '--cacheinfo', '0644', $sha, $path;
}
sub cache_rename_file($$) {
my ($old_path, $new_path) = @_;
return if $old_path eq $new_path;
my $rv = `git-ls-files --stage -- \"$old_path\"`;
chomp $rv;
unless ($? == 0 && $rv =~ /^(\d+) ([a-f0-9]+) \d+\t(.+)$/) {
print STDERR " - old file not found for rename: $old_path\n";
return;
}
my ($mode, $sha, $name) = ($1, $2, $3);
$name eq $old_path
or die "Invalid ls-files result: $name instead of $old_path\n";
system 'git-update-index', '--force-remove', $old_path;
system 'git-update-index', '--add', '--cacheinfo', $mode, $sha, $new_path;
}
sub exec_action($\%\%\%%) {
my ($action, $delmap, $editmap, $cleanmap, %flags) = @_;
$action->[1] =~ s/^\/+//;
my $path = $current_files{lc $action->[1]};
if ($action->[0] eq 'del') {
unless ($path) {
print STDERR "Deleting unknown file $action->[1]\n";
return -1;
}
return 0 if $editmap->{$path} || $delmap->{$path};
$delmap->{$path}++;
return 2 if $flags{-no_exec};
$cleanmap->{$path}++;
print STDERR "Deleting $path\n";
system 'git-update-index', '--force-remove', $path;
} elsif ($action->[0] eq 'edit') {
unless ($path) {
print STDERR "Editing unknown file $action->[1] - adding\n";
$path = $current_files{lc $action->[1]} = add_file_path $action->[1];
}
return 0 if $editmap->{$path} || ($flags{-no_exec} && $delmap->{$path});
delete $delmap->{$path};
$editmap->{$path}++;
return 2 if $flags{-no_exec};
print STDERR "Editing $path\n";
cache_file $action->[2], $path;
} elsif ($action->[0] eq 'add' || $action->[0] eq 'share') {
$path = $current_files{lc $action->[1]} = add_file_path $action->[1]
unless $path;
return 0 if $editmap->{$path} || ($flags{-no_exec} && $delmap->{$path});
delete $delmap->{$path};
$editmap->{$path}++ unless $action->[3]->{unpin};
return 2 if $flags{-no_exec};
print STDERR "Adding $path\n";
cache_file $action->[2], $path;
} elsif ($action->[0] =~ /^rename:(.+)$/) {
my $new_name = $1;
$action->[1] =~ /^(.*)\/([^\/]+)$/;
my ($dir, $old_name) = ($1, $2);
$path = $current_files{lc $action->[1]} = add_file_path $action->[1]
unless $path;
my $new_path = add_file_path $dir.'/'.$new_name;
return 0 if $editmap->{$path} || $delmap->{$path} ||
$editmap->{$new_path} || $delmap->{$new_path};
$editmap->{$path}++;
$delmap->{$path}++;
$editmap->{$new_path}++;
return 2 if $flags{-no_exec};
$cleanmap->{$path}++;
print STDERR "Renaming $path to $new_path\n";
cache_rename_file $path, $new_path;
} else {
die "Invalid action '$action->[0]'";
}
return 1;
}
sub run_commit_tree($$@) {
my ($comment, $tree, @parents) = @_;
my $name = tmpnam();
open FH, '>', $name;
print FH $comment;
close FH;
my $parents = join('', map { ' -p '.$_; } @parents);
my $cmd = "git-commit-tree $tree $parents < \"$name\"";
#print "$_ => $ENV{$_}\n" for keys %ENV;
#print "$cmd\n";
my $rv = `$cmd`;
unlink $name;
die "Could not run git-commit-tree\n" if $?;
chomp $rv;
die "Invalid result of git-commit-tree: $rv\n" unless length($rv) == 40;
return $rv;
}
sub make_commit($\@) {
my ($old_head, $ractions) = @_;
my @commit_actions;
my %delmap;
my %editmap;
my %cleanmap;
# Find the first valid action
my $action;
do {
$action = shift @$ractions or return $old_head;
} while (exec_action($action, %delmap, %editmap, %cleanmap) <= 0);
push @commit_actions, $action;
# Main scan:
my $user = $action->[6];
my $time = $action->[4];
my $comment = $action->[0] eq 'share' ? undef : $action->[5];
my %shared_comments;
for (my $i = 0; $i <= $#$ractions; ) {
my $cur_action = $ractions->[$i];
# Stop if the time range is exceeded
$time ||= $cur_action->[4];
last if $cur_action->[4] && ($cur_action->[4] - $time) > 3600;
# Stop on actions of different users that clash with the current commit.
# Otherwise skip to redo later.
if ($cur_action->[6] ne $user) {
last if exec_action($cur_action, %delmap, %editmap, %cleanmap, -no_exec => 1) == 0;
$i++;
next;
}
# Unpin comments are ignored;
unless ($cur_action->[3]->{unpin}) {
# Share actions batch comments for individual files together
if ($cur_action->[0] eq 'share') {
last if $comment;
$shared_comments{$cur_action->[5]}++;
} else {
last if %shared_comments && $cur_action->[5];
# Break on changing comment for ordinary updates
$comment ||= $cur_action->[5];
last if $cur_action->[5] && $cur_action->[5] ne $comment;
}
}
# Execute the action, or stop on conflict
last if exec_action($cur_action, %delmap, %editmap, %cleanmap) == 0;
push @commit_actions, splice(@$ractions, $i, 1);
}
$comment = join("\n", sort keys %shared_comments)
if %shared_comments;
#delete $current_files{lc $_} for keys %cleanmap;
# Store the tree
my $tree_sha = `git-write-tree`;
chomp $tree_sha;
die "Failed to write tree: $tree_sha" unless length($tree_sha) == 40;