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.
8 #### Copyright The Open University UK - 2006.
10 #### Authors: Martyn Smith <martyn@catalyst.net.nz>
11 #### Martin Langhoff <martin@catalyst.net.nz>
14 #### Released under the GNU Public License, version 2.
23 use File
::Temp qw
/tempdir tempfile/;
24 use File
::Path qw
/rmtree/;
26 use Getopt
::Long
qw(:config require_order no_ignore_case);
28 my $VERSION = '@@GIT_VERSION@@';
30 my $log = GITCVS
::log->new();
48 # Enable autoflush for STDOUT (otherwise the whole thing falls apart)
51 #### Definition and mappings of functions ####
55 'Valid-responses' => \
&req_Validresponses
,
56 'valid-requests' => \
&req_validrequests
,
57 'Directory' => \
&req_Directory
,
58 'Entry' => \
&req_Entry
,
59 'Modified' => \
&req_Modified
,
60 'Unchanged' => \
&req_Unchanged
,
61 'Questionable' => \
&req_Questionable
,
62 'Argument' => \
&req_Argument
,
63 'Argumentx' => \
&req_Argument
,
64 'expand-modules' => \
&req_expandmodules
,
66 'remove' => \
&req_remove
,
68 'update' => \
&req_update
,
73 'tag' => \
&req_CATCHALL
,
74 'status' => \
&req_status
,
75 'admin' => \
&req_CATCHALL
,
76 'history' => \
&req_CATCHALL
,
77 'watchers' => \
&req_EMPTY
,
78 'editors' => \
&req_EMPTY
,
79 'annotate' => \
&req_annotate
,
80 'Global_option' => \
&req_Globaloption
,
81 #'annotate' => \&req_CATCHALL,
84 ##############################################
87 # $state holds all the bits of information the clients sends us that could
88 # potentially be useful when it comes to actually _doing_ something.
89 my $state = { prependdir
=> '' };
91 # Work is for managing temporary working directory
94 state => undef, # undef, 1 (empty), 2 (with stuff)
101 $log->info("--------------- STARTING -----------------");
104 "Usage: git cvsserver [options] [pserver|server] [<directory> ...]\n".
105 " --base-path <path> : Prepend to requested CVSROOT\n".
106 " --strict-paths : Don't allow recursing into subdirectories\n".
107 " --export-all : Don't check for gitcvs.enabled in config\n".
108 " --version, -V : Print version information and exit\n".
109 " --help, -h, -H : Print usage information and exit\n".
111 "<directory> ... is a list of allowed directories. If no directories\n".
112 "are given, all are allowed. This is an additional restriction, gitcvs\n".
113 "access still needs to be enabled by the gitcvs.enabled config option.\n";
115 my @opts = ( 'help|h|H', 'version|V',
116 'base-path=s', 'strict-paths', 'export-all' );
117 GetOptions
( $state, @opts )
120 if ($state->{version
}) {
121 print "git-cvsserver version $VERSION\n";
124 if ($state->{help
}) {
129 my $TEMP_DIR = tempdir
( CLEANUP
=> 1 );
130 $log->debug("Temporary directory is '$TEMP_DIR'");
132 $state->{method
} = 'ext';
134 if ($ARGV[0] eq 'pserver') {
135 $state->{method
} = 'pserver';
137 } elsif ($ARGV[0] eq 'server') {
142 # everything else is a directory
143 $state->{allowed_roots
} = [ @ARGV ];
145 # don't export the whole system unless the users requests it
146 if ($state->{'export-all'} && !@
{$state->{allowed_roots
}}) {
147 die "--export-all can only be used together with an explicit whitelist\n";
150 # if we are called with a pserver argument,
151 # deal with the authentication cat before entering the
153 if ($state->{method
} eq 'pserver') {
154 my $line = <STDIN
>; chomp $line;
155 unless( $line =~ /^BEGIN (AUTH|VERIFICATION) REQUEST$/) {
156 die "E Do not understand $line - expecting BEGIN AUTH REQUEST\n";
159 $line = <STDIN
>; chomp $line;
160 unless (req_Root
('root', $line)) { # reuse Root
161 print "E Invalid root $line \n";
164 $line = <STDIN
>; chomp $line;
165 unless ($line eq 'anonymous') {
166 print "E Only anonymous user allowed via pserver\n";
167 print "I HATE YOU\n";
170 $line = <STDIN
>; chomp $line; # validate the password?
171 $line = <STDIN
>; chomp $line;
172 unless ($line eq "END $request REQUEST") {
173 die "E Do not understand $line -- expecting END $request REQUEST\n";
175 print "I LOVE YOU\n";
176 exit if $request eq 'VERIFICATION'; # cvs login
177 # and now back to our regular programme...
180 # Keep going until the client closes the connection
185 # Check to see if we've seen this method, and call appropriate function.
186 if ( /^([\w-]+)(?:\s+(.*))?$/ and defined($methods->{$1}) )
188 # use the $methods hash to call the appropriate sub for this command
189 #$log->info("Method : $1");
190 &{$methods->{$1}}($1,$2);
192 # log fatal because we don't understand this function. If this happens
193 # we're fairly screwed because we don't know if the client is expecting
194 # a response. If it is, the client will hang, we'll hang, and the whole
195 # thing will be custard.
196 $log->fatal("Don't understand command $_\n");
197 die("Unknown command $_");
201 $log->debug("Processing time : user=" . (times)[0] . " system=" . (times)[1]);
202 $log->info("--------------- FINISH -----------------");
207 # Magic catchall method.
208 # This is the method that will handle all commands we haven't yet
209 # implemented. It simply sends a warning to the log file indicating a
210 # command that hasn't been implemented has been invoked.
213 my ( $cmd, $data ) = @_;
214 $log->warn("Unhandled command : req_$cmd : $data");
217 # This method invariably succeeds with an empty response.
224 # Response expected: no. Tell the server which CVSROOT to use. Note that
225 # pathname is a local directory and not a fully qualified CVSROOT variable.
226 # pathname must already exist; if creating a new root, use the init
227 # request, not Root. pathname does not include the hostname of the server,
228 # how to access the server, etc.; by the time the CVS protocol is in use,
229 # connection, authentication, etc., are already taken care of. The Root
230 # request must be sent only once, and it must be sent before any requests
231 # other than Valid-responses, valid-requests, UseUnchanged, Set or init.
234 my ( $cmd, $data ) = @_;
235 $log->debug("req_Root : $data");
237 unless ($data =~ m
#^/#) {
238 print "error 1 Root must be an absolute pathname\n";
242 my $cvsroot = $state->{'base-path'} || '';
246 if ($state->{CVSROOT
}
247 && ($state->{CVSROOT
} ne $cvsroot)) {
248 print "error 1 Conflicting roots specified\n";
252 $state->{CVSROOT
} = $cvsroot;
254 $ENV{GIT_DIR
} = $state->{CVSROOT
} . "/";
256 if (@
{$state->{allowed_roots
}}) {
258 foreach my $dir (@
{$state->{allowed_roots
}}) {
259 next unless $dir =~ m
#^/#;
261 if ($state->{'strict-paths'}) {
262 if ($ENV{GIT_DIR
} =~ m
#^\Q$dir\E/?$#) {
266 } elsif ($ENV{GIT_DIR
} =~ m
#^\Q$dir\E(/?$|/)#) {
273 print "E $ENV{GIT_DIR} does not seem to be a valid GIT repository\n";
275 print "error 1 $ENV{GIT_DIR} is not a valid repository\n";
280 unless (-d
$ENV{GIT_DIR
} && -e
$ENV{GIT_DIR
}.'HEAD') {
281 print "E $ENV{GIT_DIR} does not seem to be a valid GIT repository\n";
283 print "error 1 $ENV{GIT_DIR} is not a valid repository\n";
287 my @gitvars = `git-config -l`;
289 print "E problems executing git-config on the server -- this is not a git repository or the PATH is not set correctly.\n";
291 print "error 1 - problem executing git-config\n";
294 foreach my $line ( @gitvars )
296 next unless ( $line =~ /^(gitcvs)\.(?:(ext|pserver)\.)?([\w-]+)=(.*)$/ );
300 $cfg->{$1}{$2}{$3} = $4;
304 my $enabled = ($cfg->{gitcvs
}{$state->{method
}}{enabled
}
305 || $cfg->{gitcvs
}{enabled
});
306 unless ($state->{'export-all'} ||
307 ($enabled && $enabled =~ /^\s*(1|true|yes)\s*$/i)) {
308 print "E GITCVS emulation needs to be enabled on this repo\n";
309 print "E the repo config file needs a [gitcvs] section added, and the parameter 'enabled' set to 1\n";
311 print "error 1 GITCVS emulation disabled\n";
315 my $logfile = $cfg->{gitcvs
}{$state->{method
}}{logfile
} || $cfg->{gitcvs
}{logfile
};
318 $log->setfile($logfile);
326 # Global_option option \n
327 # Response expected: no. Transmit one of the global options `-q', `-Q',
328 # `-l', `-t', `-r', or `-n'. option must be one of those strings, no
329 # variations (such as combining of options) are allowed. For graceful
330 # handling of valid-requests, it is probably better to make new global
331 # options separate requests, rather than trying to add them to this
335 my ( $cmd, $data ) = @_;
336 $log->debug("req_Globaloption : $data");
337 $state->{globaloptions
}{$data} = 1;
340 # Valid-responses request-list \n
341 # Response expected: no. Tell the server what responses the client will
342 # accept. request-list is a space separated list of tokens.
343 sub req_Validresponses
345 my ( $cmd, $data ) = @_;
346 $log->debug("req_Validresponses : $data");
348 # TODO : re-enable this, currently it's not particularly useful
349 #$state->{validresponses} = [ split /\s+/, $data ];
353 # Response expected: yes. Ask the server to send back a Valid-requests
355 sub req_validrequests
357 my ( $cmd, $data ) = @_;
359 $log->debug("req_validrequests");
361 $log->debug("SEND : Valid-requests " . join(" ",keys %$methods));
362 $log->debug("SEND : ok");
364 print "Valid-requests " . join(" ",keys %$methods) . "\n";
368 # Directory local-directory \n
369 # Additional data: repository \n. Response expected: no. Tell the server
370 # what directory to use. The repository should be a directory name from a
371 # previous server response. Note that this both gives a default for Entry
372 # and Modified and also for ci and the other commands; normal usage is to
373 # send Directory for each directory in which there will be an Entry or
374 # Modified, and then a final Directory for the original directory, then the
375 # command. The local-directory is relative to the top level at which the
376 # command is occurring (i.e. the last Directory which is sent before the
377 # command); to indicate that top level, `.' should be sent for
381 my ( $cmd, $data ) = @_;
383 my $repository = <STDIN
>;
387 $state->{localdir
} = $data;
388 $state->{repository
} = $repository;
389 $state->{path
} = $repository;
390 $state->{path
} =~ s/^$state->{CVSROOT}\///;
391 $state->{module
} = $1 if ($state->{path
} =~ s/^(.*?)(\/|$)//);
392 $state->{path
} .= "/" if ( $state->{path
} =~ /\S
/ );
394 $state->{directory
} = $state->{localdir
};
395 $state->{directory
} = "" if ( $state->{directory
} eq "." );
396 $state->{directory
} .= "/" if ( $state->{directory
} =~ /\S
/ );
398 if ( (not defined($state->{prependdir
}) or $state->{prependdir
} eq '') and $state->{localdir
} eq "." and $state->{path
} =~ /\S/ )
400 $log->info("Setting prepend to '$state->{path}'");
401 $state->{prependdir
} = $state->{path
};
402 foreach my $entry ( keys %{$state->{entries
}} )
404 $state->{entries
}{$state->{prependdir
} . $entry} = $state->{entries
}{$entry};
405 delete $state->{entries
}{$entry};
409 if ( defined ( $state->{prependdir
} ) )
411 $log->debug("Prepending '$state->{prependdir}' to state|directory");
412 $state->{directory
} = $state->{prependdir
} . $state->{directory
}
414 $log->debug("req_Directory : localdir=$data repository=$repository path=$state->{path} directory=$state->{directory} module=$state->{module}");
417 # Entry entry-line \n
418 # Response expected: no. Tell the server what version of a file is on the
419 # local machine. The name in entry-line is a name relative to the directory
420 # most recently specified with Directory. If the user is operating on only
421 # some files in a directory, Entry requests for only those files need be
422 # included. If an Entry request is sent without Modified, Is-modified, or
423 # Unchanged, it means the file is lost (does not exist in the working
424 # directory). If both Entry and one of Modified, Is-modified, or Unchanged
425 # are sent for the same file, Entry must be sent first. For a given file,
426 # one can send Modified, Is-modified, or Unchanged, but not more than one
430 my ( $cmd, $data ) = @_;
432 #$log->debug("req_Entry : $data");
434 my @data = split(/\//, $data);
436 $state->{entries
}{$state->{directory
}.$data[1]} = {
437 revision
=> $data[2],
438 conflict
=> $data[3],
440 tag_or_date
=> $data[5],
443 $log->info("Received entry line '$data' => '" . $state->{directory
} . $data[1] . "'");
446 # Questionable filename \n
447 # Response expected: no. Additional data: no. Tell the server to check
448 # whether filename should be ignored, and if not, next time the server
449 # sends responses, send (in a M response) `?' followed by the directory and
450 # filename. filename must not contain `/'; it needs to be a file in the
451 # directory named by the most recent Directory request.
454 my ( $cmd, $data ) = @_;
456 $log->debug("req_Questionable : $data");
457 $state->{entries
}{$state->{directory
}.$data}{questionable
} = 1;
461 # Response expected: yes. Add a file or directory. This uses any previous
462 # Argument, Directory, Entry, or Modified requests, if they have been sent.
463 # The last Directory sent specifies the working directory at the time of
464 # the operation. To add a directory, send the directory to be added using
465 # Directory and Argument requests.
468 my ( $cmd, $data ) = @_;
472 my $updater = GITCVS
::updater
->new($state->{CVSROOT
}, $state->{module
}, $log);
475 argsfromdir
($updater);
479 foreach my $filename ( @
{$state->{args
}} )
481 $filename = filecleanup
($filename);
483 my $meta = $updater->getmeta($filename);
484 my $wrev = revparse
($filename);
486 if ($wrev && $meta && ($wrev < 0))
488 # previously removed file, add back
489 $log->info("added file $filename was previously removed, send 1.$meta->{revision}");
491 print "MT +updated\n";
492 print "MT text U \n";
493 print "MT fname $filename\n";
494 print "MT newline\n";
495 print "MT -updated\n";
497 unless ( $state->{globaloptions
}{-n
} )
499 my ( $filepart, $dirpart ) = filenamesplit
($filename,1);
501 print "Created $dirpart\n";
502 print $state->{CVSROOT
} . "/$state->{module}/$filename\n";
504 # this is an "entries" line
505 my $kopts = kopts_from_path
($filename,"sha1",$meta->{filehash
});
506 $log->debug("/$filepart/1.$meta->{revision}//$kopts/");
507 print "/$filepart/1.$meta->{revision}//$kopts/\n";
509 $log->debug("SEND : u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}");
510 print "u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}\n";
512 transmitfile
($meta->{filehash
});
518 unless ( defined ( $state->{entries
}{$filename}{modified_filename
} ) )
520 print "E cvs add: nothing known about `$filename'\n";
523 # TODO : check we're not squashing an already existing file
524 if ( defined ( $state->{entries
}{$filename}{revision
} ) )
526 print "E cvs add: `$filename' has already been entered\n";
530 my ( $filepart, $dirpart ) = filenamesplit
($filename, 1);
532 print "E cvs add: scheduling file `$filename' for addition\n";
534 print "Checked-in $dirpart\n";
536 my $kopts = kopts_from_path
($filename,"file",
537 $state->{entries
}{$filename}{modified_filename
});
538 print "/$filepart/0//$kopts/\n";
540 my $requestedKopts = $state->{opt
}{k
};
541 if(defined($requestedKopts))
543 $requestedKopts = "-k$requestedKopts";
547 $requestedKopts = "";
549 if( $kopts ne $requestedKopts )
551 $log->warn("Ignoring requested -k='$requestedKopts'"
552 . " for '$filename'; detected -k='$kopts' instead");
553 #TODO: Also have option to send warning to user?
559 if ( $addcount == 1 )
561 print "E cvs add: use `cvs commit' to add this file permanently\n";
563 elsif ( $addcount > 1 )
565 print "E cvs add: use `cvs commit' to add these files permanently\n";
572 # Response expected: yes. Remove a file. This uses any previous Argument,
573 # Directory, Entry, or Modified requests, if they have been sent. The last
574 # Directory sent specifies the working directory at the time of the
575 # operation. Note that this request does not actually do anything to the
576 # repository; the only effect of a successful remove request is to supply
577 # the client with a new entries line containing `-' to indicate a removed
578 # file. In fact, the client probably could perform this operation without
579 # contacting the server, although using remove may cause the server to
580 # perform a few more checks. The client sends a subsequent ci request to
581 # actually record the removal in the repository.
584 my ( $cmd, $data ) = @_;
588 # Grab a handle to the SQLite db and do any necessary updates
589 my $updater = GITCVS
::updater
->new($state->{CVSROOT
}, $state->{module
}, $log);
592 #$log->debug("add state : " . Dumper($state));
596 foreach my $filename ( @
{$state->{args
}} )
598 $filename = filecleanup
($filename);
600 if ( defined ( $state->{entries
}{$filename}{unchanged
} ) or defined ( $state->{entries
}{$filename}{modified_filename
} ) )
602 print "E cvs remove: file `$filename' still in working directory\n";
606 my $meta = $updater->getmeta($filename);
607 my $wrev = revparse
($filename);
609 unless ( defined ( $wrev ) )
611 print "E cvs remove: nothing known about `$filename'\n";
615 if ( defined($wrev) and $wrev < 0 )
617 print "E cvs remove: file `$filename' already scheduled for removal\n";
621 unless ( $wrev == $meta->{revision
} )
623 # TODO : not sure if the format of this message is quite correct.
624 print "E cvs remove: Up to date check failed for `$filename'\n";
629 my ( $filepart, $dirpart ) = filenamesplit
($filename, 1);
631 print "E cvs remove: scheduling `$filename' for removal\n";
633 print "Checked-in $dirpart\n";
635 my $kopts = kopts_from_path
($filename,"sha1",$meta->{filehash
});
636 print "/$filepart/-1.$wrev//$kopts/\n";
643 print "E cvs remove: use `cvs commit' to remove this file permanently\n";
645 elsif ( $rmcount > 1 )
647 print "E cvs remove: use `cvs commit' to remove these files permanently\n";
653 # Modified filename \n
654 # Response expected: no. Additional data: mode, \n, file transmission. Send
655 # the server a copy of one locally modified file. filename is a file within
656 # the most recent directory sent with Directory; it must not contain `/'.
657 # If the user is operating on only some files in a directory, only those
658 # files need to be included. This can also be sent without Entry, if there
659 # is no entry for the file.
662 my ( $cmd, $data ) = @_;
666 or (print "E end of file reading mode for $data\n"), return;
670 or (print "E end of file reading size of $data\n"), return;
673 # Grab config information
674 my $blocksize = 8192;
675 my $bytesleft = $size;
678 # Get a filehandle/name to write it to
679 my ( $fh, $filename ) = tempfile
( DIR
=> $TEMP_DIR );
681 # Loop over file data writing out to temporary file.
684 $blocksize = $bytesleft if ( $bytesleft < $blocksize );
685 read STDIN
, $tmp, $blocksize;
687 $bytesleft -= $blocksize;
691 or (print "E failed to write temporary, $filename: $!\n"), return;
693 # Ensure we have something sensible for the file mode
694 if ( $mode =~ /u=(\w+)/ )
701 # Save the file data in $state
702 $state->{entries
}{$state->{directory
}.$data}{modified_filename
} = $filename;
703 $state->{entries
}{$state->{directory
}.$data}{modified_mode
} = $mode;
704 $state->{entries
}{$state->{directory
}.$data}{modified_hash
} = `git-hash-object $filename`;
705 $state->{entries
}{$state->{directory
}.$data}{modified_hash
} =~ s/\s.*$//s;
707 #$log->debug("req_Modified : file=$data mode=$mode size=$size");
710 # Unchanged filename \n
711 # Response expected: no. Tell the server that filename has not been
712 # modified in the checked out directory. The filename is a file within the
713 # most recent directory sent with Directory; it must not contain `/'.
716 my ( $cmd, $data ) = @_;
718 $state->{entries
}{$state->{directory
}.$data}{unchanged
} = 1;
720 #$log->debug("req_Unchanged : $data");
724 # Response expected: no. Save argument for use in a subsequent command.
725 # Arguments accumulate until an argument-using command is given, at which
726 # point they are forgotten.
728 # Response expected: no. Append \n followed by text to the current argument
732 my ( $cmd, $data ) = @_;
734 # Argumentx means: append to last Argument (with a newline in front)
736 $log->debug("$cmd : $data");
738 if ( $cmd eq 'Argumentx') {
739 ${$state->{arguments
}}[$#{$state->{arguments}}] .= "\n" . $data;
741 push @
{$state->{arguments
}}, $data;
746 # Response expected: yes. Expand the modules which are specified in the
747 # arguments. Returns the data in Module-expansion responses. Note that the
748 # server can assume that this is checkout or export, not rtag or rdiff; the
749 # latter do not access the working directory and thus have no need to
750 # expand modules on the client side. Expand may not be the best word for
751 # what this request does. It does not necessarily tell you all the files
752 # contained in a module, for example. Basically it is a way of telling you
753 # which working directories the server needs to know about in order to
754 # handle a checkout of the specified modules. For example, suppose that the
755 # server has a module defined by
756 # aliasmodule -a 1dir
757 # That is, one can check out aliasmodule and it will take 1dir in the
758 # repository and check it out to 1dir in the working directory. Now suppose
759 # the client already has this module checked out and is planning on using
760 # the co request to update it. Without using expand-modules, the client
761 # would have two bad choices: it could either send information about all
762 # working directories under the current directory, which could be
763 # unnecessarily slow, or it could be ignorant of the fact that aliasmodule
764 # stands for 1dir, and neglect to send information for 1dir, which would
765 # lead to incorrect operation. With expand-modules, the client would first
766 # ask for the module to be expanded:
767 sub req_expandmodules
769 my ( $cmd, $data ) = @_;
773 $log->debug("req_expandmodules : " . ( defined($data) ?
$data : "[NULL]" ) );
775 unless ( ref $state->{arguments
} eq "ARRAY" )
781 foreach my $module ( @
{$state->{arguments
}} )
783 $log->debug("SEND : Module-expansion $module");
784 print "Module-expansion $module\n";
792 # Response expected: yes. Get files from the repository. This uses any
793 # previous Argument, Directory, Entry, or Modified requests, if they have
794 # been sent. Arguments to this command are module names; the client cannot
795 # know what directories they correspond to except by (1) just sending the
796 # co request, and then seeing what directory names the server sends back in
797 # its responses, and (2) the expand-modules request.
800 my ( $cmd, $data ) = @_;
804 my $module = $state->{args
}[0];
805 $state->{module
} = $module;
806 my $checkout_path = $module;
808 # use the user specified directory if we're given it
809 $checkout_path = $state->{opt
}{d
} if ( exists ( $state->{opt
}{d
} ) );
811 $log->debug("req_co : " . ( defined($data) ?
$data : "[NULL]" ) );
813 $log->info("Checking out module '$module' ($state->{CVSROOT}) to '$checkout_path'");
815 $ENV{GIT_DIR
} = $state->{CVSROOT
} . "/";
817 # Grab a handle to the SQLite db and do any necessary updates
818 my $updater = GITCVS
::updater
->new($state->{CVSROOT
}, $module, $log);
821 $checkout_path =~ s
|/$||; # get rid of trailing slashes
823 # Eclipse seems to need the Clear-sticky command
824 # to prepare the 'Entries' file for the new directory.
825 print "Clear-sticky $checkout_path/\n";
826 print $state->{CVSROOT
} . "/$module/\n";
827 print "Clear-static-directory $checkout_path/\n";
828 print $state->{CVSROOT
} . "/$module/\n";
829 print "Clear-sticky $checkout_path/\n"; # yes, twice
830 print $state->{CVSROOT
} . "/$module/\n";
831 print "Template $checkout_path/\n";
832 print $state->{CVSROOT
} . "/$module/\n";
835 # instruct the client that we're checking out to $checkout_path
836 print "E cvs checkout: Updating $checkout_path\n";
843 my ($dir, $repodir, $remotedir, $seendirs) = @_;
844 my $parent = dirname
($dir);
847 $remotedir =~ s
|/+$||;
849 $log->debug("announcedir $dir, $repodir, $remotedir" );
851 if ($parent eq '.' || $parent eq './') {
854 # recurse to announce unseen parents first
855 if (length($parent) && !exists($seendirs->{$parent})) {
856 prepdir
($parent, $repodir, $remotedir, $seendirs);
858 # Announce that we are going to modify at the parent level
860 print "E cvs checkout: Updating $remotedir/$parent\n";
862 print "E cvs checkout: Updating $remotedir\n";
864 print "Clear-sticky $remotedir/$parent/\n";
865 print "$repodir/$parent/\n";
867 print "Clear-static-directory $remotedir/$dir/\n";
868 print "$repodir/$dir/\n";
869 print "Clear-sticky $remotedir/$parent/\n"; # yes, twice
870 print "$repodir/$parent/\n";
871 print "Template $remotedir/$dir/\n";
872 print "$repodir/$dir/\n";
875 $seendirs->{$dir} = 1;
878 foreach my $git ( @
{$updater->gethead} )
880 # Don't want to check out deleted files
881 next if ( $git->{filehash
} eq "deleted" );
883 my $fullName = $git->{name
};
884 ( $git->{name
}, $git->{dir
} ) = filenamesplit
($git->{name
});
886 if (length($git->{dir
}) && $git->{dir
} ne './'
887 && $git->{dir
} ne $lastdir ) {
888 unless (exists($seendirs{$git->{dir
}})) {
889 prepdir
($git->{dir
}, $state->{CVSROOT
} . "/$module/",
890 $checkout_path, \
%seendirs);
891 $lastdir = $git->{dir
};
892 $seendirs{$git->{dir
}} = 1;
894 print "E cvs checkout: Updating /$checkout_path/$git->{dir}\n";
897 # modification time of this file
898 print "Mod-time $git->{modified}\n";
900 # print some information to the client
901 if ( defined ( $git->{dir
} ) and $git->{dir
} ne "./" )
903 print "M U $checkout_path/$git->{dir}$git->{name}\n";
905 print "M U $checkout_path/$git->{name}\n";
908 # instruct client we're sending a file to put in this path
909 print "Created $checkout_path/" . ( defined ( $git->{dir
} ) and $git->{dir
} ne "./" ?
$git->{dir
} . "/" : "" ) . "\n";
911 print $state->{CVSROOT
} . "/$module/" . ( defined ( $git->{dir
} ) and $git->{dir
} ne "./" ?
$git->{dir
} . "/" : "" ) . "$git->{name}\n";
913 # this is an "entries" line
914 my $kopts = kopts_from_path
($fullName,"sha1",$git->{filehash
});
915 print "/$git->{name}/1.$git->{revision}//$kopts/\n";
917 print "u=$git->{mode},g=$git->{mode},o=$git->{mode}\n";
920 transmitfile
($git->{filehash
});
929 # Response expected: yes. Actually do a cvs update command. This uses any
930 # previous Argument, Directory, Entry, or Modified requests, if they have
931 # been sent. The last Directory sent specifies the working directory at the
932 # time of the operation. The -I option is not used--files which the client
933 # can decide whether to ignore are not mentioned and the client sends the
934 # Questionable request for others.
937 my ( $cmd, $data ) = @_;
939 $log->debug("req_update : " . ( defined($data) ?
$data : "[NULL]" ));
944 # It may just be a client exploring the available heads/modules
945 # in that case, list them as top level directories and leave it
946 # at that. Eclipse uses this technique to offer you a list of
947 # projects (heads in this case) to checkout.
949 if ($state->{module
} eq '') {
950 my $showref = `git show-ref --heads`;
951 print "E cvs update: Updating .\n";
952 for my $line (split '\n', $showref) {
953 if ( $line =~ m
% refs
/heads/(.*)$% ) {
954 print "E cvs update: New directory `$1'\n";
962 # Grab a handle to the SQLite db and do any necessary updates
963 my $updater = GITCVS
::updater
->new($state->{CVSROOT
}, $state->{module
}, $log);
967 argsfromdir
($updater);
969 #$log->debug("update state : " . Dumper($state));
971 # foreach file specified on the command line ...
972 foreach my $filename ( @
{$state->{args
}} )
974 $filename = filecleanup
($filename);
976 $log->debug("Processing file $filename");
978 # if we have a -C we should pretend we never saw modified stuff
979 if ( exists ( $state->{opt
}{C
} ) )
981 delete $state->{entries
}{$filename}{modified_hash
};
982 delete $state->{entries
}{$filename}{modified_filename
};
983 $state->{entries
}{$filename}{unchanged
} = 1;
987 if ( defined($state->{opt
}{r
}) and $state->{opt
}{r
} =~ /^1\.(\d+)/ )
989 $meta = $updater->getmeta($filename, $1);
991 $meta = $updater->getmeta($filename);
994 # If -p was given, "print" the contents of the requested revision.
995 if ( exists ( $state->{opt
}{p
} ) ) {
996 if ( defined ( $meta->{revision
} ) ) {
997 $log->info("Printing '$filename' revision " . $meta->{revision
});
999 transmitfile
($meta->{filehash
}, { print => 1 });
1005 if ( ! defined $meta )
1014 my $oldmeta = $meta;
1016 my $wrev = revparse
($filename);
1018 # If the working copy is an old revision, lets get that version too for comparison.
1019 if ( defined($wrev) and $wrev != $meta->{revision
} )
1021 $oldmeta = $updater->getmeta($filename, $wrev);
1024 #$log->debug("Target revision is $meta->{revision}, current working revision is $wrev");
1026 # Files are up to date if the working copy and repo copy have the same revision,
1027 # and the working copy is unmodified _and_ the user hasn't specified -C
1028 next if ( defined ( $wrev )
1029 and defined($meta->{revision
})
1030 and $wrev == $meta->{revision
}
1031 and $state->{entries
}{$filename}{unchanged
}
1032 and not exists ( $state->{opt
}{C
} ) );
1034 # If the working copy and repo copy have the same revision,
1035 # but the working copy is modified, tell the client it's modified
1036 if ( defined ( $wrev )
1037 and defined($meta->{revision
})
1038 and $wrev == $meta->{revision
}
1039 and defined($state->{entries
}{$filename}{modified_hash
})
1040 and not exists ( $state->{opt
}{C
} ) )
1042 $log->info("Tell the client the file is modified");
1043 print "MT text M \n";
1044 print "MT fname $filename\n";
1045 print "MT newline\n";
1049 if ( $meta->{filehash
} eq "deleted" )
1051 my ( $filepart, $dirpart ) = filenamesplit
($filename,1);
1053 $log->info("Removing '$filename' from working copy (no longer in the repo)");
1055 print "E cvs update: `$filename' is no longer in the repository\n";
1056 # Don't want to actually _DO_ the update if -n specified
1057 unless ( $state->{globaloptions
}{-n
} ) {
1058 print "Removed $dirpart\n";
1059 print "$filepart\n";
1062 elsif ( not defined ( $state->{entries
}{$filename}{modified_hash
} )
1063 or $state->{entries
}{$filename}{modified_hash
} eq $oldmeta->{filehash
}
1064 or $meta->{filehash
} eq 'added' )
1066 # normal update, just send the new revision (either U=Update,
1067 # or A=Add, or R=Remove)
1068 if ( defined($wrev) && $wrev < 0 )
1070 $log->info("Tell the client the file is scheduled for removal");
1071 print "MT text R \n";
1072 print "MT fname $filename\n";
1073 print "MT newline\n";
1076 elsif ( (!defined($wrev) || $wrev == 0) && (!defined($meta->{revision
}) || $meta->{revision
} == 0) )
1078 $log->info("Tell the client the file is scheduled for addition");
1079 print "MT text A \n";
1080 print "MT fname $filename\n";
1081 print "MT newline\n";
1086 $log->info("Updating '$filename' to ".$meta->{revision
});
1087 print "MT +updated\n";
1088 print "MT text U \n";
1089 print "MT fname $filename\n";
1090 print "MT newline\n";
1091 print "MT -updated\n";
1094 my ( $filepart, $dirpart ) = filenamesplit
($filename,1);
1096 # Don't want to actually _DO_ the update if -n specified
1097 unless ( $state->{globaloptions
}{-n
} )
1099 if ( defined ( $wrev ) )
1101 # instruct client we're sending a file to put in this path as a replacement
1102 print "Update-existing $dirpart\n";
1103 $log->debug("Updating existing file 'Update-existing $dirpart'");
1105 # instruct client we're sending a file to put in this path as a new file
1106 print "Clear-static-directory $dirpart\n";
1107 print $state->{CVSROOT
} . "/$state->{module}/$dirpart\n";
1108 print "Clear-sticky $dirpart\n";
1109 print $state->{CVSROOT
} . "/$state->{module}/$dirpart\n";
1111 $log->debug("Creating new file 'Created $dirpart'");
1112 print "Created $dirpart\n";
1114 print $state->{CVSROOT
} . "/$state->{module}/$filename\n";
1116 # this is an "entries" line
1117 my $kopts = kopts_from_path
($filename,"sha1",$meta->{filehash
});
1118 $log->debug("/$filepart/1.$meta->{revision}//$kopts/");
1119 print "/$filepart/1.$meta->{revision}//$kopts/\n";
1122 $log->debug("SEND : u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}");
1123 print "u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}\n";
1126 transmitfile
($meta->{filehash
});
1129 $log->info("Updating '$filename'");
1130 my ( $filepart, $dirpart ) = filenamesplit
($meta->{name
},1);
1132 my $mergeDir = setupTmpDir
();
1134 my $file_local = $filepart . ".mine";
1135 my $mergedFile = "$mergeDir/$file_local";
1136 system("ln","-s",$state->{entries
}{$filename}{modified_filename
}, $file_local);
1137 my $file_old = $filepart . "." . $oldmeta->{revision
};
1138 transmitfile
($oldmeta->{filehash
}, { targetfile
=> $file_old });
1139 my $file_new = $filepart . "." . $meta->{revision
};
1140 transmitfile
($meta->{filehash
}, { targetfile
=> $file_new });
1142 # we need to merge with the local changes ( M=successful merge, C=conflict merge )
1143 $log->info("Merging $file_local, $file_old, $file_new");
1144 print "M Merging differences between 1.$oldmeta->{revision} and 1.$meta->{revision} into $filename\n";
1146 $log->debug("Temporary directory for merge is $mergeDir");
1148 my $return = system("git", "merge-file", $file_local, $file_old, $file_new);
1155 $log->info("Merged successfully");
1156 print "M M $filename\n";
1157 $log->debug("Merged $dirpart");
1159 # Don't want to actually _DO_ the update if -n specified
1160 unless ( $state->{globaloptions
}{-n
} )
1162 print "Merged $dirpart\n";
1163 $log->debug($state->{CVSROOT
} . "/$state->{module}/$filename");
1164 print $state->{CVSROOT
} . "/$state->{module}/$filename\n";
1165 my $kopts = kopts_from_path
("$dirpart/$filepart",
1166 "file",$mergedFile);
1167 $log->debug("/$filepart/1.$meta->{revision}//$kopts/");
1168 print "/$filepart/1.$meta->{revision}//$kopts/\n";
1171 elsif ( $return == 1 )
1173 $log->info("Merged with conflicts");
1174 print "E cvs update: conflicts found in $filename\n";
1175 print "M C $filename\n";
1177 # Don't want to actually _DO_ the update if -n specified
1178 unless ( $state->{globaloptions
}{-n
} )
1180 print "Merged $dirpart\n";
1181 print $state->{CVSROOT
} . "/$state->{module}/$filename\n";
1182 my $kopts = kopts_from_path
("$dirpart/$filepart",
1183 "file",$mergedFile);
1184 print "/$filepart/1.$meta->{revision}/+/$kopts/\n";
1189 $log->warn("Merge failed");
1193 # Don't want to actually _DO_ the update if -n specified
1194 unless ( $state->{globaloptions
}{-n
} )
1197 $log->debug("SEND : u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}");
1198 print "u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}\n";
1200 # transmit file, format is single integer on a line by itself (file
1201 # size) followed by the file contents
1202 # TODO : we should copy files in blocks
1203 my $data = `cat $mergedFile`;
1204 $log->debug("File size : " . length($data));
1205 print length($data) . "\n";
1217 my ( $cmd, $data ) = @_;
1221 #$log->debug("State : " . Dumper($state));
1223 $log->info("req_ci : " . ( defined($data) ?
$data : "[NULL]" ));
1225 if ( $state->{method
} eq 'pserver')
1227 print "error 1 pserver access cannot commit\n";
1232 if ( -e
$state->{CVSROOT
} . "/index" )
1234 $log->warn("file 'index' already exists in the git repository");
1235 print "error 1 Index already exists in git repo\n";
1240 # Grab a handle to the SQLite db and do any necessary updates
1241 my $updater = GITCVS
::updater
->new($state->{CVSROOT
}, $state->{module
}, $log);
1244 # Remember where the head was at the beginning.
1245 my $parenthash = `git show-ref -s refs/heads/$state->{module}`;
1247 if ($parenthash !~ /^[0-9a-f]{40}$/) {
1248 print "error 1 pserver cannot find the current HEAD of module";
1253 setupWorkTree
($parenthash);
1255 $log->info("Lockless commit start, basing commit on '$work->{workDir}', index file is '$work->{index}'");
1257 $log->info("Created index '$work->{index}' for head $state->{module} - exit status $?");
1259 my @committedfiles = ();
1262 # foreach file specified on the command line ...
1263 foreach my $filename ( @
{$state->{args
}} )
1265 my $committedfile = $filename;
1266 $filename = filecleanup
($filename);
1268 next unless ( exists $state->{entries
}{$filename}{modified_filename
} or not $state->{entries
}{$filename}{unchanged
} );
1270 my $meta = $updater->getmeta($filename);
1271 $oldmeta{$filename} = $meta;
1273 my $wrev = revparse
($filename);
1275 my ( $filepart, $dirpart ) = filenamesplit
($filename);
1277 # do a checkout of the file if it is part of this tree
1279 system('git-checkout-index', '-f', '-u', $filename);
1281 die "Error running git-checkout-index -f -u $filename : $!";
1287 $rmflag = 1 if ( defined($wrev) and $wrev < 0 );
1288 $addflag = 1 unless ( -e
$filename );
1290 # Do up to date checking
1291 unless ( $addflag or $wrev == $meta->{revision
} or ( $rmflag and -$wrev == $meta->{revision
} ) )
1293 # fail everything if an up to date check fails
1294 print "error 1 Up to date check failed for $filename\n";
1299 push @committedfiles, $committedfile;
1300 $log->info("Committing $filename");
1302 system("mkdir","-p",$dirpart) unless ( -d
$dirpart );
1306 $log->debug("rename $state->{entries}{$filename}{modified_filename} $filename");
1307 rename $state->{entries
}{$filename}{modified_filename
},$filename;
1309 # Calculate modes to remove
1311 foreach ( qw
(r w x
) ) { $invmode .= $_ unless ( $state->{entries
}{$filename}{modified_mode
} =~ /$_/ ); }
1313 $log->debug("chmod u+" . $state->{entries
}{$filename}{modified_mode
} . "-" . $invmode . " $filename");
1314 system("chmod","u+" . $state->{entries
}{$filename}{modified_mode
} . "-" . $invmode, $filename);
1319 $log->info("Removing file '$filename'");
1321 system("git-update-index", "--remove", $filename);
1325 $log->info("Adding file '$filename'");
1326 system("git-update-index", "--add", $filename);
1328 $log->info("Updating file '$filename'");
1329 system("git-update-index", $filename);
1333 unless ( scalar(@committedfiles) > 0 )
1335 print "E No files to commit\n";
1341 my $treehash = `git-write-tree`;
1344 $log->debug("Treehash : $treehash, Parenthash : $parenthash");
1346 # write our commit message out if we have one ...
1347 my ( $msg_fh, $msg_filename ) = tempfile
( DIR
=> $TEMP_DIR );
1348 print $msg_fh $state->{opt
}{m
};# if ( exists ( $state->{opt}{m} ) );
1349 print $msg_fh "\n\nvia git-CVS emulator\n";
1352 my $commithash = `git-commit-tree $treehash -p $parenthash < $msg_filename`;
1354 $log->info("Commit hash : $commithash");
1356 unless ( $commithash =~ /[a-zA-Z0-9]{40}/ )
1358 $log->warn("Commit failed (Invalid commit hash)");
1359 print "error 1 Commit failed (unknown reason)\n";
1364 ### Emulate git-receive-pack by running hooks/update
1365 my @hook = ( $ENV{GIT_DIR
}.'hooks/update', "refs/heads/$state->{module}",
1366 $parenthash, $commithash );
1368 unless( system( @hook ) == 0 )
1370 $log->warn("Commit failed (update hook declined to update ref)");
1371 print "error 1 Commit failed (update hook declined)\n";
1378 if (system(qw(git update-ref -m), "cvsserver ci",
1379 "refs/heads/$state->{module}", $commithash, $parenthash)) {
1380 $log->warn("update-ref for $state->{module} failed.");
1381 print "error 1 Cannot commit -- update first\n";
1386 ### Emulate git-receive-pack by running hooks/post-receive
1387 my $hook = $ENV{GIT_DIR
}.'hooks/post-receive';
1389 open(my $pipe, "| $hook") || die "can't fork $!";
1391 local $SIG{PIPE
} = sub { die 'pipe broke' };
1393 print $pipe "$parenthash $commithash refs/heads/$state->{module}\n";
1395 close $pipe || die "bad pipe: $! $?";
1398 ### Then hooks/post-update
1399 $hook = $ENV{GIT_DIR
}.'hooks/post-update';
1401 system($hook, "refs/heads/$state->{module}");
1406 # foreach file specified on the command line ...
1407 foreach my $filename ( @committedfiles )
1409 $filename = filecleanup
($filename);
1411 my $meta = $updater->getmeta($filename);
1412 unless (defined $meta->{revision
}) {
1413 $meta->{revision
} = 1;
1416 my ( $filepart, $dirpart ) = filenamesplit
($filename, 1);
1418 $log->debug("Checked-in $dirpart : $filename");
1420 print "M $state->{CVSROOT}/$state->{module}/$filename,v <-- $dirpart$filepart\n";
1421 if ( defined $meta->{filehash
} && $meta->{filehash
} eq "deleted" )
1423 print "M new revision: delete; previous revision: 1.$oldmeta{$filename}{revision}\n";
1424 print "Remove-entry $dirpart\n";
1425 print "$filename\n";
1427 if ($meta->{revision
} == 1) {
1428 print "M initial revision: 1.1\n";
1430 print "M new revision: 1.$meta->{revision}; previous revision: 1.$oldmeta{$filename}{revision}\n";
1432 print "Checked-in $dirpart\n";
1433 print "$filename\n";
1434 my $kopts = kopts_from_path
($filename,"sha1",$meta->{filehash
});
1435 print "/$filepart/1.$meta->{revision}//$kopts/\n";
1445 my ( $cmd, $data ) = @_;
1449 $log->info("req_status : " . ( defined($data) ?
$data : "[NULL]" ));
1450 #$log->debug("status state : " . Dumper($state));
1452 # Grab a handle to the SQLite db and do any necessary updates
1453 my $updater = GITCVS
::updater
->new($state->{CVSROOT
}, $state->{module
}, $log);
1456 # if no files were specified, we need to work out what files we should be providing status on ...
1457 argsfromdir
($updater);
1459 # foreach file specified on the command line ...
1460 foreach my $filename ( @
{$state->{args
}} )
1462 $filename = filecleanup
($filename);
1464 next if exists($state->{opt
}{l
}) && index($filename, '/', length($state->{prependdir
})) >= 0;
1466 my $meta = $updater->getmeta($filename);
1467 my $oldmeta = $meta;
1469 my $wrev = revparse
($filename);
1471 # If the working copy is an old revision, lets get that version too for comparison.
1472 if ( defined($wrev) and $wrev != $meta->{revision
} )
1474 $oldmeta = $updater->getmeta($filename, $wrev);
1477 # TODO : All possible statuses aren't yet implemented
1479 # Files are up to date if the working copy and repo copy have the same revision, and the working copy is unmodified
1480 $status = "Up-to-date" if ( defined ( $wrev ) and defined($meta->{revision
}) and $wrev == $meta->{revision
}
1482 ( ( $state->{entries
}{$filename}{unchanged
} and ( not defined ( $state->{entries
}{$filename}{conflict
} ) or $state->{entries
}{$filename}{conflict
} !~ /^\+=/ ) )
1483 or ( defined($state->{entries
}{$filename}{modified_hash
}) and $state->{entries
}{$filename}{modified_hash
} eq $meta->{filehash
} ) )
1486 # Need checkout if the working copy has an older revision than the repo copy, and the working copy is unmodified
1487 $status ||= "Needs Checkout" if ( defined ( $wrev ) and defined ( $meta->{revision
} ) and $meta->{revision
} > $wrev
1489 ( $state->{entries
}{$filename}{unchanged
}
1490 or ( defined($state->{entries
}{$filename}{modified_hash
}) and $state->{entries
}{$filename}{modified_hash
} eq $oldmeta->{filehash
} ) )
1493 # Need checkout if it exists in the repo but doesn't have a working copy
1494 $status ||= "Needs Checkout" if ( not defined ( $wrev ) and defined ( $meta->{revision
} ) );
1496 # Locally modified if working copy and repo copy have the same revision but there are local changes
1497 $status ||= "Locally Modified" if ( defined ( $wrev ) and defined($meta->{revision
}) and $wrev == $meta->{revision
} and $state->{entries
}{$filename}{modified_filename
} );
1499 # Needs Merge if working copy revision is less than repo copy and there are local changes
1500 $status ||= "Needs Merge" if ( defined ( $wrev ) and defined ( $meta->{revision
} ) and $meta->{revision
} > $wrev and $state->{entries
}{$filename}{modified_filename
} );
1502 $status ||= "Locally Added" if ( defined ( $state->{entries
}{$filename}{revision
} ) and not defined ( $meta->{revision
} ) );
1503 $status ||= "Locally Removed" if ( defined ( $wrev ) and defined ( $meta->{revision
} ) and -$wrev == $meta->{revision
} );
1504 $status ||= "Unresolved Conflict" if ( defined ( $state->{entries
}{$filename}{conflict
} ) and $state->{entries
}{$filename}{conflict
} =~ /^\+=/ );
1505 $status ||= "File had conflicts on merge" if ( 0 );
1507 $status ||= "Unknown";
1509 my ($filepart) = filenamesplit
($filename);
1511 print "M ===================================================================\n";
1512 print "M File: $filepart\tStatus: $status\n";
1513 if ( defined($state->{entries
}{$filename}{revision
}) )
1515 print "M Working revision:\t" . $state->{entries
}{$filename}{revision
} . "\n";
1517 print "M Working revision:\tNo entry for $filename\n";
1519 if ( defined($meta->{revision
}) )
1521 print "M Repository revision:\t1." . $meta->{revision
} . "\t$state->{CVSROOT}/$state->{module}/$filename,v\n";
1522 print "M Sticky Tag:\t\t(none)\n";
1523 print "M Sticky Date:\t\t(none)\n";
1524 print "M Sticky Options:\t\t(none)\n";
1526 print "M Repository revision:\tNo revision control file\n";
1536 my ( $cmd, $data ) = @_;
1540 $log->debug("req_diff : " . ( defined($data) ?
$data : "[NULL]" ));
1541 #$log->debug("status state : " . Dumper($state));
1543 my ($revision1, $revision2);
1544 if ( defined ( $state->{opt
}{r
} ) and ref $state->{opt
}{r
} eq "ARRAY" )
1546 $revision1 = $state->{opt
}{r
}[0];
1547 $revision2 = $state->{opt
}{r
}[1];
1549 $revision1 = $state->{opt
}{r
};
1552 $revision1 =~ s/^1\.// if ( defined ( $revision1 ) );
1553 $revision2 =~ s/^1\.// if ( defined ( $revision2 ) );
1555 $log->debug("Diffing revisions " . ( defined($revision1) ?
$revision1 : "[NULL]" ) . " and " . ( defined($revision2) ?
$revision2 : "[NULL]" ) );
1557 # Grab a handle to the SQLite db and do any necessary updates
1558 my $updater = GITCVS
::updater
->new($state->{CVSROOT
}, $state->{module
}, $log);
1561 # if no files were specified, we need to work out what files we should be providing status on ...
1562 argsfromdir
($updater);
1564 # foreach file specified on the command line ...
1565 foreach my $filename ( @
{$state->{args
}} )
1567 $filename = filecleanup
($filename);
1569 my ( $fh, $file1, $file2, $meta1, $meta2, $filediff );
1571 my $wrev = revparse
($filename);
1573 # We need _something_ to diff against
1574 next unless ( defined ( $wrev ) );
1576 # if we have a -r switch, use it
1577 if ( defined ( $revision1 ) )
1579 ( undef, $file1 ) = tempfile
( DIR
=> $TEMP_DIR, OPEN
=> 0 );
1580 $meta1 = $updater->getmeta($filename, $revision1);
1581 unless ( defined ( $meta1 ) and $meta1->{filehash
} ne "deleted" )
1583 print "E File $filename at revision 1.$revision1 doesn't exist\n";
1586 transmitfile
($meta1->{filehash
}, { targetfile
=> $file1 });
1588 # otherwise we just use the working copy revision
1591 ( undef, $file1 ) = tempfile
( DIR
=> $TEMP_DIR, OPEN
=> 0 );
1592 $meta1 = $updater->getmeta($filename, $wrev);
1593 transmitfile
($meta1->{filehash
}, { targetfile
=> $file1 });
1596 # if we have a second -r switch, use it too
1597 if ( defined ( $revision2 ) )
1599 ( undef, $file2 ) = tempfile
( DIR
=> $TEMP_DIR, OPEN
=> 0 );
1600 $meta2 = $updater->getmeta($filename, $revision2);
1602 unless ( defined ( $meta2 ) and $meta2->{filehash
} ne "deleted" )
1604 print "E File $filename at revision 1.$revision2 doesn't exist\n";
1608 transmitfile
($meta2->{filehash
}, { targetfile
=> $file2 });
1610 # otherwise we just use the working copy
1613 $file2 = $state->{entries
}{$filename}{modified_filename
};
1616 # if we have been given -r, and we don't have a $file2 yet, lets get one
1617 if ( defined ( $revision1 ) and not defined ( $file2 ) )
1619 ( undef, $file2 ) = tempfile
( DIR
=> $TEMP_DIR, OPEN
=> 0 );
1620 $meta2 = $updater->getmeta($filename, $wrev);
1621 transmitfile
($meta2->{filehash
}, { targetfile
=> $file2 });
1624 # We need to have retrieved something useful
1625 next unless ( defined ( $meta1 ) );
1627 # Files to date if the working copy and repo copy have the same revision, and the working copy is unmodified
1628 next if ( not defined ( $meta2 ) and $wrev == $meta1->{revision
}
1630 ( ( $state->{entries
}{$filename}{unchanged
} and ( not defined ( $state->{entries
}{$filename}{conflict
} ) or $state->{entries
}{$filename}{conflict
} !~ /^\+=/ ) )
1631 or ( defined($state->{entries
}{$filename}{modified_hash
}) and $state->{entries
}{$filename}{modified_hash
} eq $meta1->{filehash
} ) )
1634 # Apparently we only show diffs for locally modified files
1635 next unless ( defined($meta2) or defined ( $state->{entries
}{$filename}{modified_filename
} ) );
1637 print "M Index: $filename\n";
1638 print "M ===================================================================\n";
1639 print "M RCS file: $state->{CVSROOT}/$state->{module}/$filename,v\n";
1640 print "M retrieving revision 1.$meta1->{revision}\n" if ( defined ( $meta1 ) );
1641 print "M retrieving revision 1.$meta2->{revision}\n" if ( defined ( $meta2 ) );
1643 foreach my $opt ( keys %{$state->{opt
}} )
1645 if ( ref $state->{opt
}{$opt} eq "ARRAY" )
1647 foreach my $value ( @
{$state->{opt
}{$opt}} )
1649 print "-$opt $value ";
1653 print "$state->{opt}{$opt} " if ( defined ( $state->{opt
}{$opt} ) );
1656 print "$filename\n";
1658 $log->info("Diffing $filename -r $meta1->{revision} -r " . ( $meta2->{revision
} or "workingcopy" ));
1660 ( $fh, $filediff ) = tempfile
( DIR
=> $TEMP_DIR );
1662 if ( exists $state->{opt
}{u
} )
1664 system("diff -u -L '$filename revision 1.$meta1->{revision}' -L '$filename " . ( defined($meta2->{revision
}) ?
"revision 1.$meta2->{revision}" : "working copy" ) . "' $file1 $file2 > $filediff");
1666 system("diff $file1 $file2 > $filediff");
1681 my ( $cmd, $data ) = @_;
1685 $log->debug("req_log : " . ( defined($data) ?
$data : "[NULL]" ));
1686 #$log->debug("log state : " . Dumper($state));
1688 my ( $minrev, $maxrev );
1689 if ( defined ( $state->{opt
}{r
} ) and $state->{opt
}{r
} =~ /([\d.]+)?(::?)([\d.]+)?/ )
1694 $minrev =~ s/^1\.// if ( defined ( $minrev ) );
1695 $maxrev =~ s/^1\.// if ( defined ( $maxrev ) );
1696 $minrev++ if ( defined($minrev) and $control eq "::" );
1699 # Grab a handle to the SQLite db and do any necessary updates
1700 my $updater = GITCVS
::updater
->new($state->{CVSROOT
}, $state->{module
}, $log);
1703 # if no files were specified, we need to work out what files we should be providing status on ...
1704 argsfromdir
($updater);
1706 # foreach file specified on the command line ...
1707 foreach my $filename ( @
{$state->{args
}} )
1709 $filename = filecleanup
($filename);
1711 my $headmeta = $updater->getmeta($filename);
1713 my $revisions = $updater->getlog($filename);
1714 my $totalrevisions = scalar(@
$revisions);
1716 if ( defined ( $minrev ) )
1718 $log->debug("Removing revisions less than $minrev");
1719 while ( scalar(@
$revisions) > 0 and $revisions->[-1]{revision
} < $minrev )
1724 if ( defined ( $maxrev ) )
1726 $log->debug("Removing revisions greater than $maxrev");
1727 while ( scalar(@
$revisions) > 0 and $revisions->[0]{revision
} > $maxrev )
1733 next unless ( scalar(@
$revisions) );
1736 print "M RCS file: $state->{CVSROOT}/$state->{module}/$filename,v\n";
1737 print "M Working file: $filename\n";
1738 print "M head: 1.$headmeta->{revision}\n";
1739 print "M branch:\n";
1740 print "M locks: strict\n";
1741 print "M access list:\n";
1742 print "M symbolic names:\n";
1743 print "M keyword substitution: kv\n";
1744 print "M total revisions: $totalrevisions;\tselected revisions: " . scalar(@
$revisions) . "\n";
1745 print "M description:\n";
1747 foreach my $revision ( @
$revisions )
1749 print "M ----------------------------\n";
1750 print "M revision 1.$revision->{revision}\n";
1751 # reformat the date for log output
1752 $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}) );
1753 $revision->{author
} = cvs_author
($revision->{author
});
1754 print "M date: $revision->{modified}; author: $revision->{author}; state: " . ( $revision->{filehash
} eq "deleted" ?
"dead" : "Exp" ) . "; lines: +2 -3\n";
1755 my $commitmessage = $updater->commitmessage($revision->{commithash
});
1756 $commitmessage =~ s/^/M /mg;
1757 print $commitmessage . "\n";
1759 print "M =============================================================================\n";
1767 my ( $cmd, $data ) = @_;
1769 argsplit
("annotate");
1771 $log->info("req_annotate : " . ( defined($data) ?
$data : "[NULL]" ));
1772 #$log->debug("status state : " . Dumper($state));
1774 # Grab a handle to the SQLite db and do any necessary updates
1775 my $updater = GITCVS
::updater
->new($state->{CVSROOT
}, $state->{module
}, $log);
1778 # if no files were specified, we need to work out what files we should be providing annotate on ...
1779 argsfromdir
($updater);
1781 # we'll need a temporary checkout dir
1784 $log->info("Temp checkoutdir creation successful, basing annotate session work on '$work->{workDir}', index file is '$ENV{GIT_INDEX_FILE}'");
1786 # foreach file specified on the command line ...
1787 foreach my $filename ( @
{$state->{args
}} )
1789 $filename = filecleanup
($filename);
1791 my $meta = $updater->getmeta($filename);
1793 next unless ( $meta->{revision
} );
1795 # get all the commits that this file was in
1796 # in dense format -- aka skip dead revisions
1797 my $revisions = $updater->gethistorydense($filename);
1798 my $lastseenin = $revisions->[0][2];
1800 # populate the temporary index based on the latest commit were we saw
1801 # the file -- but do it cheaply without checking out any files
1802 # TODO: if we got a revision from the client, use that instead
1803 # to look up the commithash in sqlite (still good to default to
1804 # the current head as we do now)
1805 system("git-read-tree", $lastseenin);
1808 print "E error running git-read-tree $lastseenin $ENV{GIT_INDEX_FILE} $!\n";
1811 $log->info("Created index '$ENV{GIT_INDEX_FILE}' with commit $lastseenin - exit status $?");
1813 # do a checkout of the file
1814 system('git-checkout-index', '-f', '-u', $filename);
1816 print "E error running git-checkout-index -f -u $filename : $!\n";
1820 $log->info("Annotate $filename");
1822 # Prepare a file with the commits from the linearized
1823 # history that annotate should know about. This prevents
1824 # git-jsannotate telling us about commits we are hiding
1827 my $a_hints = "$work->{workDir}/.annotate_hints";
1828 if (!open(ANNOTATEHINTS
, '>', $a_hints)) {
1829 print "E failed to open '$a_hints' for writing: $!\n";
1832 for (my $i=0; $i < @
$revisions; $i++)
1834 print ANNOTATEHINTS
$revisions->[$i][2];
1835 if ($i+1 < @
$revisions) { # have we got a parent?
1836 print ANNOTATEHINTS
' ' . $revisions->[$i+1][2];
1838 print ANNOTATEHINTS
"\n";
1841 print ANNOTATEHINTS
"\n";
1843 or (print "E failed to write $a_hints: $!\n"), return;
1845 my @cmd = (qw(git-annotate -l -S), $a_hints, $filename);
1846 if (!open(ANNOTATE
, "-|", @cmd)) {
1847 print "E error invoking ". join(' ',@cmd) .": $!\n";
1851 print "E Annotations for $filename\n";
1852 print "E ***************\n";
1853 while ( <ANNOTATE
> )
1855 if (m/^([a-zA-Z0-9]{40})\t\([^\)]*\)(.*)$/i)
1857 my $commithash = $1;
1859 unless ( defined ( $metadata->{$commithash} ) )
1861 $metadata->{$commithash} = $updater->getmeta($filename, $commithash);
1862 $metadata->{$commithash}{author
} = cvs_author
($metadata->{$commithash}{author
});
1863 $metadata->{$commithash}{modified
} = sprintf("%02d-%s-%02d", $1, $2, $3) if ( $metadata->{$commithash}{modified
} =~ /^(\d+)\s(\w+)\s\d\d(\d\d)/ );
1865 printf("M 1.%-5d (%-8s %10s): %s\n",
1866 $metadata->{$commithash}{revision
},
1867 $metadata->{$commithash}{author
},
1868 $metadata->{$commithash}{modified
},
1872 $log->warn("Error in annotate output! LINE: $_");
1873 print "E Annotate error \n";
1880 # done; get out of the tempdir
1887 # This method takes the state->{arguments} array and produces two new arrays.
1888 # The first is $state->{args} which is everything before the '--' argument, and
1889 # the second is $state->{files} which is everything after it.
1892 $state->{args
} = [];
1893 $state->{files
} = [];
1896 return unless( defined($state->{arguments
}) and ref $state->{arguments
} eq "ARRAY" );
1900 if ( defined($type) )
1903 $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 ( $type eq "co" );
1904 $opt = { v
=> 0, l
=> 0, R
=> 0 } if ( $type eq "status" );
1905 $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 ( $type eq "update" );
1906 $opt = { l
=> 0, R
=> 0, k
=> 1, D
=> 1, D
=> 1, r
=> 2 } if ( $type eq "diff" );
1907 $opt = { c
=> 0, R
=> 0, l
=> 0, f
=> 0, F
=> 1, m
=> 1, r
=> 1 } if ( $type eq "ci" );
1908 $opt = { k
=> 1, m
=> 1 } if ( $type eq "add" );
1909 $opt = { f
=> 0, l
=> 0, R
=> 0 } if ( $type eq "remove" );
1910 $opt = { l
=> 0, b
=> 0, h
=> 0, R
=> 0, t
=> 0, N
=> 0, S
=> 0, r
=> 1, d
=> 1, s
=> 1, w
=> 1 } if ( $type eq "log" );
1913 while ( scalar ( @
{$state->{arguments
}} ) > 0 )
1915 my $arg = shift @
{$state->{arguments
}};
1917 next if ( $arg eq "--" );
1918 next unless ( $arg =~ /\S/ );
1920 # if the argument looks like a switch
1921 if ( $arg =~ /^-(\w)(.*)/ )
1923 # if it's a switch that takes an argument
1926 # If this switch has already been provided
1927 if ( $opt->{$1} > 1 and exists ( $state->{opt
}{$1} ) )
1929 $state->{opt
}{$1} = [ $state->{opt
}{$1} ];
1930 if ( length($2) > 0 )
1932 push @
{$state->{opt
}{$1}},$2;
1934 push @
{$state->{opt
}{$1}}, shift @
{$state->{arguments
}};
1937 # if there's extra data in the arg, use that as the argument for the switch
1938 if ( length($2) > 0 )
1940 $state->{opt
}{$1} = $2;
1942 $state->{opt
}{$1} = shift @
{$state->{arguments
}};
1946 $state->{opt
}{$1} = undef;
1951 push @
{$state->{args
}}, $arg;
1959 foreach my $value ( @
{$state->{arguments
}} )
1961 if ( $value eq "--" )
1966 push @
{$state->{args
}}, $value if ( $mode == 0 );
1967 push @
{$state->{files
}}, $value if ( $mode == 1 );
1972 # This method uses $state->{directory} to populate $state->{args} with a list of filenames
1975 my $updater = shift;
1977 $state->{args
} = [] if ( scalar(@
{$state->{args
}}) == 1 and $state->{args
}[0] eq "." );
1979 return if ( scalar ( @
{$state->{args
}} ) > 1 );
1981 my @gethead = @
{$updater->gethead};
1984 foreach my $file (keys %{$state->{entries
}}) {
1985 if ( exists $state->{entries
}{$file}{revision
} &&
1986 $state->{entries
}{$file}{revision
} == 0 )
1988 push @gethead, { name
=> $file, filehash
=> 'added' };
1992 if ( scalar(@
{$state->{args
}}) == 1 )
1994 my $arg = $state->{args
}[0];
1995 $arg .= $state->{prependdir
} if ( defined ( $state->{prependdir
} ) );
1997 $log->info("Only one arg specified, checking for directory expansion on '$arg'");
1999 foreach my $file ( @gethead )
2001 next if ( $file->{filehash
} eq "deleted" and not defined ( $state->{entries
}{$file->{name
}} ) );
2002 next unless ( $file->{name
} =~ /^$arg\// or $file->{name
} eq $arg );
2003 push @
{$state->{args
}}, $file->{name
};
2006 shift @
{$state->{args
}} if ( scalar(@
{$state->{args
}}) > 1 );
2008 $log->info("Only one arg specified, populating file list automatically");
2010 $state->{args
} = [];
2012 foreach my $file ( @gethead )
2014 next if ( $file->{filehash
} eq "deleted" and not defined ( $state->{entries
}{$file->{name
}} ) );
2015 next unless ( $file->{name
} =~ s/^$state->{prependdir}// );
2016 push @
{$state->{args
}}, $file->{name
};
2021 # This method cleans up the $state variable after a command that uses arguments has run
2024 $state->{files
} = [];
2025 $state->{args
} = [];
2026 $state->{arguments
} = [];
2027 $state->{entries
} = {};
2032 my $filename = shift;
2034 return undef unless ( defined ( $state->{entries
}{$filename}{revision
} ) );
2036 return $1 if ( $state->{entries
}{$filename}{revision
} =~ /^1\.(\d+)/ );
2037 return -$1 if ( $state->{entries
}{$filename}{revision
} =~ /^-1\.(\d+)/ );
2042 # This method takes a file hash and does a CVS "file transfer". Its
2043 # exact behaviour depends on a second, optional hash table argument:
2044 # - If $options->{targetfile}, dump the contents to that file;
2045 # - If $options->{print}, use M/MT to transmit the contents one line
2047 # - Otherwise, transmit the size of the file, followed by the file
2051 my $filehash = shift;
2052 my $options = shift;
2054 if ( defined ( $filehash ) and $filehash eq "deleted" )
2056 $log->warn("filehash is 'deleted'");
2060 die "Need filehash" unless ( defined ( $filehash ) and $filehash =~ /^[a-zA-Z0-9]{40}$/ );
2062 my $type = `git-cat-file -t $filehash`;
2065 die ( "Invalid type '$type' (expected 'blob')" ) unless ( defined ( $type ) and $type eq "blob" );
2067 my $size = `git-cat-file -s $filehash`;
2070 $log->debug("transmitfile($filehash) size=$size, type=$type");
2072 if ( open my $fh, '-|', "git-cat-file", "blob", $filehash )
2074 if ( defined ( $options->{targetfile
} ) )
2076 my $targetfile = $options->{targetfile
};
2077 open NEWFILE
, ">", $targetfile or die("Couldn't open '$targetfile' for writing : $!");
2078 print NEWFILE
$_ while ( <$fh> );
2079 close NEWFILE
or die("Failed to write '$targetfile': $!");
2080 } elsif ( defined ( $options->{print} ) && $options->{print} ) {
2085 print 'MT text ', $_, "\n";
2090 print while ( <$fh> );
2092 close $fh or die ("Couldn't close filehandle for transmitfile(): $!");
2094 die("Couldn't execute git-cat-file");
2098 # This method takes a file name, and returns ( $dirpart, $filepart ) which
2099 # refers to the directory portion and the file portion of the filename
2103 my $filename = shift;
2104 my $fixforlocaldir = shift;
2106 my ( $filepart, $dirpart ) = ( $filename, "." );
2107 ( $filepart, $dirpart ) = ( $2, $1 ) if ( $filename =~ /(.*)\/(.*)/ );
2110 if ( $fixforlocaldir )
2112 $dirpart =~ s/^$state->{prependdir}//;
2115 return ( $filepart, $dirpart );
2120 my $filename = shift;
2122 return undef unless(defined($filename));
2123 if ( $filename =~ /^\// )
2125 print "E absolute filenames '$filename' not supported by server\n";
2129 $filename =~ s/^\.\///g
;
2130 $filename = $state->{prependdir
} . $filename;
2136 if( !defined($state->{CVSROOT
}) )
2138 print "error 1 CVSROOT not specified\n";
2142 if( $ENV{GIT_DIR
} ne ($state->{CVSROOT
} . '/') )
2144 print "error 1 Internally inconsistent CVSROOT\n";
2150 # Setup working directory in a work tree with the requested version
2151 # loaded in the index.
2158 if( ( defined($work->{state}) && $work->{state} != 1 ) ||
2159 defined($work->{tmpDir
}) )
2161 $log->warn("Bad work tree state management");
2162 print "error 1 Internal setup multiple work trees without cleanup\n";
2167 $work->{workDir
} = tempdir
( DIR
=> $TEMP_DIR );
2169 if( !defined($work->{index}) )
2171 (undef, $work->{index}) = tempfile
( DIR
=> $TEMP_DIR, OPEN
=> 0 );
2174 chdir $work->{workDir
} or
2175 die "Unable to chdir to $work->{workDir}\n";
2177 $log->info("Setting up GIT_WORK_TREE as '.' in '$work->{workDir}', index file is '$work->{index}'");
2179 $ENV{GIT_WORK_TREE
} = ".";
2180 $ENV{GIT_INDEX_FILE
} = $work->{index};
2185 system("git","read-tree",$ver);
2188 $log->warn("Error running git-read-tree");
2189 die "Error running git-read-tree $ver in $work->{workDir} $!\n";
2192 # else # req_annotate reads tree for each file
2195 # Ensure current directory is in some kind of working directory,
2196 # with a recent version loaded in the index.
2199 if( defined($work->{tmpDir
}) )
2201 $log->warn("Bad work tree state management [ensureWorkTree()]");
2202 print "error 1 Internal setup multiple dirs without cleanup\n";
2206 if( $work->{state} )
2213 if( !defined($work->{emptyDir
}) )
2215 $work->{emptyDir
} = tempdir
( DIR
=> $TEMP_DIR, OPEN
=> 0);
2217 chdir $work->{emptyDir
} or
2218 die "Unable to chdir to $work->{emptyDir}\n";
2220 my $ver = `git show-ref -s refs/heads/$state->{module}`;
2222 if ($ver !~ /^[0-9a-f]{40}$/)
2224 $log->warn("Error from git show-ref -s refs/head$state->{module}");
2225 print "error 1 cannot find the current HEAD of module";
2230 if( !defined($work->{index}) )
2232 (undef, $work->{index}) = tempfile
( DIR
=> $TEMP_DIR, OPEN
=> 0 );
2235 $ENV{GIT_WORK_TREE
} = ".";
2236 $ENV{GIT_INDEX_FILE
} = $work->{index};
2239 system("git","read-tree",$ver);
2242 die "Error running git-read-tree $ver $!\n";
2246 # Cleanup working directory that is not needed any longer.
2249 if( ! $work->{state} )
2254 chdir "/" or die "Unable to chdir '/'\n";
2256 if( defined($work->{workDir
}) )
2258 rmtree
( $work->{workDir
} );
2259 undef $work->{workDir
};
2261 undef $work->{state};
2264 # Setup a temporary directory (not a working tree), typically for
2265 # merging dirty state as in req_update.
2268 $work->{tmpDir
} = tempdir
( DIR
=> $TEMP_DIR );
2269 chdir $work->{tmpDir
} or die "Unable to chdir $work->{tmpDir}\n";
2271 return $work->{tmpDir
};
2274 # Clean up a previously setupTmpDir. Restore previous work tree if
2278 if ( !defined($work->{tmpDir
}) )
2280 $log->warn("cleanup tmpdir that has not been setup");
2281 die "Cleanup tmpDir that has not been setup\n";
2283 if( defined($work->{state}) )
2285 if( $work->{state} == 1 )
2287 chdir $work->{emptyDir
} or
2288 die "Unable to chdir to $work->{emptyDir}\n";
2290 elsif( $work->{state} == 2 )
2292 chdir $work->{workDir
} or
2293 die "Unable to chdir to $work->{emptyDir}\n";
2297 $log->warn("Inconsistent work dir state");
2298 die "Inconsistent work dir state\n";
2303 chdir "/" or die "Unable to chdir '/'\n";
2307 # Given a path, this function returns a string containing the kopts
2308 # that should go into that path's Entries line. For example, a binary
2309 # file should get -kb.
2312 my ($path, $srcType, $name) = @_;
2314 if ( defined ( $cfg->{gitcvs
}{usecrlfattr
} ) and
2315 $cfg->{gitcvs
}{usecrlfattr
} =~ /\s*(1|true|yes)\s*$/i )
2317 my ($val) = check_attr
( "crlf", $path );
2318 if ( $val eq "set" )
2322 elsif ( $val eq "unset" )
2328 $log->info("Unrecognized check_attr crlf $path : $val");
2332 if ( defined ( $cfg->{gitcvs
}{allbinary
} ) )
2334 if( ($cfg->{gitcvs
}{allbinary
} =~ /^\s*(1|true|yes)\s*$/i) )
2338 elsif( ($cfg->{gitcvs
}{allbinary
} =~ /^\s*guess\s*$/i) )
2340 if( $srcType eq "sha1Or-k" &&
2343 my ($ret)=$state->{entries
}{$path}{options
};
2344 if( !defined($ret) )
2346 $ret=$state->{opt
}{k
};
2356 if( ! ($ret=~/^(|-kb|-kkv|-kkvl|-kk|-ko|-kv)$/) )
2358 print "E Bad -k option\n";
2359 $log->warn("Bad -k option: $ret");
2360 die "Error: Bad -k option: $ret\n";
2367 if( is_binary
($srcType,$name) )
2369 $log->debug("... as binary");
2374 $log->debug("... as text");
2379 # Return "" to give no special treatment to any path
2385 my ($attr,$path) = @_;
2387 if ( open my $fh, '-|', "git", "check-attr", $attr, "--", $path )
2391 $val =~ s/.*: ([^:\r\n]*)\s*$/$1/;
2400 # This should have the same heuristics as convert.c:is_binary() and related.
2401 # Note that the bare CR test is done by callers in convert.c.
2404 my ($srcType,$name) = @_;
2405 $log->debug("is_binary($srcType,$name)");
2407 # Minimize amount of interpreted code run in the inner per-character
2408 # loop for large files, by totalling each character value and
2409 # then analyzing the totals.
2412 for($i=0;$i<256;$i++)
2417 my $fh = open_blob_or_die
($srcType,$name);
2419 while( defined($line=<$fh>) )
2421 # Any '\0' and bare CR are considered binary.
2422 if( $line =~ /\0|(\r[^\n])/ )
2428 # Count up each character in the line:
2429 my $len=length($line);
2430 for($i=0;$i<$len;$i++)
2432 $counts[ord(substr($line,$i,1))]++;
2437 # Don't count CR and LF as either printable/nonprintable
2438 $counts[ord("\n")]=0;
2439 $counts[ord("\r")]=0;
2441 # Categorize individual character count into printable and nonprintable:
2444 for($i=0;$i<256;$i++)
2452 $nonprintable+=$counts[$i];
2454 elsif( $i==127 ) # DEL
2456 $nonprintable+=$counts[$i];
2460 $printable+=$counts[$i];
2464 return ($printable >> 7) < $nonprintable;
2467 # Returns open file handle. Possible invocations:
2468 # - open_blob_or_die("file",$filename);
2469 # - open_blob_or_die("sha1",$filehash);
2470 sub open_blob_or_die
2472 my ($srcType,$name) = @_;
2474 if( $srcType eq "file" )
2476 if( !open $fh,"<",$name )
2478 $log->warn("Unable to open file $name: $!");
2479 die "Unable to open file $name: $!\n";
2482 elsif( $srcType eq "sha1" || $srcType eq "sha1Or-k" )
2484 unless ( defined ( $name ) and $name =~ /^[a-zA-Z0-9]{40}$/ )
2486 $log->warn("Need filehash");
2487 die "Need filehash\n";
2490 my $type = `git cat-file -t $name`;
2493 unless ( defined ( $type ) and $type eq "blob" )
2495 $log->warn("Invalid type '$type' for '$name'");
2496 die ( "Invalid type '$type' (expected 'blob')" )
2499 my $size = `git cat-file -s $name`;
2502 $log->debug("open_blob_or_die($name) size=$size, type=$type");
2504 unless( open $fh, '-|', "git", "cat-file", "blob", $name )
2506 $log->warn("Unable to open sha1 $name");
2507 die "Unable to open sha1 $name\n";
2512 $log->warn("Unknown type of blob source: $srcType");
2513 die "Unknown type of blob source: $srcType\n";
2518 # Generate a CVS author name from Git author information, by taking
2519 # the first eight characters of the user part of the email address.
2522 my $author_line = shift;
2523 (my $author) = $author_line =~ /<([^>@]{1,8})/;
2528 package GITCVS
::log;
2531 #### Copyright The Open University UK - 2006.
2533 #### Authors: Martyn Smith <martyn@catalyst.net.nz>
2534 #### Martin Langhoff <martin@catalyst.net.nz>
2547 This module provides very crude logging with a similar interface to
2556 Creates a new log object, optionally you can specify a filename here to
2557 indicate the file to log to. If no log file is specified, you can specify one
2558 later with method setfile, or indicate you no longer want logging with method
2561 Until one of these methods is called, all log calls will buffer messages ready
2568 my $filename = shift;
2572 bless $self, $class;
2574 if ( defined ( $filename ) )
2576 open $self->{fh
}, ">>", $filename or die("Couldn't open '$filename' for writing : $!");
2584 This methods takes a filename, and attempts to open that file as the log file.
2585 If successful, all buffered data is written out to the file, and any further
2586 logging is written directly to the file.
2592 my $filename = shift;
2594 if ( defined ( $filename ) )
2596 open $self->{fh
}, ">>", $filename or die("Couldn't open '$filename' for writing : $!");
2599 return unless ( defined ( $self->{buffer
} ) and ref $self->{buffer
} eq "ARRAY" );
2601 while ( my $line = shift @
{$self->{buffer
}} )
2603 print {$self->{fh
}} $line;
2609 This method indicates no logging is going to be used. It flushes any entries in
2610 the internal buffer, and sets a flag to ensure no further data is put there.
2619 return unless ( defined ( $self->{buffer
} ) and ref $self->{buffer
} eq "ARRAY" );
2621 $self->{buffer
} = [];
2626 Internal method. Returns true if the log file is open, false otherwise.
2633 return 1 if ( defined ( $self->{fh
} ) and ref $self->{fh
} eq "GLOB" );
2637 =head2 debug info warn fatal
2639 These four methods are wrappers to _log. They provide the actual interface for
2643 sub debug
{ my $self = shift; $self->_log("debug", @_); }
2644 sub info
{ my $self = shift; $self->_log("info" , @_); }
2645 sub warn { my $self = shift; $self->_log("warn" , @_); }
2646 sub fatal
{ my $self = shift; $self->_log("fatal", @_); }
2650 This is an internal method called by the logging functions. It generates a
2651 timestamp and pushes the logged line either to file, or internal buffer.
2659 return if ( $self->{nolog
} );
2661 my @time = localtime;
2662 my $timestring = sprintf("%4d-%02d-%02d %02d:%02d:%02d : %-5s",
2672 if ( $self->_logopen )
2674 print {$self->{fh
}} $timestring . " - " . join(" ",@_) . "\n";
2676 push @
{$self->{buffer
}}, $timestring . " - " . join(" ",@_) . "\n";
2682 This method simply closes the file handle if one is open
2689 if ( $self->_logopen )
2695 package GITCVS
::updater
;
2698 #### Copyright The Open University UK - 2006.
2700 #### Authors: Martyn Smith <martyn@catalyst.net.nz>
2701 #### Martin Langhoff <martin@catalyst.net.nz>
2723 die "Need to specify a git repository" unless ( defined($config) and -d
$config );
2724 die "Need to specify a module" unless ( defined($module) );
2726 $class = ref($class) || $class;
2730 bless $self, $class;
2732 $self->{valid_tables
} = {'revision' => 1,
2733 'revision_ix1' => 1,
2734 'revision_ix2' => 1,
2740 $self->{module
} = $module;
2741 $self->{git_path
} = $config . "/";
2743 $self->{log} = $log;
2745 die "Git repo '$self->{git_path}' doesn't exist" unless ( -d
$self->{git_path
} );
2747 $self->{dbdriver
} = $cfg->{gitcvs
}{$state->{method
}}{dbdriver
} ||
2748 $cfg->{gitcvs
}{dbdriver
} || "SQLite";
2749 $self->{dbname
} = $cfg->{gitcvs
}{$state->{method
}}{dbname
} ||
2750 $cfg->{gitcvs
}{dbname
} || "%Ggitcvs.%m.sqlite";
2751 $self->{dbuser
} = $cfg->{gitcvs
}{$state->{method
}}{dbuser
} ||
2752 $cfg->{gitcvs
}{dbuser
} || "";
2753 $self->{dbpass
} = $cfg->{gitcvs
}{$state->{method
}}{dbpass
} ||
2754 $cfg->{gitcvs
}{dbpass
} || "";
2755 $self->{dbtablenameprefix
} = $cfg->{gitcvs
}{$state->{method
}}{dbtablenameprefix
} ||
2756 $cfg->{gitcvs
}{dbtablenameprefix
} || "";
2757 my %mapping = ( m
=> $module,
2758 a
=> $state->{method
},
2759 u
=> getlogin || getpwuid($<) || $<,
2760 G
=> $self->{git_path
},
2761 g
=> mangle_dirname
($self->{git_path
}),
2763 $self->{dbname
} =~ s/%([mauGg])/$mapping{$1}/eg;
2764 $self->{dbuser
} =~ s/%([mauGg])/$mapping{$1}/eg;
2765 $self->{dbtablenameprefix
} =~ s/%([mauGg])/$mapping{$1}/eg;
2766 $self->{dbtablenameprefix
} = mangle_tablename
($self->{dbtablenameprefix
});
2768 die "Invalid char ':' in dbdriver" if $self->{dbdriver
} =~ /:/;
2769 die "Invalid char ';' in dbname" if $self->{dbname
} =~ /;/;
2770 $self->{dbh
} = DBI
->connect("dbi:$self->{dbdriver}:dbname=$self->{dbname}",
2773 die "Error connecting to database\n" unless defined $self->{dbh
};
2775 $self->{tables
} = {};
2776 foreach my $table ( keys %{$self->{dbh
}->table_info(undef,undef,undef,'TABLE')->fetchall_hashref('TABLE_NAME')} )
2778 $self->{tables
}{$table} = 1;
2781 # Construct the revision table if required
2782 unless ( $self->{tables
}{$self->tablename("revision")} )
2784 my $tablename = $self->tablename("revision");
2785 my $ix1name = $self->tablename("revision_ix1");
2786 my $ix2name = $self->tablename("revision_ix2");
2788 CREATE TABLE $tablename (
2790 revision INTEGER NOT NULL,
2791 filehash TEXT NOT NULL,
2792 commithash TEXT NOT NULL,
2793 author TEXT NOT NULL,
2794 modified TEXT NOT NULL,
2799 CREATE INDEX $ix1name
2800 ON $tablename (name,revision)
2803 CREATE INDEX $ix2name
2804 ON $tablename (name,commithash)
2808 # Construct the head table if required
2809 unless ( $self->{tables
}{$self->tablename("head")} )
2811 my $tablename = $self->tablename("head");
2812 my $ix1name = $self->tablename("head_ix1");
2814 CREATE TABLE $tablename (
2816 revision INTEGER NOT NULL,
2817 filehash TEXT NOT NULL,
2818 commithash TEXT NOT NULL,
2819 author TEXT NOT NULL,
2820 modified TEXT NOT NULL,
2825 CREATE INDEX $ix1name
2826 ON $tablename (name)
2830 # Construct the properties table if required
2831 unless ( $self->{tables
}{$self->tablename("properties")} )
2833 my $tablename = $self->tablename("properties");
2835 CREATE TABLE $tablename (
2836 key TEXT NOT NULL PRIMARY KEY,
2842 # Construct the commitmsgs table if required
2843 unless ( $self->{tables
}{$self->tablename("commitmsgs")} )
2845 my $tablename = $self->tablename("commitmsgs");
2847 CREATE TABLE $tablename (
2848 key TEXT NOT NULL PRIMARY KEY,
2865 if (exists $self->{valid_tables
}{$name}) {
2866 return $self->{dbtablenameprefix
} . $name;
2879 # first lets get the commit list
2880 $ENV{GIT_DIR
} = $self->{git_path
};
2882 my $commitsha1 = `git rev-parse $self->{module}`;
2885 my $commitinfo = `git cat-file commit $self->{module} 2>&1`;
2886 unless ( $commitinfo =~ /tree\s+[a-zA-Z0-9]{40}/ )
2888 die("Invalid module '$self->{module}'");
2893 my $lastcommit = $self->_get_prop("last_commit");
2895 if (defined $lastcommit && $lastcommit eq $commitsha1) { # up-to-date
2899 # Start exclusive lock here...
2900 $self->{dbh
}->begin_work() or die "Cannot lock database for BEGIN";
2902 # TODO: log processing is memory bound
2903 # if we can parse into a 2nd file that is in reverse order
2904 # we can probably do something really efficient
2905 my @git_log_params = ('--pretty', '--parents', '--topo-order');
2907 if (defined $lastcommit) {
2908 push @git_log_params, "$lastcommit..$self->{module}";
2910 push @git_log_params, $self->{module
};
2912 # git-rev-list is the backend / plumbing version of git-log
2913 open(GITLOG
, '-|', 'git-rev-list', @git_log_params) or die "Cannot call git-rev-list: $!";
2922 if (m/^commit\s+(.*)$/) {
2923 # on ^commit lines put the just seen commit in the stack
2924 # and prime things for the next one
2927 unshift @commits, \
%copy;
2930 my @parents = split(m/\s+/, $1);
2931 $commit{hash
} = shift @parents;
2932 $commit{parents
} = \
@parents;
2933 } elsif (m/^(\w+?):\s+(.*)$/ && !exists($commit{message
})) {
2934 # on rfc822-like lines seen before we see any message,
2935 # lowercase the entry and put it in the hash as key-value
2936 $commit{lc($1)} = $2;
2938 # message lines - skip initial empty line
2939 # and trim whitespace
2940 if (!exists($commit{message
}) && m/^\s*$/) {
2941 # define it to mark the end of headers
2942 $commit{message
} = '';
2945 s/^\s+//; s/\s+$//; # trim ws
2946 $commit{message
} .= $_ . "\n";
2951 unshift @commits, \
%commit if ( keys %commit );
2953 # Now all the commits are in the @commits bucket
2954 # ordered by time DESC. for each commit that needs processing,
2955 # determine whether it's following the last head we've seen or if
2956 # it's on its own branch, grab a file list, and add whatever's changed
2957 # NOTE: $lastcommit refers to the last commit from previous run
2958 # $lastpicked is the last commit we picked in this run
2961 if (defined $lastcommit) {
2962 $lastpicked = $lastcommit;
2965 my $committotal = scalar(@commits);
2966 my $commitcount = 0;
2968 # Load the head table into $head (for cached lookups during the update process)
2969 foreach my $file ( @
{$self->gethead()} )
2971 $head->{$file->{name
}} = $file;
2974 foreach my $commit ( @commits )
2976 $self->{log}->debug("GITCVS::updater - Processing commit $commit->{hash} (" . (++$commitcount) . " of $committotal)");
2977 if (defined $lastpicked)
2979 if (!in_array
($lastpicked, @
{$commit->{parents
}}))
2981 # skip, we'll see this delta
2982 # as part of a merge later
2983 # warn "skipping off-track $commit->{hash}\n";
2985 } elsif (@
{$commit->{parents
}} > 1) {
2986 # it is a merge commit, for each parent that is
2987 # not $lastpicked, see if we can get a log
2988 # from the merge-base to that parent to put it
2989 # in the message as a merge summary.
2990 my @parents = @
{$commit->{parents
}};
2991 foreach my $parent (@parents) {
2992 # git-merge-base can potentially (but rarely) throw
2993 # several candidate merge bases. let's assume
2994 # that the first one is the best one.
2995 if ($parent eq $lastpicked) {
2999 safe_pipe_capture
('git-merge-base',
3000 $lastpicked, $parent);
3002 # The two branches may not be related at all,
3003 # in which case merge base simply fails to find
3004 # any, but that's Ok.
3010 # print "want to log between $base $parent \n";
3011 open(GITLOG
, '-|', 'git-log', '--pretty=medium', "$base..$parent")
3012 or die "Cannot call git-log: $!";
3016 if (!defined $mergedhash) {
3017 if (m/^commit\s+(.+)$/) {
3023 # grab the first line that looks non-rfc822
3024 # aka has content after leading space
3025 if (m/^\s+(\S.*)$/) {
3027 $title = substr($title,0,100); # truncate
3028 unshift @merged, "$mergedhash $title";
3035 $commit->{mergemsg
} = $commit->{message
};
3036 $commit->{mergemsg
} .= "\nSummary of merged commits:\n\n";
3037 foreach my $summary (@merged) {
3038 $commit->{mergemsg
} .= "\t$summary\n";
3040 $commit->{mergemsg
} .= "\n\n";
3041 # print "Message for $commit->{hash} \n$commit->{mergemsg}";
3048 # convert the date to CVS-happy format
3049 $commit->{date
} = "$2 $1 $4 $3 $5" if ( $commit->{date
} =~ /^\w+\s+(\w+)\s+(\d+)\s+(\d+:\d+:\d+)\s+(\d+)\s+([+-]\d+)$/ );
3051 if ( defined ( $lastpicked ) )
3053 my $filepipe = open(FILELIST
, '-|', 'git-diff-tree', '-z', '-r', $lastpicked, $commit->{hash
}) or die("Cannot call git-diff-tree : $!");
3055 while ( <FILELIST
> )
3058 unless ( /^:\d{6}\s+\d{3}(\d)\d{2}\s+[a-zA-Z0-9]{40}\s+([a-zA-Z0-9]{40})\s+(\w)$/o )
3060 die("Couldn't process git-diff-tree line : $_");
3062 my ($mode, $hash, $change) = ($1, $2, $3);
3063 my $name = <FILELIST
>;
3066 # $log->debug("File mode=$mode, hash=$hash, change=$change, name=$name");
3069 $git_perms .= "r" if ( $mode & 4 );
3070 $git_perms .= "w" if ( $mode & 2 );
3071 $git_perms .= "x" if ( $mode & 1 );
3072 $git_perms = "rw" if ( $git_perms eq "" );
3074 if ( $change eq "D" )
3076 #$log->debug("DELETE $name");
3079 revision
=> $head->{$name}{revision
} + 1,
3080 filehash
=> "deleted",
3081 commithash
=> $commit->{hash
},
3082 modified
=> $commit->{date
},
3083 author
=> $commit->{author
},
3086 $self->insert_rev($name, $head->{$name}{revision
}, $hash, $commit->{hash
}, $commit->{date
}, $commit->{author
}, $git_perms);
3088 elsif ( $change eq "M" || $change eq "T" )
3090 #$log->debug("MODIFIED $name");
3093 revision
=> $head->{$name}{revision
} + 1,
3095 commithash
=> $commit->{hash
},
3096 modified
=> $commit->{date
},
3097 author
=> $commit->{author
},
3100 $self->insert_rev($name, $head->{$name}{revision
}, $hash, $commit->{hash
}, $commit->{date
}, $commit->{author
}, $git_perms);
3102 elsif ( $change eq "A" )
3104 #$log->debug("ADDED $name");
3107 revision
=> $head->{$name}{revision
} ?
$head->{$name}{revision
}+1 : 1,
3109 commithash
=> $commit->{hash
},
3110 modified
=> $commit->{date
},
3111 author
=> $commit->{author
},
3114 $self->insert_rev($name, $head->{$name}{revision
}, $hash, $commit->{hash
}, $commit->{date
}, $commit->{author
}, $git_perms);
3118 $log->warn("UNKNOWN FILE CHANGE mode=$mode, hash=$hash, change=$change, name=$name");
3124 # this is used to detect files removed from the repo
3125 my $seen_files = {};
3127 my $filepipe = open(FILELIST
, '-|', 'git-ls-tree', '-z', '-r', $commit->{hash
}) or die("Cannot call git-ls-tree : $!");
3129 while ( <FILELIST
> )
3132 unless ( /^(\d+)\s+(\w+)\s+([a-zA-Z0-9]+)\t(.*)$/o )
3134 die("Couldn't process git-ls-tree line : $_");
3137 my ( $git_perms, $git_type, $git_hash, $git_filename ) = ( $1, $2, $3, $4 );
3139 $seen_files->{$git_filename} = 1;
3141 my ( $oldhash, $oldrevision, $oldmode ) = (
3142 $head->{$git_filename}{filehash
},
3143 $head->{$git_filename}{revision
},
3144 $head->{$git_filename}{mode
}
3147 if ( $git_perms =~ /^\d\d\d(\d)\d\d/o )
3150 $git_perms .= "r" if ( $1 & 4 );
3151 $git_perms .= "w" if ( $1 & 2 );
3152 $git_perms .= "x" if ( $1 & 1 );
3157 # unless the file exists with the same hash, we need to update it ...
3158 unless ( defined($oldhash) and $oldhash eq $git_hash and defined($oldmode) and $oldmode eq $git_perms )
3160 my $newrevision = ( $oldrevision or 0 ) + 1;
3162 $head->{$git_filename} = {
3163 name
=> $git_filename,
3164 revision
=> $newrevision,
3165 filehash
=> $git_hash,
3166 commithash
=> $commit->{hash
},
3167 modified
=> $commit->{date
},
3168 author
=> $commit->{author
},
3173 $self->insert_rev($git_filename, $newrevision, $git_hash, $commit->{hash
}, $commit->{date
}, $commit->{author
}, $git_perms);
3178 # Detect deleted files
3179 foreach my $file ( keys %$head )
3181 unless ( exists $seen_files->{$file} or $head->{$file}{filehash
} eq "deleted" )
3183 $head->{$file}{revision
}++;
3184 $head->{$file}{filehash
} = "deleted";
3185 $head->{$file}{commithash
} = $commit->{hash
};
3186 $head->{$file}{modified
} = $commit->{date
};
3187 $head->{$file}{author
} = $commit->{author
};
3189 $self->insert_rev($file, $head->{$file}{revision
}, $head->{$file}{filehash
}, $commit->{hash
}, $commit->{date
}, $commit->{author
}, $head->{$file}{mode
});
3192 # END : "Detect deleted files"
3196 if (exists $commit->{mergemsg
})
3198 $self->insert_mergelog($commit->{hash
}, $commit->{mergemsg
});
3201 $lastpicked = $commit->{hash
};
3203 $self->_set_prop("last_commit", $commit->{hash
});
3206 $self->delete_head();
3207 foreach my $file ( keys %$head )
3211 $head->{$file}{revision
},
3212 $head->{$file}{filehash
},
3213 $head->{$file}{commithash
},
3214 $head->{$file}{modified
},
3215 $head->{$file}{author
},
3216 $head->{$file}{mode
},
3219 # invalidate the gethead cache
3220 $self->{gethead_cache
} = undef;
3223 # Ending exclusive lock here
3224 $self->{dbh
}->commit() or die "Failed to commit changes to SQLite";
3231 my $revision = shift;
3232 my $filehash = shift;
3233 my $commithash = shift;
3234 my $modified = shift;
3237 my $tablename = $self->tablename("revision");
3239 my $insert_rev = $self->{dbh
}->prepare_cached("INSERT INTO $tablename (name, revision, filehash, commithash, modified, author, mode) VALUES (?,?,?,?,?,?,?)",{},1);
3240 $insert_rev->execute($name, $revision, $filehash, $commithash, $modified, $author, $mode);
3248 my $tablename = $self->tablename("commitmsgs");
3250 my $insert_mergelog = $self->{dbh
}->prepare_cached("INSERT INTO $tablename (key, value) VALUES (?,?)",{},1);
3251 $insert_mergelog->execute($key, $value);
3257 my $tablename = $self->tablename("head");
3259 my $delete_head = $self->{dbh
}->prepare_cached("DELETE FROM $tablename",{},1);
3260 $delete_head->execute();
3267 my $revision = shift;
3268 my $filehash = shift;
3269 my $commithash = shift;
3270 my $modified = shift;
3273 my $tablename = $self->tablename("head");
3275 my $insert_head = $self->{dbh
}->prepare_cached("INSERT INTO $tablename (name, revision, filehash, commithash, modified, author, mode) VALUES (?,?,?,?,?,?,?)",{},1);
3276 $insert_head->execute($name, $revision, $filehash, $commithash, $modified, $author, $mode);
3282 my $filename = shift;
3283 my $tablename = $self->tablename("head");
3285 my $db_query = $self->{dbh
}->prepare_cached("SELECT filehash, revision, mode FROM $tablename WHERE name=?",{},1);
3286 $db_query->execute($filename);
3287 my ( $hash, $revision, $mode ) = $db_query->fetchrow_array;
3289 return ( $hash, $revision, $mode );
3296 my $tablename = $self->tablename("properties");
3298 my $db_query = $self->{dbh
}->prepare_cached("SELECT value FROM $tablename WHERE key=?",{},1);
3299 $db_query->execute($key);
3300 my ( $value ) = $db_query->fetchrow_array;
3310 my $tablename = $self->tablename("properties");
3312 my $db_query = $self->{dbh
}->prepare_cached("UPDATE $tablename SET value=? WHERE key=?",{},1);
3313 $db_query->execute($value, $key);
3315 unless ( $db_query->rows )
3317 $db_query = $self->{dbh
}->prepare_cached("INSERT INTO $tablename (key, value) VALUES (?,?)",{},1);
3318 $db_query->execute($key, $value);
3331 my $tablename = $self->tablename("head");
3333 return $self->{gethead_cache
} if ( defined ( $self->{gethead_cache
} ) );
3335 my $db_query = $self->{dbh
}->prepare_cached("SELECT name, filehash, mode, revision, modified, commithash, author FROM $tablename ORDER BY name ASC",{},1);
3336 $db_query->execute();
3339 while ( my $file = $db_query->fetchrow_hashref )
3344 $self->{gethead_cache
} = $tree;
3356 my $filename = shift;
3357 my $tablename = $self->tablename("revision");
3359 my $db_query = $self->{dbh
}->prepare_cached("SELECT name, filehash, author, mode, revision, modified, commithash FROM $tablename WHERE name=? ORDER BY revision DESC",{},1);
3360 $db_query->execute($filename);
3363 while ( my $file = $db_query->fetchrow_hashref )
3373 This function takes a filename (with path) argument and returns a hashref of
3374 metadata for that file.
3381 my $filename = shift;
3382 my $revision = shift;
3383 my $tablename_rev = $self->tablename("revision");
3384 my $tablename_head = $self->tablename("head");
3387 if ( defined($revision) and $revision =~ /^\d+$/ )
3389 $db_query = $self->{dbh
}->prepare_cached("SELECT * FROM $tablename_rev WHERE name=? AND revision=?",{},1);
3390 $db_query->execute($filename, $revision);
3392 elsif ( defined($revision) and $revision =~ /^[a-zA-Z0-9]{40}$/ )
3394 $db_query = $self->{dbh
}->prepare_cached("SELECT * FROM $tablename_rev WHERE name=? AND commithash=?",{},1);
3395 $db_query->execute($filename, $revision);
3397 $db_query = $self->{dbh
}->prepare_cached("SELECT * FROM $tablename_head WHERE name=?",{},1);
3398 $db_query->execute($filename);
3401 return $db_query->fetchrow_hashref;
3404 =head2 commitmessage
3406 this function takes a commithash and returns the commit message for that commit
3412 my $commithash = shift;
3413 my $tablename = $self->tablename("commitmsgs");
3415 die("Need commithash") unless ( defined($commithash) and $commithash =~ /^[a-zA-Z0-9]{40}$/ );
3418 $db_query = $self->{dbh
}->prepare_cached("SELECT value FROM $tablename WHERE key=?",{},1);
3419 $db_query->execute($commithash);
3421 my ( $message ) = $db_query->fetchrow_array;
3423 if ( defined ( $message ) )
3425 $message .= " " if ( $message =~ /\n$/ );
3429 my @lines = safe_pipe_capture
("git-cat-file", "commit", $commithash);
3430 shift @lines while ( $lines[0] =~ /\S/ );
3431 $message = join("",@lines);
3432 $message .= " " if ( $message =~ /\n$/ );
3438 This function takes a filename (with path) argument and returns an arrayofarrays
3439 containing revision,filehash,commithash ordered by revision descending
3445 my $filename = shift;
3446 my $tablename = $self->tablename("revision");
3449 $db_query = $self->{dbh
}->prepare_cached("SELECT revision, filehash, commithash FROM $tablename WHERE name=? ORDER BY revision DESC",{},1);
3450 $db_query->execute($filename);
3452 return $db_query->fetchall_arrayref;
3455 =head2 gethistorydense
3457 This function takes a filename (with path) argument and returns an arrayofarrays
3458 containing revision,filehash,commithash ordered by revision descending.
3460 This version of gethistory skips deleted entries -- so it is useful for annotate.
3461 The 'dense' part is a reference to a '--dense' option available for git-rev-list
3462 and other git tools that depend on it.
3468 my $filename = shift;
3469 my $tablename = $self->tablename("revision");
3472 $db_query = $self->{dbh
}->prepare_cached("SELECT revision, filehash, commithash FROM $tablename WHERE name=? AND filehash!='deleted' ORDER BY revision DESC",{},1);
3473 $db_query->execute($filename);
3475 return $db_query->fetchall_arrayref;
3480 from Array::PAT - mimics the in_array() function
3481 found in PHP. Yuck but works for small arrays.
3486 my ($check, @array) = @_;
3488 foreach my $test (@array){
3489 if($check eq $test){
3496 =head2 safe_pipe_capture
3498 an alternative to `command` that allows input to be passed as an array
3499 to work around shell problems with weird characters in arguments
3502 sub safe_pipe_capture
{
3506 if (my $pid = open my $child, '-|') {
3507 @output = (<$child>);
3508 close $child or die join(' ',@_).": $! $?";
3510 exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
3512 return wantarray ?
@output : join('',@output);
3515 =head2 mangle_dirname
3517 create a string from a directory name that is suitable to use as
3518 part of a filename, mainly by converting all chars except \w.- to _
3521 sub mangle_dirname
{
3522 my $dirname = shift;
3523 return unless defined $dirname;
3525 $dirname =~ s/[^\w.-]/_/g;
3530 =head2 mangle_tablename
3532 create a string from a that is suitable to use as part of an SQL table
3533 name, mainly by converting all chars except \w to _
3536 sub mangle_tablename
{
3537 my $tablename = shift;
3538 return unless defined $tablename;
3540 $tablename =~ s/[^\w_]/_/g;