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{1147# TODO: If it has been modified in the sandbox, error out1148# with the appropriate message, rather than deleting a modified1149# file.11501151my($filepart,$dirpart) = filenamesplit($filename,1);11521153$log->info("Removing '$filename' from working copy (no longer in the repo)");11541155print"E cvs update: `$filename' is no longer in the repository\n";1156# Don't want to actually _DO_ the update if -n specified1157unless($state->{globaloptions}{-n} ) {1158print"Removed$dirpart\n";1159print"$filepart\n";1160}1161}1162elsif(not defined($state->{entries}{$filename}{modified_hash} )1163or$state->{entries}{$filename}{modified_hash}eq$oldmeta->{filehash}1164or$meta->{filehash}eq'added')1165{1166# normal update, just send the new revision (either U=Update,1167# or A=Add, or R=Remove)1168if(defined($wrev) &&$wrev<0)1169{1170$log->info("Tell the client the file is scheduled for removal");1171print"MT text R\n";1172print"MT fname$filename\n";1173print"MT newline\n";1174next;1175}1176elsif( (!defined($wrev) ||$wrev==0) && (!defined($meta->{revision}) ||$meta->{revision} ==0) )1177{1178$log->info("Tell the client the file is scheduled for addition");1179print"MT text A\n";1180print"MT fname$filename\n";1181print"MT newline\n";1182next;11831184}1185else{1186$log->info("Updating '$filename' to ".$meta->{revision});1187print"MT +updated\n";1188print"MT text U\n";1189print"MT fname$filename\n";1190print"MT newline\n";1191print"MT -updated\n";1192}11931194my($filepart,$dirpart) = filenamesplit($filename,1);11951196# Don't want to actually _DO_ the update if -n specified1197unless($state->{globaloptions}{-n} )1198{1199if(defined($wrev) )1200{1201# instruct client we're sending a file to put in this path as a replacement1202print"Update-existing$dirpart\n";1203$log->debug("Updating existing file 'Update-existing$dirpart'");1204}else{1205# instruct client we're sending a file to put in this path as a new file1206print"Clear-static-directory$dirpart\n";1207print$state->{CVSROOT} ."/$state->{module}/$dirpart\n";1208print"Clear-sticky$dirpart\n";1209print$state->{CVSROOT} ."/$state->{module}/$dirpart\n";12101211$log->debug("Creating new file 'Created$dirpart'");1212print"Created$dirpart\n";1213}1214print$state->{CVSROOT} ."/$state->{module}/$filename\n";12151216# this is an "entries" line1217my$kopts= kopts_from_path($filename,"sha1",$meta->{filehash});1218$log->debug("/$filepart/1.$meta->{revision}//$kopts/");1219print"/$filepart/1.$meta->{revision}//$kopts/\n";12201221# permissions1222$log->debug("SEND : u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}");1223print"u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}\n";12241225# transmit file1226 transmitfile($meta->{filehash});1227}1228}else{1229$log->info("Updating '$filename'");1230my($filepart,$dirpart) = filenamesplit($meta->{name},1);12311232my$mergeDir= setupTmpDir();12331234my$file_local=$filepart.".mine";1235my$mergedFile="$mergeDir/$file_local";1236system("ln","-s",$state->{entries}{$filename}{modified_filename},$file_local);1237my$file_old=$filepart.".".$oldmeta->{revision};1238 transmitfile($oldmeta->{filehash}, { targetfile =>$file_old});1239my$file_new=$filepart.".".$meta->{revision};1240 transmitfile($meta->{filehash}, { targetfile =>$file_new});12411242# we need to merge with the local changes ( M=successful merge, C=conflict merge )1243$log->info("Merging$file_local,$file_old,$file_new");1244print"M Merging differences between 1.$oldmeta->{revision} and 1.$meta->{revision} into$filename\n";12451246$log->debug("Temporary directory for merge is$mergeDir");12471248my$return=system("git","merge-file",$file_local,$file_old,$file_new);1249$return>>=8;12501251 cleanupTmpDir();12521253if($return==0)1254{1255$log->info("Merged successfully");1256print"M M$filename\n";1257$log->debug("Merged$dirpart");12581259# Don't want to actually _DO_ the update if -n specified1260unless($state->{globaloptions}{-n} )1261{1262print"Merged$dirpart\n";1263$log->debug($state->{CVSROOT} ."/$state->{module}/$filename");1264print$state->{CVSROOT} ."/$state->{module}/$filename\n";1265my$kopts= kopts_from_path("$dirpart/$filepart",1266"file",$mergedFile);1267$log->debug("/$filepart/1.$meta->{revision}//$kopts/");1268print"/$filepart/1.$meta->{revision}//$kopts/\n";1269}1270}1271elsif($return==1)1272{1273$log->info("Merged with conflicts");1274print"E cvs update: conflicts found in$filename\n";1275print"M C$filename\n";12761277# Don't want to actually _DO_ the update if -n specified1278unless($state->{globaloptions}{-n} )1279{1280print"Merged$dirpart\n";1281print$state->{CVSROOT} ."/$state->{module}/$filename\n";1282my$kopts= kopts_from_path("$dirpart/$filepart",1283"file",$mergedFile);1284print"/$filepart/1.$meta->{revision}/+/$kopts/\n";1285}1286}1287else1288{1289$log->warn("Merge failed");1290next;1291}12921293# Don't want to actually _DO_ the update if -n specified1294unless($state->{globaloptions}{-n} )1295{1296# permissions1297$log->debug("SEND : u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}");1298print"u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}\n";12991300# transmit file, format is single integer on a line by itself (file1301# size) followed by the file contents1302# TODO : we should copy files in blocks1303my$data=`cat$mergedFile`;1304$log->debug("File size : " . length($data));1305 print length($data) . "\n";1306 print$data;1307 }1308 }13091310 }13111312 print "ok\n";1313}13141315sub req_ci1316{1317 my ($cmd,$data) =@_;13181319 argsplit("ci");13201321 #$log->debug("State : " . Dumper($state));13221323$log->info("req_ci : " . ( defined($data) ?$data: "[NULL]" ));13241325 if ($state->{method} eq 'pserver' and$state->{user} eq 'anonymous' )1326 {1327 print "error 1 anonymous user cannot commit via pserver\n";1328 cleanupWorkTree();1329 exit;1330 }13311332 if ( -e$state->{CVSROOT} . "/index" )1333 {1334$log->warn("file 'index' already exists in the git repository");1335 print "error 1 Index already exists in git repo\n";1336 cleanupWorkTree();1337 exit;1338 }13391340 # Grab a handle to the SQLite db and do any necessary updates1341 my$updater= GITCVS::updater->new($state->{CVSROOT},$state->{module},$log);1342$updater->update();13431344 # Remember where the head was at the beginning.1345 my$parenthash= `git show-ref -s refs/heads/$state->{module}`;1346 chomp$parenthash;1347 if ($parenthash!~ /^[0-9a-f]{40}$/) {1348 print "error 1 pserver cannot find the current HEAD of module";1349 cleanupWorkTree();1350 exit;1351 }13521353 setupWorkTree($parenthash);13541355$log->info("Lockless commit start, basing commit on '$work->{workDir}', index file is '$work->{index}'");13561357$log->info("Created index '$work->{index}' for head$state->{module} - exit status$?");13581359 my@committedfiles= ();1360 my%oldmeta;13611362 # foreach file specified on the command line ...1363 foreach my$filename( @{$state->{args}} )1364 {1365 my$committedfile=$filename;1366$filename= filecleanup($filename);13671368 next unless ( exists$state->{entries}{$filename}{modified_filename} or not$state->{entries}{$filename}{unchanged} );13691370 my$meta=$updater->getmeta($filename);1371$oldmeta{$filename} =$meta;13721373 my$wrev= revparse($filename);13741375 my ($filepart,$dirpart) = filenamesplit($filename);13761377 # do a checkout of the file if it is part of this tree1378 if ($wrev) {1379 system('git', 'checkout-index', '-f', '-u',$filename);1380 unless ($?== 0) {1381 die "Error running git-checkout-index -f -u$filename:$!";1382 }1383 }13841385 my$addflag= 0;1386 my$rmflag= 0;1387$rmflag= 1 if ( defined($wrev) and$wrev< 0 );1388$addflag= 1 unless ( -e$filename);13891390 # Do up to date checking1391 unless ($addflagor$wrev==$meta->{revision} or ($rmflagand -$wrev==$meta->{revision} ) )1392 {1393 # fail everything if an up to date check fails1394 print "error 1 Up to date check failed for$filename\n";1395 cleanupWorkTree();1396 exit;1397 }13981399 push@committedfiles,$committedfile;1400$log->info("Committing$filename");14011402 system("mkdir","-p",$dirpart) unless ( -d$dirpart);14031404 unless ($rmflag)1405 {1406$log->debug("rename$state->{entries}{$filename}{modified_filename}$filename");1407 rename$state->{entries}{$filename}{modified_filename},$filename;14081409 # Calculate modes to remove1410 my$invmode= "";1411 foreach ( qw (r w x) ) {$invmode.=$_unless ($state->{entries}{$filename}{modified_mode} =~ /$_/); }14121413$log->debug("chmod u+" .$state->{entries}{$filename}{modified_mode} . "-" .$invmode. "$filename");1414 system("chmod","u+" .$state->{entries}{$filename}{modified_mode} . "-" .$invmode,$filename);1415 }14161417 if ($rmflag)1418 {1419$log->info("Removing file '$filename'");1420 unlink($filename);1421 system("git", "update-index", "--remove",$filename);1422 }1423 elsif ($addflag)1424 {1425$log->info("Adding file '$filename'");1426 system("git", "update-index", "--add",$filename);1427 } else {1428$log->info("Updating file '$filename'");1429 system("git", "update-index",$filename);1430 }1431 }14321433 unless ( scalar(@committedfiles) > 0 )1434 {1435 print "E No files to commit\n";1436 print "ok\n";1437 cleanupWorkTree();1438 return;1439 }14401441 my$treehash= `git write-tree`;1442 chomp$treehash;14431444$log->debug("Treehash :$treehash, Parenthash :$parenthash");14451446 # write our commit message out if we have one ...1447 my ($msg_fh,$msg_filename) = tempfile( DIR =>$TEMP_DIR);1448 print$msg_fh$state->{opt}{m};# if ( exists ($state->{opt}{m} ) );1449 if ( defined ($cfg->{gitcvs}{commitmsgannotation} ) ) {1450 if ($cfg->{gitcvs}{commitmsgannotation} !~ /^\s*$/) {1451 print$msg_fh"\n\n".$cfg->{gitcvs}{commitmsgannotation}."\n"1452 }1453 } else {1454 print$msg_fh"\n\nvia git-CVS emulator\n";1455 }1456 close$msg_fh;14571458 my$commithash= `git commit-tree $treehash-p $parenthash<$msg_filename`;1459chomp($commithash);1460$log->info("Commit hash :$commithash");14611462unless($commithash=~/[a-zA-Z0-9]{40}/)1463{1464$log->warn("Commit failed (Invalid commit hash)");1465print"error 1 Commit failed (unknown reason)\n";1466 cleanupWorkTree();1467exit;1468}14691470### Emulate git-receive-pack by running hooks/update1471my@hook= ($ENV{GIT_DIR}.'hooks/update',"refs/heads/$state->{module}",1472$parenthash,$commithash);1473if( -x $hook[0] ) {1474unless(system(@hook) ==0)1475{1476$log->warn("Commit failed (update hook declined to update ref)");1477print"error 1 Commit failed (update hook declined)\n";1478 cleanupWorkTree();1479exit;1480}1481}14821483### Update the ref1484if(system(qw(git update-ref -m),"cvsserver ci",1485"refs/heads/$state->{module}",$commithash,$parenthash)) {1486$log->warn("update-ref for$state->{module} failed.");1487print"error 1 Cannot commit -- update first\n";1488 cleanupWorkTree();1489exit;1490}14911492### Emulate git-receive-pack by running hooks/post-receive1493my$hook=$ENV{GIT_DIR}.'hooks/post-receive';1494if( -x $hook) {1495open(my$pipe,"|$hook") ||die"can't fork$!";14961497local$SIG{PIPE} =sub{die'pipe broke'};14981499print$pipe"$parenthash$commithashrefs/heads/$state->{module}\n";15001501close$pipe||die"bad pipe:$!$?";1502}15031504$updater->update();15051506### Then hooks/post-update1507$hook=$ENV{GIT_DIR}.'hooks/post-update';1508if(-x $hook) {1509system($hook,"refs/heads/$state->{module}");1510}15111512# foreach file specified on the command line ...1513foreachmy$filename(@committedfiles)1514{1515$filename= filecleanup($filename);15161517my$meta=$updater->getmeta($filename);1518unless(defined$meta->{revision}) {1519$meta->{revision} =1;1520}15211522my($filepart,$dirpart) = filenamesplit($filename,1);15231524$log->debug("Checked-in$dirpart:$filename");15251526print"M$state->{CVSROOT}/$state->{module}/$filename,v <--$dirpart$filepart\n";1527if(defined$meta->{filehash} &&$meta->{filehash}eq"deleted")1528{1529print"M new revision: delete; previous revision: 1.$oldmeta{$filename}{revision}\n";1530print"Remove-entry$dirpart\n";1531print"$filename\n";1532}else{1533if($meta->{revision} ==1) {1534print"M initial revision: 1.1\n";1535}else{1536print"M new revision: 1.$meta->{revision}; previous revision: 1.$oldmeta{$filename}{revision}\n";1537}1538print"Checked-in$dirpart\n";1539print"$filename\n";1540my$kopts= kopts_from_path($filename,"sha1",$meta->{filehash});1541print"/$filepart/1.$meta->{revision}//$kopts/\n";1542}1543}15441545 cleanupWorkTree();1546print"ok\n";1547}15481549sub req_status1550{1551my($cmd,$data) =@_;15521553 argsplit("status");15541555$log->info("req_status : ". (defined($data) ?$data:"[NULL]"));1556#$log->debug("status state : " . Dumper($state));15571558# Grab a handle to the SQLite db and do any necessary updates1559my$updater= GITCVS::updater->new($state->{CVSROOT},$state->{module},$log);1560$updater->update();15611562# if no files were specified, we need to work out what files we should be providing status on ...1563 argsfromdir($updater);15641565# foreach file specified on the command line ...1566foreachmy$filename( @{$state->{args}} )1567{1568$filename= filecleanup($filename);15691570next ifexists($state->{opt}{l}) &&index($filename,'/',length($state->{prependdir})) >=0;15711572my$meta=$updater->getmeta($filename);1573my$oldmeta=$meta;15741575my$wrev= revparse($filename);15761577# If the working copy is an old revision, lets get that version too for comparison.1578if(defined($wrev)and$wrev!=$meta->{revision} )1579{1580$oldmeta=$updater->getmeta($filename,$wrev);1581}15821583# TODO : All possible statuses aren't yet implemented1584my$status;1585# Files are up to date if the working copy and repo copy have the same revision, and the working copy is unmodified1586$status="Up-to-date"if(defined($wrev)and defined($meta->{revision})and$wrev==$meta->{revision}1587and1588( ($state->{entries}{$filename}{unchanged}and(not defined($state->{entries}{$filename}{conflict} )or$state->{entries}{$filename}{conflict} !~/^\+=/) )1589or(defined($state->{entries}{$filename}{modified_hash})and$state->{entries}{$filename}{modified_hash}eq$meta->{filehash} ) )1590);15911592# Need checkout if the working copy has an older revision than the repo copy, and the working copy is unmodified1593$status||="Needs Checkout"if(defined($wrev)and defined($meta->{revision} )and$meta->{revision} >$wrev1594and1595($state->{entries}{$filename}{unchanged}1596or(defined($state->{entries}{$filename}{modified_hash})and$state->{entries}{$filename}{modified_hash}eq$oldmeta->{filehash} ) )1597);15981599# Need checkout if it exists in the repo but doesn't have a working copy1600$status||="Needs Checkout"if(not defined($wrev)and defined($meta->{revision} ) );16011602# Locally modified if working copy and repo copy have the same revision but there are local changes1603$status||="Locally Modified"if(defined($wrev)and defined($meta->{revision})and$wrev==$meta->{revision}and$state->{entries}{$filename}{modified_filename} );16041605# Needs Merge if working copy revision is less than repo copy and there are local changes1606$status||="Needs Merge"if(defined($wrev)and defined($meta->{revision} )and$meta->{revision} >$wrevand$state->{entries}{$filename}{modified_filename} );16071608$status||="Locally Added"if(defined($state->{entries}{$filename}{revision} )and not defined($meta->{revision} ) );1609$status||="Locally Removed"if(defined($wrev)and defined($meta->{revision} )and-$wrev==$meta->{revision} );1610$status||="Unresolved Conflict"if(defined($state->{entries}{$filename}{conflict} )and$state->{entries}{$filename}{conflict} =~/^\+=/);1611$status||="File had conflicts on merge"if(0);16121613$status||="Unknown";16141615my($filepart) = filenamesplit($filename);16161617print"M ===================================================================\n";1618print"M File:$filepart\tStatus:$status\n";1619if(defined($state->{entries}{$filename}{revision}) )1620{1621print"M Working revision:\t".$state->{entries}{$filename}{revision} ."\n";1622}else{1623print"M Working revision:\tNo entry for$filename\n";1624}1625if(defined($meta->{revision}) )1626{1627print"M Repository revision:\t1.".$meta->{revision} ."\t$state->{CVSROOT}/$state->{module}/$filename,v\n";1628print"M Sticky Tag:\t\t(none)\n";1629print"M Sticky Date:\t\t(none)\n";1630print"M Sticky Options:\t\t(none)\n";1631}else{1632print"M Repository revision:\tNo revision control file\n";1633}1634print"M\n";1635}16361637print"ok\n";1638}16391640sub req_diff1641{1642my($cmd,$data) =@_;16431644 argsplit("diff");16451646$log->debug("req_diff : ". (defined($data) ?$data:"[NULL]"));1647#$log->debug("status state : " . Dumper($state));16481649my($revision1,$revision2);1650if(defined($state->{opt}{r} )and ref$state->{opt}{r}eq"ARRAY")1651{1652$revision1=$state->{opt}{r}[0];1653$revision2=$state->{opt}{r}[1];1654}else{1655$revision1=$state->{opt}{r};1656}16571658$revision1=~s/^1\.//if(defined($revision1) );1659$revision2=~s/^1\.//if(defined($revision2) );16601661$log->debug("Diffing revisions ". (defined($revision1) ?$revision1:"[NULL]") ." and ". (defined($revision2) ?$revision2:"[NULL]") );16621663# Grab a handle to the SQLite db and do any necessary updates1664my$updater= GITCVS::updater->new($state->{CVSROOT},$state->{module},$log);1665$updater->update();16661667# if no files were specified, we need to work out what files we should be providing status on ...1668 argsfromdir($updater);16691670# foreach file specified on the command line ...1671foreachmy$filename( @{$state->{args}} )1672{1673$filename= filecleanup($filename);16741675my($fh,$file1,$file2,$meta1,$meta2,$filediff);16761677my$wrev= revparse($filename);16781679# We need _something_ to diff against1680next unless(defined($wrev) );16811682# if we have a -r switch, use it1683if(defined($revision1) )1684{1685(undef,$file1) = tempfile( DIR =>$TEMP_DIR, OPEN =>0);1686$meta1=$updater->getmeta($filename,$revision1);1687unless(defined($meta1)and$meta1->{filehash}ne"deleted")1688{1689print"E File$filenameat revision 1.$revision1doesn't exist\n";1690next;1691}1692 transmitfile($meta1->{filehash}, { targetfile =>$file1});1693}1694# otherwise we just use the working copy revision1695else1696{1697(undef,$file1) = tempfile( DIR =>$TEMP_DIR, OPEN =>0);1698$meta1=$updater->getmeta($filename,$wrev);1699 transmitfile($meta1->{filehash}, { targetfile =>$file1});1700}17011702# if we have a second -r switch, use it too1703if(defined($revision2) )1704{1705(undef,$file2) = tempfile( DIR =>$TEMP_DIR, OPEN =>0);1706$meta2=$updater->getmeta($filename,$revision2);17071708unless(defined($meta2)and$meta2->{filehash}ne"deleted")1709{1710print"E File$filenameat revision 1.$revision2doesn't exist\n";1711next;1712}17131714 transmitfile($meta2->{filehash}, { targetfile =>$file2});1715}1716# otherwise we just use the working copy1717else1718{1719$file2=$state->{entries}{$filename}{modified_filename};1720}17211722# if we have been given -r, and we don't have a $file2 yet, lets get one1723if(defined($revision1)and not defined($file2) )1724{1725(undef,$file2) = tempfile( DIR =>$TEMP_DIR, OPEN =>0);1726$meta2=$updater->getmeta($filename,$wrev);1727 transmitfile($meta2->{filehash}, { targetfile =>$file2});1728}17291730# We need to have retrieved something useful1731next unless(defined($meta1) );17321733# Files to date if the working copy and repo copy have the same revision, and the working copy is unmodified1734next if(not defined($meta2)and$wrev==$meta1->{revision}1735and1736( ($state->{entries}{$filename}{unchanged}and(not defined($state->{entries}{$filename}{conflict} )or$state->{entries}{$filename}{conflict} !~/^\+=/) )1737or(defined($state->{entries}{$filename}{modified_hash})and$state->{entries}{$filename}{modified_hash}eq$meta1->{filehash} ) )1738);17391740# Apparently we only show diffs for locally modified files1741next unless(defined($meta2)or defined($state->{entries}{$filename}{modified_filename} ) );17421743print"M Index:$filename\n";1744print"M ===================================================================\n";1745print"M RCS file:$state->{CVSROOT}/$state->{module}/$filename,v\n";1746print"M retrieving revision 1.$meta1->{revision}\n"if(defined($meta1) );1747print"M retrieving revision 1.$meta2->{revision}\n"if(defined($meta2) );1748print"M diff ";1749foreachmy$opt(keys%{$state->{opt}} )1750{1751if(ref$state->{opt}{$opt}eq"ARRAY")1752{1753foreachmy$value( @{$state->{opt}{$opt}} )1754{1755print"-$opt$value";1756}1757}else{1758print"-$opt";1759print"$state->{opt}{$opt} "if(defined($state->{opt}{$opt} ) );1760}1761}1762print"$filename\n";17631764$log->info("Diffing$filename-r$meta1->{revision} -r ". ($meta2->{revision}or"workingcopy"));17651766($fh,$filediff) = tempfile ( DIR =>$TEMP_DIR);17671768if(exists$state->{opt}{u} )1769{1770system("diff -u -L '$filenamerevision 1.$meta1->{revision}' -L '$filename". (defined($meta2->{revision}) ?"revision 1.$meta2->{revision}":"working copy") ."'$file1$file2>$filediff");1771}else{1772system("diff$file1$file2>$filediff");1773}17741775while( <$fh> )1776{1777print"M$_";1778}1779close$fh;1780}17811782print"ok\n";1783}17841785sub req_log1786{1787my($cmd,$data) =@_;17881789 argsplit("log");17901791$log->debug("req_log : ". (defined($data) ?$data:"[NULL]"));1792#$log->debug("log state : " . Dumper($state));17931794my($minrev,$maxrev);1795if(defined($state->{opt}{r} )and$state->{opt}{r} =~/([\d.]+)?(::?)([\d.]+)?/)1796{1797my$control=$2;1798$minrev=$1;1799$maxrev=$3;1800$minrev=~s/^1\.//if(defined($minrev) );1801$maxrev=~s/^1\.//if(defined($maxrev) );1802$minrev++if(defined($minrev)and$controleq"::");1803}18041805# Grab a handle to the SQLite db and do any necessary updates1806my$updater= GITCVS::updater->new($state->{CVSROOT},$state->{module},$log);1807$updater->update();18081809# if no files were specified, we need to work out what files we should be providing status on ...1810 argsfromdir($updater);18111812# foreach file specified on the command line ...1813foreachmy$filename( @{$state->{args}} )1814{1815$filename= filecleanup($filename);18161817my$headmeta=$updater->getmeta($filename);18181819my$revisions=$updater->getlog($filename);1820my$totalrevisions=scalar(@$revisions);18211822if(defined($minrev) )1823{1824$log->debug("Removing revisions less than$minrev");1825while(scalar(@$revisions) >0and$revisions->[-1]{revision} <$minrev)1826{1827pop@$revisions;1828}1829}1830if(defined($maxrev) )1831{1832$log->debug("Removing revisions greater than$maxrev");1833while(scalar(@$revisions) >0and$revisions->[0]{revision} >$maxrev)1834{1835shift@$revisions;1836}1837}18381839next unless(scalar(@$revisions) );18401841print"M\n";1842print"M RCS file:$state->{CVSROOT}/$state->{module}/$filename,v\n";1843print"M Working file:$filename\n";1844print"M head: 1.$headmeta->{revision}\n";1845print"M branch:\n";1846print"M locks: strict\n";1847print"M access list:\n";1848print"M symbolic names:\n";1849print"M keyword substitution: kv\n";1850print"M total revisions:$totalrevisions;\tselected revisions: ".scalar(@$revisions) ."\n";1851print"M description:\n";18521853foreachmy$revision(@$revisions)1854{1855print"M ----------------------------\n";1856print"M revision 1.$revision->{revision}\n";1857# reformat the date for log output1858$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}) );1859$revision->{author} = cvs_author($revision->{author});1860print"M date:$revision->{modified}; author:$revision->{author}; state: ". ($revision->{filehash}eq"deleted"?"dead":"Exp") ."; lines: +2 -3\n";1861my$commitmessage=$updater->commitmessage($revision->{commithash});1862$commitmessage=~s/^/M /mg;1863print$commitmessage."\n";1864}1865print"M =============================================================================\n";1866}18671868print"ok\n";1869}18701871sub req_annotate1872{1873my($cmd,$data) =@_;18741875 argsplit("annotate");18761877$log->info("req_annotate : ". (defined($data) ?$data:"[NULL]"));1878#$log->debug("status state : " . Dumper($state));18791880# Grab a handle to the SQLite db and do any necessary updates1881my$updater= GITCVS::updater->new($state->{CVSROOT},$state->{module},$log);1882$updater->update();18831884# if no files were specified, we need to work out what files we should be providing annotate on ...1885 argsfromdir($updater);18861887# we'll need a temporary checkout dir1888 setupWorkTree();18891890$log->info("Temp checkoutdir creation successful, basing annotate session work on '$work->{workDir}', index file is '$ENV{GIT_INDEX_FILE}'");18911892# foreach file specified on the command line ...1893foreachmy$filename( @{$state->{args}} )1894{1895$filename= filecleanup($filename);18961897my$meta=$updater->getmeta($filename);18981899next unless($meta->{revision} );19001901# get all the commits that this file was in1902# in dense format -- aka skip dead revisions1903my$revisions=$updater->gethistorydense($filename);1904my$lastseenin=$revisions->[0][2];19051906# populate the temporary index based on the latest commit were we saw1907# the file -- but do it cheaply without checking out any files1908# TODO: if we got a revision from the client, use that instead1909# to look up the commithash in sqlite (still good to default to1910# the current head as we do now)1911system("git","read-tree",$lastseenin);1912unless($?==0)1913{1914print"E error running git-read-tree$lastseenin$ENV{GIT_INDEX_FILE}$!\n";1915return;1916}1917$log->info("Created index '$ENV{GIT_INDEX_FILE}' with commit$lastseenin- exit status$?");19181919# do a checkout of the file1920system('git','checkout-index','-f','-u',$filename);1921unless($?==0) {1922print"E error running git-checkout-index -f -u$filename:$!\n";1923return;1924}19251926$log->info("Annotate$filename");19271928# Prepare a file with the commits from the linearized1929# history that annotate should know about. This prevents1930# git-jsannotate telling us about commits we are hiding1931# from the client.19321933my$a_hints="$work->{workDir}/.annotate_hints";1934if(!open(ANNOTATEHINTS,'>',$a_hints)) {1935print"E failed to open '$a_hints' for writing:$!\n";1936return;1937}1938for(my$i=0;$i<@$revisions;$i++)1939{1940print ANNOTATEHINTS $revisions->[$i][2];1941if($i+1<@$revisions) {# have we got a parent?1942print ANNOTATEHINTS ' '.$revisions->[$i+1][2];1943}1944print ANNOTATEHINTS "\n";1945}19461947print ANNOTATEHINTS "\n";1948close ANNOTATEHINTS1949or(print"E failed to write$a_hints:$!\n"),return;19501951my@cmd= (qw(git annotate -l -S),$a_hints,$filename);1952if(!open(ANNOTATE,"-|",@cmd)) {1953print"E error invoking ".join(' ',@cmd) .":$!\n";1954return;1955}1956my$metadata= {};1957print"E Annotations for$filename\n";1958print"E ***************\n";1959while( <ANNOTATE> )1960{1961if(m/^([a-zA-Z0-9]{40})\t\([^\)]*\)(.*)$/i)1962{1963my$commithash=$1;1964my$data=$2;1965unless(defined($metadata->{$commithash} ) )1966{1967$metadata->{$commithash} =$updater->getmeta($filename,$commithash);1968$metadata->{$commithash}{author} = cvs_author($metadata->{$commithash}{author});1969$metadata->{$commithash}{modified} =sprintf("%02d-%s-%02d",$1,$2,$3)if($metadata->{$commithash}{modified} =~/^(\d+)\s(\w+)\s\d\d(\d\d)/);1970}1971printf("M 1.%-5d (%-8s%10s):%s\n",1972$metadata->{$commithash}{revision},1973$metadata->{$commithash}{author},1974$metadata->{$commithash}{modified},1975$data1976);1977}else{1978$log->warn("Error in annotate output! LINE:$_");1979print"E Annotate error\n";1980next;1981}1982}1983close ANNOTATE;1984}19851986# done; get out of the tempdir1987 cleanupWorkTree();19881989print"ok\n";19901991}19921993# This method takes the state->{arguments} array and produces two new arrays.1994# The first is $state->{args} which is everything before the '--' argument, and1995# the second is $state->{files} which is everything after it.1996sub argsplit1997{1998$state->{args} = [];1999$state->{files} = [];2000$state->{opt} = {};20012002return unless(defined($state->{arguments})and ref$state->{arguments}eq"ARRAY");20032004my$type=shift;20052006if(defined($type) )2007{2008my$opt= {};2009$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");2010$opt= { v =>0, l =>0, R =>0}if($typeeq"status");2011$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");2012$opt= { l =>0, R =>0, k =>1, D =>1, D =>1, r =>2}if($typeeq"diff");2013$opt= { c =>0, R =>0, l =>0, f =>0, F =>1, m =>1, r =>1}if($typeeq"ci");2014$opt= { k =>1, m =>1}if($typeeq"add");2015$opt= { f =>0, l =>0, R =>0}if($typeeq"remove");2016$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");201720182019while(scalar( @{$state->{arguments}} ) >0)2020{2021my$arg=shift@{$state->{arguments}};20222023next if($argeq"--");2024next unless($arg=~/\S/);20252026# if the argument looks like a switch2027if($arg=~/^-(\w)(.*)/)2028{2029# if it's a switch that takes an argument2030if($opt->{$1} )2031{2032# If this switch has already been provided2033if($opt->{$1} >1and exists($state->{opt}{$1} ) )2034{2035$state->{opt}{$1} = [$state->{opt}{$1} ];2036if(length($2) >0)2037{2038push@{$state->{opt}{$1}},$2;2039}else{2040push@{$state->{opt}{$1}},shift@{$state->{arguments}};2041}2042}else{2043# if there's extra data in the arg, use that as the argument for the switch2044if(length($2) >0)2045{2046$state->{opt}{$1} =$2;2047}else{2048$state->{opt}{$1} =shift@{$state->{arguments}};2049}2050}2051}else{2052$state->{opt}{$1} =undef;2053}2054}2055else2056{2057push@{$state->{args}},$arg;2058}2059}2060}2061else2062{2063my$mode=0;20642065foreachmy$value( @{$state->{arguments}} )2066{2067if($valueeq"--")2068{2069$mode++;2070next;2071}2072push@{$state->{args}},$valueif($mode==0);2073push@{$state->{files}},$valueif($mode==1);2074}2075}2076}20772078# This method uses $state->{directory} to populate $state->{args} with a list of filenames2079sub argsfromdir2080{2081my$updater=shift;20822083$state->{args} = []if(scalar(@{$state->{args}}) ==1and$state->{args}[0]eq".");20842085return if(scalar( @{$state->{args}} ) >1);20862087my@gethead= @{$updater->gethead};20882089# push added files2090foreachmy$file(keys%{$state->{entries}}) {2091if(exists$state->{entries}{$file}{revision} &&2092$state->{entries}{$file}{revision} ==0)2093{2094push@gethead, { name =>$file, filehash =>'added'};2095}2096}20972098if(scalar(@{$state->{args}}) ==1)2099{2100my$arg=$state->{args}[0];2101$arg.=$state->{prependdir}if(defined($state->{prependdir} ) );21022103$log->info("Only one arg specified, checking for directory expansion on '$arg'");21042105foreachmy$file(@gethead)2106{2107next if($file->{filehash}eq"deleted"and not defined($state->{entries}{$file->{name}} ) );2108next unless($file->{name} =~/^$arg\//or$file->{name}eq$arg);2109push@{$state->{args}},$file->{name};2110}21112112shift@{$state->{args}}if(scalar(@{$state->{args}}) >1);2113}else{2114$log->info("Only one arg specified, populating file list automatically");21152116$state->{args} = [];21172118foreachmy$file(@gethead)2119{2120next if($file->{filehash}eq"deleted"and not defined($state->{entries}{$file->{name}} ) );2121next unless($file->{name} =~s/^$state->{prependdir}//);2122push@{$state->{args}},$file->{name};2123}2124}2125}21262127# This method cleans up the $state variable after a command that uses arguments has run2128sub statecleanup2129{2130$state->{files} = [];2131$state->{args} = [];2132$state->{arguments} = [];2133$state->{entries} = {};2134}21352136# Return working directory revision int "X" from CVS revision "1.X" out2137# of the the working directory "entries" state, for the given filename.2138# Return negative "X" to represent the file is scheduled for removal2139# when it is committed.2140sub revparse2141{2142my$filename=shift;21432144returnundefunless(defined($state->{entries}{$filename}{revision} ) );21452146return$1if($state->{entries}{$filename}{revision} =~/^1\.(\d+)/);2147return-$1if($state->{entries}{$filename}{revision} =~/^-1\.(\d+)/);21482149returnundef;2150}21512152# This method takes a file hash and does a CVS "file transfer". Its2153# exact behaviour depends on a second, optional hash table argument:2154# - If $options->{targetfile}, dump the contents to that file;2155# - If $options->{print}, use M/MT to transmit the contents one line2156# at a time;2157# - Otherwise, transmit the size of the file, followed by the file2158# contents.2159sub transmitfile2160{2161my$filehash=shift;2162my$options=shift;21632164if(defined($filehash)and$filehasheq"deleted")2165{2166$log->warn("filehash is 'deleted'");2167return;2168}21692170die"Need filehash"unless(defined($filehash)and$filehash=~/^[a-zA-Z0-9]{40}$/);21712172my$type=`git cat-file -t$filehash`;2173 chomp$type;21742175 die ( "Invalid type '$type' (expected 'blob')" ) unless ( defined ($type) and$typeeq "blob" );21762177 my$size= `git cat-file -s $filehash`;2178chomp$size;21792180$log->debug("transmitfile($filehash) size=$size, type=$type");21812182if(open my$fh,'-|',"git","cat-file","blob",$filehash)2183{2184if(defined($options->{targetfile} ) )2185{2186my$targetfile=$options->{targetfile};2187open NEWFILE,">",$targetfileor die("Couldn't open '$targetfile' for writing :$!");2188print NEWFILE $_while( <$fh> );2189close NEWFILE or die("Failed to write '$targetfile':$!");2190}elsif(defined($options->{print} ) &&$options->{print} ) {2191while( <$fh> ) {2192if(/\n\z/) {2193print'M ',$_;2194}else{2195print'MT text ',$_,"\n";2196}2197}2198}else{2199print"$size\n";2200printwhile( <$fh> );2201}2202close$fhor die("Couldn't close filehandle for transmitfile():$!");2203}else{2204die("Couldn't execute git-cat-file");2205}2206}22072208# This method takes a file name, and returns ( $dirpart, $filepart ) which2209# refers to the directory portion and the file portion of the filename2210# respectively2211sub filenamesplit2212{2213my$filename=shift;2214my$fixforlocaldir=shift;22152216my($filepart,$dirpart) = ($filename,".");2217($filepart,$dirpart) = ($2,$1)if($filename=~/(.*)\/(.*)/ );2218$dirpart.="/";22192220if($fixforlocaldir)2221{2222$dirpart=~s/^$state->{prependdir}//;2223}22242225return($filepart,$dirpart);2226}22272228sub filecleanup2229{2230my$filename=shift;22312232returnundefunless(defined($filename));2233if($filename=~/^\// )2234{2235print"E absolute filenames '$filename' not supported by server\n";2236returnundef;2237}22382239$filename=~s/^\.\///g;2240$filename=$state->{prependdir} .$filename;2241return$filename;2242}22432244sub validateGitDir2245{2246if( !defined($state->{CVSROOT}) )2247{2248print"error 1 CVSROOT not specified\n";2249 cleanupWorkTree();2250exit;2251}2252if($ENV{GIT_DIR}ne($state->{CVSROOT} .'/') )2253{2254print"error 1 Internally inconsistent CVSROOT\n";2255 cleanupWorkTree();2256exit;2257}2258}22592260# Setup working directory in a work tree with the requested version2261# loaded in the index.2262sub setupWorkTree2263{2264my($ver) =@_;22652266 validateGitDir();22672268if( (defined($work->{state}) &&$work->{state} !=1) ||2269defined($work->{tmpDir}) )2270{2271$log->warn("Bad work tree state management");2272print"error 1 Internal setup multiple work trees without cleanup\n";2273 cleanupWorkTree();2274exit;2275}22762277$work->{workDir} = tempdir ( DIR =>$TEMP_DIR);22782279if( !defined($work->{index}) )2280{2281(undef,$work->{index}) = tempfile ( DIR =>$TEMP_DIR, OPEN =>0);2282}22832284chdir$work->{workDir}or2285die"Unable to chdir to$work->{workDir}\n";22862287$log->info("Setting up GIT_WORK_TREE as '.' in '$work->{workDir}', index file is '$work->{index}'");22882289$ENV{GIT_WORK_TREE} =".";2290$ENV{GIT_INDEX_FILE} =$work->{index};2291$work->{state} =2;22922293if($ver)2294{2295system("git","read-tree",$ver);2296unless($?==0)2297{2298$log->warn("Error running git-read-tree");2299die"Error running git-read-tree$verin$work->{workDir}$!\n";2300}2301}2302# else # req_annotate reads tree for each file2303}23042305# Ensure current directory is in some kind of working directory,2306# with a recent version loaded in the index.2307sub ensureWorkTree2308{2309if(defined($work->{tmpDir}) )2310{2311$log->warn("Bad work tree state management [ensureWorkTree()]");2312print"error 1 Internal setup multiple dirs without cleanup\n";2313 cleanupWorkTree();2314exit;2315}2316if($work->{state} )2317{2318return;2319}23202321 validateGitDir();23222323if( !defined($work->{emptyDir}) )2324{2325$work->{emptyDir} = tempdir ( DIR =>$TEMP_DIR, OPEN =>0);2326}2327chdir$work->{emptyDir}or2328die"Unable to chdir to$work->{emptyDir}\n";23292330my$ver=`git show-ref -s refs/heads/$state->{module}`;2331chomp$ver;2332if($ver!~/^[0-9a-f]{40}$/)2333{2334$log->warn("Error from git show-ref -s refs/head$state->{module}");2335print"error 1 cannot find the current HEAD of module";2336 cleanupWorkTree();2337exit;2338}23392340if( !defined($work->{index}) )2341{2342(undef,$work->{index}) = tempfile ( DIR =>$TEMP_DIR, OPEN =>0);2343}23442345$ENV{GIT_WORK_TREE} =".";2346$ENV{GIT_INDEX_FILE} =$work->{index};2347$work->{state} =1;23482349system("git","read-tree",$ver);2350unless($?==0)2351{2352die"Error running git-read-tree$ver$!\n";2353}2354}23552356# Cleanup working directory that is not needed any longer.2357sub cleanupWorkTree2358{2359if( !$work->{state} )2360{2361return;2362}23632364chdir"/"or die"Unable to chdir '/'\n";23652366if(defined($work->{workDir}) )2367{2368 rmtree($work->{workDir} );2369undef$work->{workDir};2370}2371undef$work->{state};2372}23732374# Setup a temporary directory (not a working tree), typically for2375# merging dirty state as in req_update.2376sub setupTmpDir2377{2378$work->{tmpDir} = tempdir ( DIR =>$TEMP_DIR);2379chdir$work->{tmpDir}or die"Unable to chdir$work->{tmpDir}\n";23802381return$work->{tmpDir};2382}23832384# Clean up a previously setupTmpDir. Restore previous work tree if2385# appropriate.2386sub cleanupTmpDir2387{2388if( !defined($work->{tmpDir}) )2389{2390$log->warn("cleanup tmpdir that has not been setup");2391die"Cleanup tmpDir that has not been setup\n";2392}2393if(defined($work->{state}) )2394{2395if($work->{state} ==1)2396{2397chdir$work->{emptyDir}or2398die"Unable to chdir to$work->{emptyDir}\n";2399}2400elsif($work->{state} ==2)2401{2402chdir$work->{workDir}or2403die"Unable to chdir to$work->{emptyDir}\n";2404}2405else2406{2407$log->warn("Inconsistent work dir state");2408die"Inconsistent work dir state\n";2409}2410}2411else2412{2413chdir"/"or die"Unable to chdir '/'\n";2414}2415}24162417# Given a path, this function returns a string containing the kopts2418# that should go into that path's Entries line. For example, a binary2419# file should get -kb.2420sub kopts_from_path2421{2422my($path,$srcType,$name) =@_;24232424if(defined($cfg->{gitcvs}{usecrlfattr} )and2425$cfg->{gitcvs}{usecrlfattr} =~/\s*(1|true|yes)\s*$/i)2426{2427my($val) = check_attr("text",$path);2428if($valeq"unspecified")2429{2430$val= check_attr("crlf",$path);2431}2432if($valeq"unset")2433{2434return"-kb"2435}2436elsif( check_attr("eol",$path)ne"unspecified"||2437$valeq"set"||$valeq"input")2438{2439return"";2440}2441else2442{2443$log->info("Unrecognized check_attr crlf$path:$val");2444}2445}24462447if(defined($cfg->{gitcvs}{allbinary} ) )2448{2449if( ($cfg->{gitcvs}{allbinary} =~/^\s*(1|true|yes)\s*$/i) )2450{2451return"-kb";2452}2453elsif( ($cfg->{gitcvs}{allbinary} =~/^\s*guess\s*$/i) )2454{2455if( is_binary($srcType,$name) )2456{2457$log->debug("... as binary");2458return"-kb";2459}2460else2461{2462$log->debug("... as text");2463}2464}2465}2466# Return "" to give no special treatment to any path2467return"";2468}24692470sub check_attr2471{2472my($attr,$path) =@_;2473 ensureWorkTree();2474if(open my$fh,'-|',"git","check-attr",$attr,"--",$path)2475{2476my$val= <$fh>;2477close$fh;2478$val=~s/.*: ([^:\r\n]*)\s*$/$1/;2479return$val;2480}2481else2482{2483returnundef;2484}2485}24862487# This should have the same heuristics as convert.c:is_binary() and related.2488# Note that the bare CR test is done by callers in convert.c.2489sub is_binary2490{2491my($srcType,$name) =@_;2492$log->debug("is_binary($srcType,$name)");24932494# Minimize amount of interpreted code run in the inner per-character2495# loop for large files, by totalling each character value and2496# then analyzing the totals.2497my@counts;2498my$i;2499for($i=0;$i<256;$i++)2500{2501$counts[$i]=0;2502}25032504my$fh= open_blob_or_die($srcType,$name);2505my$line;2506while(defined($line=<$fh>) )2507{2508# Any '\0' and bare CR are considered binary.2509if($line=~/\0|(\r[^\n])/)2510{2511close($fh);2512return1;2513}25142515# Count up each character in the line:2516my$len=length($line);2517for($i=0;$i<$len;$i++)2518{2519$counts[ord(substr($line,$i,1))]++;2520}2521}2522close$fh;25232524# Don't count CR and LF as either printable/nonprintable2525$counts[ord("\n")]=0;2526$counts[ord("\r")]=0;25272528# Categorize individual character count into printable and nonprintable:2529my$printable=0;2530my$nonprintable=0;2531for($i=0;$i<256;$i++)2532{2533if($i<32&&2534$i!=ord("\b") &&2535$i!=ord("\t") &&2536$i!=033&&# ESC2537$i!=014)# FF2538{2539$nonprintable+=$counts[$i];2540}2541elsif($i==127)# DEL2542{2543$nonprintable+=$counts[$i];2544}2545else2546{2547$printable+=$counts[$i];2548}2549}25502551return($printable>>7) <$nonprintable;2552}25532554# Returns open file handle. Possible invocations:2555# - open_blob_or_die("file",$filename);2556# - open_blob_or_die("sha1",$filehash);2557sub open_blob_or_die2558{2559my($srcType,$name) =@_;2560my($fh);2561if($srcTypeeq"file")2562{2563if( !open$fh,"<",$name)2564{2565$log->warn("Unable to open file$name:$!");2566die"Unable to open file$name:$!\n";2567}2568}2569elsif($srcTypeeq"sha1")2570{2571unless(defined($name)and$name=~/^[a-zA-Z0-9]{40}$/)2572{2573$log->warn("Need filehash");2574die"Need filehash\n";2575}25762577my$type=`git cat-file -t$name`;2578 chomp$type;25792580 unless ( defined ($type) and$typeeq "blob" )2581 {2582$log->warn("Invalid type '$type' for '$name'");2583 die ( "Invalid type '$type' (expected 'blob')" )2584 }25852586 my$size= `git cat-file -s $name`;2587chomp$size;25882589$log->debug("open_blob_or_die($name) size=$size, type=$type");25902591unless(open$fh,'-|',"git","cat-file","blob",$name)2592{2593$log->warn("Unable to open sha1$name");2594die"Unable to open sha1$name\n";2595}2596}2597else2598{2599$log->warn("Unknown type of blob source:$srcType");2600die"Unknown type of blob source:$srcType\n";2601}2602return$fh;2603}26042605# Generate a CVS author name from Git author information, by taking the local2606# part of the email address and replacing characters not in the Portable2607# Filename Character Set (see IEEE Std 1003.1-2001, 3.276) by underscores. CVS2608# Login names are Unix login names, which should be restricted to this2609# character set.2610sub cvs_author2611{2612my$author_line=shift;2613(my$author) =$author_line=~/<([^@>]*)/;26142615$author=~s/[^-a-zA-Z0-9_.]/_/g;2616$author=~s/^-/_/;26172618$author;2619}262026212622sub descramble2623{2624# This table is from src/scramble.c in the CVS source2625my@SHIFTS= (26260,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,262716,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,2628114,120,53,79,96,109,72,108,70,64,76,67,116,74,68,87,2629111,52,75,119,49,34,82,81,95,65,112,86,118,110,122,105,263041,57,83,43,46,102,40,89,38,103,45,50,42,123,91,35,2631125,55,54,66,124,126,59,47,92,71,115,78,88,107,106,56,263236,121,117,104,101,100,69,73,99,63,94,93,39,37,61,48,263358,113,32,90,44,98,60,51,33,97,62,77,84,80,85,223,2634225,216,187,166,229,189,222,188,141,249,148,200,184,136,248,190,2635199,170,181,204,138,232,218,183,255,234,220,247,213,203,226,193,2636174,172,228,252,217,201,131,230,197,211,145,238,161,179,160,212,2637207,221,254,173,202,146,224,151,140,196,205,130,135,133,143,246,2638192,159,244,239,185,168,215,144,139,165,180,157,147,186,214,176,2639227,231,219,169,175,156,206,198,129,164,150,210,154,177,134,127,2640182,128,158,208,162,132,167,209,149,241,153,251,237,236,171,195,2641243,233,253,240,194,250,191,155,142,137,245,235,163,242,178,1522642);2643my($str) =@_;26442645# This should never happen, the same password format (A) has been2646# used by CVS since the beginning of time2647{2648my$fmt=substr($str,0,1);2649die"invalid password format `$fmt'"unless$fmteq'A';2650}26512652my@str=unpack"C*",substr($str,1);2653my$ret=join'',map{chr$SHIFTS[$_] }@str;2654return$ret;2655}265626572658package GITCVS::log;26592660####2661#### Copyright The Open University UK - 2006.2662####2663#### Authors: Martyn Smith <martyn@catalyst.net.nz>2664#### Martin Langhoff <martin@laptop.org>2665####2666####26672668use strict;2669use warnings;26702671=head1 NAME26722673GITCVS::log26742675=head1 DESCRIPTION26762677This module provides very crude logging with a similar interface to2678Log::Log4perl26792680=head1 METHODS26812682=cut26832684=head2 new26852686Creates a new log object, optionally you can specify a filename here to2687indicate the file to log to. If no log file is specified, you can specify one2688later with method setfile, or indicate you no longer want logging with method2689nofile.26902691Until one of these methods is called, all log calls will buffer messages ready2692to write out.26932694=cut2695sub new2696{2697my$class=shift;2698my$filename=shift;26992700my$self= {};27012702bless$self,$class;27032704if(defined($filename) )2705{2706open$self->{fh},">>",$filenameor die("Couldn't open '$filename' for writing :$!");2707}27082709return$self;2710}27112712=head2 setfile27132714This methods takes a filename, and attempts to open that file as the log file.2715If successful, all buffered data is written out to the file, and any further2716logging is written directly to the file.27172718=cut2719sub setfile2720{2721my$self=shift;2722my$filename=shift;27232724if(defined($filename) )2725{2726open$self->{fh},">>",$filenameor die("Couldn't open '$filename' for writing :$!");2727}27282729return unless(defined($self->{buffer} )and ref$self->{buffer}eq"ARRAY");27302731while(my$line=shift@{$self->{buffer}} )2732{2733print{$self->{fh}}$line;2734}2735}27362737=head2 nofile27382739This method indicates no logging is going to be used. It flushes any entries in2740the internal buffer, and sets a flag to ensure no further data is put there.27412742=cut2743sub nofile2744{2745my$self=shift;27462747$self->{nolog} =1;27482749return unless(defined($self->{buffer} )and ref$self->{buffer}eq"ARRAY");27502751$self->{buffer} = [];2752}27532754=head2 _logopen27552756Internal method. Returns true if the log file is open, false otherwise.27572758=cut2759sub _logopen2760{2761my$self=shift;27622763return1if(defined($self->{fh} )and ref$self->{fh}eq"GLOB");2764return0;2765}27662767=head2 debug info warn fatal27682769These four methods are wrappers to _log. They provide the actual interface for2770logging data.27712772=cut2773sub debug {my$self=shift;$self->_log("debug",@_); }2774sub info {my$self=shift;$self->_log("info",@_); }2775subwarn{my$self=shift;$self->_log("warn",@_); }2776sub fatal {my$self=shift;$self->_log("fatal",@_); }27772778=head2 _log27792780This is an internal method called by the logging functions. It generates a2781timestamp and pushes the logged line either to file, or internal buffer.27822783=cut2784sub _log2785{2786my$self=shift;2787my$level=shift;27882789return if($self->{nolog} );27902791my@time=localtime;2792my$timestring=sprintf("%4d-%02d-%02d%02d:%02d:%02d: %-5s",2793$time[5] +1900,2794$time[4] +1,2795$time[3],2796$time[2],2797$time[1],2798$time[0],2799uc$level,2800);28012802if($self->_logopen)2803{2804print{$self->{fh}}$timestring." - ".join(" ",@_) ."\n";2805}else{2806push@{$self->{buffer}},$timestring." - ".join(" ",@_) ."\n";2807}2808}28092810=head2 DESTROY28112812This method simply closes the file handle if one is open28132814=cut2815sub DESTROY2816{2817my$self=shift;28182819if($self->_logopen)2820{2821close$self->{fh};2822}2823}28242825package GITCVS::updater;28262827####2828#### Copyright The Open University UK - 2006.2829####2830#### Authors: Martyn Smith <martyn@catalyst.net.nz>2831#### Martin Langhoff <martin@laptop.org>2832####2833####28342835use strict;2836use warnings;2837use DBI;28382839=head1 METHODS28402841=cut28422843=head2 new28442845=cut2846sub new2847{2848my$class=shift;2849my$config=shift;2850my$module=shift;2851my$log=shift;28522853die"Need to specify a git repository"unless(defined($config)and-d $config);2854die"Need to specify a module"unless(defined($module) );28552856$class=ref($class) ||$class;28572858my$self= {};28592860bless$self,$class;28612862$self->{valid_tables} = {'revision'=>1,2863'revision_ix1'=>1,2864'revision_ix2'=>1,2865'head'=>1,2866'head_ix1'=>1,2867'properties'=>1,2868'commitmsgs'=>1};28692870$self->{module} =$module;2871$self->{git_path} =$config."/";28722873$self->{log} =$log;28742875die"Git repo '$self->{git_path}' doesn't exist"unless( -d $self->{git_path} );28762877$self->{dbdriver} =$cfg->{gitcvs}{$state->{method}}{dbdriver} ||2878$cfg->{gitcvs}{dbdriver} ||"SQLite";2879$self->{dbname} =$cfg->{gitcvs}{$state->{method}}{dbname} ||2880$cfg->{gitcvs}{dbname} ||"%Ggitcvs.%m.sqlite";2881$self->{dbuser} =$cfg->{gitcvs}{$state->{method}}{dbuser} ||2882$cfg->{gitcvs}{dbuser} ||"";2883$self->{dbpass} =$cfg->{gitcvs}{$state->{method}}{dbpass} ||2884$cfg->{gitcvs}{dbpass} ||"";2885$self->{dbtablenameprefix} =$cfg->{gitcvs}{$state->{method}}{dbtablenameprefix} ||2886$cfg->{gitcvs}{dbtablenameprefix} ||"";2887my%mapping= ( m =>$module,2888 a =>$state->{method},2889 u =>getlogin||getpwuid($<) || $<,2890 G =>$self->{git_path},2891 g => mangle_dirname($self->{git_path}),2892);2893$self->{dbname} =~s/%([mauGg])/$mapping{$1}/eg;2894$self->{dbuser} =~s/%([mauGg])/$mapping{$1}/eg;2895$self->{dbtablenameprefix} =~s/%([mauGg])/$mapping{$1}/eg;2896$self->{dbtablenameprefix} = mangle_tablename($self->{dbtablenameprefix});28972898die"Invalid char ':' in dbdriver"if$self->{dbdriver} =~/:/;2899die"Invalid char ';' in dbname"if$self->{dbname} =~/;/;2900$self->{dbh} = DBI->connect("dbi:$self->{dbdriver}:dbname=$self->{dbname}",2901$self->{dbuser},2902$self->{dbpass});2903die"Error connecting to database\n"unlessdefined$self->{dbh};29042905$self->{tables} = {};2906foreachmy$table(keys%{$self->{dbh}->table_info(undef,undef,undef,'TABLE')->fetchall_hashref('TABLE_NAME')} )2907{2908$self->{tables}{$table} =1;2909}29102911# Construct the revision table if required2912# The revision table stores an entry for each file, each time that file2913# changes.2914# numberOfRecords = O( numCommits * averageNumChangedFilesPerCommit )2915# This is not sufficient to support "-r {commithash}" for any2916# files except files that were modified by that commit (also,2917# some places in the code ignore/effectively strip out -r in2918# some cases, before it gets passed to getmeta()).2919# The "filehash" field typically has a git blob hash, but can also2920# be set to "dead" to indicate that the given version of the file2921# should not exist in the sandbox.2922unless($self->{tables}{$self->tablename("revision")} )2923{2924my$tablename=$self->tablename("revision");2925my$ix1name=$self->tablename("revision_ix1");2926my$ix2name=$self->tablename("revision_ix2");2927$self->{dbh}->do("2928 CREATE TABLE$tablename(2929 name TEXT NOT NULL,2930 revision INTEGER NOT NULL,2931 filehash TEXT NOT NULL,2932 commithash TEXT NOT NULL,2933 author TEXT NOT NULL,2934 modified TEXT NOT NULL,2935 mode TEXT NOT NULL2936 )2937 ");2938$self->{dbh}->do("2939 CREATE INDEX$ix1name2940 ON$tablename(name,revision)2941 ");2942$self->{dbh}->do("2943 CREATE INDEX$ix2name2944 ON$tablename(name,commithash)2945 ");2946}29472948# Construct the head table if required2949# The head table (along with the "last_commit" entry in the property2950# table) is the persisted working state of the "sub update" subroutine.2951# All of it's data is read entirely first, and completely recreated2952# last, every time "sub update" runs.2953# This is also used by "sub getmeta" when it is asked for the latest2954# version of a file (as opposed to some specific version).2955# Another way of thinking about it is as a single slice out of2956# "revisions", giving just the most recent revision information for2957# each file.2958unless($self->{tables}{$self->tablename("head")} )2959{2960my$tablename=$self->tablename("head");2961my$ix1name=$self->tablename("head_ix1");2962$self->{dbh}->do("2963 CREATE TABLE$tablename(2964 name TEXT NOT NULL,2965 revision INTEGER NOT NULL,2966 filehash TEXT NOT NULL,2967 commithash TEXT NOT NULL,2968 author TEXT NOT NULL,2969 modified TEXT NOT NULL,2970 mode TEXT NOT NULL2971 )2972 ");2973$self->{dbh}->do("2974 CREATE INDEX$ix1name2975 ON$tablename(name)2976 ");2977}29782979# Construct the properties table if required2980# - "last_commit" - Used by "sub update".2981unless($self->{tables}{$self->tablename("properties")} )2982{2983my$tablename=$self->tablename("properties");2984$self->{dbh}->do("2985 CREATE TABLE$tablename(2986 key TEXT NOT NULL PRIMARY KEY,2987 value TEXT2988 )2989 ");2990}29912992# Construct the commitmsgs table if required2993# The commitmsgs table is only used for merge commits, since2994# "sub update" will only keep one branch of parents. Shortlogs2995# for ignored commits (i.e. not on the chosen branch) will be used2996# to construct a replacement "collapsed" merge commit message,2997# which will be stored in this table. See also "sub commitmessage".2998unless($self->{tables}{$self->tablename("commitmsgs")} )2999{3000my$tablename=$self->tablename("commitmsgs");3001$self->{dbh}->do("3002 CREATE TABLE$tablename(3003 key TEXT NOT NULL PRIMARY KEY,3004 value TEXT3005 )3006 ");3007}30083009return$self;3010}30113012=head2 tablename30133014=cut3015sub tablename3016{3017my$self=shift;3018my$name=shift;30193020if(exists$self->{valid_tables}{$name}) {3021return$self->{dbtablenameprefix} .$name;3022}else{3023returnundef;3024}3025}30263027=head2 update30283029Bring the database up to date with the latest changes from3030the git repository.30313032Internal working state is read out of the "head" table and the3033"last_commit" property, then it updates "revisions" based on that, and3034finally it writes the new internal state back to the "head" table3035so it can be used as a starting point the next time update is called.30363037=cut3038sub update3039{3040my$self=shift;30413042# first lets get the commit list3043$ENV{GIT_DIR} =$self->{git_path};30443045my$commitsha1=`git rev-parse$self->{module}`;3046chomp$commitsha1;30473048my$commitinfo=`git cat-file commit$self->{module} 2>&1`;3049unless($commitinfo=~/tree\s+[a-zA-Z0-9]{40}/)3050{3051die("Invalid module '$self->{module}'");3052}305330543055my$git_log;3056my$lastcommit=$self->_get_prop("last_commit");30573058if(defined$lastcommit&&$lastcommiteq$commitsha1) {# up-to-date3059return1;3060}30613062# Start exclusive lock here...3063$self->{dbh}->begin_work()or die"Cannot lock database for BEGIN";30643065# TODO: log processing is memory bound3066# if we can parse into a 2nd file that is in reverse order3067# we can probably do something really efficient3068my@git_log_params= ('--pretty','--parents','--topo-order');30693070if(defined$lastcommit) {3071push@git_log_params,"$lastcommit..$self->{module}";3072}else{3073push@git_log_params,$self->{module};3074}3075# git-rev-list is the backend / plumbing version of git-log3076open(GITLOG,'-|','git','rev-list',@git_log_params)or die"Cannot call git-rev-list:$!";30773078my@commits;30793080my%commit= ();30813082while( <GITLOG> )3083{3084chomp;3085if(m/^commit\s+(.*)$/) {3086# on ^commit lines put the just seen commit in the stack3087# and prime things for the next one3088if(keys%commit) {3089my%copy=%commit;3090unshift@commits, \%copy;3091%commit= ();3092}3093my@parents=split(m/\s+/,$1);3094$commit{hash} =shift@parents;3095$commit{parents} = \@parents;3096}elsif(m/^(\w+?):\s+(.*)$/&& !exists($commit{message})) {3097# on rfc822-like lines seen before we see any message,3098# lowercase the entry and put it in the hash as key-value3099$commit{lc($1)} =$2;3100}else{3101# message lines - skip initial empty line3102# and trim whitespace3103if(!exists($commit{message}) &&m/^\s*$/) {3104# define it to mark the end of headers3105$commit{message} ='';3106next;3107}3108s/^\s+//;s/\s+$//;# trim ws3109$commit{message} .=$_."\n";3110}3111}3112close GITLOG;31133114unshift@commits, \%commitif(keys%commit);31153116# Now all the commits are in the @commits bucket3117# ordered by time DESC. for each commit that needs processing,3118# determine whether it's following the last head we've seen or if3119# it's on its own branch, grab a file list, and add whatever's changed3120# NOTE: $lastcommit refers to the last commit from previous run3121# $lastpicked is the last commit we picked in this run3122my$lastpicked;3123my$head= {};3124if(defined$lastcommit) {3125$lastpicked=$lastcommit;3126}31273128my$committotal=scalar(@commits);3129my$commitcount=0;31303131# Load the head table into $head (for cached lookups during the update process)3132foreachmy$file( @{$self->gethead()} )3133{3134$head->{$file->{name}} =$file;3135}31363137foreachmy$commit(@commits)3138{3139$self->{log}->debug("GITCVS::updater - Processing commit$commit->{hash} (". (++$commitcount) ." of$committotal)");3140if(defined$lastpicked)3141{3142if(!in_array($lastpicked, @{$commit->{parents}}))3143{3144# skip, we'll see this delta3145# as part of a merge later3146# warn "skipping off-track $commit->{hash}\n";3147next;3148}elsif(@{$commit->{parents}} >1) {3149# it is a merge commit, for each parent that is3150# not $lastpicked (not given a CVS revision number),3151# see if we can get a log3152# from the merge-base to that parent to put it3153# in the message as a merge summary.3154my@parents= @{$commit->{parents}};3155foreachmy$parent(@parents) {3156if($parenteq$lastpicked) {3157next;3158}3159# git-merge-base can potentially (but rarely) throw3160# several candidate merge bases. let's assume3161# that the first one is the best one.3162my$base=eval{3163 safe_pipe_capture('git','merge-base',3164$lastpicked,$parent);3165};3166# The two branches may not be related at all,3167# in which case merge base simply fails to find3168# any, but that's Ok.3169next if($@);31703171chomp$base;3172if($base) {3173my@merged;3174# print "want to log between $base $parent \n";3175open(GITLOG,'-|','git','log','--pretty=medium',"$base..$parent")3176or die"Cannot call git-log:$!";3177my$mergedhash;3178while(<GITLOG>) {3179chomp;3180if(!defined$mergedhash) {3181if(m/^commit\s+(.+)$/) {3182$mergedhash=$1;3183}else{3184next;3185}3186}else{3187# grab the first line that looks non-rfc8223188# aka has content after leading space3189if(m/^\s+(\S.*)$/) {3190my$title=$1;3191$title=substr($title,0,100);# truncate3192unshift@merged,"$mergedhash$title";3193undef$mergedhash;3194}3195}3196}3197close GITLOG;3198if(@merged) {3199$commit->{mergemsg} =$commit->{message};3200$commit->{mergemsg} .="\nSummary of merged commits:\n\n";3201foreachmy$summary(@merged) {3202$commit->{mergemsg} .="\t$summary\n";3203}3204$commit->{mergemsg} .="\n\n";3205# print "Message for $commit->{hash} \n$commit->{mergemsg}";3206}3207}3208}3209}3210}32113212# convert the date to CVS-happy format3213$commit->{date} ="$2$1$4$3$5"if($commit->{date} =~/^\w+\s+(\w+)\s+(\d+)\s+(\d+:\d+:\d+)\s+(\d+)\s+([+-]\d+)$/);32143215if(defined($lastpicked) )3216{3217my$filepipe=open(FILELIST,'-|','git','diff-tree','-z','-r',$lastpicked,$commit->{hash})or die("Cannot call git-diff-tree :$!");3218local($/) ="\0";3219while( <FILELIST> )3220{3221chomp;3222unless(/^:\d{6}\s+\d{3}(\d)\d{2}\s+[a-zA-Z0-9]{40}\s+([a-zA-Z0-9]{40})\s+(\w)$/o)3223{3224die("Couldn't process git-diff-tree line :$_");3225}3226my($mode,$hash,$change) = ($1,$2,$3);3227my$name= <FILELIST>;3228chomp($name);32293230# $log->debug("File mode=$mode, hash=$hash, change=$change, name=$name");32313232my$git_perms="";3233$git_perms.="r"if($mode&4);3234$git_perms.="w"if($mode&2);3235$git_perms.="x"if($mode&1);3236$git_perms="rw"if($git_permseq"");32373238if($changeeq"D")3239{3240#$log->debug("DELETE $name");3241$head->{$name} = {3242 name =>$name,3243 revision =>$head->{$name}{revision} +1,3244 filehash =>"deleted",3245 commithash =>$commit->{hash},3246 modified =>$commit->{date},3247 author =>$commit->{author},3248 mode =>$git_perms,3249};3250$self->insert_rev($name,$head->{$name}{revision},$hash,$commit->{hash},$commit->{date},$commit->{author},$git_perms);3251}3252elsif($changeeq"M"||$changeeq"T")3253{3254#$log->debug("MODIFIED $name");3255$head->{$name} = {3256 name =>$name,3257 revision =>$head->{$name}{revision} +1,3258 filehash =>$hash,3259 commithash =>$commit->{hash},3260 modified =>$commit->{date},3261 author =>$commit->{author},3262 mode =>$git_perms,3263};3264$self->insert_rev($name,$head->{$name}{revision},$hash,$commit->{hash},$commit->{date},$commit->{author},$git_perms);3265}3266elsif($changeeq"A")3267{3268#$log->debug("ADDED $name");3269$head->{$name} = {3270 name =>$name,3271 revision =>$head->{$name}{revision} ?$head->{$name}{revision}+1:1,3272 filehash =>$hash,3273 commithash =>$commit->{hash},3274 modified =>$commit->{date},3275 author =>$commit->{author},3276 mode =>$git_perms,3277};3278$self->insert_rev($name,$head->{$name}{revision},$hash,$commit->{hash},$commit->{date},$commit->{author},$git_perms);3279}3280else3281{3282$log->warn("UNKNOWN FILE CHANGE mode=$mode, hash=$hash, change=$change, name=$name");3283die;3284}3285}3286close FILELIST;3287}else{3288# this is used to detect files removed from the repo3289my$seen_files= {};32903291my$filepipe=open(FILELIST,'-|','git','ls-tree','-z','-r',$commit->{hash})or die("Cannot call git-ls-tree :$!");3292local$/="\0";3293while( <FILELIST> )3294{3295chomp;3296unless(/^(\d+)\s+(\w+)\s+([a-zA-Z0-9]+)\t(.*)$/o)3297{3298die("Couldn't process git-ls-tree line :$_");3299}33003301my($git_perms,$git_type,$git_hash,$git_filename) = ($1,$2,$3,$4);33023303$seen_files->{$git_filename} =1;33043305my($oldhash,$oldrevision,$oldmode) = (3306$head->{$git_filename}{filehash},3307$head->{$git_filename}{revision},3308$head->{$git_filename}{mode}3309);33103311if($git_perms=~/^\d\d\d(\d)\d\d/o)3312{3313$git_perms="";3314$git_perms.="r"if($1&4);3315$git_perms.="w"if($1&2);3316$git_perms.="x"if($1&1);3317}else{3318$git_perms="rw";3319}33203321# unless the file exists with the same hash, we need to update it ...3322unless(defined($oldhash)and$oldhasheq$git_hashand defined($oldmode)and$oldmodeeq$git_perms)3323{3324my$newrevision= ($oldrevisionor0) +1;33253326$head->{$git_filename} = {3327 name =>$git_filename,3328 revision =>$newrevision,3329 filehash =>$git_hash,3330 commithash =>$commit->{hash},3331 modified =>$commit->{date},3332 author =>$commit->{author},3333 mode =>$git_perms,3334};333533363337$self->insert_rev($git_filename,$newrevision,$git_hash,$commit->{hash},$commit->{date},$commit->{author},$git_perms);3338}3339}3340close FILELIST;33413342# Detect deleted files3343foreachmy$file(keys%$head)3344{3345unless(exists$seen_files->{$file}or$head->{$file}{filehash}eq"deleted")3346{3347$head->{$file}{revision}++;3348$head->{$file}{filehash} ="deleted";3349$head->{$file}{commithash} =$commit->{hash};3350$head->{$file}{modified} =$commit->{date};3351$head->{$file}{author} =$commit->{author};33523353$self->insert_rev($file,$head->{$file}{revision},$head->{$file}{filehash},$commit->{hash},$commit->{date},$commit->{author},$head->{$file}{mode});3354}3355}3356# END : "Detect deleted files"3357}335833593360if(exists$commit->{mergemsg})3361{3362$self->insert_mergelog($commit->{hash},$commit->{mergemsg});3363}33643365$lastpicked=$commit->{hash};33663367$self->_set_prop("last_commit",$commit->{hash});3368}33693370$self->delete_head();3371foreachmy$file(keys%$head)3372{3373$self->insert_head(3374$file,3375$head->{$file}{revision},3376$head->{$file}{filehash},3377$head->{$file}{commithash},3378$head->{$file}{modified},3379$head->{$file}{author},3380$head->{$file}{mode},3381);3382}3383# invalidate the gethead cache3384$self->{gethead_cache} =undef;338533863387# Ending exclusive lock here3388$self->{dbh}->commit()or die"Failed to commit changes to SQLite";3389}33903391sub insert_rev3392{3393my$self=shift;3394my$name=shift;3395my$revision=shift;3396my$filehash=shift;3397my$commithash=shift;3398my$modified=shift;3399my$author=shift;3400my$mode=shift;3401my$tablename=$self->tablename("revision");34023403my$insert_rev=$self->{dbh}->prepare_cached("INSERT INTO$tablename(name, revision, filehash, commithash, modified, author, mode) VALUES (?,?,?,?,?,?,?)",{},1);3404$insert_rev->execute($name,$revision,$filehash,$commithash,$modified,$author,$mode);3405}34063407sub insert_mergelog3408{3409my$self=shift;3410my$key=shift;3411my$value=shift;3412my$tablename=$self->tablename("commitmsgs");34133414my$insert_mergelog=$self->{dbh}->prepare_cached("INSERT INTO$tablename(key, value) VALUES (?,?)",{},1);3415$insert_mergelog->execute($key,$value);3416}34173418sub delete_head3419{3420my$self=shift;3421my$tablename=$self->tablename("head");34223423my$delete_head=$self->{dbh}->prepare_cached("DELETE FROM$tablename",{},1);3424$delete_head->execute();3425}34263427sub insert_head3428{3429my$self=shift;3430my$name=shift;3431my$revision=shift;3432my$filehash=shift;3433my$commithash=shift;3434my$modified=shift;3435my$author=shift;3436my$mode=shift;3437my$tablename=$self->tablename("head");34383439my$insert_head=$self->{dbh}->prepare_cached("INSERT INTO$tablename(name, revision, filehash, commithash, modified, author, mode) VALUES (?,?,?,?,?,?,?)",{},1);3440$insert_head->execute($name,$revision,$filehash,$commithash,$modified,$author,$mode);3441}34423443sub _headrev3444{3445my$self=shift;3446my$filename=shift;3447my$tablename=$self->tablename("head");34483449my$db_query=$self->{dbh}->prepare_cached("SELECT filehash, revision, mode FROM$tablenameWHERE name=?",{},1);3450$db_query->execute($filename);3451my($hash,$revision,$mode) =$db_query->fetchrow_array;34523453return($hash,$revision,$mode);3454}34553456sub _get_prop3457{3458my$self=shift;3459my$key=shift;3460my$tablename=$self->tablename("properties");34613462my$db_query=$self->{dbh}->prepare_cached("SELECT value FROM$tablenameWHERE key=?",{},1);3463$db_query->execute($key);3464my($value) =$db_query->fetchrow_array;34653466return$value;3467}34683469sub _set_prop3470{3471my$self=shift;3472my$key=shift;3473my$value=shift;3474my$tablename=$self->tablename("properties");34753476my$db_query=$self->{dbh}->prepare_cached("UPDATE$tablenameSET value=? WHERE key=?",{},1);3477$db_query->execute($value,$key);34783479unless($db_query->rows)3480{3481$db_query=$self->{dbh}->prepare_cached("INSERT INTO$tablename(key, value) VALUES (?,?)",{},1);3482$db_query->execute($key,$value);3483}34843485return$value;3486}34873488=head2 gethead34893490=cut34913492sub gethead3493{3494my$self=shift;3495my$tablename=$self->tablename("head");34963497return$self->{gethead_cache}if(defined($self->{gethead_cache} ) );34983499my$db_query=$self->{dbh}->prepare_cached("SELECT name, filehash, mode, revision, modified, commithash, author FROM$tablenameORDER BY name ASC",{},1);3500$db_query->execute();35013502my$tree= [];3503while(my$file=$db_query->fetchrow_hashref)3504{3505push@$tree,$file;3506}35073508$self->{gethead_cache} =$tree;35093510return$tree;3511}35123513=head2 getlog35143515=cut35163517sub getlog3518{3519my$self=shift;3520my$filename=shift;3521my$tablename=$self->tablename("revision");35223523my$db_query=$self->{dbh}->prepare_cached("SELECT name, filehash, author, mode, revision, modified, commithash FROM$tablenameWHERE name=? ORDER BY revision DESC",{},1);3524$db_query->execute($filename);35253526my$tree= [];3527while(my$file=$db_query->fetchrow_hashref)3528{3529push@$tree,$file;3530}35313532return$tree;3533}35343535=head2 getmeta35363537This function takes a filename (with path) argument and returns a hashref of3538metadata for that file.35393540=cut35413542sub getmeta3543{3544my$self=shift;3545my$filename=shift;3546my$revision=shift;3547my$tablename_rev=$self->tablename("revision");3548my$tablename_head=$self->tablename("head");35493550my$db_query;3551if(defined($revision)and$revision=~/^\d+$/)3552{3553$db_query=$self->{dbh}->prepare_cached("SELECT * FROM$tablename_revWHERE name=? AND revision=?",{},1);3554$db_query->execute($filename,$revision);3555}3556elsif(defined($revision)and$revision=~/^[a-zA-Z0-9]{40}$/)3557{3558$db_query=$self->{dbh}->prepare_cached("SELECT * FROM$tablename_revWHERE name=? AND commithash=?",{},1);3559$db_query->execute($filename,$revision);3560}else{3561$db_query=$self->{dbh}->prepare_cached("SELECT * FROM$tablename_headWHERE name=?",{},1);3562$db_query->execute($filename);3563}35643565return$db_query->fetchrow_hashref;3566}35673568=head2 commitmessage35693570this function takes a commithash and returns the commit message for that commit35713572=cut3573sub commitmessage3574{3575my$self=shift;3576my$commithash=shift;3577my$tablename=$self->tablename("commitmsgs");35783579die("Need commithash")unless(defined($commithash)and$commithash=~/^[a-zA-Z0-9]{40}$/);35803581my$db_query;3582$db_query=$self->{dbh}->prepare_cached("SELECT value FROM$tablenameWHERE key=?",{},1);3583$db_query->execute($commithash);35843585my($message) =$db_query->fetchrow_array;35863587if(defined($message) )3588{3589$message.=" "if($message=~/\n$/);3590return$message;3591}35923593my@lines= safe_pipe_capture("git","cat-file","commit",$commithash);3594shift@lineswhile($lines[0] =~/\S/);3595$message=join("",@lines);3596$message.=" "if($message=~/\n$/);3597return$message;3598}35993600=head2 gethistory36013602This function takes a filename (with path) argument and returns an arrayofarrays3603containing revision,filehash,commithash ordered by revision descending36043605=cut3606sub gethistory3607{3608my$self=shift;3609my$filename=shift;3610my$tablename=$self->tablename("revision");36113612my$db_query;3613$db_query=$self->{dbh}->prepare_cached("SELECT revision, filehash, commithash FROM$tablenameWHERE name=? ORDER BY revision DESC",{},1);3614$db_query->execute($filename);36153616return$db_query->fetchall_arrayref;3617}36183619=head2 gethistorydense36203621This function takes a filename (with path) argument and returns an arrayofarrays3622containing revision,filehash,commithash ordered by revision descending.36233624This version of gethistory skips deleted entries -- so it is useful for annotate.3625The 'dense' part is a reference to a '--dense' option available for git-rev-list3626and other git tools that depend on it.36273628=cut3629sub gethistorydense3630{3631my$self=shift;3632my$filename=shift;3633my$tablename=$self->tablename("revision");36343635my$db_query;3636$db_query=$self->{dbh}->prepare_cached("SELECT revision, filehash, commithash FROM$tablenameWHERE name=? AND filehash!='deleted' ORDER BY revision DESC",{},1);3637$db_query->execute($filename);36383639return$db_query->fetchall_arrayref;3640}36413642=head2 in_array()36433644from Array::PAT - mimics the in_array() function3645found in PHP. Yuck but works for small arrays.36463647=cut3648sub in_array3649{3650my($check,@array) =@_;3651my$retval=0;3652foreachmy$test(@array){3653if($checkeq$test){3654$retval=1;3655}3656}3657return$retval;3658}36593660=head2 safe_pipe_capture36613662an alternative to `command` that allows input to be passed as an array3663to work around shell problems with weird characters in arguments36643665=cut3666sub safe_pipe_capture {36673668my@output;36693670if(my$pid=open my$child,'-|') {3671@output= (<$child>);3672close$childor die join(' ',@_).":$!$?";3673}else{3674exec(@_)or die"$!$?";# exec() can fail the executable can't be found3675}3676returnwantarray?@output:join('',@output);3677}36783679=head2 mangle_dirname36803681create a string from a directory name that is suitable to use as3682part of a filename, mainly by converting all chars except \w.- to _36833684=cut3685sub mangle_dirname {3686my$dirname=shift;3687return unlessdefined$dirname;36883689$dirname=~s/[^\w.-]/_/g;36903691return$dirname;3692}36933694=head2 mangle_tablename36953696create a string from a that is suitable to use as part of an SQL table3697name, mainly by converting all chars except \w to _36983699=cut3700sub mangle_tablename {3701my$tablename=shift;3702return unlessdefined$tablename;37033704$tablename=~s/[^\w_]/_/g;37053706return$tablename;3707}370837091;