forked from gitster/git
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-cvsserver.perl
executable file
·2616 lines (2130 loc) · 85.9 KB
/
git-cvsserver.perl
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
####
#### This application is a CVS emulation layer for git.
#### It is intended for clients to connect over SSH.
#### See the documentation for more details.
####
#### Copyright The Open University UK - 2006.
####
#### Authors: Martyn Smith <martyn@catalyst.net.nz>
#### Martin Langhoff <martin@catalyst.net.nz>
####
####
#### Released under the GNU Public License, version 2.
####
####
use strict;
use warnings;
use Fcntl;
use File::Temp qw/tempdir tempfile/;
use File::Basename;
my $log = GITCVS::log->new();
my $cfg;
my $DATE_LIST = {
Jan => "01",
Feb => "02",
Mar => "03",
Apr => "04",
May => "05",
Jun => "06",
Jul => "07",
Aug => "08",
Sep => "09",
Oct => "10",
Nov => "11",
Dec => "12",
};
# Enable autoflush for STDOUT (otherwise the whole thing falls apart)
$| = 1;
#### Definition and mappings of functions ####
my $methods = {
'Root' => \&req_Root,
'Valid-responses' => \&req_Validresponses,
'valid-requests' => \&req_validrequests,
'Directory' => \&req_Directory,
'Entry' => \&req_Entry,
'Modified' => \&req_Modified,
'Unchanged' => \&req_Unchanged,
'Questionable' => \&req_Questionable,
'Argument' => \&req_Argument,
'Argumentx' => \&req_Argument,
'expand-modules' => \&req_expandmodules,
'add' => \&req_add,
'remove' => \&req_remove,
'co' => \&req_co,
'update' => \&req_update,
'ci' => \&req_ci,
'diff' => \&req_diff,
'log' => \&req_log,
'rlog' => \&req_log,
'tag' => \&req_CATCHALL,
'status' => \&req_status,
'admin' => \&req_CATCHALL,
'history' => \&req_CATCHALL,
'watchers' => \&req_CATCHALL,
'editors' => \&req_CATCHALL,
'annotate' => \&req_annotate,
'Global_option' => \&req_Globaloption,
#'annotate' => \&req_CATCHALL,
};
##############################################
# $state holds all the bits of information the clients sends us that could
# potentially be useful when it comes to actually _doing_ something.
my $state = {};
$log->info("--------------- STARTING -----------------");
my $TEMP_DIR = tempdir( CLEANUP => 1 );
$log->debug("Temporary directory is '$TEMP_DIR'");
# if we are called with a pserver argument,
# deal with the authentication cat before entereing the
# main loop
if (@ARGV && $ARGV[0] eq 'pserver') {
my $line = <STDIN>; chomp $line;
unless( $line eq 'BEGIN AUTH REQUEST') {
die "E Do not understand $line - expecting BEGIN AUTH REQUEST\n";
}
$line = <STDIN>; chomp $line;
req_Root('root', $line) # reuse Root
or die "E Invalid root $line \n";
$line = <STDIN>; chomp $line;
unless ($line eq 'anonymous') {
print "E Only anonymous user allowed via pserver\n";
print "I HATE YOU\n";
}
$line = <STDIN>; chomp $line; # validate the password?
$line = <STDIN>; chomp $line;
unless ($line eq 'END AUTH REQUEST') {
die "E Do not understand $line -- expecting END AUTH REQUEST\n";
}
print "I LOVE YOU\n";
# and now back to our regular programme...
}
# Keep going until the client closes the connection
while (<STDIN>)
{
chomp;
# Check to see if we've seen this method, and call appropiate function.
if ( /^([\w-]+)(?:\s+(.*))?$/ and defined($methods->{$1}) )
{
# use the $methods hash to call the appropriate sub for this command
#$log->info("Method : $1");
&{$methods->{$1}}($1,$2);
} else {
# log fatal because we don't understand this function. If this happens
# we're fairly screwed because we don't know if the client is expecting
# a response. If it is, the client will hang, we'll hang, and the whole
# thing will be custard.
$log->fatal("Don't understand command $_\n");
die("Unknown command $_");
}
}
$log->debug("Processing time : user=" . (times)[0] . " system=" . (times)[1]);
$log->info("--------------- FINISH -----------------");
# Magic catchall method.
# This is the method that will handle all commands we haven't yet
# implemented. It simply sends a warning to the log file indicating a
# command that hasn't been implemented has been invoked.
sub req_CATCHALL
{
my ( $cmd, $data ) = @_;
$log->warn("Unhandled command : req_$cmd : $data");
}
# Root pathname \n
# Response expected: no. Tell the server which CVSROOT to use. Note that
# pathname is a local directory and not a fully qualified CVSROOT variable.
# pathname must already exist; if creating a new root, use the init
# request, not Root. pathname does not include the hostname of the server,
# how to access the server, etc.; by the time the CVS protocol is in use,
# connection, authentication, etc., are already taken care of. The Root
# request must be sent only once, and it must be sent before any requests
# other than Valid-responses, valid-requests, UseUnchanged, Set or init.
sub req_Root
{
my ( $cmd, $data ) = @_;
$log->debug("req_Root : $data");
$state->{CVSROOT} = $data;
$ENV{GIT_DIR} = $state->{CVSROOT} . "/";
unless (-d $ENV{GIT_DIR} && -e $ENV{GIT_DIR}.'HEAD') {
print "E $ENV{GIT_DIR} does not seem to be a valid GIT repository\n";
print "E \n";
print "error 1 $ENV{GIT_DIR} is not a valid repository\n";
return 0;
}
my @gitvars = `git-var -l`;
if ($?) {
print "E problems executing git-var on the server -- this is not a git repository or the PATH is not set correcly.\n";
print "E \n";
print "error 1 - problem executing git-var\n";
return 0;
}
foreach my $line ( @gitvars )
{
next unless ( $line =~ /^(.*?)\.(.*?)=(.*)$/ );
$cfg->{$1}{$2} = $3;
}
unless ( defined ( $cfg->{gitcvs}{enabled} ) and $cfg->{gitcvs}{enabled} =~ /^\s*(1|true|yes)\s*$/i )
{
print "E GITCVS emulation needs to be enabled on this repo\n";
print "E the repo config file needs a [gitcvs] section added, and the parameter 'enabled' set to 1\n";
print "E \n";
print "error 1 GITCVS emulation disabled\n";
return 0;
}
if ( defined ( $cfg->{gitcvs}{logfile} ) )
{
$log->setfile($cfg->{gitcvs}{logfile});
} else {
$log->nofile();
}
return 1;
}
# Global_option option \n
# Response expected: no. Transmit one of the global options `-q', `-Q',
# `-l', `-t', `-r', or `-n'. option must be one of those strings, no
# variations (such as combining of options) are allowed. For graceful
# handling of valid-requests, it is probably better to make new global
# options separate requests, rather than trying to add them to this
# request.
sub req_Globaloption
{
my ( $cmd, $data ) = @_;
$log->debug("req_Globaloption : $data");
# TODO : is this data useful ???
}
# Valid-responses request-list \n
# Response expected: no. Tell the server what responses the client will
# accept. request-list is a space separated list of tokens.
sub req_Validresponses
{
my ( $cmd, $data ) = @_;
$log->debug("req_Validrepsonses : $data");
# TODO : re-enable this, currently it's not particularly useful
#$state->{validresponses} = [ split /\s+/, $data ];
}
# valid-requests \n
# Response expected: yes. Ask the server to send back a Valid-requests
# response.
sub req_validrequests
{
my ( $cmd, $data ) = @_;
$log->debug("req_validrequests");
$log->debug("SEND : Valid-requests " . join(" ",keys %$methods));
$log->debug("SEND : ok");
print "Valid-requests " . join(" ",keys %$methods) . "\n";
print "ok\n";
}
# Directory local-directory \n
# Additional data: repository \n. Response expected: no. Tell the server
# what directory to use. The repository should be a directory name from a
# previous server response. Note that this both gives a default for Entry
# and Modified and also for ci and the other commands; normal usage is to
# send Directory for each directory in which there will be an Entry or
# Modified, and then a final Directory for the original directory, then the
# command. The local-directory is relative to the top level at which the
# command is occurring (i.e. the last Directory which is sent before the
# command); to indicate that top level, `.' should be sent for
# local-directory.
sub req_Directory
{
my ( $cmd, $data ) = @_;
my $repository = <STDIN>;
chomp $repository;
$state->{localdir} = $data;
$state->{repository} = $repository;
$state->{directory} = $repository;
$state->{directory} =~ s/^$state->{CVSROOT}\///;
$state->{module} = $1 if ($state->{directory} =~ s/^(.*?)(\/|$)//);
$state->{directory} .= "/" if ( $state->{directory} =~ /\S/ );
$log->debug("req_Directory : localdir=$data repository=$repository directory=$state->{directory} module=$state->{module}");
}
# Entry entry-line \n
# Response expected: no. Tell the server what version of a file is on the
# local machine. The name in entry-line is a name relative to the directory
# most recently specified with Directory. If the user is operating on only
# some files in a directory, Entry requests for only those files need be
# included. If an Entry request is sent without Modified, Is-modified, or
# Unchanged, it means the file is lost (does not exist in the working
# directory). If both Entry and one of Modified, Is-modified, or Unchanged
# are sent for the same file, Entry must be sent first. For a given file,
# one can send Modified, Is-modified, or Unchanged, but not more than one
# of these three.
sub req_Entry
{
my ( $cmd, $data ) = @_;
$log->debug("req_Entry : $data");
my @data = split(/\//, $data);
$state->{entries}{$state->{directory}.$data[1]} = {
revision => $data[2],
conflict => $data[3],
options => $data[4],
tag_or_date => $data[5],
};
}
# add \n
# Response expected: yes. Add a file or directory. This uses any previous
# Argument, Directory, Entry, or Modified requests, if they have been sent.
# The last Directory sent specifies the working directory at the time of
# the operation. To add a directory, send the directory to be added using
# Directory and Argument requests.
sub req_add
{
my ( $cmd, $data ) = @_;
argsplit("add");
my $addcount = 0;
foreach my $filename ( @{$state->{args}} )
{
$filename = filecleanup($filename);
unless ( defined ( $state->{entries}{$filename}{modified_filename} ) )
{
print "E cvs add: nothing known about `$filename'\n";
next;
}
# TODO : check we're not squashing an already existing file
if ( defined ( $state->{entries}{$filename}{revision} ) )
{
print "E cvs add: `$filename' has already been entered\n";
next;
}
my ( $filepart, $dirpart ) = filenamesplit($filename);
print "E cvs add: scheduling file `$filename' for addition\n";
print "Checked-in $dirpart\n";
print "$filename\n";
print "/$filepart/0///\n";
$addcount++;
}
if ( $addcount == 1 )
{
print "E cvs add: use `cvs commit' to add this file permanently\n";
}
elsif ( $addcount > 1 )
{
print "E cvs add: use `cvs commit' to add these files permanently\n";
}
print "ok\n";
}
# remove \n
# Response expected: yes. Remove a file. This uses any previous Argument,
# Directory, Entry, or Modified requests, if they have been sent. The last
# Directory sent specifies the working directory at the time of the
# operation. Note that this request does not actually do anything to the
# repository; the only effect of a successful remove request is to supply
# the client with a new entries line containing `-' to indicate a removed
# file. In fact, the client probably could perform this operation without
# contacting the server, although using remove may cause the server to
# perform a few more checks. The client sends a subsequent ci request to
# actually record the removal in the repository.
sub req_remove
{
my ( $cmd, $data ) = @_;
argsplit("remove");
# Grab a handle to the SQLite db and do any necessary updates
my $updater = GITCVS::updater->new($state->{CVSROOT}, $state->{module}, $log);
$updater->update();
#$log->debug("add state : " . Dumper($state));
my $rmcount = 0;
foreach my $filename ( @{$state->{args}} )
{
$filename = filecleanup($filename);
if ( defined ( $state->{entries}{$filename}{unchanged} ) or defined ( $state->{entries}{$filename}{modified_filename} ) )
{
print "E cvs remove: file `$filename' still in working directory\n";
next;
}
my $meta = $updater->getmeta($filename);
my $wrev = revparse($filename);
unless ( defined ( $wrev ) )
{
print "E cvs remove: nothing known about `$filename'\n";
next;
}
if ( defined($wrev) and $wrev < 0 )
{
print "E cvs remove: file `$filename' already scheduled for removal\n";
next;
}
unless ( $wrev == $meta->{revision} )
{
# TODO : not sure if the format of this message is quite correct.
print "E cvs remove: Up to date check failed for `$filename'\n";
next;
}
my ( $filepart, $dirpart ) = filenamesplit($filename);
print "E cvs remove: scheduling `$filename' for removal\n";
print "Checked-in $dirpart\n";
print "$filename\n";
print "/$filepart/-1.$wrev///\n";
$rmcount++;
}
if ( $rmcount == 1 )
{
print "E cvs remove: use `cvs commit' to remove this file permanently\n";
}
elsif ( $rmcount > 1 )
{
print "E cvs remove: use `cvs commit' to remove these files permanently\n";
}
print "ok\n";
}
# Modified filename \n
# Response expected: no. Additional data: mode, \n, file transmission. Send
# the server a copy of one locally modified file. filename is a file within
# the most recent directory sent with Directory; it must not contain `/'.
# If the user is operating on only some files in a directory, only those
# files need to be included. This can also be sent without Entry, if there
# is no entry for the file.
sub req_Modified
{
my ( $cmd, $data ) = @_;
my $mode = <STDIN>;
chomp $mode;
my $size = <STDIN>;
chomp $size;
# Grab config information
my $blocksize = 8192;
my $bytesleft = $size;
my $tmp;
# Get a filehandle/name to write it to
my ( $fh, $filename ) = tempfile( DIR => $TEMP_DIR );
# Loop over file data writing out to temporary file.
while ( $bytesleft )
{
$blocksize = $bytesleft if ( $bytesleft < $blocksize );
read STDIN, $tmp, $blocksize;
print $fh $tmp;
$bytesleft -= $blocksize;
}
close $fh;
# Ensure we have something sensible for the file mode
if ( $mode =~ /u=(\w+)/ )
{
$mode = $1;
} else {
$mode = "rw";
}
# Save the file data in $state
$state->{entries}{$state->{directory}.$data}{modified_filename} = $filename;
$state->{entries}{$state->{directory}.$data}{modified_mode} = $mode;
$state->{entries}{$state->{directory}.$data}{modified_hash} = `git-hash-object $filename`;
$state->{entries}{$state->{directory}.$data}{modified_hash} =~ s/\s.*$//s;
#$log->debug("req_Modified : file=$data mode=$mode size=$size");
}
# Unchanged filename \n
# Response expected: no. Tell the server that filename has not been
# modified in the checked out directory. The filename is a file within the
# most recent directory sent with Directory; it must not contain `/'.
sub req_Unchanged
{
my ( $cmd, $data ) = @_;
$state->{entries}{$state->{directory}.$data}{unchanged} = 1;
#$log->debug("req_Unchanged : $data");
}
# Questionable filename \n
# Response expected: no. Additional data: no.
# Tell the server to check whether filename should be ignored,
# and if not, next time the server sends responses, send (in
# a M response) `?' followed by the directory and filename.
# filename must not contain `/'; it needs to be a file in the
# directory named by the most recent Directory request.
sub req_Questionable
{
my ( $cmd, $data ) = @_;
$state->{entries}{$state->{directory}.$data}{questionable} = 1;
#$log->debug("req_Questionable : $data");
}
# Argument text \n
# Response expected: no. Save argument for use in a subsequent command.
# Arguments accumulate until an argument-using command is given, at which
# point they are forgotten.
# Argumentx text \n
# Response expected: no. Append \n followed by text to the current argument
# being saved.
sub req_Argument
{
my ( $cmd, $data ) = @_;
# TODO : Not quite sure how Argument and Argumentx differ, but I assume
# it's for multi-line arguments ... somehow ...
$log->debug("$cmd : $data");
push @{$state->{arguments}}, $data;
}
# expand-modules \n
# Response expected: yes. Expand the modules which are specified in the
# arguments. Returns the data in Module-expansion responses. Note that the
# server can assume that this is checkout or export, not rtag or rdiff; the
# latter do not access the working directory and thus have no need to
# expand modules on the client side. Expand may not be the best word for
# what this request does. It does not necessarily tell you all the files
# contained in a module, for example. Basically it is a way of telling you
# which working directories the server needs to know about in order to
# handle a checkout of the specified modules. For example, suppose that the
# server has a module defined by
# aliasmodule -a 1dir
# That is, one can check out aliasmodule and it will take 1dir in the
# repository and check it out to 1dir in the working directory. Now suppose
# the client already has this module checked out and is planning on using
# the co request to update it. Without using expand-modules, the client
# would have two bad choices: it could either send information about all
# working directories under the current directory, which could be
# unnecessarily slow, or it could be ignorant of the fact that aliasmodule
# stands for 1dir, and neglect to send information for 1dir, which would
# lead to incorrect operation. With expand-modules, the client would first
# ask for the module to be expanded:
sub req_expandmodules
{
my ( $cmd, $data ) = @_;
argsplit();
$log->debug("req_expandmodules : " . ( defined($data) ? $data : "[NULL]" ) );
unless ( ref $state->{arguments} eq "ARRAY" )
{
print "ok\n";
return;
}
foreach my $module ( @{$state->{arguments}} )
{
$log->debug("SEND : Module-expansion $module");
print "Module-expansion $module\n";
}
print "ok\n";
statecleanup();
}
# co \n
# Response expected: yes. Get files from the repository. This uses any
# previous Argument, Directory, Entry, or Modified requests, if they have
# been sent. Arguments to this command are module names; the client cannot
# know what directories they correspond to except by (1) just sending the
# co request, and then seeing what directory names the server sends back in
# its responses, and (2) the expand-modules request.
sub req_co
{
my ( $cmd, $data ) = @_;
argsplit("co");
my $module = $state->{args}[0];
my $checkout_path = $module;
# use the user specified directory if we're given it
$checkout_path = $state->{opt}{d} if ( exists ( $state->{opt}{d} ) );
$log->debug("req_co : " . ( defined($data) ? $data : "[NULL]" ) );
$log->info("Checking out module '$module' ($state->{CVSROOT}) to '$checkout_path'");
$ENV{GIT_DIR} = $state->{CVSROOT} . "/";
# Grab a handle to the SQLite db and do any necessary updates
my $updater = GITCVS::updater->new($state->{CVSROOT}, $module, $log);
$updater->update();
$checkout_path =~ s|/$||; # get rid of trailing slashes
# Eclipse seems to need the Clear-sticky command
# to prepare the 'Entries' file for the new directory.
print "Clear-sticky $checkout_path/\n";
print $state->{CVSROOT} . "/$module/\n";
print "Clear-static-directory $checkout_path/\n";
print $state->{CVSROOT} . "/$module/\n";
print "Clear-sticky $checkout_path/\n"; # yes, twice
print $state->{CVSROOT} . "/$module/\n";
print "Template $checkout_path/\n";
print $state->{CVSROOT} . "/$module/\n";
print "0\n";
# instruct the client that we're checking out to $checkout_path
print "E cvs checkout: Updating $checkout_path\n";
my %seendirs = ();
my $lastdir ='';
# recursive
sub prepdir {
my ($dir, $repodir, $remotedir, $seendirs) = @_;
my $parent = dirname($dir);
$dir =~ s|/+$||;
$repodir =~ s|/+$||;
$remotedir =~ s|/+$||;
$parent =~ s|/+$||;
$log->debug("announcedir $dir, $repodir, $remotedir" );
if ($parent eq '.' || $parent eq './') {
$parent = '';
}
# recurse to announce unseen parents first
if (length($parent) && !exists($seendirs->{$parent})) {
prepdir($parent, $repodir, $remotedir, $seendirs);
}
# Announce that we are going to modify at the parent level
if ($parent) {
print "E cvs checkout: Updating $remotedir/$parent\n";
} else {
print "E cvs checkout: Updating $remotedir\n";
}
print "Clear-sticky $remotedir/$parent/\n";
print "$repodir/$parent/\n";
print "Clear-static-directory $remotedir/$dir/\n";
print "$repodir/$dir/\n";
print "Clear-sticky $remotedir/$parent/\n"; # yes, twice
print "$repodir/$parent/\n";
print "Template $remotedir/$dir/\n";
print "$repodir/$dir/\n";
print "0\n";
$seendirs->{$dir} = 1;
}
foreach my $git ( @{$updater->gethead} )
{
# Don't want to check out deleted files
next if ( $git->{filehash} eq "deleted" );
( $git->{name}, $git->{dir} ) = filenamesplit($git->{name});
if (length($git->{dir}) && $git->{dir} ne './'
&& $git->{dir} ne $lastdir ) {
unless (exists($seendirs{$git->{dir}})) {
prepdir($git->{dir}, $state->{CVSROOT} . "/$module/",
$checkout_path, \%seendirs);
$lastdir = $git->{dir};
$seendirs{$git->{dir}} = 1;
}
print "E cvs checkout: Updating /$checkout_path/$git->{dir}\n";
}
# modification time of this file
print "Mod-time $git->{modified}\n";
# print some information to the client
if ( defined ( $git->{dir} ) and $git->{dir} ne "./" )
{
print "M U $checkout_path/$git->{dir}$git->{name}\n";
} else {
print "M U $checkout_path/$git->{name}\n";
}
# instruct client we're sending a file to put in this path
print "Created $checkout_path/" . ( defined ( $git->{dir} ) and $git->{dir} ne "./" ? $git->{dir} . "/" : "" ) . "\n";
print $state->{CVSROOT} . "/$module/" . ( defined ( $git->{dir} ) and $git->{dir} ne "./" ? $git->{dir} . "/" : "" ) . "$git->{name}\n";
# this is an "entries" line
print "/$git->{name}/1.$git->{revision}///\n";
# permissions
print "u=$git->{mode},g=$git->{mode},o=$git->{mode}\n";
# transmit file
transmitfile($git->{filehash});
}
print "ok\n";
statecleanup();
}
# update \n
# Response expected: yes. Actually do a cvs update command. This uses any
# previous Argument, Directory, Entry, or Modified requests, if they have
# been sent. The last Directory sent specifies the working directory at the
# time of the operation. The -I option is not used--files which the client
# can decide whether to ignore are not mentioned and the client sends the
# Questionable request for others.
sub req_update
{
my ( $cmd, $data ) = @_;
$log->debug("req_update : " . ( defined($data) ? $data : "[NULL]" ));
argsplit("update");
#
# It may just be a client exploring the available heads/modukles
# in that case, list them as top level directories and leave it
# at that. Eclipse uses this technique to offer you a list of
# projects (heads in this case) to checkout.
#
if ($state->{module} eq '') {
print "E cvs update: Updating .\n";
opendir HEADS, $state->{CVSROOT} . '/refs/heads';
while (my $head = readdir(HEADS)) {
if (-f $state->{CVSROOT} . '/refs/heads/' . $head) {
print "E cvs update: New directory `$head'\n";
}
}
closedir HEADS;
print "ok\n";
return 1;
}
# Grab a handle to the SQLite db and do any necessary updates
my $updater = GITCVS::updater->new($state->{CVSROOT}, $state->{module}, $log);
$updater->update();
# if no files were specified, we need to work out what files we should be providing status on ...
argsfromdir($updater) if ( scalar ( @{$state->{args}} ) == 0 );
#$log->debug("update state : " . Dumper($state));
# foreach file specified on the commandline ...
foreach my $filename ( @{$state->{args}} )
{
$filename = filecleanup($filename);
# if we have a -C we should pretend we never saw modified stuff
if ( exists ( $state->{opt}{C} ) )
{
delete $state->{entries}{$filename}{modified_hash};
delete $state->{entries}{$filename}{modified_filename};
$state->{entries}{$filename}{unchanged} = 1;
}
my $meta;
if ( defined($state->{opt}{r}) and $state->{opt}{r} =~ /^1\.(\d+)/ )
{
$meta = $updater->getmeta($filename, $1);
} else {
$meta = $updater->getmeta($filename);
}
next unless ( $meta->{revision} );
my $oldmeta = $meta;
my $wrev = revparse($filename);
# If the working copy is an old revision, lets get that version too for comparison.
if ( defined($wrev) and $wrev != $meta->{revision} )
{
$oldmeta = $updater->getmeta($filename, $wrev);
}
#$log->debug("Target revision is $meta->{revision}, current working revision is $wrev");
# Files are up to date if the working copy and repo copy have the same revision,
# and the working copy is unmodified _and_ the user hasn't specified -C
next if ( defined ( $wrev )
and defined($meta->{revision})
and $wrev == $meta->{revision}
and $state->{entries}{$filename}{unchanged}
and not exists ( $state->{opt}{C} ) );
# If the working copy and repo copy have the same revision,
# but the working copy is modified, tell the client it's modified
if ( defined ( $wrev )
and defined($meta->{revision})
and $wrev == $meta->{revision}
and not exists ( $state->{opt}{C} ) )
{
$log->info("Tell the client the file is modified");
print "MT text U\n";
print "MT fname $filename\n";
print "MT newline\n";
next;
}
if ( $meta->{filehash} eq "deleted" )
{
my ( $filepart, $dirpart ) = filenamesplit($filename);
$log->info("Removing '$filename' from working copy (no longer in the repo)");
print "E cvs update: `$filename' is no longer in the repository\n";
print "Removed $dirpart\n";
print "$filepart\n";
}
elsif ( not defined ( $state->{entries}{$filename}{modified_hash} )
or $state->{entries}{$filename}{modified_hash} eq $oldmeta->{filehash} )
{
$log->info("Updating '$filename'");
# normal update, just send the new revision (either U=Update, or A=Add, or R=Remove)
print "MT +updated\n";
print "MT text U\n";
print "MT fname $filename\n";
print "MT newline\n";
print "MT -updated\n";
my ( $filepart, $dirpart ) = filenamesplit($filename);
$dirpart =~ s/^$state->{directory}//;
if ( defined ( $wrev ) )
{
# instruct client we're sending a file to put in this path as a replacement
print "Update-existing $dirpart\n";
$log->debug("Updating existing file 'Update-existing $dirpart'");
} else {
# instruct client we're sending a file to put in this path as a new file
print "Created $dirpart\n";
$log->debug("Creating new file 'Created $dirpart'");
}
print $state->{CVSROOT} . "/$state->{module}/$filename\n";
# this is an "entries" line
$log->debug("/$filepart/1.$meta->{revision}///");
print "/$filepart/1.$meta->{revision}///\n";
# permissions
$log->debug("SEND : u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}");
print "u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}\n";
# transmit file
transmitfile($meta->{filehash});
} else {
$log->info("Updating '$filename'");
my ( $filepart, $dirpart ) = filenamesplit($meta->{name});
my $dir = tempdir( DIR => $TEMP_DIR, CLEANUP => 1 ) . "/";
chdir $dir;
my $file_local = $filepart . ".mine";
system("ln","-s",$state->{entries}{$filename}{modified_filename}, $file_local);
my $file_old = $filepart . "." . $oldmeta->{revision};
transmitfile($oldmeta->{filehash}, $file_old);
my $file_new = $filepart . "." . $meta->{revision};
transmitfile($meta->{filehash}, $file_new);
# we need to merge with the local changes ( M=successful merge, C=conflict merge )
$log->info("Merging $file_local, $file_old, $file_new");
$log->debug("Temporary directory for merge is $dir");
my $return = system("merge", $file_local, $file_old, $file_new);
$return >>= 8;
if ( $return == 0 )
{
$log->info("Merged successfully");
print "M M $filename\n";
$log->debug("Update-existing $dirpart");
print "Update-existing $dirpart\n";
$log->debug($state->{CVSROOT} . "/$state->{module}/$filename");
print $state->{CVSROOT} . "/$state->{module}/$filename\n";
$log->debug("/$filepart/1.$meta->{revision}///");
print "/$filepart/1.$meta->{revision}///\n";
}
elsif ( $return == 1 )
{
$log->info("Merged with conflicts");
print "M C $filename\n";
print "Update-existing $dirpart\n";
print $state->{CVSROOT} . "/$state->{module}/$filename\n";
print "/$filepart/1.$meta->{revision}/+//\n";
}
else
{
$log->warn("Merge failed");
next;
}
# permissions
$log->debug("SEND : u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}");
print "u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}\n";
# transmit file, format is single integer on a line by itself (file
# size) followed by the file contents
# TODO : we should copy files in blocks
my $data = `cat $file_local`;
$log->debug("File size : " . length($data));
print length($data) . "\n";
print $data;
chdir "/";
}
}
print "ok\n";
}
sub req_ci
{
my ( $cmd, $data ) = @_;
argsplit("ci");
#$log->debug("State : " . Dumper($state));
$log->info("req_ci : " . ( defined($data) ? $data : "[NULL]" ));
if ( @ARGV && $ARGV[0] eq 'pserver')
{
print "error 1 pserver access cannot commit\n";
exit;
}
if ( -e $state->{CVSROOT} . "/index" )
{
print "error 1 Index already exists in git repo\n";
exit;
}
my $lockfile = "$state->{CVSROOT}/refs/heads/$state->{module}.lock";
unless ( sysopen(LOCKFILE,$lockfile,O_EXCL|O_CREAT|O_WRONLY) )
{
print "error 1 Lock file '$lockfile' already exists, please try again\n";
exit;
}
# Grab a handle to the SQLite db and do any necessary updates
my $updater = GITCVS::updater->new($state->{CVSROOT}, $state->{module}, $log);
$updater->update();
my $tmpdir = tempdir ( DIR => $TEMP_DIR );
my ( undef, $file_index ) = tempfile ( DIR => $TEMP_DIR, OPEN => 0 );
$log->info("Lock successful, basing commit on '$tmpdir', index file is '$file_index'");
$ENV{GIT_DIR} = $state->{CVSROOT} . "/";
$ENV{GIT_INDEX_FILE} = $file_index;
chdir $tmpdir;
# populate the temporary index based
system("git-read-tree", $state->{module});
unless ($? == 0)
{
die "Error running git-read-tree $state->{module} $file_index $!";
}
$log->info("Created index '$file_index' with for head $state->{module} - exit status $?");
my @committedfiles = ();
# foreach file specified on the commandline ...
foreach my $filename ( @{$state->{args}} )
{
$filename = filecleanup($filename);
next unless ( exists $state->{entries}{$filename}{modified_filename} or not $state->{entries}{$filename}{unchanged} );
my $meta = $updater->getmeta($filename);
my $wrev = revparse($filename);
my ( $filepart, $dirpart ) = filenamesplit($filename);