1#!/usr/bin/perl 2 3#### 4#### This application is a CVS emulation layer for git. 5#### It is intended for clients to connect over SSH. 6#### See the documentation for more details. 7#### 8#### Copyright The Open University UK - 2006. 9#### 10#### Authors: Martyn Smith <martyn@catalyst.net.nz> 11#### Martin Langhoff <martin@laptop.org> 12#### 13#### 14#### Released under the GNU Public License, version 2. 15#### 16#### 17 18use5.008; 19use strict; 20use warnings; 21use bytes; 22 23use Fcntl; 24use File::Temp qw/tempdir tempfile/; 25use File::Path qw/rmtree/; 26use File::Basename; 27use Getopt::Long qw(:config require_order no_ignore_case); 28 29my$VERSION='@@GIT_VERSION@@'; 30 31my$log= GITCVS::log->new(); 32my$cfg; 33 34my$DATE_LIST= { 35 Jan =>"01", 36 Feb =>"02", 37 Mar =>"03", 38 Apr =>"04", 39 May =>"05", 40 Jun =>"06", 41 Jul =>"07", 42 Aug =>"08", 43 Sep =>"09", 44 Oct =>"10", 45 Nov =>"11", 46 Dec =>"12", 47}; 48 49# Enable autoflush for STDOUT (otherwise the whole thing falls apart) 50$| =1; 51 52#### Definition and mappings of functions #### 53 54my$methods= { 55'Root'=> \&req_Root, 56'Valid-responses'=> \&req_Validresponses, 57'valid-requests'=> \&req_validrequests, 58'Directory'=> \&req_Directory, 59'Entry'=> \&req_Entry, 60'Modified'=> \&req_Modified, 61'Unchanged'=> \&req_Unchanged, 62'Questionable'=> \&req_Questionable, 63'Argument'=> \&req_Argument, 64'Argumentx'=> \&req_Argument, 65'expand-modules'=> \&req_expandmodules, 66'add'=> \&req_add, 67'remove'=> \&req_remove, 68'co'=> \&req_co, 69'update'=> \&req_update, 70'ci'=> \&req_ci, 71'diff'=> \&req_diff, 72'log'=> \&req_log, 73'rlog'=> \&req_log, 74'tag'=> \&req_CATCHALL, 75'status'=> \&req_status, 76'admin'=> \&req_CATCHALL, 77'history'=> \&req_CATCHALL, 78'watchers'=> \&req_EMPTY, 79'editors'=> \&req_EMPTY, 80'noop'=> \&req_EMPTY, 81'annotate'=> \&req_annotate, 82'Global_option'=> \&req_Globaloption, 83#'annotate' => \&req_CATCHALL, 84}; 85 86############################################## 87 88 89# $state holds all the bits of information the clients sends us that could 90# potentially be useful when it comes to actually _doing_ something. 91my$state= { prependdir =>''}; 92 93# Work is for managing temporary working directory 94my$work= 95{ 96state=>undef,# undef, 1 (empty), 2 (with stuff) 97 workDir =>undef, 98index=>undef, 99 emptyDir =>undef, 100 tmpDir =>undef 101}; 102 103$log->info("--------------- STARTING -----------------"); 104 105my$usage= 106"Usage: git cvsserver [options] [pserver|server] [<directory> ...]\n". 107" --base-path <path> : Prepend to requested CVSROOT\n". 108" Can be read from GIT_CVSSERVER_BASE_PATH\n". 109" --strict-paths : Don't allow recursing into subdirectories\n". 110" --export-all : Don't check for gitcvs.enabled in config\n". 111" --version, -V : Print version information and exit\n". 112" -h, -H : Print usage information and exit\n". 113"\n". 114"<directory> ... is a list of allowed directories. If no directories\n". 115"are given, all are allowed. This is an additional restriction, gitcvs\n". 116"access still needs to be enabled by the gitcvs.enabled config option.\n". 117"Alternately, one directory may be specified in GIT_CVSSERVER_ROOT.\n"; 118 119my@opts= ('h|H','version|V', 120'base-path=s','strict-paths','export-all'); 121GetOptions($state,@opts) 122or die$usage; 123 124if($state->{version}) { 125print"git-cvsserver version$VERSION\n"; 126exit; 127} 128if($state->{help}) { 129print$usage; 130exit; 131} 132 133my$TEMP_DIR= tempdir( CLEANUP =>1); 134$log->debug("Temporary directory is '$TEMP_DIR'"); 135 136$state->{method} ='ext'; 137if(@ARGV) { 138if($ARGV[0]eq'pserver') { 139$state->{method} ='pserver'; 140shift@ARGV; 141}elsif($ARGV[0]eq'server') { 142shift@ARGV; 143} 144} 145 146# everything else is a directory 147$state->{allowed_roots} = [@ARGV]; 148 149# don't export the whole system unless the users requests it 150if($state->{'export-all'} && !@{$state->{allowed_roots}}) { 151die"--export-all can only be used together with an explicit whitelist\n"; 152} 153 154# Environment handling for running under git-shell 155if(exists$ENV{GIT_CVSSERVER_BASE_PATH}) { 156if($state->{'base-path'}) { 157die"Cannot specify base path both ways.\n"; 158} 159my$base_path=$ENV{GIT_CVSSERVER_BASE_PATH}; 160$state->{'base-path'} =$base_path; 161$log->debug("Picked up base path '$base_path' from environment.\n"); 162} 163if(exists$ENV{GIT_CVSSERVER_ROOT}) { 164if(@{$state->{allowed_roots}}) { 165die"Cannot specify roots both ways:@ARGV\n"; 166} 167my$allowed_root=$ENV{GIT_CVSSERVER_ROOT}; 168$state->{allowed_roots} = [$allowed_root]; 169$log->debug("Picked up allowed root '$allowed_root' from environment.\n"); 170} 171 172# if we are called with a pserver argument, 173# deal with the authentication cat before entering the 174# main loop 175if($state->{method}eq'pserver') { 176my$line= <STDIN>;chomp$line; 177unless($line=~/^BEGIN (AUTH|VERIFICATION) REQUEST$/) { 178die"E Do not understand$line- expecting BEGIN AUTH REQUEST\n"; 179} 180my$request=$1; 181$line= <STDIN>;chomp$line; 182unless(req_Root('root',$line)) {# reuse Root 183print"E Invalid root$line\n"; 184exit1; 185} 186$line= <STDIN>;chomp$line; 187my$user=$line; 188$line= <STDIN>;chomp$line; 189my$password=$line; 190 191if($usereq'anonymous') { 192# "A" will be 1 byte, use length instead in case the 193# encryption method ever changes (yeah, right!) 194if(length($password) >1) { 195print"E Don't supply a password for the `anonymous' user\n"; 196print"I HATE YOU\n"; 197exit1; 198} 199 200# Fall through to LOVE 201}else{ 202# Trying to authenticate a user 203if(not exists$cfg->{gitcvs}->{authdb}) { 204print"E the repo config file needs a [gitcvs] section with an 'authdb' parameter set to the filename of the authentication database\n"; 205print"I HATE YOU\n"; 206exit1; 207} 208 209my$authdb=$cfg->{gitcvs}->{authdb}; 210 211unless(-e $authdb) { 212print"E The authentication database specified in [gitcvs.authdb] does not exist\n"; 213print"I HATE YOU\n"; 214exit1; 215} 216 217my$auth_ok; 218open my$passwd,"<",$authdbor die$!; 219while(<$passwd>) { 220if(m{^\Q$user\E:(.*)}) { 221if(crypt($user, descramble($password))eq$1) { 222$auth_ok=1; 223} 224}; 225} 226close$passwd; 227 228unless($auth_ok) { 229print"I HATE YOU\n"; 230exit1; 231} 232 233# Fall through to LOVE 234} 235 236# For checking whether the user is anonymous on commit 237$state->{user} =$user; 238 239$line= <STDIN>;chomp$line; 240unless($lineeq"END$requestREQUEST") { 241die"E Do not understand$line-- expecting END$requestREQUEST\n"; 242} 243print"I LOVE YOU\n"; 244exit if$requesteq'VERIFICATION';# cvs login 245# and now back to our regular programme... 246} 247 248# Keep going until the client closes the connection 249while(<STDIN>) 250{ 251chomp; 252 253# Check to see if we've seen this method, and call appropriate function. 254if(/^([\w-]+)(?:\s+(.*))?$/and defined($methods->{$1}) ) 255{ 256# use the $methods hash to call the appropriate sub for this command 257#$log->info("Method : $1"); 258&{$methods->{$1}}($1,$2); 259}else{ 260# log fatal because we don't understand this function. If this happens 261# we're fairly screwed because we don't know if the client is expecting 262# a response. If it is, the client will hang, we'll hang, and the whole 263# thing will be custard. 264$log->fatal("Don't understand command$_\n"); 265die("Unknown command$_"); 266} 267} 268 269$log->debug("Processing time : user=". (times)[0] ." system=". (times)[1]); 270$log->info("--------------- FINISH -----------------"); 271 272chdir'/'; 273exit0; 274 275# Magic catchall method. 276# This is the method that will handle all commands we haven't yet 277# implemented. It simply sends a warning to the log file indicating a 278# command that hasn't been implemented has been invoked. 279sub req_CATCHALL 280{ 281my($cmd,$data) =@_; 282$log->warn("Unhandled command : req_$cmd:$data"); 283} 284 285# This method invariably succeeds with an empty response. 286sub req_EMPTY 287{ 288print"ok\n"; 289} 290 291# Root pathname \n 292# Response expected: no. Tell the server which CVSROOT to use. Note that 293# pathname is a local directory and not a fully qualified CVSROOT variable. 294# pathname must already exist; if creating a new root, use the init 295# request, not Root. pathname does not include the hostname of the server, 296# how to access the server, etc.; by the time the CVS protocol is in use, 297# connection, authentication, etc., are already taken care of. The Root 298# request must be sent only once, and it must be sent before any requests 299# other than Valid-responses, valid-requests, UseUnchanged, Set or init. 300sub req_Root 301{ 302my($cmd,$data) =@_; 303$log->debug("req_Root :$data"); 304 305unless($data=~ m#^/#) { 306print"error 1 Root must be an absolute pathname\n"; 307return0; 308} 309 310my$cvsroot=$state->{'base-path'} ||''; 311$cvsroot=~ s#/+$##; 312$cvsroot.=$data; 313 314if($state->{CVSROOT} 315&& ($state->{CVSROOT}ne$cvsroot)) { 316print"error 1 Conflicting roots specified\n"; 317return0; 318} 319 320$state->{CVSROOT} =$cvsroot; 321 322$ENV{GIT_DIR} =$state->{CVSROOT} ."/"; 323 324if(@{$state->{allowed_roots}}) { 325my$allowed=0; 326foreachmy$dir(@{$state->{allowed_roots}}) { 327next unless$dir=~ m#^/#; 328$dir=~ s#/+$##; 329if($state->{'strict-paths'}) { 330if($ENV{GIT_DIR} =~ m#^\Q$dir\E/?$#) { 331$allowed=1; 332last; 333} 334}elsif($ENV{GIT_DIR} =~ m#^\Q$dir\E(/?$|/)#) { 335$allowed=1; 336last; 337} 338} 339 340unless($allowed) { 341print"E$ENV{GIT_DIR} does not seem to be a valid GIT repository\n"; 342print"E\n"; 343print"error 1$ENV{GIT_DIR} is not a valid repository\n"; 344return0; 345} 346} 347 348unless(-d $ENV{GIT_DIR} && -e $ENV{GIT_DIR}.'HEAD') { 349print"E$ENV{GIT_DIR} does not seem to be a valid GIT repository\n"; 350print"E\n"; 351print"error 1$ENV{GIT_DIR} is not a valid repository\n"; 352return0; 353} 354 355my@gitvars=`git config -l`; 356if($?) { 357print"E problems executing git-config on the server -- this is not a git repository or the PATH is not set correctly.\n"; 358print"E\n"; 359print"error 1 - problem executing git-config\n"; 360return0; 361} 362foreachmy$line(@gitvars) 363{ 364next unless($line=~/^(gitcvs)\.(?:(ext|pserver)\.)?([\w-]+)=(.*)$/); 365unless($2) { 366$cfg->{$1}{$3} =$4; 367}else{ 368$cfg->{$1}{$2}{$3} =$4; 369} 370} 371 372my$enabled= ($cfg->{gitcvs}{$state->{method}}{enabled} 373||$cfg->{gitcvs}{enabled}); 374unless($state->{'export-all'} || 375($enabled&&$enabled=~/^\s*(1|true|yes)\s*$/i)) { 376print"E GITCVS emulation needs to be enabled on this repo\n"; 377print"E the repo config file needs a [gitcvs] section added, and the parameter 'enabled' set to 1\n"; 378print"E\n"; 379print"error 1 GITCVS emulation disabled\n"; 380return0; 381} 382 383my$logfile=$cfg->{gitcvs}{$state->{method}}{logfile} ||$cfg->{gitcvs}{logfile}; 384if($logfile) 385{ 386$log->setfile($logfile); 387}else{ 388$log->nofile(); 389} 390 391return1; 392} 393 394# Global_option option \n 395# Response expected: no. Transmit one of the global options `-q', `-Q', 396# `-l', `-t', `-r', or `-n'. option must be one of those strings, no 397# variations (such as combining of options) are allowed. For graceful 398# handling of valid-requests, it is probably better to make new global 399# options separate requests, rather than trying to add them to this 400# request. 401sub req_Globaloption 402{ 403my($cmd,$data) =@_; 404$log->debug("req_Globaloption :$data"); 405$state->{globaloptions}{$data} =1; 406} 407 408# Valid-responses request-list \n 409# Response expected: no. Tell the server what responses the client will 410# accept. request-list is a space separated list of tokens. 411sub req_Validresponses 412{ 413my($cmd,$data) =@_; 414$log->debug("req_Validresponses :$data"); 415 416# TODO : re-enable this, currently it's not particularly useful 417#$state->{validresponses} = [ split /\s+/, $data ]; 418} 419 420# valid-requests \n 421# Response expected: yes. Ask the server to send back a Valid-requests 422# response. 423sub req_validrequests 424{ 425my($cmd,$data) =@_; 426 427$log->debug("req_validrequests"); 428 429$log->debug("SEND : Valid-requests ".join(" ",keys%$methods)); 430$log->debug("SEND : ok"); 431 432print"Valid-requests ".join(" ",keys%$methods) ."\n"; 433print"ok\n"; 434} 435 436# Directory local-directory \n 437# Additional data: repository \n. Response expected: no. Tell the server 438# what directory to use. The repository should be a directory name from a 439# previous server response. Note that this both gives a default for Entry 440# and Modified and also for ci and the other commands; normal usage is to 441# send Directory for each directory in which there will be an Entry or 442# Modified, and then a final Directory for the original directory, then the 443# command. The local-directory is relative to the top level at which the 444# command is occurring (i.e. the last Directory which is sent before the 445# command); to indicate that top level, `.' should be sent for 446# local-directory. 447sub req_Directory 448{ 449my($cmd,$data) =@_; 450 451my$repository= <STDIN>; 452chomp$repository; 453 454 455$state->{localdir} =$data; 456$state->{repository} =$repository; 457$state->{path} =$repository; 458$state->{path} =~s/^\Q$state->{CVSROOT}\E\///; 459$state->{module} =$1if($state->{path} =~s/^(.*?)(\/|$)//); 460$state->{path} .="/"if($state->{path} =~ /\S/ ); 461 462$state->{directory} =$state->{localdir}; 463$state->{directory} =""if($state->{directory}eq"."); 464$state->{directory} .="/"if($state->{directory} =~ /\S/ ); 465 466if( (not defined($state->{prependdir})or$state->{prependdir}eq'')and$state->{localdir}eq"."and$state->{path} =~/\S/) 467{ 468$log->info("Setting prepend to '$state->{path}'"); 469$state->{prependdir} =$state->{path}; 470foreachmy$entry(keys%{$state->{entries}} ) 471{ 472$state->{entries}{$state->{prependdir} .$entry} =$state->{entries}{$entry}; 473delete$state->{entries}{$entry}; 474} 475} 476 477if(defined($state->{prependdir} ) ) 478{ 479$log->debug("Prepending '$state->{prependdir}' to state|directory"); 480$state->{directory} =$state->{prependdir} .$state->{directory} 481} 482$log->debug("req_Directory : localdir=$datarepository=$repositorypath=$state->{path} directory=$state->{directory} module=$state->{module}"); 483} 484 485# Entry entry-line \n 486# Response expected: no. Tell the server what version of a file is on the 487# local machine. The name in entry-line is a name relative to the directory 488# most recently specified with Directory. If the user is operating on only 489# some files in a directory, Entry requests for only those files need be 490# included. If an Entry request is sent without Modified, Is-modified, or 491# Unchanged, it means the file is lost (does not exist in the working 492# directory). If both Entry and one of Modified, Is-modified, or Unchanged 493# are sent for the same file, Entry must be sent first. For a given file, 494# one can send Modified, Is-modified, or Unchanged, but not more than one 495# of these three. 496sub req_Entry 497{ 498my($cmd,$data) =@_; 499 500#$log->debug("req_Entry : $data"); 501 502my@data=split(/\//,$data); 503 504$state->{entries}{$state->{directory}.$data[1]} = { 505 revision =>$data[2], 506 conflict =>$data[3], 507 options =>$data[4], 508 tag_or_date =>$data[5], 509}; 510 511$log->info("Received entry line '$data' => '".$state->{directory} .$data[1] ."'"); 512} 513 514# Questionable filename \n 515# Response expected: no. Additional data: no. Tell the server to check 516# whether filename should be ignored, and if not, next time the server 517# sends responses, send (in a M response) `?' followed by the directory and 518# filename. filename must not contain `/'; it needs to be a file in the 519# directory named by the most recent Directory request. 520sub req_Questionable 521{ 522my($cmd,$data) =@_; 523 524$log->debug("req_Questionable :$data"); 525$state->{entries}{$state->{directory}.$data}{questionable} =1; 526} 527 528# add \n 529# Response expected: yes. Add a file or directory. This uses any previous 530# Argument, Directory, Entry, or Modified requests, if they have been sent. 531# The last Directory sent specifies the working directory at the time of 532# the operation. To add a directory, send the directory to be added using 533# Directory and Argument requests. 534sub req_add 535{ 536my($cmd,$data) =@_; 537 538 argsplit("add"); 539 540my$updater= GITCVS::updater->new($state->{CVSROOT},$state->{module},$log); 541$updater->update(); 542 543 argsfromdir($updater); 544 545my$addcount=0; 546 547foreachmy$filename( @{$state->{args}} ) 548{ 549$filename= filecleanup($filename); 550 551my$meta=$updater->getmeta($filename); 552my$wrev= revparse($filename); 553 554if($wrev&&$meta&& ($wrev<0)) 555{ 556# previously removed file, add back 557$log->info("added file$filenamewas previously removed, send 1.$meta->{revision}"); 558 559print"MT +updated\n"; 560print"MT text U\n"; 561print"MT fname$filename\n"; 562print"MT newline\n"; 563print"MT -updated\n"; 564 565unless($state->{globaloptions}{-n} ) 566{ 567my($filepart,$dirpart) = filenamesplit($filename,1); 568 569print"Created$dirpart\n"; 570print$state->{CVSROOT} ."/$state->{module}/$filename\n"; 571 572# this is an "entries" line 573my$kopts= kopts_from_path($filename,"sha1",$meta->{filehash}); 574$log->debug("/$filepart/1.$meta->{revision}//$kopts/"); 575print"/$filepart/1.$meta->{revision}//$kopts/\n"; 576# permissions 577$log->debug("SEND : u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}"); 578print"u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}\n"; 579# transmit file 580 transmitfile($meta->{filehash}); 581} 582 583next; 584} 585 586unless(defined($state->{entries}{$filename}{modified_filename} ) ) 587{ 588print"E cvs add: nothing known about `$filename'\n"; 589next; 590} 591# TODO : check we're not squashing an already existing file 592if(defined($state->{entries}{$filename}{revision} ) ) 593{ 594print"E cvs add: `$filename' has already been entered\n"; 595next; 596} 597 598my($filepart,$dirpart) = filenamesplit($filename,1); 599 600print"E cvs add: scheduling file `$filename' for addition\n"; 601 602print"Checked-in$dirpart\n"; 603print"$filename\n"; 604my$kopts= kopts_from_path($filename,"file", 605$state->{entries}{$filename}{modified_filename}); 606print"/$filepart/0//$kopts/\n"; 607 608my$requestedKopts=$state->{opt}{k}; 609if(defined($requestedKopts)) 610{ 611$requestedKopts="-k$requestedKopts"; 612} 613else 614{ 615$requestedKopts=""; 616} 617if($koptsne$requestedKopts) 618{ 619$log->warn("Ignoring requested -k='$requestedKopts'" 620." for '$filename'; detected -k='$kopts' instead"); 621#TODO: Also have option to send warning to user? 622} 623 624$addcount++; 625} 626 627if($addcount==1) 628{ 629print"E cvs add: use `cvs commit' to add this file permanently\n"; 630} 631elsif($addcount>1) 632{ 633print"E cvs add: use `cvs commit' to add these files permanently\n"; 634} 635 636print"ok\n"; 637} 638 639# remove \n 640# Response expected: yes. Remove a file. This uses any previous Argument, 641# Directory, Entry, or Modified requests, if they have been sent. The last 642# Directory sent specifies the working directory at the time of the 643# operation. Note that this request does not actually do anything to the 644# repository; the only effect of a successful remove request is to supply 645# the client with a new entries line containing `-' to indicate a removed 646# file. In fact, the client probably could perform this operation without 647# contacting the server, although using remove may cause the server to 648# perform a few more checks. The client sends a subsequent ci request to 649# actually record the removal in the repository. 650sub req_remove 651{ 652my($cmd,$data) =@_; 653 654 argsplit("remove"); 655 656# Grab a handle to the SQLite db and do any necessary updates 657my$updater= GITCVS::updater->new($state->{CVSROOT},$state->{module},$log); 658$updater->update(); 659 660#$log->debug("add state : " . Dumper($state)); 661 662my$rmcount=0; 663 664foreachmy$filename( @{$state->{args}} ) 665{ 666$filename= filecleanup($filename); 667 668if(defined($state->{entries}{$filename}{unchanged} )or defined($state->{entries}{$filename}{modified_filename} ) ) 669{ 670print"E cvs remove: file `$filename' still in working directory\n"; 671next; 672} 673 674my$meta=$updater->getmeta($filename); 675my$wrev= revparse($filename); 676 677unless(defined($wrev) ) 678{ 679print"E cvs remove: nothing known about `$filename'\n"; 680next; 681} 682 683if(defined($wrev)and$wrev<0) 684{ 685print"E cvs remove: file `$filename' already scheduled for removal\n"; 686next; 687} 688 689unless($wrev==$meta->{revision} ) 690{ 691# TODO : not sure if the format of this message is quite correct. 692print"E cvs remove: Up to date check failed for `$filename'\n"; 693next; 694} 695 696 697my($filepart,$dirpart) = filenamesplit($filename,1); 698 699print"E cvs remove: scheduling `$filename' for removal\n"; 700 701print"Checked-in$dirpart\n"; 702print"$filename\n"; 703my$kopts= kopts_from_path($filename,"sha1",$meta->{filehash}); 704print"/$filepart/-1.$wrev//$kopts/\n"; 705 706$rmcount++; 707} 708 709if($rmcount==1) 710{ 711print"E cvs remove: use `cvs commit' to remove this file permanently\n"; 712} 713elsif($rmcount>1) 714{ 715print"E cvs remove: use `cvs commit' to remove these files permanently\n"; 716} 717 718print"ok\n"; 719} 720 721# Modified filename \n 722# Response expected: no. Additional data: mode, \n, file transmission. Send 723# the server a copy of one locally modified file. filename is a file within 724# the most recent directory sent with Directory; it must not contain `/'. 725# If the user is operating on only some files in a directory, only those 726# files need to be included. This can also be sent without Entry, if there 727# is no entry for the file. 728sub req_Modified 729{ 730my($cmd,$data) =@_; 731 732my$mode= <STDIN>; 733defined$mode 734or(print"E end of file reading mode for$data\n"),return; 735chomp$mode; 736my$size= <STDIN>; 737defined$size 738or(print"E end of file reading size of$data\n"),return; 739chomp$size; 740 741# Grab config information 742my$blocksize=8192; 743my$bytesleft=$size; 744my$tmp; 745 746# Get a filehandle/name to write it to 747my($fh,$filename) = tempfile( DIR =>$TEMP_DIR); 748 749# Loop over file data writing out to temporary file. 750while($bytesleft) 751{ 752$blocksize=$bytesleftif($bytesleft<$blocksize); 753read STDIN,$tmp,$blocksize; 754print$fh $tmp; 755$bytesleft-=$blocksize; 756} 757 758close$fh 759or(print"E failed to write temporary,$filename:$!\n"),return; 760 761# Ensure we have something sensible for the file mode 762if($mode=~/u=(\w+)/) 763{ 764$mode=$1; 765}else{ 766$mode="rw"; 767} 768 769# Save the file data in $state 770$state->{entries}{$state->{directory}.$data}{modified_filename} =$filename; 771$state->{entries}{$state->{directory}.$data}{modified_mode} =$mode; 772$state->{entries}{$state->{directory}.$data}{modified_hash} =`git hash-object$filename`; 773$state->{entries}{$state->{directory}.$data}{modified_hash} =~ s/\s.*$//s; 774 775 #$log->debug("req_Modified : file=$datamode=$modesize=$size"); 776} 777 778# Unchanged filename\n 779# Response expected: no. Tell the server that filename has not been 780# modified in the checked out directory. The filename is a file within the 781# most recent directory sent with Directory; it must not contain `/'. 782sub req_Unchanged 783{ 784 my ($cmd,$data) =@_; 785 786$state->{entries}{$state->{directory}.$data}{unchanged} = 1; 787 788 #$log->debug("req_Unchanged :$data"); 789} 790 791# Argument text\n 792# Response expected: no. Save argument for use in a subsequent command. 793# Arguments accumulate until an argument-using command is given, at which 794# point they are forgotten. 795# Argumentx text\n 796# Response expected: no. Append\nfollowed by text to the current argument 797# being saved. 798sub req_Argument 799{ 800 my ($cmd,$data) =@_; 801 802 # Argumentx means: append to last Argument (with a newline in front) 803 804$log->debug("$cmd:$data"); 805 806 if ($cmdeq 'Argumentx') { 807 ${$state->{arguments}}[$#{$state->{arguments}}] .= "\n" .$data; 808 } else { 809 push @{$state->{arguments}},$data; 810 } 811} 812 813# expand-modules\n 814# Response expected: yes. Expand the modules which are specified in the 815# arguments. Returns the data in Module-expansion responses. Note that the 816# server can assume that this is checkout or export, not rtag or rdiff; the 817# latter do not access the working directory and thus have no need to 818# expand modules on the client side. Expand may not be the best word for 819# what this request does. It does not necessarily tell you all the files 820# contained in a module, for example. Basically it is a way of telling you 821# which working directories the server needs to know about in order to 822# handle a checkout of the specified modules. For example, suppose that the 823# server has a module defined by 824# aliasmodule -a 1dir 825# That is, one can check out aliasmodule and it will take 1dir in the 826# repository and check it out to 1dir in the working directory. Now suppose 827# the client already has this module checked out and is planning on using 828# the co request to update it. Without using expand-modules, the client 829# would have two bad choices: it could either send information about all 830# working directories under the current directory, which could be 831# unnecessarily slow, or it could be ignorant of the fact that aliasmodule 832# stands for 1dir, and neglect to send information for 1dir, which would 833# lead to incorrect operation. With expand-modules, the client would first 834# ask for the module to be expanded: 835sub req_expandmodules 836{ 837 my ($cmd,$data) =@_; 838 839 argsplit(); 840 841$log->debug("req_expandmodules : " . ( defined($data) ?$data: "[NULL]" ) ); 842 843 unless ( ref$state->{arguments} eq "ARRAY" ) 844 { 845 print "ok\n"; 846 return; 847 } 848 849 foreach my$module( @{$state->{arguments}} ) 850 { 851$log->debug("SEND : Module-expansion$module"); 852 print "Module-expansion$module\n"; 853 } 854 855 print "ok\n"; 856 statecleanup(); 857} 858 859# co\n 860# Response expected: yes. Get files from the repository. This uses any 861# previous Argument, Directory, Entry, or Modified requests, if they have 862# been sent. Arguments to this command are module names; the client cannot 863# know what directories they correspond to except by (1) just sending the 864# co request, and then seeing what directory names the server sends back in 865# its responses, and (2) the expand-modules request. 866sub req_co 867{ 868 my ($cmd,$data) =@_; 869 870 argsplit("co"); 871 872 # Provide list of modules, if -c was used. 873 if (exists$state->{opt}{c}) { 874 my$showref= `git show-ref --heads`; 875 for my$line(split '\n',$showref) { 876 if ($line=~ m% refs/heads/(.*)$%) { 877 print "M$1\t$1\n"; 878 } 879 } 880 print "ok\n"; 881 return 1; 882 } 883 884 my$module=$state->{args}[0]; 885$state->{module} =$module; 886 my$checkout_path=$module; 887 888 # use the user specified directory if we're given it 889$checkout_path=$state->{opt}{d}if(exists($state->{opt}{d} ) ); 890 891$log->debug("req_co : ". (defined($data) ?$data:"[NULL]") ); 892 893$log->info("Checking out module '$module' ($state->{CVSROOT}) to '$checkout_path'"); 894 895$ENV{GIT_DIR} =$state->{CVSROOT} ."/"; 896 897# Grab a handle to the SQLite db and do any necessary updates 898my$updater= GITCVS::updater->new($state->{CVSROOT},$module,$log); 899$updater->update(); 900 901$checkout_path=~ s|/$||;# get rid of trailing slashes 902 903# Eclipse seems to need the Clear-sticky command 904# to prepare the 'Entries' file for the new directory. 905print"Clear-sticky$checkout_path/\n"; 906print$state->{CVSROOT} ."/$module/\n"; 907print"Clear-static-directory$checkout_path/\n"; 908print$state->{CVSROOT} ."/$module/\n"; 909print"Clear-sticky$checkout_path/\n";# yes, twice 910print$state->{CVSROOT} ."/$module/\n"; 911print"Template$checkout_path/\n"; 912print$state->{CVSROOT} ."/$module/\n"; 913print"0\n"; 914 915# instruct the client that we're checking out to $checkout_path 916print"E cvs checkout: Updating$checkout_path\n"; 917 918my%seendirs= (); 919my$lastdir=''; 920 921# recursive 922sub prepdir { 923my($dir,$repodir,$remotedir,$seendirs) =@_; 924my$parent= dirname($dir); 925$dir=~ s|/+$||; 926$repodir=~ s|/+$||; 927$remotedir=~ s|/+$||; 928$parent=~ s|/+$||; 929$log->debug("announcedir$dir,$repodir,$remotedir"); 930 931if($parenteq'.'||$parenteq'./') { 932$parent=''; 933} 934# recurse to announce unseen parents first 935if(length($parent) && !exists($seendirs->{$parent})) { 936 prepdir($parent,$repodir,$remotedir,$seendirs); 937} 938# Announce that we are going to modify at the parent level 939if($parent) { 940print"E cvs checkout: Updating$remotedir/$parent\n"; 941}else{ 942print"E cvs checkout: Updating$remotedir\n"; 943} 944print"Clear-sticky$remotedir/$parent/\n"; 945print"$repodir/$parent/\n"; 946 947print"Clear-static-directory$remotedir/$dir/\n"; 948print"$repodir/$dir/\n"; 949print"Clear-sticky$remotedir/$parent/\n";# yes, twice 950print"$repodir/$parent/\n"; 951print"Template$remotedir/$dir/\n"; 952print"$repodir/$dir/\n"; 953print"0\n"; 954 955$seendirs->{$dir} =1; 956} 957 958foreachmy$git( @{$updater->gethead} ) 959{ 960# Don't want to check out deleted files 961next if($git->{filehash}eq"deleted"); 962 963my$fullName=$git->{name}; 964($git->{name},$git->{dir} ) = filenamesplit($git->{name}); 965 966if(length($git->{dir}) &&$git->{dir}ne'./' 967&&$git->{dir}ne$lastdir) { 968unless(exists($seendirs{$git->{dir}})) { 969 prepdir($git->{dir},$state->{CVSROOT} ."/$module/", 970$checkout_path, \%seendirs); 971$lastdir=$git->{dir}; 972$seendirs{$git->{dir}} =1; 973} 974print"E cvs checkout: Updating /$checkout_path/$git->{dir}\n"; 975} 976 977# modification time of this file 978print"Mod-time$git->{modified}\n"; 979 980# print some information to the client 981if(defined($git->{dir} )and$git->{dir}ne"./") 982{ 983print"M U$checkout_path/$git->{dir}$git->{name}\n"; 984}else{ 985print"M U$checkout_path/$git->{name}\n"; 986} 987 988# instruct client we're sending a file to put in this path 989print"Created$checkout_path/". (defined($git->{dir} )and$git->{dir}ne"./"?$git->{dir} ."/":"") ."\n"; 990 991print$state->{CVSROOT} ."/$module/". (defined($git->{dir} )and$git->{dir}ne"./"?$git->{dir} ."/":"") ."$git->{name}\n"; 992 993# this is an "entries" line 994my$kopts= kopts_from_path($fullName,"sha1",$git->{filehash}); 995print"/$git->{name}/1.$git->{revision}//$kopts/\n"; 996# permissions 997print"u=$git->{mode},g=$git->{mode},o=$git->{mode}\n"; 998 999# transmit file1000 transmitfile($git->{filehash});1001}10021003print"ok\n";10041005 statecleanup();1006}10071008# update \n1009# Response expected: yes. Actually do a cvs update command. This uses any1010# previous Argument, Directory, Entry, or Modified requests, if they have1011# been sent. The last Directory sent specifies the working directory at the1012# time of the operation. The -I option is not used--files which the client1013# can decide whether to ignore are not mentioned and the client sends the1014# Questionable request for others.1015sub req_update1016{1017my($cmd,$data) =@_;10181019$log->debug("req_update : ". (defined($data) ?$data:"[NULL]"));10201021 argsplit("update");10221023#1024# It may just be a client exploring the available heads/modules1025# in that case, list them as top level directories and leave it1026# at that. Eclipse uses this technique to offer you a list of1027# projects (heads in this case) to checkout.1028#1029if($state->{module}eq'') {1030my$showref=`git show-ref --heads`;1031print"E cvs update: Updating .\n";1032formy$line(split'\n',$showref) {1033if($line=~ m% refs/heads/(.*)$%) {1034print"E cvs update: New directory `$1'\n";1035}1036}1037print"ok\n";1038return1;1039}104010411042# Grab a handle to the SQLite db and do any necessary updates1043my$updater= GITCVS::updater->new($state->{CVSROOT},$state->{module},$log);10441045$updater->update();10461047 argsfromdir($updater);10481049#$log->debug("update state : " . Dumper($state));10501051my$last_dirname="///";10521053# foreach file specified on the command line ...1054foreachmy$filename( @{$state->{args}} )1055{1056$filename= filecleanup($filename);10571058$log->debug("Processing file$filename");10591060unless($state->{globaloptions}{-Q} ||$state->{globaloptions}{-q} )1061{1062my$cur_dirname= dirname($filename);1063if($cur_dirnamene$last_dirname)1064{1065$last_dirname=$cur_dirname;1066if($cur_dirnameeq"")1067{1068$cur_dirname=".";1069}1070print"E cvs update: Updating$cur_dirname\n";1071}1072}10731074# if we have a -C we should pretend we never saw modified stuff1075if(exists($state->{opt}{C} ) )1076{1077delete$state->{entries}{$filename}{modified_hash};1078delete$state->{entries}{$filename}{modified_filename};1079$state->{entries}{$filename}{unchanged} =1;1080}10811082my$meta;1083if(defined($state->{opt}{r})and$state->{opt}{r} =~/^1\.(\d+)/)1084{1085$meta=$updater->getmeta($filename,$1);1086}else{1087$meta=$updater->getmeta($filename);1088}10891090# If -p was given, "print" the contents of the requested revision.1091if(exists($state->{opt}{p} ) ) {1092if(defined($meta->{revision} ) ) {1093$log->info("Printing '$filename' revision ".$meta->{revision});10941095 transmitfile($meta->{filehash}, {print=>1});1096}10971098next;1099}11001101if( !defined$meta)1102{1103$meta= {1104 name =>$filename,1105 revision =>0,1106 filehash =>'added'1107};1108}11091110my$oldmeta=$meta;11111112my$wrev= revparse($filename);11131114# If the working copy is an old revision, lets get that version too for comparison.1115if(defined($wrev)and$wrev!=$meta->{revision} )1116{1117$oldmeta=$updater->getmeta($filename,$wrev);1118}11191120#$log->debug("Target revision is $meta->{revision}, current working revision is $wrev");11211122# Files are up to date if the working copy and repo copy have the same revision,1123# and the working copy is unmodified _and_ the user hasn't specified -C1124next if(defined($wrev)1125and defined($meta->{revision})1126and$wrev==$meta->{revision}1127and$state->{entries}{$filename}{unchanged}1128and not exists($state->{opt}{C} ) );11291130# If the working copy and repo copy have the same revision,1131# but the working copy is modified, tell the client it's modified1132if(defined($wrev)1133and defined($meta->{revision})1134and$wrev==$meta->{revision}1135and defined($state->{entries}{$filename}{modified_hash})1136and not exists($state->{opt}{C} ) )1137{1138$log->info("Tell the client the file is modified");1139print"MT text M\n";1140print"MT fname$filename\n";1141print"MT newline\n";1142next;1143}11441145if($meta->{filehash}eq"deleted")1146{1147my($filepart,$dirpart) = filenamesplit($filename,1);11481149$log->info("Removing '$filename' from working copy (no longer in the repo)");11501151print"E cvs update: `$filename' is no longer in the repository\n";1152# Don't want to actually _DO_ the update if -n specified1153unless($state->{globaloptions}{-n} ) {1154print"Removed$dirpart\n";1155print"$filepart\n";1156}1157}1158elsif(not defined($state->{entries}{$filename}{modified_hash} )1159or$state->{entries}{$filename}{modified_hash}eq$oldmeta->{filehash}1160or$meta->{filehash}eq'added')1161{1162# normal update, just send the new revision (either U=Update,1163# or A=Add, or R=Remove)1164if(defined($wrev) &&$wrev<0)1165{1166$log->info("Tell the client the file is scheduled for removal");1167print"MT text R\n";1168print"MT fname$filename\n";1169print"MT newline\n";1170next;1171}1172elsif( (!defined($wrev) ||$wrev==0) && (!defined($meta->{revision}) ||$meta->{revision} ==0) )1173{1174$log->info("Tell the client the file is scheduled for addition");1175print"MT text A\n";1176print"MT fname$filename\n";1177print"MT newline\n";1178next;11791180}1181else{1182$log->info("Updating '$filename' to ".$meta->{revision});1183print"MT +updated\n";1184print"MT text U\n";1185print"MT fname$filename\n";1186print"MT newline\n";1187print"MT -updated\n";1188}11891190my($filepart,$dirpart) = filenamesplit($filename,1);11911192# Don't want to actually _DO_ the update if -n specified1193unless($state->{globaloptions}{-n} )1194{1195if(defined($wrev) )1196{1197# instruct client we're sending a file to put in this path as a replacement1198print"Update-existing$dirpart\n";1199$log->debug("Updating existing file 'Update-existing$dirpart'");1200}else{1201# instruct client we're sending a file to put in this path as a new file1202print"Clear-static-directory$dirpart\n";1203print$state->{CVSROOT} ."/$state->{module}/$dirpart\n";1204print"Clear-sticky$dirpart\n";1205print$state->{CVSROOT} ."/$state->{module}/$dirpart\n";12061207$log->debug("Creating new file 'Created$dirpart'");1208print"Created$dirpart\n";1209}1210print$state->{CVSROOT} ."/$state->{module}/$filename\n";12111212# this is an "entries" line1213my$kopts= kopts_from_path($filename,"sha1",$meta->{filehash});1214$log->debug("/$filepart/1.$meta->{revision}//$kopts/");1215print"/$filepart/1.$meta->{revision}//$kopts/\n";12161217# permissions1218$log->debug("SEND : u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}");1219print"u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}\n";12201221# transmit file1222 transmitfile($meta->{filehash});1223}1224}else{1225$log->info("Updating '$filename'");1226my($filepart,$dirpart) = filenamesplit($meta->{name},1);12271228my$mergeDir= setupTmpDir();12291230my$file_local=$filepart.".mine";1231my$mergedFile="$mergeDir/$file_local";1232system("ln","-s",$state->{entries}{$filename}{modified_filename},$file_local);1233my$file_old=$filepart.".".$oldmeta->{revision};1234 transmitfile($oldmeta->{filehash}, { targetfile =>$file_old});1235my$file_new=$filepart.".".$meta->{revision};1236 transmitfile($meta->{filehash}, { targetfile =>$file_new});12371238# we need to merge with the local changes ( M=successful merge, C=conflict merge )1239$log->info("Merging$file_local,$file_old,$file_new");1240print"M Merging differences between 1.$oldmeta->{revision} and 1.$meta->{revision} into$filename\n";12411242$log->debug("Temporary directory for merge is$mergeDir");12431244my$return=system("git","merge-file",$file_local,$file_old,$file_new);1245$return>>=8;12461247 cleanupTmpDir();12481249if($return==0)1250{1251$log->info("Merged successfully");1252print"M M$filename\n";1253$log->debug("Merged$dirpart");12541255# Don't want to actually _DO_ the update if -n specified1256unless($state->{globaloptions}{-n} )1257{1258print"Merged$dirpart\n";1259$log->debug($state->{CVSROOT} ."/$state->{module}/$filename");1260print$state->{CVSROOT} ."/$state->{module}/$filename\n";1261my$kopts= kopts_from_path("$dirpart/$filepart",1262"file",$mergedFile);1263$log->debug("/$filepart/1.$meta->{revision}//$kopts/");1264print"/$filepart/1.$meta->{revision}//$kopts/\n";1265}1266}1267elsif($return==1)1268{1269$log->info("Merged with conflicts");1270print"E cvs update: conflicts found in$filename\n";1271print"M C$filename\n";12721273# Don't want to actually _DO_ the update if -n specified1274unless($state->{globaloptions}{-n} )1275{1276print"Merged$dirpart\n";1277print$state->{CVSROOT} ."/$state->{module}/$filename\n";1278my$kopts= kopts_from_path("$dirpart/$filepart",1279"file",$mergedFile);1280print"/$filepart/1.$meta->{revision}/+/$kopts/\n";1281}1282}1283else1284{1285$log->warn("Merge failed");1286next;1287}12881289# Don't want to actually _DO_ the update if -n specified1290unless($state->{globaloptions}{-n} )1291{1292# permissions1293$log->debug("SEND : u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}");1294print"u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}\n";12951296# transmit file, format is single integer on a line by itself (file1297# size) followed by the file contents1298# TODO : we should copy files in blocks1299my$data=`cat$mergedFile`;1300$log->debug("File size : " . length($data));1301 print length($data) . "\n";1302 print$data;1303 }1304 }13051306 }13071308 print "ok\n";1309}13101311sub req_ci1312{1313 my ($cmd,$data) =@_;13141315 argsplit("ci");13161317 #$log->debug("State : " . Dumper($state));13181319$log->info("req_ci : " . ( defined($data) ?$data: "[NULL]" ));13201321 if ($state->{method} eq 'pserver' and$state->{user} eq 'anonymous' )1322 {1323 print "error 1 anonymous user cannot commit via pserver\n";1324 cleanupWorkTree();1325 exit;1326 }13271328 if ( -e$state->{CVSROOT} . "/index" )1329 {1330$log->warn("file 'index' already exists in the git repository");1331 print "error 1 Index already exists in git repo\n";1332 cleanupWorkTree();1333 exit;1334 }13351336 # Grab a handle to the SQLite db and do any necessary updates1337 my$updater= GITCVS::updater->new($state->{CVSROOT},$state->{module},$log);1338$updater->update();13391340 # Remember where the head was at the beginning.1341 my$parenthash= `git show-ref -s refs/heads/$state->{module}`;1342 chomp$parenthash;1343 if ($parenthash!~ /^[0-9a-f]{40}$/) {1344 print "error 1 pserver cannot find the current HEAD of module";1345 cleanupWorkTree();1346 exit;1347 }13481349 setupWorkTree($parenthash);13501351$log->info("Lockless commit start, basing commit on '$work->{workDir}', index file is '$work->{index}'");13521353$log->info("Created index '$work->{index}' for head$state->{module} - exit status$?");13541355 my@committedfiles= ();1356 my%oldmeta;13571358 # foreach file specified on the command line ...1359 foreach my$filename( @{$state->{args}} )1360 {1361 my$committedfile=$filename;1362$filename= filecleanup($filename);13631364 next unless ( exists$state->{entries}{$filename}{modified_filename} or not$state->{entries}{$filename}{unchanged} );13651366 my$meta=$updater->getmeta($filename);1367$oldmeta{$filename} =$meta;13681369 my$wrev= revparse($filename);13701371 my ($filepart,$dirpart) = filenamesplit($filename);13721373 # do a checkout of the file if it is part of this tree1374 if ($wrev) {1375 system('git', 'checkout-index', '-f', '-u',$filename);1376 unless ($?== 0) {1377 die "Error running git-checkout-index -f -u$filename:$!";1378 }1379 }13801381 my$addflag= 0;1382 my$rmflag= 0;1383$rmflag= 1 if ( defined($wrev) and$wrev< 0 );1384$addflag= 1 unless ( -e$filename);13851386 # Do up to date checking1387 unless ($addflagor$wrev==$meta->{revision} or ($rmflagand -$wrev==$meta->{revision} ) )1388 {1389 # fail everything if an up to date check fails1390 print "error 1 Up to date check failed for$filename\n";1391 cleanupWorkTree();1392 exit;1393 }13941395 push@committedfiles,$committedfile;1396$log->info("Committing$filename");13971398 system("mkdir","-p",$dirpart) unless ( -d$dirpart);13991400 unless ($rmflag)1401 {1402$log->debug("rename$state->{entries}{$filename}{modified_filename}$filename");1403 rename$state->{entries}{$filename}{modified_filename},$filename;14041405 # Calculate modes to remove1406 my$invmode= "";1407 foreach ( qw (r w x) ) {$invmode.=$_unless ($state->{entries}{$filename}{modified_mode} =~ /$_/); }14081409$log->debug("chmod u+" .$state->{entries}{$filename}{modified_mode} . "-" .$invmode. "$filename");1410 system("chmod","u+" .$state->{entries}{$filename}{modified_mode} . "-" .$invmode,$filename);1411 }14121413 if ($rmflag)1414 {1415$log->info("Removing file '$filename'");1416 unlink($filename);1417 system("git", "update-index", "--remove",$filename);1418 }1419 elsif ($addflag)1420 {1421$log->info("Adding file '$filename'");1422 system("git", "update-index", "--add",$filename);1423 } else {1424$log->info("Updating file '$filename'");1425 system("git", "update-index",$filename);1426 }1427 }14281429 unless ( scalar(@committedfiles) > 0 )1430 {1431 print "E No files to commit\n";1432 print "ok\n";1433 cleanupWorkTree();1434 return;1435 }14361437 my$treehash= `git write-tree`;1438 chomp$treehash;14391440$log->debug("Treehash :$treehash, Parenthash :$parenthash");14411442 # write our commit message out if we have one ...1443 my ($msg_fh,$msg_filename) = tempfile( DIR =>$TEMP_DIR);1444 print$msg_fh$state->{opt}{m};# if ( exists ($state->{opt}{m} ) );1445 if ( defined ($cfg->{gitcvs}{commitmsgannotation} ) ) {1446 if ($cfg->{gitcvs}{commitmsgannotation} !~ /^\s*$/) {1447 print$msg_fh"\n\n".$cfg->{gitcvs}{commitmsgannotation}."\n"1448 }1449 } else {1450 print$msg_fh"\n\nvia git-CVS emulator\n";1451 }1452 close$msg_fh;14531454 my$commithash= `git commit-tree $treehash-p $parenthash<$msg_filename`;1455chomp($commithash);1456$log->info("Commit hash :$commithash");14571458unless($commithash=~/[a-zA-Z0-9]{40}/)1459{1460$log->warn("Commit failed (Invalid commit hash)");1461print"error 1 Commit failed (unknown reason)\n";1462 cleanupWorkTree();1463exit;1464}14651466### Emulate git-receive-pack by running hooks/update1467my@hook= ($ENV{GIT_DIR}.'hooks/update',"refs/heads/$state->{module}",1468$parenthash,$commithash);1469if( -x $hook[0] ) {1470unless(system(@hook) ==0)1471{1472$log->warn("Commit failed (update hook declined to update ref)");1473print"error 1 Commit failed (update hook declined)\n";1474 cleanupWorkTree();1475exit;1476}1477}14781479### Update the ref1480if(system(qw(git update-ref -m),"cvsserver ci",1481"refs/heads/$state->{module}",$commithash,$parenthash)) {1482$log->warn("update-ref for$state->{module} failed.");1483print"error 1 Cannot commit -- update first\n";1484 cleanupWorkTree();1485exit;1486}14871488### Emulate git-receive-pack by running hooks/post-receive1489my$hook=$ENV{GIT_DIR}.'hooks/post-receive';1490if( -x $hook) {1491open(my$pipe,"|$hook") ||die"can't fork$!";14921493local$SIG{PIPE} =sub{die'pipe broke'};14941495print$pipe"$parenthash$commithashrefs/heads/$state->{module}\n";14961497close$pipe||die"bad pipe:$!$?";1498}14991500$updater->update();15011502### Then hooks/post-update1503$hook=$ENV{GIT_DIR}.'hooks/post-update';1504if(-x $hook) {1505system($hook,"refs/heads/$state->{module}");1506}15071508# foreach file specified on the command line ...1509foreachmy$filename(@committedfiles)1510{1511$filename= filecleanup($filename);15121513my$meta=$updater->getmeta($filename);1514unless(defined$meta->{revision}) {1515$meta->{revision} =1;1516}15171518my($filepart,$dirpart) = filenamesplit($filename,1);15191520$log->debug("Checked-in$dirpart:$filename");15211522print"M$state->{CVSROOT}/$state->{module}/$filename,v <--$dirpart$filepart\n";1523if(defined$meta->{filehash} &&$meta->{filehash}eq"deleted")1524{1525print"M new revision: delete; previous revision: 1.$oldmeta{$filename}{revision}\n";1526print"Remove-entry$dirpart\n";1527print"$filename\n";1528}else{1529if($meta->{revision} ==1) {1530print"M initial revision: 1.1\n";1531}else{1532print"M new revision: 1.$meta->{revision}; previous revision: 1.$oldmeta{$filename}{revision}\n";1533}1534print"Checked-in$dirpart\n";1535print"$filename\n";1536my$kopts= kopts_from_path($filename,"sha1",$meta->{filehash});1537print"/$filepart/1.$meta->{revision}//$kopts/\n";1538}1539}15401541 cleanupWorkTree();1542print"ok\n";1543}15441545sub req_status1546{1547my($cmd,$data) =@_;15481549 argsplit("status");15501551$log->info("req_status : ". (defined($data) ?$data:"[NULL]"));1552#$log->debug("status state : " . Dumper($state));15531554# Grab a handle to the SQLite db and do any necessary updates1555my$updater= GITCVS::updater->new($state->{CVSROOT},$state->{module},$log);1556$updater->update();15571558# if no files were specified, we need to work out what files we should be providing status on ...1559 argsfromdir($updater);15601561# foreach file specified on the command line ...1562foreachmy$filename( @{$state->{args}} )1563{1564$filename= filecleanup($filename);15651566next ifexists($state->{opt}{l}) &&index($filename,'/',length($state->{prependdir})) >=0;15671568my$meta=$updater->getmeta($filename);1569my$oldmeta=$meta;15701571my$wrev= revparse($filename);15721573# If the working copy is an old revision, lets get that version too for comparison.1574if(defined($wrev)and$wrev!=$meta->{revision} )1575{1576$oldmeta=$updater->getmeta($filename,$wrev);1577}15781579# TODO : All possible statuses aren't yet implemented1580my$status;1581# Files are up to date if the working copy and repo copy have the same revision, and the working copy is unmodified1582$status="Up-to-date"if(defined($wrev)and defined($meta->{revision})and$wrev==$meta->{revision}1583and1584( ($state->{entries}{$filename}{unchanged}and(not defined($state->{entries}{$filename}{conflict} )or$state->{entries}{$filename}{conflict} !~/^\+=/) )1585or(defined($state->{entries}{$filename}{modified_hash})and$state->{entries}{$filename}{modified_hash}eq$meta->{filehash} ) )1586);15871588# Need checkout if the working copy has an older revision than the repo copy, and the working copy is unmodified1589$status||="Needs Checkout"if(defined($wrev)and defined($meta->{revision} )and$meta->{revision} >$wrev1590and1591($state->{entries}{$filename}{unchanged}1592or(defined($state->{entries}{$filename}{modified_hash})and$state->{entries}{$filename}{modified_hash}eq$oldmeta->{filehash} ) )1593);15941595# Need checkout if it exists in the repo but doesn't have a working copy1596$status||="Needs Checkout"if(not defined($wrev)and defined($meta->{revision} ) );15971598# Locally modified if working copy and repo copy have the same revision but there are local changes1599$status||="Locally Modified"if(defined($wrev)and defined($meta->{revision})and$wrev==$meta->{revision}and$state->{entries}{$filename}{modified_filename} );16001601# Needs Merge if working copy revision is less than repo copy and there are local changes1602$status||="Needs Merge"if(defined($wrev)and defined($meta->{revision} )and$meta->{revision} >$wrevand$state->{entries}{$filename}{modified_filename} );16031604$status||="Locally Added"if(defined($state->{entries}{$filename}{revision} )and not defined($meta->{revision} ) );1605$status||="Locally Removed"if(defined($wrev)and defined($meta->{revision} )and-$wrev==$meta->{revision} );1606$status||="Unresolved Conflict"if(defined($state->{entries}{$filename}{conflict} )and$state->{entries}{$filename}{conflict} =~/^\+=/);1607$status||="File had conflicts on merge"if(0);16081609$status||="Unknown";16101611my($filepart) = filenamesplit($filename);16121613print"M ===================================================================\n";1614print"M File:$filepart\tStatus:$status\n";1615if(defined($state->{entries}{$filename}{revision}) )1616{1617print"M Working revision:\t".$state->{entries}{$filename}{revision} ."\n";1618}else{1619print"M Working revision:\tNo entry for$filename\n";1620}1621if(defined($meta->{revision}) )1622{1623print"M Repository revision:\t1.".$meta->{revision} ."\t$state->{CVSROOT}/$state->{module}/$filename,v\n";1624print"M Sticky Tag:\t\t(none)\n";1625print"M Sticky Date:\t\t(none)\n";1626print"M Sticky Options:\t\t(none)\n";1627}else{1628print"M Repository revision:\tNo revision control file\n";1629}1630print"M\n";1631}16321633print"ok\n";1634}16351636sub req_diff1637{1638my($cmd,$data) =@_;16391640 argsplit("diff");16411642$log->debug("req_diff : ". (defined($data) ?$data:"[NULL]"));1643#$log->debug("status state : " . Dumper($state));16441645my($revision1,$revision2);1646if(defined($state->{opt}{r} )and ref$state->{opt}{r}eq"ARRAY")1647{1648$revision1=$state->{opt}{r}[0];1649$revision2=$state->{opt}{r}[1];1650}else{1651$revision1=$state->{opt}{r};1652}16531654$revision1=~s/^1\.//if(defined($revision1) );1655$revision2=~s/^1\.//if(defined($revision2) );16561657$log->debug("Diffing revisions ". (defined($revision1) ?$revision1:"[NULL]") ." and ". (defined($revision2) ?$revision2:"[NULL]") );16581659# Grab a handle to the SQLite db and do any necessary updates1660my$updater= GITCVS::updater->new($state->{CVSROOT},$state->{module},$log);1661$updater->update();16621663# if no files were specified, we need to work out what files we should be providing status on ...1664 argsfromdir($updater);16651666# foreach file specified on the command line ...1667foreachmy$filename( @{$state->{args}} )1668{1669$filename= filecleanup($filename);16701671my($fh,$file1,$file2,$meta1,$meta2,$filediff);16721673my$wrev= revparse($filename);16741675# We need _something_ to diff against1676next unless(defined($wrev) );16771678# if we have a -r switch, use it1679if(defined($revision1) )1680{1681(undef,$file1) = tempfile( DIR =>$TEMP_DIR, OPEN =>0);1682$meta1=$updater->getmeta($filename,$revision1);1683unless(defined($meta1)and$meta1->{filehash}ne"deleted")1684{1685print"E File$filenameat revision 1.$revision1doesn't exist\n";1686next;1687}1688 transmitfile($meta1->{filehash}, { targetfile =>$file1});1689}1690# otherwise we just use the working copy revision1691else1692{1693(undef,$file1) = tempfile( DIR =>$TEMP_DIR, OPEN =>0);1694$meta1=$updater->getmeta($filename,$wrev);1695 transmitfile($meta1->{filehash}, { targetfile =>$file1});1696}16971698# if we have a second -r switch, use it too1699if(defined($revision2) )1700{1701(undef,$file2) = tempfile( DIR =>$TEMP_DIR, OPEN =>0);1702$meta2=$updater->getmeta($filename,$revision2);17031704unless(defined($meta2)and$meta2->{filehash}ne"deleted")1705{1706print"E File$filenameat revision 1.$revision2doesn't exist\n";1707next;1708}17091710 transmitfile($meta2->{filehash}, { targetfile =>$file2});1711}1712# otherwise we just use the working copy1713else1714{1715$file2=$state->{entries}{$filename}{modified_filename};1716}17171718# if we have been given -r, and we don't have a $file2 yet, lets get one1719if(defined($revision1)and not defined($file2) )1720{1721(undef,$file2) = tempfile( DIR =>$TEMP_DIR, OPEN =>0);1722$meta2=$updater->getmeta($filename,$wrev);1723 transmitfile($meta2->{filehash}, { targetfile =>$file2});1724}17251726# We need to have retrieved something useful1727next unless(defined($meta1) );17281729# Files to date if the working copy and repo copy have the same revision, and the working copy is unmodified1730next if(not defined($meta2)and$wrev==$meta1->{revision}1731and1732( ($state->{entries}{$filename}{unchanged}and(not defined($state->{entries}{$filename}{conflict} )or$state->{entries}{$filename}{conflict} !~/^\+=/) )1733or(defined($state->{entries}{$filename}{modified_hash})and$state->{entries}{$filename}{modified_hash}eq$meta1->{filehash} ) )1734);17351736# Apparently we only show diffs for locally modified files1737next unless(defined($meta2)or defined($state->{entries}{$filename}{modified_filename} ) );17381739print"M Index:$filename\n";1740print"M ===================================================================\n";1741print"M RCS file:$state->{CVSROOT}/$state->{module}/$filename,v\n";1742print"M retrieving revision 1.$meta1->{revision}\n"if(defined($meta1) );1743print"M retrieving revision 1.$meta2->{revision}\n"if(defined($meta2) );1744print"M diff ";1745foreachmy$opt(keys%{$state->{opt}} )1746{1747if(ref$state->{opt}{$opt}eq"ARRAY")1748{1749foreachmy$value( @{$state->{opt}{$opt}} )1750{1751print"-$opt$value";1752}1753}else{1754print"-$opt";1755print"$state->{opt}{$opt} "if(defined($state->{opt}{$opt} ) );1756}1757}1758print"$filename\n";17591760$log->info("Diffing$filename-r$meta1->{revision} -r ". ($meta2->{revision}or"workingcopy"));17611762($fh,$filediff) = tempfile ( DIR =>$TEMP_DIR);17631764if(exists$state->{opt}{u} )1765{1766system("diff -u -L '$filenamerevision 1.$meta1->{revision}' -L '$filename". (defined($meta2->{revision}) ?"revision 1.$meta2->{revision}":"working copy") ."'$file1$file2>$filediff");1767}else{1768system("diff$file1$file2>$filediff");1769}17701771while( <$fh> )1772{1773print"M$_";1774}1775close$fh;1776}17771778print"ok\n";1779}17801781sub req_log1782{1783my($cmd,$data) =@_;17841785 argsplit("log");17861787$log->debug("req_log : ". (defined($data) ?$data:"[NULL]"));1788#$log->debug("log state : " . Dumper($state));17891790my($minrev,$maxrev);1791if(defined($state->{opt}{r} )and$state->{opt}{r} =~/([\d.]+)?(::?)([\d.]+)?/)1792{1793my$control=$2;1794$minrev=$1;1795$maxrev=$3;1796$minrev=~s/^1\.//if(defined($minrev) );1797$maxrev=~s/^1\.//if(defined($maxrev) );1798$minrev++if(defined($minrev)and$controleq"::");1799}18001801# Grab a handle to the SQLite db and do any necessary updates1802my$updater= GITCVS::updater->new($state->{CVSROOT},$state->{module},$log);1803$updater->update();18041805# if no files were specified, we need to work out what files we should be providing status on ...1806 argsfromdir($updater);18071808# foreach file specified on the command line ...1809foreachmy$filename( @{$state->{args}} )1810{1811$filename= filecleanup($filename);18121813my$headmeta=$updater->getmeta($filename);18141815my$revisions=$updater->getlog($filename);1816my$totalrevisions=scalar(@$revisions);18171818if(defined($minrev) )1819{1820$log->debug("Removing revisions less than$minrev");1821while(scalar(@$revisions) >0and$revisions->[-1]{revision} <$minrev)1822{1823pop@$revisions;1824}1825}1826if(defined($maxrev) )1827{1828$log->debug("Removing revisions greater than$maxrev");1829while(scalar(@$revisions) >0and$revisions->[0]{revision} >$maxrev)1830{1831shift@$revisions;1832}1833}18341835next unless(scalar(@$revisions) );18361837print"M\n";1838print"M RCS file:$state->{CVSROOT}/$state->{module}/$filename,v\n";1839print"M Working file:$filename\n";1840print"M head: 1.$headmeta->{revision}\n";1841print"M branch:\n";1842print"M locks: strict\n";1843print"M access list:\n";1844print"M symbolic names:\n";1845print"M keyword substitution: kv\n";1846print"M total revisions:$totalrevisions;\tselected revisions: ".scalar(@$revisions) ."\n";1847print"M description:\n";18481849foreachmy$revision(@$revisions)1850{1851print"M ----------------------------\n";1852print"M revision 1.$revision->{revision}\n";1853# reformat the date for log output1854$revision->{modified} =sprintf('%04d/%02d/%02d%s',$3,$DATE_LIST->{$2},$1,$4)if($revision->{modified} =~/(\d+)\s+(\w+)\s+(\d+)\s+(\S+)/and defined($DATE_LIST->{$2}) );1855$revision->{author} = cvs_author($revision->{author});1856print"M date:$revision->{modified}; author:$revision->{author}; state: ". ($revision->{filehash}eq"deleted"?"dead":"Exp") ."; lines: +2 -3\n";1857my$commitmessage=$updater->commitmessage($revision->{commithash});1858$commitmessage=~s/^/M /mg;1859print$commitmessage."\n";1860}1861print"M =============================================================================\n";1862}18631864print"ok\n";1865}18661867sub req_annotate1868{1869my($cmd,$data) =@_;18701871 argsplit("annotate");18721873$log->info("req_annotate : ". (defined($data) ?$data:"[NULL]"));1874#$log->debug("status state : " . Dumper($state));18751876# Grab a handle to the SQLite db and do any necessary updates1877my$updater= GITCVS::updater->new($state->{CVSROOT},$state->{module},$log);1878$updater->update();18791880# if no files were specified, we need to work out what files we should be providing annotate on ...1881 argsfromdir($updater);18821883# we'll need a temporary checkout dir1884 setupWorkTree();18851886$log->info("Temp checkoutdir creation successful, basing annotate session work on '$work->{workDir}', index file is '$ENV{GIT_INDEX_FILE}'");18871888# foreach file specified on the command line ...1889foreachmy$filename( @{$state->{args}} )1890{1891$filename= filecleanup($filename);18921893my$meta=$updater->getmeta($filename);18941895next unless($meta->{revision} );18961897# get all the commits that this file was in1898# in dense format -- aka skip dead revisions1899my$revisions=$updater->gethistorydense($filename);1900my$lastseenin=$revisions->[0][2];19011902# populate the temporary index based on the latest commit were we saw1903# the file -- but do it cheaply without checking out any files1904# TODO: if we got a revision from the client, use that instead1905# to look up the commithash in sqlite (still good to default to1906# the current head as we do now)1907system("git","read-tree",$lastseenin);1908unless($?==0)1909{1910print"E error running git-read-tree$lastseenin$ENV{GIT_INDEX_FILE}$!\n";1911return;1912}1913$log->info("Created index '$ENV{GIT_INDEX_FILE}' with commit$lastseenin- exit status$?");19141915# do a checkout of the file1916system('git','checkout-index','-f','-u',$filename);1917unless($?==0) {1918print"E error running git-checkout-index -f -u$filename:$!\n";1919return;1920}19211922$log->info("Annotate$filename");19231924# Prepare a file with the commits from the linearized1925# history that annotate should know about. This prevents1926# git-jsannotate telling us about commits we are hiding1927# from the client.19281929my$a_hints="$work->{workDir}/.annotate_hints";1930if(!open(ANNOTATEHINTS,'>',$a_hints)) {1931print"E failed to open '$a_hints' for writing:$!\n";1932return;1933}1934for(my$i=0;$i<@$revisions;$i++)1935{1936print ANNOTATEHINTS $revisions->[$i][2];1937if($i+1<@$revisions) {# have we got a parent?1938print ANNOTATEHINTS ' '.$revisions->[$i+1][2];1939}1940print ANNOTATEHINTS "\n";1941}19421943print ANNOTATEHINTS "\n";1944close ANNOTATEHINTS1945or(print"E failed to write$a_hints:$!\n"),return;19461947my@cmd= (qw(git annotate -l -S),$a_hints,$filename);1948if(!open(ANNOTATE,"-|",@cmd)) {1949print"E error invoking ".join(' ',@cmd) .":$!\n";1950return;1951}1952my$metadata= {};1953print"E Annotations for$filename\n";1954print"E ***************\n";1955while( <ANNOTATE> )1956{1957if(m/^([a-zA-Z0-9]{40})\t\([^\)]*\)(.*)$/i)1958{1959my$commithash=$1;1960my$data=$2;1961unless(defined($metadata->{$commithash} ) )1962{1963$metadata->{$commithash} =$updater->getmeta($filename,$commithash);1964$metadata->{$commithash}{author} = cvs_author($metadata->{$commithash}{author});1965$metadata->{$commithash}{modified} =sprintf("%02d-%s-%02d",$1,$2,$3)if($metadata->{$commithash}{modified} =~/^(\d+)\s(\w+)\s\d\d(\d\d)/);1966}1967printf("M 1.%-5d (%-8s%10s):%s\n",1968$metadata->{$commithash}{revision},1969$metadata->{$commithash}{author},1970$metadata->{$commithash}{modified},1971$data1972);1973}else{1974$log->warn("Error in annotate output! LINE:$_");1975print"E Annotate error\n";1976next;1977}1978}1979close ANNOTATE;1980}19811982# done; get out of the tempdir1983 cleanupWorkTree();19841985print"ok\n";19861987}19881989# This method takes the state->{arguments} array and produces two new arrays.1990# The first is $state->{args} which is everything before the '--' argument, and1991# the second is $state->{files} which is everything after it.1992sub argsplit1993{1994$state->{args} = [];1995$state->{files} = [];1996$state->{opt} = {};19971998return unless(defined($state->{arguments})and ref$state->{arguments}eq"ARRAY");19992000my$type=shift;20012002if(defined($type) )2003{2004my$opt= {};2005$opt= { A =>0, N =>0, P =>0, R =>0, c =>0, f =>0, l =>0, n =>0, p =>0, s =>0, r =>1, D =>1, d =>1, k =>1, j =>1, }if($typeeq"co");2006$opt= { v =>0, l =>0, R =>0}if($typeeq"status");2007$opt= { A =>0, P =>0, C =>0, d =>0, f =>0, l =>0, R =>0, p =>0, k =>1, r =>1, D =>1, j =>1, I =>1, W =>1}if($typeeq"update");2008$opt= { l =>0, R =>0, k =>1, D =>1, D =>1, r =>2}if($typeeq"diff");2009$opt= { c =>0, R =>0, l =>0, f =>0, F =>1, m =>1, r =>1}if($typeeq"ci");2010$opt= { k =>1, m =>1}if($typeeq"add");2011$opt= { f =>0, l =>0, R =>0}if($typeeq"remove");2012$opt= { l =>0, b =>0, h =>0, R =>0, t =>0, N =>0, S =>0, r =>1, d =>1, s =>1, w =>1}if($typeeq"log");201320142015while(scalar( @{$state->{arguments}} ) >0)2016{2017my$arg=shift@{$state->{arguments}};20182019next if($argeq"--");2020next unless($arg=~/\S/);20212022# if the argument looks like a switch2023if($arg=~/^-(\w)(.*)/)2024{2025# if it's a switch that takes an argument2026if($opt->{$1} )2027{2028# If this switch has already been provided2029if($opt->{$1} >1and exists($state->{opt}{$1} ) )2030{2031$state->{opt}{$1} = [$state->{opt}{$1} ];2032if(length($2) >0)2033{2034push@{$state->{opt}{$1}},$2;2035}else{2036push@{$state->{opt}{$1}},shift@{$state->{arguments}};2037}2038}else{2039# if there's extra data in the arg, use that as the argument for the switch2040if(length($2) >0)2041{2042$state->{opt}{$1} =$2;2043}else{2044$state->{opt}{$1} =shift@{$state->{arguments}};2045}2046}2047}else{2048$state->{opt}{$1} =undef;2049}2050}2051else2052{2053push@{$state->{args}},$arg;2054}2055}2056}2057else2058{2059my$mode=0;20602061foreachmy$value( @{$state->{arguments}} )2062{2063if($valueeq"--")2064{2065$mode++;2066next;2067}2068push@{$state->{args}},$valueif($mode==0);2069push@{$state->{files}},$valueif($mode==1);2070}2071}2072}20732074# This method uses $state->{directory} to populate $state->{args} with a list of filenames2075sub argsfromdir2076{2077my$updater=shift;20782079$state->{args} = []if(scalar(@{$state->{args}}) ==1and$state->{args}[0]eq".");20802081return if(scalar( @{$state->{args}} ) >1);20822083my@gethead= @{$updater->gethead};20842085# push added files2086foreachmy$file(keys%{$state->{entries}}) {2087if(exists$state->{entries}{$file}{revision} &&2088$state->{entries}{$file}{revision} ==0)2089{2090push@gethead, { name =>$file, filehash =>'added'};2091}2092}20932094if(scalar(@{$state->{args}}) ==1)2095{2096my$arg=$state->{args}[0];2097$arg.=$state->{prependdir}if(defined($state->{prependdir} ) );20982099$log->info("Only one arg specified, checking for directory expansion on '$arg'");21002101foreachmy$file(@gethead)2102{2103next if($file->{filehash}eq"deleted"and not defined($state->{entries}{$file->{name}} ) );2104next unless($file->{name} =~/^$arg\//or$file->{name}eq$arg);2105push@{$state->{args}},$file->{name};2106}21072108shift@{$state->{args}}if(scalar(@{$state->{args}}) >1);2109}else{2110$log->info("Only one arg specified, populating file list automatically");21112112$state->{args} = [];21132114foreachmy$file(@gethead)2115{2116next if($file->{filehash}eq"deleted"and not defined($state->{entries}{$file->{name}} ) );2117next unless($file->{name} =~s/^$state->{prependdir}//);2118push@{$state->{args}},$file->{name};2119}2120}2121}21222123# This method cleans up the $state variable after a command that uses arguments has run2124sub statecleanup2125{2126$state->{files} = [];2127$state->{args} = [];2128$state->{arguments} = [];2129$state->{entries} = {};2130}21312132# Return working directory revision int "X" from CVS revision "1.X" out2133# of the the working directory "entries" state, for the given filename.2134# Return negative "X" to represent the file is scheduled for removal2135# when it is committed.2136sub revparse2137{2138my$filename=shift;21392140returnundefunless(defined($state->{entries}{$filename}{revision} ) );21412142return$1if($state->{entries}{$filename}{revision} =~/^1\.(\d+)/);2143return-$1if($state->{entries}{$filename}{revision} =~/^-1\.(\d+)/);21442145returnundef;2146}21472148# This method takes a file hash and does a CVS "file transfer". Its2149# exact behaviour depends on a second, optional hash table argument:2150# - If $options->{targetfile}, dump the contents to that file;2151# - If $options->{print}, use M/MT to transmit the contents one line2152# at a time;2153# - Otherwise, transmit the size of the file, followed by the file2154# contents.2155sub transmitfile2156{2157my$filehash=shift;2158my$options=shift;21592160if(defined($filehash)and$filehasheq"deleted")2161{2162$log->warn("filehash is 'deleted'");2163return;2164}21652166die"Need filehash"unless(defined($filehash)and$filehash=~/^[a-zA-Z0-9]{40}$/);21672168my$type=`git cat-file -t$filehash`;2169 chomp$type;21702171 die ( "Invalid type '$type' (expected 'blob')" ) unless ( defined ($type) and$typeeq "blob" );21722173 my$size= `git cat-file -s $filehash`;2174chomp$size;21752176$log->debug("transmitfile($filehash) size=$size, type=$type");21772178if(open my$fh,'-|',"git","cat-file","blob",$filehash)2179{2180if(defined($options->{targetfile} ) )2181{2182my$targetfile=$options->{targetfile};2183open NEWFILE,">",$targetfileor die("Couldn't open '$targetfile' for writing :$!");2184print NEWFILE $_while( <$fh> );2185close NEWFILE or die("Failed to write '$targetfile':$!");2186}elsif(defined($options->{print} ) &&$options->{print} ) {2187while( <$fh> ) {2188if(/\n\z/) {2189print'M ',$_;2190}else{2191print'MT text ',$_,"\n";2192}2193}2194}else{2195print"$size\n";2196printwhile( <$fh> );2197}2198close$fhor die("Couldn't close filehandle for transmitfile():$!");2199}else{2200die("Couldn't execute git-cat-file");2201}2202}22032204# This method takes a file name, and returns ( $dirpart, $filepart ) which2205# refers to the directory portion and the file portion of the filename2206# respectively2207sub filenamesplit2208{2209my$filename=shift;2210my$fixforlocaldir=shift;22112212my($filepart,$dirpart) = ($filename,".");2213($filepart,$dirpart) = ($2,$1)if($filename=~/(.*)\/(.*)/ );2214$dirpart.="/";22152216if($fixforlocaldir)2217{2218$dirpart=~s/^$state->{prependdir}//;2219}22202221return($filepart,$dirpart);2222}22232224sub filecleanup2225{2226my$filename=shift;22272228returnundefunless(defined($filename));2229if($filename=~/^\// )2230{2231print"E absolute filenames '$filename' not supported by server\n";2232returnundef;2233}22342235$filename=~s/^\.\///g;2236$filename=$state->{prependdir} .$filename;2237return$filename;2238}22392240sub validateGitDir2241{2242if( !defined($state->{CVSROOT}) )2243{2244print"error 1 CVSROOT not specified\n";2245 cleanupWorkTree();2246exit;2247}2248if($ENV{GIT_DIR}ne($state->{CVSROOT} .'/') )2249{2250print"error 1 Internally inconsistent CVSROOT\n";2251 cleanupWorkTree();2252exit;2253}2254}22552256# Setup working directory in a work tree with the requested version2257# loaded in the index.2258sub setupWorkTree2259{2260my($ver) =@_;22612262 validateGitDir();22632264if( (defined($work->{state}) &&$work->{state} !=1) ||2265defined($work->{tmpDir}) )2266{2267$log->warn("Bad work tree state management");2268print"error 1 Internal setup multiple work trees without cleanup\n";2269 cleanupWorkTree();2270exit;2271}22722273$work->{workDir} = tempdir ( DIR =>$TEMP_DIR);22742275if( !defined($work->{index}) )2276{2277(undef,$work->{index}) = tempfile ( DIR =>$TEMP_DIR, OPEN =>0);2278}22792280chdir$work->{workDir}or2281die"Unable to chdir to$work->{workDir}\n";22822283$log->info("Setting up GIT_WORK_TREE as '.' in '$work->{workDir}', index file is '$work->{index}'");22842285$ENV{GIT_WORK_TREE} =".";2286$ENV{GIT_INDEX_FILE} =$work->{index};2287$work->{state} =2;22882289if($ver)2290{2291system("git","read-tree",$ver);2292unless($?==0)2293{2294$log->warn("Error running git-read-tree");2295die"Error running git-read-tree$verin$work->{workDir}$!\n";2296}2297}2298# else # req_annotate reads tree for each file2299}23002301# Ensure current directory is in some kind of working directory,2302# with a recent version loaded in the index.2303sub ensureWorkTree2304{2305if(defined($work->{tmpDir}) )2306{2307$log->warn("Bad work tree state management [ensureWorkTree()]");2308print"error 1 Internal setup multiple dirs without cleanup\n";2309 cleanupWorkTree();2310exit;2311}2312if($work->{state} )2313{2314return;2315}23162317 validateGitDir();23182319if( !defined($work->{emptyDir}) )2320{2321$work->{emptyDir} = tempdir ( DIR =>$TEMP_DIR, OPEN =>0);2322}2323chdir$work->{emptyDir}or2324die"Unable to chdir to$work->{emptyDir}\n";23252326my$ver=`git show-ref -s refs/heads/$state->{module}`;2327chomp$ver;2328if($ver!~/^[0-9a-f]{40}$/)2329{2330$log->warn("Error from git show-ref -s refs/head$state->{module}");2331print"error 1 cannot find the current HEAD of module";2332 cleanupWorkTree();2333exit;2334}23352336if( !defined($work->{index}) )2337{2338(undef,$work->{index}) = tempfile ( DIR =>$TEMP_DIR, OPEN =>0);2339}23402341$ENV{GIT_WORK_TREE} =".";2342$ENV{GIT_INDEX_FILE} =$work->{index};2343$work->{state} =1;23442345system("git","read-tree",$ver);2346unless($?==0)2347{2348die"Error running git-read-tree$ver$!\n";2349}2350}23512352# Cleanup working directory that is not needed any longer.2353sub cleanupWorkTree2354{2355if( !$work->{state} )2356{2357return;2358}23592360chdir"/"or die"Unable to chdir '/'\n";23612362if(defined($work->{workDir}) )2363{2364 rmtree($work->{workDir} );2365undef$work->{workDir};2366}2367undef$work->{state};2368}23692370# Setup a temporary directory (not a working tree), typically for2371# merging dirty state as in req_update.2372sub setupTmpDir2373{2374$work->{tmpDir} = tempdir ( DIR =>$TEMP_DIR);2375chdir$work->{tmpDir}or die"Unable to chdir$work->{tmpDir}\n";23762377return$work->{tmpDir};2378}23792380# Clean up a previously setupTmpDir. Restore previous work tree if2381# appropriate.2382sub cleanupTmpDir2383{2384if( !defined($work->{tmpDir}) )2385{2386$log->warn("cleanup tmpdir that has not been setup");2387die"Cleanup tmpDir that has not been setup\n";2388}2389if(defined($work->{state}) )2390{2391if($work->{state} ==1)2392{2393chdir$work->{emptyDir}or2394die"Unable to chdir to$work->{emptyDir}\n";2395}2396elsif($work->{state} ==2)2397{2398chdir$work->{workDir}or2399die"Unable to chdir to$work->{emptyDir}\n";2400}2401else2402{2403$log->warn("Inconsistent work dir state");2404die"Inconsistent work dir state\n";2405}2406}2407else2408{2409chdir"/"or die"Unable to chdir '/'\n";2410}2411}24122413# Given a path, this function returns a string containing the kopts2414# that should go into that path's Entries line. For example, a binary2415# file should get -kb.2416sub kopts_from_path2417{2418my($path,$srcType,$name) =@_;24192420if(defined($cfg->{gitcvs}{usecrlfattr} )and2421$cfg->{gitcvs}{usecrlfattr} =~/\s*(1|true|yes)\s*$/i)2422{2423my($val) = check_attr("text",$path);2424if($valeq"unspecified")2425{2426$val= check_attr("crlf",$path);2427}2428if($valeq"unset")2429{2430return"-kb"2431}2432elsif( check_attr("eol",$path)ne"unspecified"||2433$valeq"set"||$valeq"input")2434{2435return"";2436}2437else2438{2439$log->info("Unrecognized check_attr crlf$path:$val");2440}2441}24422443if(defined($cfg->{gitcvs}{allbinary} ) )2444{2445if( ($cfg->{gitcvs}{allbinary} =~/^\s*(1|true|yes)\s*$/i) )2446{2447return"-kb";2448}2449elsif( ($cfg->{gitcvs}{allbinary} =~/^\s*guess\s*$/i) )2450{2451if( is_binary($srcType,$name) )2452{2453$log->debug("... as binary");2454return"-kb";2455}2456else2457{2458$log->debug("... as text");2459}2460}2461}2462# Return "" to give no special treatment to any path2463return"";2464}24652466sub check_attr2467{2468my($attr,$path) =@_;2469 ensureWorkTree();2470if(open my$fh,'-|',"git","check-attr",$attr,"--",$path)2471{2472my$val= <$fh>;2473close$fh;2474$val=~s/.*: ([^:\r\n]*)\s*$/$1/;2475return$val;2476}2477else2478{2479returnundef;2480}2481}24822483# This should have the same heuristics as convert.c:is_binary() and related.2484# Note that the bare CR test is done by callers in convert.c.2485sub is_binary2486{2487my($srcType,$name) =@_;2488$log->debug("is_binary($srcType,$name)");24892490# Minimize amount of interpreted code run in the inner per-character2491# loop for large files, by totalling each character value and2492# then analyzing the totals.2493my@counts;2494my$i;2495for($i=0;$i<256;$i++)2496{2497$counts[$i]=0;2498}24992500my$fh= open_blob_or_die($srcType,$name);2501my$line;2502while(defined($line=<$fh>) )2503{2504# Any '\0' and bare CR are considered binary.2505if($line=~/\0|(\r[^\n])/)2506{2507close($fh);2508return1;2509}25102511# Count up each character in the line:2512my$len=length($line);2513for($i=0;$i<$len;$i++)2514{2515$counts[ord(substr($line,$i,1))]++;2516}2517}2518close$fh;25192520# Don't count CR and LF as either printable/nonprintable2521$counts[ord("\n")]=0;2522$counts[ord("\r")]=0;25232524# Categorize individual character count into printable and nonprintable:2525my$printable=0;2526my$nonprintable=0;2527for($i=0;$i<256;$i++)2528{2529if($i<32&&2530$i!=ord("\b") &&2531$i!=ord("\t") &&2532$i!=033&&# ESC2533$i!=014)# FF2534{2535$nonprintable+=$counts[$i];2536}2537elsif($i==127)# DEL2538{2539$nonprintable+=$counts[$i];2540}2541else2542{2543$printable+=$counts[$i];2544}2545}25462547return($printable>>7) <$nonprintable;2548}25492550# Returns open file handle. Possible invocations:2551# - open_blob_or_die("file",$filename);2552# - open_blob_or_die("sha1",$filehash);2553sub open_blob_or_die2554{2555my($srcType,$name) =@_;2556my($fh);2557if($srcTypeeq"file")2558{2559if( !open$fh,"<",$name)2560{2561$log->warn("Unable to open file$name:$!");2562die"Unable to open file$name:$!\n";2563}2564}2565elsif($srcTypeeq"sha1")2566{2567unless(defined($name)and$name=~/^[a-zA-Z0-9]{40}$/)2568{2569$log->warn("Need filehash");2570die"Need filehash\n";2571}25722573my$type=`git cat-file -t$name`;2574 chomp$type;25752576 unless ( defined ($type) and$typeeq "blob" )2577 {2578$log->warn("Invalid type '$type' for '$name'");2579 die ( "Invalid type '$type' (expected 'blob')" )2580 }25812582 my$size= `git cat-file -s $name`;2583chomp$size;25842585$log->debug("open_blob_or_die($name) size=$size, type=$type");25862587unless(open$fh,'-|',"git","cat-file","blob",$name)2588{2589$log->warn("Unable to open sha1$name");2590die"Unable to open sha1$name\n";2591}2592}2593else2594{2595$log->warn("Unknown type of blob source:$srcType");2596die"Unknown type of blob source:$srcType\n";2597}2598return$fh;2599}26002601# Generate a CVS author name from Git author information, by taking the local2602# part of the email address and replacing characters not in the Portable2603# Filename Character Set (see IEEE Std 1003.1-2001, 3.276) by underscores. CVS2604# Login names are Unix login names, which should be restricted to this2605# character set.2606sub cvs_author2607{2608my$author_line=shift;2609(my$author) =$author_line=~/<([^@>]*)/;26102611$author=~s/[^-a-zA-Z0-9_.]/_/g;2612$author=~s/^-/_/;26132614$author;2615}261626172618sub descramble2619{2620# This table is from src/scramble.c in the CVS source2621my@SHIFTS= (26220,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,262316,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,2624114,120,53,79,96,109,72,108,70,64,76,67,116,74,68,87,2625111,52,75,119,49,34,82,81,95,65,112,86,118,110,122,105,262641,57,83,43,46,102,40,89,38,103,45,50,42,123,91,35,2627125,55,54,66,124,126,59,47,92,71,115,78,88,107,106,56,262836,121,117,104,101,100,69,73,99,63,94,93,39,37,61,48,262958,113,32,90,44,98,60,51,33,97,62,77,84,80,85,223,2630225,216,187,166,229,189,222,188,141,249,148,200,184,136,248,190,2631199,170,181,204,138,232,218,183,255,234,220,247,213,203,226,193,2632174,172,228,252,217,201,131,230,197,211,145,238,161,179,160,212,2633207,221,254,173,202,146,224,151,140,196,205,130,135,133,143,246,2634192,159,244,239,185,168,215,144,139,165,180,157,147,186,214,176,2635227,231,219,169,175,156,206,198,129,164,150,210,154,177,134,127,2636182,128,158,208,162,132,167,209,149,241,153,251,237,236,171,195,2637243,233,253,240,194,250,191,155,142,137,245,235,163,242,178,1522638);2639my($str) =@_;26402641# This should never happen, the same password format (A) has been2642# used by CVS since the beginning of time2643{2644my$fmt=substr($str,0,1);2645die"invalid password format `$fmt'"unless$fmteq'A';2646}26472648my@str=unpack"C*",substr($str,1);2649my$ret=join'',map{chr$SHIFTS[$_] }@str;2650return$ret;2651}265226532654package GITCVS::log;26552656####2657#### Copyright The Open University UK - 2006.2658####2659#### Authors: Martyn Smith <martyn@catalyst.net.nz>2660#### Martin Langhoff <martin@laptop.org>2661####2662####26632664use strict;2665use warnings;26662667=head1 NAME26682669GITCVS::log26702671=head1 DESCRIPTION26722673This module provides very crude logging with a similar interface to2674Log::Log4perl26752676=head1 METHODS26772678=cut26792680=head2 new26812682Creates a new log object, optionally you can specify a filename here to2683indicate the file to log to. If no log file is specified, you can specify one2684later with method setfile, or indicate you no longer want logging with method2685nofile.26862687Until one of these methods is called, all log calls will buffer messages ready2688to write out.26892690=cut2691sub new2692{2693my$class=shift;2694my$filename=shift;26952696my$self= {};26972698bless$self,$class;26992700if(defined($filename) )2701{2702open$self->{fh},">>",$filenameor die("Couldn't open '$filename' for writing :$!");2703}27042705return$self;2706}27072708=head2 setfile27092710This methods takes a filename, and attempts to open that file as the log file.2711If successful, all buffered data is written out to the file, and any further2712logging is written directly to the file.27132714=cut2715sub setfile2716{2717my$self=shift;2718my$filename=shift;27192720if(defined($filename) )2721{2722open$self->{fh},">>",$filenameor die("Couldn't open '$filename' for writing :$!");2723}27242725return unless(defined($self->{buffer} )and ref$self->{buffer}eq"ARRAY");27262727while(my$line=shift@{$self->{buffer}} )2728{2729print{$self->{fh}}$line;2730}2731}27322733=head2 nofile27342735This method indicates no logging is going to be used. It flushes any entries in2736the internal buffer, and sets a flag to ensure no further data is put there.27372738=cut2739sub nofile2740{2741my$self=shift;27422743$self->{nolog} =1;27442745return unless(defined($self->{buffer} )and ref$self->{buffer}eq"ARRAY");27462747$self->{buffer} = [];2748}27492750=head2 _logopen27512752Internal method. Returns true if the log file is open, false otherwise.27532754=cut2755sub _logopen2756{2757my$self=shift;27582759return1if(defined($self->{fh} )and ref$self->{fh}eq"GLOB");2760return0;2761}27622763=head2 debug info warn fatal27642765These four methods are wrappers to _log. They provide the actual interface for2766logging data.27672768=cut2769sub debug {my$self=shift;$self->_log("debug",@_); }2770sub info {my$self=shift;$self->_log("info",@_); }2771subwarn{my$self=shift;$self->_log("warn",@_); }2772sub fatal {my$self=shift;$self->_log("fatal",@_); }27732774=head2 _log27752776This is an internal method called by the logging functions. It generates a2777timestamp and pushes the logged line either to file, or internal buffer.27782779=cut2780sub _log2781{2782my$self=shift;2783my$level=shift;27842785return if($self->{nolog} );27862787my@time=localtime;2788my$timestring=sprintf("%4d-%02d-%02d%02d:%02d:%02d: %-5s",2789$time[5] +1900,2790$time[4] +1,2791$time[3],2792$time[2],2793$time[1],2794$time[0],2795uc$level,2796);27972798if($self->_logopen)2799{2800print{$self->{fh}}$timestring." - ".join(" ",@_) ."\n";2801}else{2802push@{$self->{buffer}},$timestring." - ".join(" ",@_) ."\n";2803}2804}28052806=head2 DESTROY28072808This method simply closes the file handle if one is open28092810=cut2811sub DESTROY2812{2813my$self=shift;28142815if($self->_logopen)2816{2817close$self->{fh};2818}2819}28202821package GITCVS::updater;28222823####2824#### Copyright The Open University UK - 2006.2825####2826#### Authors: Martyn Smith <martyn@catalyst.net.nz>2827#### Martin Langhoff <martin@laptop.org>2828####2829####28302831use strict;2832use warnings;2833use DBI;28342835=head1 METHODS28362837=cut28382839=head2 new28402841=cut2842sub new2843{2844my$class=shift;2845my$config=shift;2846my$module=shift;2847my$log=shift;28482849die"Need to specify a git repository"unless(defined($config)and-d $config);2850die"Need to specify a module"unless(defined($module) );28512852$class=ref($class) ||$class;28532854my$self= {};28552856bless$self,$class;28572858$self->{valid_tables} = {'revision'=>1,2859'revision_ix1'=>1,2860'revision_ix2'=>1,2861'head'=>1,2862'head_ix1'=>1,2863'properties'=>1,2864'commitmsgs'=>1};28652866$self->{module} =$module;2867$self->{git_path} =$config."/";28682869$self->{log} =$log;28702871die"Git repo '$self->{git_path}' doesn't exist"unless( -d $self->{git_path} );28722873$self->{dbdriver} =$cfg->{gitcvs}{$state->{method}}{dbdriver} ||2874$cfg->{gitcvs}{dbdriver} ||"SQLite";2875$self->{dbname} =$cfg->{gitcvs}{$state->{method}}{dbname} ||2876$cfg->{gitcvs}{dbname} ||"%Ggitcvs.%m.sqlite";2877$self->{dbuser} =$cfg->{gitcvs}{$state->{method}}{dbuser} ||2878$cfg->{gitcvs}{dbuser} ||"";2879$self->{dbpass} =$cfg->{gitcvs}{$state->{method}}{dbpass} ||2880$cfg->{gitcvs}{dbpass} ||"";2881$self->{dbtablenameprefix} =$cfg->{gitcvs}{$state->{method}}{dbtablenameprefix} ||2882$cfg->{gitcvs}{dbtablenameprefix} ||"";2883my%mapping= ( m =>$module,2884 a =>$state->{method},2885 u =>getlogin||getpwuid($<) || $<,2886 G =>$self->{git_path},2887 g => mangle_dirname($self->{git_path}),2888);2889$self->{dbname} =~s/%([mauGg])/$mapping{$1}/eg;2890$self->{dbuser} =~s/%([mauGg])/$mapping{$1}/eg;2891$self->{dbtablenameprefix} =~s/%([mauGg])/$mapping{$1}/eg;2892$self->{dbtablenameprefix} = mangle_tablename($self->{dbtablenameprefix});28932894die"Invalid char ':' in dbdriver"if$self->{dbdriver} =~/:/;2895die"Invalid char ';' in dbname"if$self->{dbname} =~/;/;2896$self->{dbh} = DBI->connect("dbi:$self->{dbdriver}:dbname=$self->{dbname}",2897$self->{dbuser},2898$self->{dbpass});2899die"Error connecting to database\n"unlessdefined$self->{dbh};29002901$self->{tables} = {};2902foreachmy$table(keys%{$self->{dbh}->table_info(undef,undef,undef,'TABLE')->fetchall_hashref('TABLE_NAME')} )2903{2904$self->{tables}{$table} =1;2905}29062907# Construct the revision table if required2908# The revision table stores an entry for each file, each time that file2909# changes.2910# numberOfRecords = O( numCommits * averageNumChangedFilesPerCommit )2911# This is not sufficient to support "-r {commithash}" for any2912# files except files that were modified by that commit (also,2913# some places in the code ignore/effectively strip out -r in2914# some cases, before it gets passed to getmeta()).2915# The "filehash" field typically has a git blob hash, but can also2916# be set to "dead" to indicate that the given version of the file2917# should not exist in the sandbox.2918unless($self->{tables}{$self->tablename("revision")} )2919{2920my$tablename=$self->tablename("revision");2921my$ix1name=$self->tablename("revision_ix1");2922my$ix2name=$self->tablename("revision_ix2");2923$self->{dbh}->do("2924 CREATE TABLE$tablename(2925 name TEXT NOT NULL,2926 revision INTEGER NOT NULL,2927 filehash TEXT NOT NULL,2928 commithash TEXT NOT NULL,2929 author TEXT NOT NULL,2930 modified TEXT NOT NULL,2931 mode TEXT NOT NULL2932 )2933 ");2934$self->{dbh}->do("2935 CREATE INDEX$ix1name2936 ON$tablename(name,revision)2937 ");2938$self->{dbh}->do("2939 CREATE INDEX$ix2name2940 ON$tablename(name,commithash)2941 ");2942}29432944# Construct the head table if required2945# The head table (along with the "last_commit" entry in the property2946# table) is the persisted working state of the "sub update" subroutine.2947# All of it's data is read entirely first, and completely recreated2948# last, every time "sub update" runs.2949# This is also used by "sub getmeta" when it is asked for the latest2950# version of a file (as opposed to some specific version).2951# Another way of thinking about it is as a single slice out of2952# "revisions", giving just the most recent revision information for2953# each file.2954unless($self->{tables}{$self->tablename("head")} )2955{2956my$tablename=$self->tablename("head");2957my$ix1name=$self->tablename("head_ix1");2958$self->{dbh}->do("2959 CREATE TABLE$tablename(2960 name TEXT NOT NULL,2961 revision INTEGER NOT NULL,2962 filehash TEXT NOT NULL,2963 commithash TEXT NOT NULL,2964 author TEXT NOT NULL,2965 modified TEXT NOT NULL,2966 mode TEXT NOT NULL2967 )2968 ");2969$self->{dbh}->do("2970 CREATE INDEX$ix1name2971 ON$tablename(name)2972 ");2973}29742975# Construct the properties table if required2976# - "last_commit" - Used by "sub update".2977unless($self->{tables}{$self->tablename("properties")} )2978{2979my$tablename=$self->tablename("properties");2980$self->{dbh}->do("2981 CREATE TABLE$tablename(2982 key TEXT NOT NULL PRIMARY KEY,2983 value TEXT2984 )2985 ");2986}29872988# Construct the commitmsgs table if required2989# The commitmsgs table is only used for merge commits, since2990# "sub update" will only keep one branch of parents. Shortlogs2991# for ignored commits (i.e. not on the chosen branch) will be used2992# to construct a replacement "collapsed" merge commit message,2993# which will be stored in this table. See also "sub commitmessage".2994unless($self->{tables}{$self->tablename("commitmsgs")} )2995{2996my$tablename=$self->tablename("commitmsgs");2997$self->{dbh}->do("2998 CREATE TABLE$tablename(2999 key TEXT NOT NULL PRIMARY KEY,3000 value TEXT3001 )3002 ");3003}30043005return$self;3006}30073008=head2 tablename30093010=cut3011sub tablename3012{3013my$self=shift;3014my$name=shift;30153016if(exists$self->{valid_tables}{$name}) {3017return$self->{dbtablenameprefix} .$name;3018}else{3019returnundef;3020}3021}30223023=head2 update30243025Bring the database up to date with the latest changes from3026the git repository.30273028Internal working state is read out of the "head" table and the3029"last_commit" property, then it updates "revisions" based on that, and3030finally it writes the new internal state back to the "head" table3031so it can be used as a starting point the next time update is called.30323033=cut3034sub update3035{3036my$self=shift;30373038# first lets get the commit list3039$ENV{GIT_DIR} =$self->{git_path};30403041my$commitsha1=`git rev-parse$self->{module}`;3042chomp$commitsha1;30433044my$commitinfo=`git cat-file commit$self->{module} 2>&1`;3045unless($commitinfo=~/tree\s+[a-zA-Z0-9]{40}/)3046{3047die("Invalid module '$self->{module}'");3048}304930503051my$git_log;3052my$lastcommit=$self->_get_prop("last_commit");30533054if(defined$lastcommit&&$lastcommiteq$commitsha1) {# up-to-date3055return1;3056}30573058# Start exclusive lock here...3059$self->{dbh}->begin_work()or die"Cannot lock database for BEGIN";30603061# TODO: log processing is memory bound3062# if we can parse into a 2nd file that is in reverse order3063# we can probably do something really efficient3064my@git_log_params= ('--pretty','--parents','--topo-order');30653066if(defined$lastcommit) {3067push@git_log_params,"$lastcommit..$self->{module}";3068}else{3069push@git_log_params,$self->{module};3070}3071# git-rev-list is the backend / plumbing version of git-log3072open(GITLOG,'-|','git','rev-list',@git_log_params)or die"Cannot call git-rev-list:$!";30733074my@commits;30753076my%commit= ();30773078while( <GITLOG> )3079{3080chomp;3081if(m/^commit\s+(.*)$/) {3082# on ^commit lines put the just seen commit in the stack3083# and prime things for the next one3084if(keys%commit) {3085my%copy=%commit;3086unshift@commits, \%copy;3087%commit= ();3088}3089my@parents=split(m/\s+/,$1);3090$commit{hash} =shift@parents;3091$commit{parents} = \@parents;3092}elsif(m/^(\w+?):\s+(.*)$/&& !exists($commit{message})) {3093# on rfc822-like lines seen before we see any message,3094# lowercase the entry and put it in the hash as key-value3095$commit{lc($1)} =$2;3096}else{3097# message lines - skip initial empty line3098# and trim whitespace3099if(!exists($commit{message}) &&m/^\s*$/) {3100# define it to mark the end of headers3101$commit{message} ='';3102next;3103}3104s/^\s+//;s/\s+$//;# trim ws3105$commit{message} .=$_."\n";3106}3107}3108close GITLOG;31093110unshift@commits, \%commitif(keys%commit);31113112# Now all the commits are in the @commits bucket3113# ordered by time DESC. for each commit that needs processing,3114# determine whether it's following the last head we've seen or if3115# it's on its own branch, grab a file list, and add whatever's changed3116# NOTE: $lastcommit refers to the last commit from previous run3117# $lastpicked is the last commit we picked in this run3118my$lastpicked;3119my$head= {};3120if(defined$lastcommit) {3121$lastpicked=$lastcommit;3122}31233124my$committotal=scalar(@commits);3125my$commitcount=0;31263127# Load the head table into $head (for cached lookups during the update process)3128foreachmy$file( @{$self->gethead()} )3129{3130$head->{$file->{name}} =$file;3131}31323133foreachmy$commit(@commits)3134{3135$self->{log}->debug("GITCVS::updater - Processing commit$commit->{hash} (". (++$commitcount) ." of$committotal)");3136if(defined$lastpicked)3137{3138if(!in_array($lastpicked, @{$commit->{parents}}))3139{3140# skip, we'll see this delta3141# as part of a merge later3142# warn "skipping off-track $commit->{hash}\n";3143next;3144}elsif(@{$commit->{parents}} >1) {3145# it is a merge commit, for each parent that is3146# not $lastpicked (not given a CVS revision number),3147# see if we can get a log3148# from the merge-base to that parent to put it3149# in the message as a merge summary.3150my@parents= @{$commit->{parents}};3151foreachmy$parent(@parents) {3152if($parenteq$lastpicked) {3153next;3154}3155# git-merge-base can potentially (but rarely) throw3156# several candidate merge bases. let's assume3157# that the first one is the best one.3158my$base=eval{3159 safe_pipe_capture('git','merge-base',3160$lastpicked,$parent);3161};3162# The two branches may not be related at all,3163# in which case merge base simply fails to find3164# any, but that's Ok.3165next if($@);31663167chomp$base;3168if($base) {3169my@merged;3170# print "want to log between $base $parent \n";3171open(GITLOG,'-|','git','log','--pretty=medium',"$base..$parent")3172or die"Cannot call git-log:$!";3173my$mergedhash;3174while(<GITLOG>) {3175chomp;3176if(!defined$mergedhash) {3177if(m/^commit\s+(.+)$/) {3178$mergedhash=$1;3179}else{3180next;3181}3182}else{3183# grab the first line that looks non-rfc8223184# aka has content after leading space3185if(m/^\s+(\S.*)$/) {3186my$title=$1;3187$title=substr($title,0,100);# truncate3188unshift@merged,"$mergedhash$title";3189undef$mergedhash;3190}3191}3192}3193close GITLOG;3194if(@merged) {3195$commit->{mergemsg} =$commit->{message};3196$commit->{mergemsg} .="\nSummary of merged commits:\n\n";3197foreachmy$summary(@merged) {3198$commit->{mergemsg} .="\t$summary\n";3199}3200$commit->{mergemsg} .="\n\n";3201# print "Message for $commit->{hash} \n$commit->{mergemsg}";3202}3203}3204}3205}3206}32073208# convert the date to CVS-happy format3209$commit->{date} ="$2$1$4$3$5"if($commit->{date} =~/^\w+\s+(\w+)\s+(\d+)\s+(\d+:\d+:\d+)\s+(\d+)\s+([+-]\d+)$/);32103211if(defined($lastpicked) )3212{3213my$filepipe=open(FILELIST,'-|','git','diff-tree','-z','-r',$lastpicked,$commit->{hash})or die("Cannot call git-diff-tree :$!");3214local($/) ="\0";3215while( <FILELIST> )3216{3217chomp;3218unless(/^:\d{6}\s+\d{3}(\d)\d{2}\s+[a-zA-Z0-9]{40}\s+([a-zA-Z0-9]{40})\s+(\w)$/o)3219{3220die("Couldn't process git-diff-tree line :$_");3221}3222my($mode,$hash,$change) = ($1,$2,$3);3223my$name= <FILELIST>;3224chomp($name);32253226# $log->debug("File mode=$mode, hash=$hash, change=$change, name=$name");32273228my$git_perms="";3229$git_perms.="r"if($mode&4);3230$git_perms.="w"if($mode&2);3231$git_perms.="x"if($mode&1);3232$git_perms="rw"if($git_permseq"");32333234if($changeeq"D")3235{3236#$log->debug("DELETE $name");3237$head->{$name} = {3238 name =>$name,3239 revision =>$head->{$name}{revision} +1,3240 filehash =>"deleted",3241 commithash =>$commit->{hash},3242 modified =>$commit->{date},3243 author =>$commit->{author},3244 mode =>$git_perms,3245};3246$self->insert_rev($name,$head->{$name}{revision},$hash,$commit->{hash},$commit->{date},$commit->{author},$git_perms);3247}3248elsif($changeeq"M"||$changeeq"T")3249{3250#$log->debug("MODIFIED $name");3251$head->{$name} = {3252 name =>$name,3253 revision =>$head->{$name}{revision} +1,3254 filehash =>$hash,3255 commithash =>$commit->{hash},3256 modified =>$commit->{date},3257 author =>$commit->{author},3258 mode =>$git_perms,3259};3260$self->insert_rev($name,$head->{$name}{revision},$hash,$commit->{hash},$commit->{date},$commit->{author},$git_perms);3261}3262elsif($changeeq"A")3263{3264#$log->debug("ADDED $name");3265$head->{$name} = {3266 name =>$name,3267 revision =>$head->{$name}{revision} ?$head->{$name}{revision}+1:1,3268 filehash =>$hash,3269 commithash =>$commit->{hash},3270 modified =>$commit->{date},3271 author =>$commit->{author},3272 mode =>$git_perms,3273};3274$self->insert_rev($name,$head->{$name}{revision},$hash,$commit->{hash},$commit->{date},$commit->{author},$git_perms);3275}3276else3277{3278$log->warn("UNKNOWN FILE CHANGE mode=$mode, hash=$hash, change=$change, name=$name");3279die;3280}3281}3282close FILELIST;3283}else{3284# this is used to detect files removed from the repo3285my$seen_files= {};32863287my$filepipe=open(FILELIST,'-|','git','ls-tree','-z','-r',$commit->{hash})or die("Cannot call git-ls-tree :$!");3288local$/="\0";3289while( <FILELIST> )3290{3291chomp;3292unless(/^(\d+)\s+(\w+)\s+([a-zA-Z0-9]+)\t(.*)$/o)3293{3294die("Couldn't process git-ls-tree line :$_");3295}32963297my($git_perms,$git_type,$git_hash,$git_filename) = ($1,$2,$3,$4);32983299$seen_files->{$git_filename} =1;33003301my($oldhash,$oldrevision,$oldmode) = (3302$head->{$git_filename}{filehash},3303$head->{$git_filename}{revision},3304$head->{$git_filename}{mode}3305);33063307if($git_perms=~/^\d\d\d(\d)\d\d/o)3308{3309$git_perms="";3310$git_perms.="r"if($1&4);3311$git_perms.="w"if($1&2);3312$git_perms.="x"if($1&1);3313}else{3314$git_perms="rw";3315}33163317# unless the file exists with the same hash, we need to update it ...3318unless(defined($oldhash)and$oldhasheq$git_hashand defined($oldmode)and$oldmodeeq$git_perms)3319{3320my$newrevision= ($oldrevisionor0) +1;33213322$head->{$git_filename} = {3323 name =>$git_filename,3324 revision =>$newrevision,3325 filehash =>$git_hash,3326 commithash =>$commit->{hash},3327 modified =>$commit->{date},3328 author =>$commit->{author},3329 mode =>$git_perms,3330};333133323333$self->insert_rev($git_filename,$newrevision,$git_hash,$commit->{hash},$commit->{date},$commit->{author},$git_perms);3334}3335}3336close FILELIST;33373338# Detect deleted files3339foreachmy$file(keys%$head)3340{3341unless(exists$seen_files->{$file}or$head->{$file}{filehash}eq"deleted")3342{3343$head->{$file}{revision}++;3344$head->{$file}{filehash} ="deleted";3345$head->{$file}{commithash} =$commit->{hash};3346$head->{$file}{modified} =$commit->{date};3347$head->{$file}{author} =$commit->{author};33483349$self->insert_rev($file,$head->{$file}{revision},$head->{$file}{filehash},$commit->{hash},$commit->{date},$commit->{author},$head->{$file}{mode});3350}3351}3352# END : "Detect deleted files"3353}335433553356if(exists$commit->{mergemsg})3357{3358$self->insert_mergelog($commit->{hash},$commit->{mergemsg});3359}33603361$lastpicked=$commit->{hash};33623363$self->_set_prop("last_commit",$commit->{hash});3364}33653366$self->delete_head();3367foreachmy$file(keys%$head)3368{3369$self->insert_head(3370$file,3371$head->{$file}{revision},3372$head->{$file}{filehash},3373$head->{$file}{commithash},3374$head->{$file}{modified},3375$head->{$file}{author},3376$head->{$file}{mode},3377);3378}3379# invalidate the gethead cache3380$self->{gethead_cache} =undef;338133823383# Ending exclusive lock here3384$self->{dbh}->commit()or die"Failed to commit changes to SQLite";3385}33863387sub insert_rev3388{3389my$self=shift;3390my$name=shift;3391my$revision=shift;3392my$filehash=shift;3393my$commithash=shift;3394my$modified=shift;3395my$author=shift;3396my$mode=shift;3397my$tablename=$self->tablename("revision");33983399my$insert_rev=$self->{dbh}->prepare_cached("INSERT INTO$tablename(name, revision, filehash, commithash, modified, author, mode) VALUES (?,?,?,?,?,?,?)",{},1);3400$insert_rev->execute($name,$revision,$filehash,$commithash,$modified,$author,$mode);3401}34023403sub insert_mergelog3404{3405my$self=shift;3406my$key=shift;3407my$value=shift;3408my$tablename=$self->tablename("commitmsgs");34093410my$insert_mergelog=$self->{dbh}->prepare_cached("INSERT INTO$tablename(key, value) VALUES (?,?)",{},1);3411$insert_mergelog->execute($key,$value);3412}34133414sub delete_head3415{3416my$self=shift;3417my$tablename=$self->tablename("head");34183419my$delete_head=$self->{dbh}->prepare_cached("DELETE FROM$tablename",{},1);3420$delete_head->execute();3421}34223423sub insert_head3424{3425my$self=shift;3426my$name=shift;3427my$revision=shift;3428my$filehash=shift;3429my$commithash=shift;3430my$modified=shift;3431my$author=shift;3432my$mode=shift;3433my$tablename=$self->tablename("head");34343435my$insert_head=$self->{dbh}->prepare_cached("INSERT INTO$tablename(name, revision, filehash, commithash, modified, author, mode) VALUES (?,?,?,?,?,?,?)",{},1);3436$insert_head->execute($name,$revision,$filehash,$commithash,$modified,$author,$mode);3437}34383439sub _headrev3440{3441my$self=shift;3442my$filename=shift;3443my$tablename=$self->tablename("head");34443445my$db_query=$self->{dbh}->prepare_cached("SELECT filehash, revision, mode FROM$tablenameWHERE name=?",{},1);3446$db_query->execute($filename);3447my($hash,$revision,$mode) =$db_query->fetchrow_array;34483449return($hash,$revision,$mode);3450}34513452sub _get_prop3453{3454my$self=shift;3455my$key=shift;3456my$tablename=$self->tablename("properties");34573458my$db_query=$self->{dbh}->prepare_cached("SELECT value FROM$tablenameWHERE key=?",{},1);3459$db_query->execute($key);3460my($value) =$db_query->fetchrow_array;34613462return$value;3463}34643465sub _set_prop3466{3467my$self=shift;3468my$key=shift;3469my$value=shift;3470my$tablename=$self->tablename("properties");34713472my$db_query=$self->{dbh}->prepare_cached("UPDATE$tablenameSET value=? WHERE key=?",{},1);3473$db_query->execute($value,$key);34743475unless($db_query->rows)3476{3477$db_query=$self->{dbh}->prepare_cached("INSERT INTO$tablename(key, value) VALUES (?,?)",{},1);3478$db_query->execute($key,$value);3479}34803481return$value;3482}34833484=head2 gethead34853486=cut34873488sub gethead3489{3490my$self=shift;3491my$tablename=$self->tablename("head");34923493return$self->{gethead_cache}if(defined($self->{gethead_cache} ) );34943495my$db_query=$self->{dbh}->prepare_cached("SELECT name, filehash, mode, revision, modified, commithash, author FROM$tablenameORDER BY name ASC",{},1);3496$db_query->execute();34973498my$tree= [];3499while(my$file=$db_query->fetchrow_hashref)3500{3501push@$tree,$file;3502}35033504$self->{gethead_cache} =$tree;35053506return$tree;3507}35083509=head2 getlog35103511=cut35123513sub getlog3514{3515my$self=shift;3516my$filename=shift;3517my$tablename=$self->tablename("revision");35183519my$db_query=$self->{dbh}->prepare_cached("SELECT name, filehash, author, mode, revision, modified, commithash FROM$tablenameWHERE name=? ORDER BY revision DESC",{},1);3520$db_query->execute($filename);35213522my$tree= [];3523while(my$file=$db_query->fetchrow_hashref)3524{3525push@$tree,$file;3526}35273528return$tree;3529}35303531=head2 getmeta35323533This function takes a filename (with path) argument and returns a hashref of3534metadata for that file.35353536=cut35373538sub getmeta3539{3540my$self=shift;3541my$filename=shift;3542my$revision=shift;3543my$tablename_rev=$self->tablename("revision");3544my$tablename_head=$self->tablename("head");35453546my$db_query;3547if(defined($revision)and$revision=~/^\d+$/)3548{3549$db_query=$self->{dbh}->prepare_cached("SELECT * FROM$tablename_revWHERE name=? AND revision=?",{},1);3550$db_query->execute($filename,$revision);3551}3552elsif(defined($revision)and$revision=~/^[a-zA-Z0-9]{40}$/)3553{3554$db_query=$self->{dbh}->prepare_cached("SELECT * FROM$tablename_revWHERE name=? AND commithash=?",{},1);3555$db_query->execute($filename,$revision);3556}else{3557$db_query=$self->{dbh}->prepare_cached("SELECT * FROM$tablename_headWHERE name=?",{},1);3558$db_query->execute($filename);3559}35603561return$db_query->fetchrow_hashref;3562}35633564=head2 commitmessage35653566this function takes a commithash and returns the commit message for that commit35673568=cut3569sub commitmessage3570{3571my$self=shift;3572my$commithash=shift;3573my$tablename=$self->tablename("commitmsgs");35743575die("Need commithash")unless(defined($commithash)and$commithash=~/^[a-zA-Z0-9]{40}$/);35763577my$db_query;3578$db_query=$self->{dbh}->prepare_cached("SELECT value FROM$tablenameWHERE key=?",{},1);3579$db_query->execute($commithash);35803581my($message) =$db_query->fetchrow_array;35823583if(defined($message) )3584{3585$message.=" "if($message=~/\n$/);3586return$message;3587}35883589my@lines= safe_pipe_capture("git","cat-file","commit",$commithash);3590shift@lineswhile($lines[0] =~/\S/);3591$message=join("",@lines);3592$message.=" "if($message=~/\n$/);3593return$message;3594}35953596=head2 gethistory35973598This function takes a filename (with path) argument and returns an arrayofarrays3599containing revision,filehash,commithash ordered by revision descending36003601=cut3602sub gethistory3603{3604my$self=shift;3605my$filename=shift;3606my$tablename=$self->tablename("revision");36073608my$db_query;3609$db_query=$self->{dbh}->prepare_cached("SELECT revision, filehash, commithash FROM$tablenameWHERE name=? ORDER BY revision DESC",{},1);3610$db_query->execute($filename);36113612return$db_query->fetchall_arrayref;3613}36143615=head2 gethistorydense36163617This function takes a filename (with path) argument and returns an arrayofarrays3618containing revision,filehash,commithash ordered by revision descending.36193620This version of gethistory skips deleted entries -- so it is useful for annotate.3621The 'dense' part is a reference to a '--dense' option available for git-rev-list3622and other git tools that depend on it.36233624=cut3625sub gethistorydense3626{3627my$self=shift;3628my$filename=shift;3629my$tablename=$self->tablename("revision");36303631my$db_query;3632$db_query=$self->{dbh}->prepare_cached("SELECT revision, filehash, commithash FROM$tablenameWHERE name=? AND filehash!='deleted' ORDER BY revision DESC",{},1);3633$db_query->execute($filename);36343635return$db_query->fetchall_arrayref;3636}36373638=head2 in_array()36393640from Array::PAT - mimics the in_array() function3641found in PHP. Yuck but works for small arrays.36423643=cut3644sub in_array3645{3646my($check,@array) =@_;3647my$retval=0;3648foreachmy$test(@array){3649if($checkeq$test){3650$retval=1;3651}3652}3653return$retval;3654}36553656=head2 safe_pipe_capture36573658an alternative to `command` that allows input to be passed as an array3659to work around shell problems with weird characters in arguments36603661=cut3662sub safe_pipe_capture {36633664my@output;36653666if(my$pid=open my$child,'-|') {3667@output= (<$child>);3668close$childor die join(' ',@_).":$!$?";3669}else{3670exec(@_)or die"$!$?";# exec() can fail the executable can't be found3671}3672returnwantarray?@output:join('',@output);3673}36743675=head2 mangle_dirname36763677create a string from a directory name that is suitable to use as3678part of a filename, mainly by converting all chars except \w.- to _36793680=cut3681sub mangle_dirname {3682my$dirname=shift;3683return unlessdefined$dirname;36843685$dirname=~s/[^\w.-]/_/g;36863687return$dirname;3688}36893690=head2 mangle_tablename36913692create a string from a that is suitable to use as part of an SQL table3693name, mainly by converting all chars except \w to _36943695=cut3696sub mangle_tablename {3697my$tablename=shift;3698return unlessdefined$tablename;36993700$tablename=~s/[^\w_]/_/g;37013702return$tablename;3703}370437051;