2006-02-22 17:50:15 +08:00
#!/usr/bin/perl
####
#### This application is a CVS emulation layer for git.
#### It is intended for clients to connect over SSH.
#### See the documentation for more details.
####
#### Copyright The Open University UK - 2006.
####
#### Authors: Martyn Smith <martyn@catalyst.net.nz>
2010-10-06 03:44:08 +08:00
#### Martin Langhoff <martin@laptop.org>
2006-02-22 17:50:15 +08:00
####
####
#### Released under the GNU Public License, version 2.
####
####
2010-09-25 04:00:52 +08:00
use 5.008;
2006-02-22 17:50:15 +08:00
use strict;
use warnings;
2006-12-07 11:38:50 +08:00
use bytes;
2006-02-22 17:50:15 +08:00
use Fcntl;
use File::Temp qw/tempdir tempfile/;
2008-05-15 12:35:46 +08:00
use File::Path qw/rmtree/;
2006-02-22 17:50:15 +08:00
use File::Basename;
2007-06-07 22:57:01 +08:00
use Getopt::Long qw(:config require_order no_ignore_case);
my $VERSION = '@@GIT_VERSION@@';
2006-02-22 17:50:15 +08:00
my $log = GITCVS::log->new();
my $cfg;
my $DATE_LIST = {
Jan => "01",
Feb => "02",
Mar => "03",
Apr => "04",
May => "05",
Jun => "06",
Jul => "07",
Aug => "08",
Sep => "09",
Oct => "10",
Nov => "11",
Dec => "12",
};
# Enable autoflush for STDOUT (otherwise the whole thing falls apart)
$| = 1;
#### Definition and mappings of functions ####
2012-10-14 13:42:19 +08:00
# NOTE: Despite the existence of req_CATCHALL and req_EMPTY unimplemented
# requests, this list is incomplete. It is missing many rarer/optional
# requests. Perhaps some clients require a claim of support for
# these specific requests for main functionality to work?
2006-02-22 17:50:15 +08:00
my $methods = {
'Root' => \&req_Root,
'Valid-responses' => \&req_Validresponses,
'valid-requests' => \&req_validrequests,
'Directory' => \&req_Directory,
2012-10-14 13:42:28 +08:00
'Sticky' => \&req_Sticky,
2006-02-22 17:50:15 +08:00
'Entry' => \&req_Entry,
'Modified' => \&req_Modified,
'Unchanged' => \&req_Unchanged,
2006-03-01 14:30:35 +08:00
'Questionable' => \&req_Questionable,
2006-02-22 17:50:15 +08:00
'Argument' => \&req_Argument,
'Argumentx' => \&req_Argument,
'expand-modules' => \&req_expandmodules,
'add' => \&req_add,
'remove' => \&req_remove,
'co' => \&req_co,
'update' => \&req_update,
'ci' => \&req_ci,
'diff' => \&req_diff,
'log' => \&req_log,
2006-03-01 14:30:35 +08:00
'rlog' => \&req_log,
2006-02-22 17:50:15 +08:00
'tag' => \&req_CATCHALL,
'status' => \&req_status,
'admin' => \&req_CATCHALL,
'history' => \&req_CATCHALL,
2008-03-28 06:17:26 +08:00
'watchers' => \&req_EMPTY,
'editors' => \&req_EMPTY,
2009-01-30 09:12:27 +08:00
'noop' => \&req_EMPTY,
2006-02-22 17:50:15 +08:00
'annotate' => \&req_annotate,
'Global_option' => \&req_Globaloption,
};
##############################################
# $state holds all the bits of information the clients sends us that could
# potentially be useful when it comes to actually _doing_ something.
2006-07-25 18:48:52 +08:00
my $state = { prependdir => '' };
2008-05-15 12:35:46 +08:00
# Work is for managing temporary working directory
my $work =
{
state => undef, # undef, 1 (empty), 2 (with stuff)
workDir => undef,
index => undef,
emptyDir => undef,
tmpDir => undef
};
2006-02-22 17:50:15 +08:00
$log->info("--------------- STARTING -----------------");
2007-06-07 22:57:01 +08:00
my $usage =
2013-02-24 08:50:16 +08:00
"usage: git cvsserver [options] [pserver|server] [<directory> ...]\n".
2007-06-07 22:57:01 +08:00
" --base-path <path> : Prepend to requested CVSROOT\n".
CVS Server: Support reading base and roots from environment
The Gitosis single-account Git/ssh hosting system runs git commands
through git-shell after confirming that the connecting user is
authorized to access the requested repository. This works well for
upload-pack and receive-pack, which take a repository argument through
git-shell. This doesn't work so well for `cvs server', which is passed
through literally, with no arguments. Allowing arguments risks
sneaking in `--export-all', so that restriction should be maintained.
Despite that, passing a repository root is necessary for per-user
access control by the hosting software, and passing a base path
improves usability without weakening security. Thus, git-cvsserver
needs to come up with these values at runtime by some other
means. Since git-shell preserves the environment for other purposes,
the environment can carry these arguments as well.
Thus, modify git-cvsserver to read $GIT_CVSSERVER_{BASE_PATH,ROOT} in
the absence of equivalent command line arguments.
Signed-off-by: Phil Miller <mille121@illinois.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-12-31 03:35:31 +08:00
" Can be read from GIT_CVSSERVER_BASE_PATH\n".
2007-06-07 22:57:01 +08:00
" --strict-paths : Don't allow recursing into subdirectories\n".
" --export-all : Don't check for gitcvs.enabled in config\n".
" --version, -V : Print version information and exit\n".
2011-10-04 02:21:36 +08:00
" -h, -H : Print usage information and exit\n".
2007-06-07 22:57:01 +08:00
"\n".
"<directory> ... is a list of allowed directories. If no directories\n".
"are given, all are allowed. This is an additional restriction, gitcvs\n".
CVS Server: Support reading base and roots from environment
The Gitosis single-account Git/ssh hosting system runs git commands
through git-shell after confirming that the connecting user is
authorized to access the requested repository. This works well for
upload-pack and receive-pack, which take a repository argument through
git-shell. This doesn't work so well for `cvs server', which is passed
through literally, with no arguments. Allowing arguments risks
sneaking in `--export-all', so that restriction should be maintained.
Despite that, passing a repository root is necessary for per-user
access control by the hosting software, and passing a base path
improves usability without weakening security. Thus, git-cvsserver
needs to come up with these values at runtime by some other
means. Since git-shell preserves the environment for other purposes,
the environment can carry these arguments as well.
Thus, modify git-cvsserver to read $GIT_CVSSERVER_{BASE_PATH,ROOT} in
the absence of equivalent command line arguments.
Signed-off-by: Phil Miller <mille121@illinois.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-12-31 03:35:31 +08:00
"access still needs to be enabled by the gitcvs.enabled config option.\n".
"Alternately, one directory may be specified in GIT_CVSSERVER_ROOT.\n";
2007-06-07 22:57:01 +08:00
2011-10-04 02:21:36 +08:00
my @opts = ( 'h|H', 'version|V',
2007-06-07 22:57:01 +08:00
'base-path=s', 'strict-paths', 'export-all' );
GetOptions( $state, @opts )
or die $usage;
if ($state->{version}) {
print "git-cvsserver version $VERSION\n";
exit;
}
if ($state->{help}) {
print $usage;
exit;
}
2006-02-22 17:50:15 +08:00
my $TEMP_DIR = tempdir( CLEANUP => 1 );
$log->debug("Temporary directory is '$TEMP_DIR'");
2007-06-07 22:57:01 +08:00
$state->{method} = 'ext';
if (@ARGV) {
if ($ARGV[0] eq 'pserver') {
$state->{method} = 'pserver';
shift @ARGV;
} elsif ($ARGV[0] eq 'server') {
shift @ARGV;
}
}
# everything else is a directory
$state->{allowed_roots} = [ @ARGV ];
2007-06-15 09:01:53 +08:00
# don't export the whole system unless the users requests it
if ($state->{'export-all'} && !@{$state->{allowed_roots}}) {
2022-07-20 02:32:14 +08:00
die "--export-all can only be used together with an explicit '<directory>...' list\n";
2007-06-15 09:01:53 +08:00
}
CVS Server: Support reading base and roots from environment
The Gitosis single-account Git/ssh hosting system runs git commands
through git-shell after confirming that the connecting user is
authorized to access the requested repository. This works well for
upload-pack and receive-pack, which take a repository argument through
git-shell. This doesn't work so well for `cvs server', which is passed
through literally, with no arguments. Allowing arguments risks
sneaking in `--export-all', so that restriction should be maintained.
Despite that, passing a repository root is necessary for per-user
access control by the hosting software, and passing a base path
improves usability without weakening security. Thus, git-cvsserver
needs to come up with these values at runtime by some other
means. Since git-shell preserves the environment for other purposes,
the environment can carry these arguments as well.
Thus, modify git-cvsserver to read $GIT_CVSSERVER_{BASE_PATH,ROOT} in
the absence of equivalent command line arguments.
Signed-off-by: Phil Miller <mille121@illinois.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-12-31 03:35:31 +08:00
# Environment handling for running under git-shell
if (exists $ENV{GIT_CVSSERVER_BASE_PATH}) {
if ($state->{'base-path'}) {
die "Cannot specify base path both ways.\n";
}
my $base_path = $ENV{GIT_CVSSERVER_BASE_PATH};
$state->{'base-path'} = $base_path;
$log->debug("Picked up base path '$base_path' from environment.\n");
}
if (exists $ENV{GIT_CVSSERVER_ROOT}) {
if (@{$state->{allowed_roots}}) {
die "Cannot specify roots both ways: @ARGV\n";
}
my $allowed_root = $ENV{GIT_CVSSERVER_ROOT};
$state->{allowed_roots} = [ $allowed_root ];
$log->debug("Picked up allowed root '$allowed_root' from environment.\n");
}
2006-03-04 15:30:04 +08:00
# if we are called with a pserver argument,
2006-04-26 14:59:28 +08:00
# deal with the authentication cat before entering the
2006-03-04 15:30:04 +08:00
# main loop
2007-06-07 22:57:01 +08:00
if ($state->{method} eq 'pserver') {
2006-03-04 15:30:04 +08:00
my $line = <STDIN>; chomp $line;
2007-05-27 20:33:10 +08:00
unless( $line =~ /^BEGIN (AUTH|VERIFICATION) REQUEST$/) {
2006-03-04 15:30:04 +08:00
die "E Do not understand $line - expecting BEGIN AUTH REQUEST\n";
}
2007-05-27 20:33:10 +08:00
my $request = $1;
2006-03-04 15:30:04 +08:00
$line = <STDIN>; chomp $line;
2007-10-17 22:05:47 +08:00
unless (req_Root('root', $line)) { # reuse Root
print "E Invalid root $line \n";
exit 1;
}
2006-03-04 15:30:04 +08:00
$line = <STDIN>; chomp $line;
2010-05-15 23:06:46 +08:00
my $user = $line;
$line = <STDIN>; chomp $line;
my $password = $line;
2010-05-15 10:46:02 +08:00
if ($user eq 'anonymous') {
# "A" will be 1 byte, use length instead in case the
# encryption method ever changes (yeah, right!)
if (length($password) > 1 ) {
print "E Don't supply a password for the `anonymous' user\n";
print "I HATE YOU\n";
exit 1;
}
# Fall through to LOVE
} else {
2010-05-15 23:06:46 +08:00
# Trying to authenticate a user
2010-05-15 23:07:54 +08:00
if (not exists $cfg->{gitcvs}->{authdb}) {
2010-05-15 10:46:02 +08:00
print "E the repo config file needs a [gitcvs] section with an 'authdb' parameter set to the filename of the authentication database\n";
print "I HATE YOU\n";
exit 1;
}
my $authdb = $cfg->{gitcvs}->{authdb};
unless (-e $authdb) {
print "E The authentication database specified in [gitcvs.authdb] does not exist\n";
2010-05-15 23:06:46 +08:00
print "I HATE YOU\n";
exit 1;
2010-05-15 23:07:54 +08:00
}
2010-05-15 10:46:01 +08:00
my $auth_ok;
2010-05-15 10:46:02 +08:00
open my $passwd, "<", $authdb or die $!;
2010-05-15 10:46:01 +08:00
while (<$passwd>) {
if (m{^\Q$user\E:(.*)}) {
2021-09-15 16:09:47 +08:00
my $hash = crypt(descramble($password), $1);
if (defined $hash and $hash eq $1) {
2010-05-15 10:46:01 +08:00
$auth_ok = 1;
}
2021-09-15 16:09:47 +08:00
}
2010-05-15 10:46:01 +08:00
}
close $passwd;
unless ($auth_ok) {
2010-05-15 23:06:46 +08:00
print "I HATE YOU\n";
exit 1;
}
2010-05-15 10:46:02 +08:00
# Fall through to LOVE
2006-03-04 15:30:04 +08:00
}
2010-05-15 23:06:46 +08:00
# For checking whether the user is anonymous on commit
$state->{user} = $user;
2006-03-04 15:30:04 +08:00
$line = <STDIN>; chomp $line;
2007-05-27 20:33:10 +08:00
unless ($line eq "END $request REQUEST") {
die "E Do not understand $line -- expecting END $request REQUEST\n";
2006-03-04 15:30:04 +08:00
}
print "I LOVE YOU\n";
2007-05-27 20:33:10 +08:00
exit if $request eq 'VERIFICATION'; # cvs login
2006-03-04 15:30:04 +08:00
# and now back to our regular programme...
}
2006-02-22 17:50:15 +08:00
# Keep going until the client closes the connection
while (<STDIN>)
{
chomp;
2006-04-26 14:59:28 +08:00
# Check to see if we've seen this method, and call appropriate function.
2006-02-22 17:50:15 +08:00
if ( /^([\w-]+)(?:\s+(.*))?$/ and defined($methods->{$1}) )
{
# use the $methods hash to call the appropriate sub for this command
#$log->info("Method : $1");
&{$methods->{$1}}($1,$2);
} else {
# log fatal because we don't understand this function. If this happens
# we're fairly screwed because we don't know if the client is expecting
# a response. If it is, the client will hang, we'll hang, and the whole
# thing will be custard.
$log->fatal("Don't understand command $_\n");
die("Unknown command $_");
}
}
$log->debug("Processing time : user=" . (times)[0] . " system=" . (times)[1]);
$log->info("--------------- FINISH -----------------");
2008-05-15 12:35:46 +08:00
chdir '/';
exit 0;
2006-02-22 17:50:15 +08:00
# Magic catchall method.
# This is the method that will handle all commands we haven't yet
# implemented. It simply sends a warning to the log file indicating a
# command that hasn't been implemented has been invoked.
sub req_CATCHALL
{
my ( $cmd, $data ) = @_;
$log->warn("Unhandled command : req_$cmd : $data");
}
2008-03-28 06:17:26 +08:00
# This method invariably succeeds with an empty response.
sub req_EMPTY
{
print "ok\n";
}
2006-02-22 17:50:15 +08:00
# Root pathname \n
# Response expected: no. Tell the server which CVSROOT to use. Note that
# pathname is a local directory and not a fully qualified CVSROOT variable.
# pathname must already exist; if creating a new root, use the init
# request, not Root. pathname does not include the hostname of the server,
# how to access the server, etc.; by the time the CVS protocol is in use,
# connection, authentication, etc., are already taken care of. The Root
# request must be sent only once, and it must be sent before any requests
# other than Valid-responses, valid-requests, UseUnchanged, Set or init.
sub req_Root
{
my ( $cmd, $data ) = @_;
$log->debug("req_Root : $data");
2007-06-07 22:57:00 +08:00
unless ($data =~ m#^/#) {
print "error 1 Root must be an absolute pathname\n";
return 0;
}
2007-06-15 09:01:52 +08:00
my $cvsroot = $state->{'base-path'} || '';
$cvsroot =~ s#/+$##;
$cvsroot .= $data;
2007-06-07 22:57:00 +08:00
if ($state->{CVSROOT}
2007-06-15 09:01:52 +08:00
&& ($state->{CVSROOT} ne $cvsroot)) {
2007-06-07 22:57:00 +08:00
print "error 1 Conflicting roots specified\n";
return 0;
}
2007-06-15 09:01:52 +08:00
$state->{CVSROOT} = $cvsroot;
2006-02-22 17:50:15 +08:00
$ENV{GIT_DIR} = $state->{CVSROOT} . "/";
2007-06-07 22:57:01 +08:00
if (@{$state->{allowed_roots}}) {
my $allowed = 0;
foreach my $dir (@{$state->{allowed_roots}}) {
next unless $dir =~ m#^/#;
$dir =~ s#/+$##;
if ($state->{'strict-paths'}) {
if ($ENV{GIT_DIR} =~ m#^\Q$dir\E/?$#) {
$allowed = 1;
last;
}
} elsif ($ENV{GIT_DIR} =~ m#^\Q$dir\E(/?$|/)#) {
$allowed = 1;
last;
}
}
unless ($allowed) {
print "E $ENV{GIT_DIR} does not seem to be a valid GIT repository\n";
print "E \n";
print "error 1 $ENV{GIT_DIR} is not a valid repository\n";
return 0;
}
}
2006-03-04 12:47:22 +08:00
unless (-d $ENV{GIT_DIR} && -e $ENV{GIT_DIR}.'HEAD') {
print "E $ENV{GIT_DIR} does not seem to be a valid GIT repository\n";
2007-06-07 22:57:01 +08:00
print "E \n";
print "error 1 $ENV{GIT_DIR} is not a valid repository\n";
2006-03-04 12:47:22 +08:00
return 0;
}
2006-02-22 17:50:15 +08:00
2017-09-11 13:45:54 +08:00
my @gitvars = safe_pipe_capture(qw(git config -l));
2006-03-04 12:47:22 +08:00
if ($?) {
2007-01-29 08:16:53 +08:00
print "E problems executing git-config on the server -- this is not a git repository or the PATH is not set correctly.\n";
2006-03-04 12:47:22 +08:00
print "E \n";
2007-01-29 08:16:53 +08:00
print "error 1 - problem executing git-config\n";
2006-03-04 12:47:22 +08:00
return 0;
}
foreach my $line ( @gitvars )
2006-02-22 17:50:15 +08:00
{
2020-06-23 02:04:16 +08:00
next unless ( $line =~ /^(gitcvs|extensions)\.(?:(ext|pserver)\.)?([\w-]+)=(.*)$/ );
2007-05-13 08:16:24 +08:00
unless ($2) {
$cfg->{$1}{$3} = $4;
2007-03-19 23:55:58 +08:00
} else {
$cfg->{$1}{$2}{$3} = $4;
}
2006-02-22 17:50:15 +08:00
}
2007-05-21 08:57:27 +08:00
my $enabled = ($cfg->{gitcvs}{$state->{method}}{enabled}
|| $cfg->{gitcvs}{enabled});
2007-06-15 09:01:53 +08:00
unless ($state->{'export-all'} ||
($enabled && $enabled =~ /^\s*(1|true|yes)\s*$/i)) {
2006-02-22 17:50:15 +08:00
print "E GITCVS emulation needs to be enabled on this repo\n";
print "E the repo config file needs a [gitcvs] section added, and the parameter 'enabled' set to 1\n";
print "E \n";
print "error 1 GITCVS emulation disabled\n";
2006-03-04 15:30:04 +08:00
return 0;
2006-02-22 17:50:15 +08:00
}
2007-03-19 23:55:59 +08:00
my $logfile = $cfg->{gitcvs}{$state->{method}}{logfile} || $cfg->{gitcvs}{logfile};
if ( $logfile )
2006-02-22 17:50:15 +08:00
{
2007-03-19 23:55:59 +08:00
$log->setfile($logfile);
2006-02-22 17:50:15 +08:00
} else {
$log->nofile();
}
2006-03-04 15:30:04 +08:00
2020-06-23 02:04:16 +08:00
$state->{rawsz} = ($cfg->{'extensions'}{'objectformat'} || 'sha1') eq 'sha256' ? 32 : 20;
$state->{hexsz} = $state->{rawsz} * 2;
2006-03-04 15:30:04 +08:00
return 1;
2006-02-22 17:50:15 +08:00
}
# Global_option option \n
# Response expected: no. Transmit one of the global options `-q', `-Q',
# `-l', `-t', `-r', or `-n'. option must be one of those strings, no
# variations (such as combining of options) are allowed. For graceful
# handling of valid-requests, it is probably better to make new global
# options separate requests, rather than trying to add them to this
# request.
sub req_Globaloption
{
my ( $cmd, $data ) = @_;
$log->debug("req_Globaloption : $data");
2006-03-27 11:51:42 +08:00
$state->{globaloptions}{$data} = 1;
2006-02-22 17:50:15 +08:00
}
# Valid-responses request-list \n
# Response expected: no. Tell the server what responses the client will
# accept. request-list is a space separated list of tokens.
sub req_Validresponses
{
my ( $cmd, $data ) = @_;
2006-04-26 14:59:28 +08:00
$log->debug("req_Validresponses : $data");
2006-02-22 17:50:15 +08:00
# TODO : re-enable this, currently it's not particularly useful
#$state->{validresponses} = [ split /\s+/, $data ];
}
# valid-requests \n
# Response expected: yes. Ask the server to send back a Valid-requests
# response.
sub req_validrequests
{
my ( $cmd, $data ) = @_;
$log->debug("req_validrequests");
2013-10-30 16:44:43 +08:00
$log->debug("SEND : Valid-requests " . join(" ",sort keys %$methods));
2006-02-22 17:50:15 +08:00
$log->debug("SEND : ok");
2013-10-30 16:44:43 +08:00
print "Valid-requests " . join(" ",sort keys %$methods) . "\n";
2006-02-22 17:50:15 +08:00
print "ok\n";
}
# Directory local-directory \n
# Additional data: repository \n. Response expected: no. Tell the server
# what directory to use. The repository should be a directory name from a
# previous server response. Note that this both gives a default for Entry
# and Modified and also for ci and the other commands; normal usage is to
# send Directory for each directory in which there will be an Entry or
# Modified, and then a final Directory for the original directory, then the
# command. The local-directory is relative to the top level at which the
# command is occurring (i.e. the last Directory which is sent before the
# command); to indicate that top level, `.' should be sent for
# local-directory.
sub req_Directory
{
my ( $cmd, $data ) = @_;
my $repository = <STDIN>;
chomp $repository;
$state->{localdir} = $data;
$state->{repository} = $repository;
2006-03-27 11:51:42 +08:00
$state->{path} = $repository;
2010-01-26 22:47:16 +08:00
$state->{path} =~ s/^\Q$state->{CVSROOT}\E\///;
2006-03-27 11:51:42 +08:00
$state->{module} = $1 if ($state->{path} =~ s/^(.*?)(\/|$)//);
$state->{path} .= "/" if ( $state->{path} =~ /\S/ );
$state->{directory} = $state->{localdir};
$state->{directory} = "" if ( $state->{directory} eq "." );
2006-02-22 17:50:15 +08:00
$state->{directory} .= "/" if ( $state->{directory} =~ /\S/ );
2006-10-11 06:33:28 +08:00
if ( (not defined($state->{prependdir}) or $state->{prependdir} eq '') and $state->{localdir} eq "." and $state->{path} =~ /\S/ )
2006-03-27 11:51:42 +08:00
{
$log->info("Setting prepend to '$state->{path}'");
$state->{prependdir} = $state->{path};
2012-10-14 13:42:28 +08:00
my %entries;
2006-03-27 11:51:42 +08:00
foreach my $entry ( keys %{$state->{entries}} )
{
2012-10-14 13:42:28 +08:00
$entries{$state->{prependdir} . $entry} = $state->{entries}{$entry};
2006-03-27 11:51:42 +08:00
}
2012-10-14 13:42:28 +08:00
$state->{entries}=\%entries;
my %dirMap;
foreach my $dir ( keys %{$state->{dirMap}} )
{
$dirMap{$state->{prependdir} . $dir} = $state->{dirMap}{$dir};
}
$state->{dirMap}=\%dirMap;
2006-03-27 11:51:42 +08:00
}
if ( defined ( $state->{prependdir} ) )
{
$log->debug("Prepending '$state->{prependdir}' to state|directory");
$state->{directory} = $state->{prependdir} . $state->{directory}
}
2012-10-14 13:42:28 +08:00
if ( ! defined($state->{dirMap}{$state->{directory}}) )
{
$state->{dirMap}{$state->{directory}} =
{
'names' => {}
#'tagspec' => undef
};
}
2006-03-28 09:24:27 +08:00
$log->debug("req_Directory : localdir=$data repository=$repository path=$state->{path} directory=$state->{directory} module=$state->{module}");
2006-02-22 17:50:15 +08:00
}
2012-10-14 13:42:28 +08:00
# Sticky tagspec \n
# Response expected: no. Tell the server that the directory most
# recently specified with Directory has a sticky tag or date
# tagspec. The first character of tagspec is T for a tag, D for
# a date, or some other character supplied by a Set-sticky
# response from a previous request to the server. The remainder
# of tagspec contains the actual tag or date, again as supplied
# by Set-sticky.
# The server should remember Static-directory and Sticky requests
# for a particular directory; the client need not resend them each
# time it sends a Directory request for a given directory. However,
# the server is not obliged to remember them beyond the context
# of a single command.
sub req_Sticky
{
my ( $cmd, $tagspec ) = @_;
my ( $stickyInfo );
if($tagspec eq "")
{
# nothing
}
elsif($tagspec=~/^T([^ ]+)\s*$/)
{
$stickyInfo = { 'tag' => $1 };
}
elsif($tagspec=~/^D([0-9.]+)\s*$/)
{
$stickyInfo= { 'date' => $1 };
}
else
{
die "Unknown tag_or_date format\n";
}
$state->{dirMap}{$state->{directory}}{stickyInfo}=$stickyInfo;
$log->debug("req_Sticky : tagspec=$tagspec repository=$state->{repository}"
. " path=$state->{path} directory=$state->{directory}"
. " module=$state->{module}");
}
2006-02-22 17:50:15 +08:00
# Entry entry-line \n
# Response expected: no. Tell the server what version of a file is on the
# local machine. The name in entry-line is a name relative to the directory
# most recently specified with Directory. If the user is operating on only
# some files in a directory, Entry requests for only those files need be
# included. If an Entry request is sent without Modified, Is-modified, or
# Unchanged, it means the file is lost (does not exist in the working
# directory). If both Entry and one of Modified, Is-modified, or Unchanged
# are sent for the same file, Entry must be sent first. For a given file,
# one can send Modified, Is-modified, or Unchanged, but not more than one
# of these three.
sub req_Entry
{
my ( $cmd, $data ) = @_;
2006-03-27 11:51:42 +08:00
#$log->debug("req_Entry : $data");
2006-02-22 17:50:15 +08:00
2012-10-14 13:42:23 +08:00
my @data = split(/\//, $data, -1);
2006-02-22 17:50:15 +08:00
$state->{entries}{$state->{directory}.$data[1]} = {
revision => $data[2],
conflict => $data[3],
options => $data[4],
tag_or_date => $data[5],
};
2006-03-27 11:51:42 +08:00
2012-10-14 13:42:28 +08:00
$state->{dirMap}{$state->{directory}}{names}{$data[1]} = 'F';
2006-03-27 11:51:42 +08:00
$log->info("Received entry line '$data' => '" . $state->{directory} . $data[1] . "'");
}
# Questionable filename \n
# Response expected: no. Additional data: no. Tell the server to check
# whether filename should be ignored, and if not, next time the server
# sends responses, send (in a M response) `?' followed by the directory and
# filename. filename must not contain `/'; it needs to be a file in the
# directory named by the most recent Directory request.
sub req_Questionable
{
my ( $cmd, $data ) = @_;
$log->debug("req_Questionable : $data");
$state->{entries}{$state->{directory}.$data}{questionable} = 1;
2006-02-22 17:50:15 +08:00
}
# add \n
# Response expected: yes. Add a file or directory. This uses any previous
# Argument, Directory, Entry, or Modified requests, if they have been sent.
# The last Directory sent specifies the working directory at the time of
# the operation. To add a directory, send the directory to be added using
# Directory and Argument requests.
sub req_add
{
my ( $cmd, $data ) = @_;
argsplit("add");
2007-04-12 06:51:33 +08:00
my $updater = GITCVS::updater->new($state->{CVSROOT}, $state->{module}, $log);
$updater->update();
2006-02-22 17:50:15 +08:00
my $addcount = 0;
foreach my $filename ( @{$state->{args}} )
{
$filename = filecleanup($filename);
2012-10-14 13:42:31 +08:00
# no -r, -A, or -D with add
my $stickyInfo = resolveStickyInfo($filename);
my $meta = $updater->getmeta($filename,$stickyInfo);
2007-04-12 06:51:33 +08:00
my $wrev = revparse($filename);
2012-10-14 13:42:21 +08:00
if ($wrev && $meta && ($wrev=~/^-/))
2007-04-12 06:51:33 +08:00
{
# previously removed file, add back
2012-10-14 13:42:21 +08:00
$log->info("added file $filename was previously removed, send $meta->{revision}");
2007-04-12 06:51:33 +08:00
print "MT +updated\n";
print "MT text U \n";
print "MT fname $filename\n";
print "MT newline\n";
print "MT -updated\n";
unless ( $state->{globaloptions}{-n} )
{
my ( $filepart, $dirpart ) = filenamesplit($filename,1);
print "Created $dirpart\n";
print $state->{CVSROOT} . "/$state->{module}/$filename\n";
# this is an "entries" line
2008-05-15 12:35:48 +08:00
my $kopts = kopts_from_path($filename,"sha1",$meta->{filehash});
2012-10-14 13:42:31 +08:00
my $entryLine = "/$filepart/$meta->{revision}//$kopts/";
$entryLine .= getStickyTagOrDate($stickyInfo);
$log->debug($entryLine);
print "$entryLine\n";
2007-04-12 06:51:33 +08:00
# permissions
$log->debug("SEND : u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}");
print "u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}\n";
# transmit file
transmitfile($meta->{filehash});
}
next;
}
2006-02-22 17:50:15 +08:00
unless ( defined ( $state->{entries}{$filename}{modified_filename} ) )
{
print "E cvs add: nothing known about `$filename'\n";
next;
}
# TODO : check we're not squashing an already existing file
if ( defined ( $state->{entries}{$filename}{revision} ) )
{
print "E cvs add: `$filename' has already been entered\n";
next;
}
2006-03-27 11:51:42 +08:00
my ( $filepart, $dirpart ) = filenamesplit($filename, 1);
2006-02-22 17:50:15 +08:00
print "E cvs add: scheduling file `$filename' for addition\n";
print "Checked-in $dirpart\n";
print "$filename\n";
2008-05-15 12:35:48 +08:00
my $kopts = kopts_from_path($filename,"file",
$state->{entries}{$filename}{modified_filename});
2012-10-14 13:42:31 +08:00
print "/$filepart/0//$kopts/" .
getStickyTagOrDate($stickyInfo) . "\n";
2006-02-22 17:50:15 +08:00
2008-05-15 12:35:47 +08:00
my $requestedKopts = $state->{opt}{k};
if(defined($requestedKopts))
{
$requestedKopts = "-k$requestedKopts";
}
else
{
$requestedKopts = "";
}
if( $kopts ne $requestedKopts )
{
$log->warn("Ignoring requested -k='$requestedKopts'"
. " for '$filename'; detected -k='$kopts' instead");
#TODO: Also have option to send warning to user?
}
2006-02-22 17:50:15 +08:00
$addcount++;
}
if ( $addcount == 1 )
{
print "E cvs add: use `cvs commit' to add this file permanently\n";
}
elsif ( $addcount > 1 )
{
print "E cvs add: use `cvs commit' to add these files permanently\n";
}
print "ok\n";
}
# remove \n
# Response expected: yes. Remove a file. This uses any previous Argument,
# Directory, Entry, or Modified requests, if they have been sent. The last
# Directory sent specifies the working directory at the time of the
# operation. Note that this request does not actually do anything to the
# repository; the only effect of a successful remove request is to supply
# the client with a new entries line containing `-' to indicate a removed
# file. In fact, the client probably could perform this operation without
# contacting the server, although using remove may cause the server to
# perform a few more checks. The client sends a subsequent ci request to
# actually record the removal in the repository.
sub req_remove
{
my ( $cmd, $data ) = @_;
argsplit("remove");
# Grab a handle to the SQLite db and do any necessary updates
my $updater = GITCVS::updater->new($state->{CVSROOT}, $state->{module}, $log);
$updater->update();
#$log->debug("add state : " . Dumper($state));
my $rmcount = 0;
foreach my $filename ( @{$state->{args}} )
{
$filename = filecleanup($filename);
if ( defined ( $state->{entries}{$filename}{unchanged} ) or defined ( $state->{entries}{$filename}{modified_filename} ) )
{
print "E cvs remove: file `$filename' still in working directory\n";
next;
}
2012-10-14 13:42:31 +08:00
# only from entries
my $stickyInfo = resolveStickyInfo($filename);
my $meta = $updater->getmeta($filename,$stickyInfo);
2006-02-22 17:50:15 +08:00
my $wrev = revparse($filename);
unless ( defined ( $wrev ) )
{
print "E cvs remove: nothing known about `$filename'\n";
next;
}
2012-10-14 13:42:21 +08:00
if ( defined($wrev) and ($wrev=~/^-/) )
2006-02-22 17:50:15 +08:00
{
print "E cvs remove: file `$filename' already scheduled for removal\n";
next;
}
2012-10-14 13:42:21 +08:00
unless ( $wrev eq $meta->{revision} )
2006-02-22 17:50:15 +08:00
{
# TODO : not sure if the format of this message is quite correct.
print "E cvs remove: Up to date check failed for `$filename'\n";
next;
}
2006-03-27 11:51:42 +08:00
my ( $filepart, $dirpart ) = filenamesplit($filename, 1);
2006-02-22 17:50:15 +08:00
print "E cvs remove: scheduling `$filename' for removal\n";
print "Checked-in $dirpart\n";
print "$filename\n";
2008-05-15 12:35:48 +08:00
my $kopts = kopts_from_path($filename,"sha1",$meta->{filehash});
2012-10-14 13:42:31 +08:00
print "/$filepart/-$wrev//$kopts/" . getStickyTagOrDate($stickyInfo) . "\n";
2006-02-22 17:50:15 +08:00
$rmcount++;
}
if ( $rmcount == 1 )
{
print "E cvs remove: use `cvs commit' to remove this file permanently\n";
}
elsif ( $rmcount > 1 )
{
print "E cvs remove: use `cvs commit' to remove these files permanently\n";
}
print "ok\n";
}
# Modified filename \n
# Response expected: no. Additional data: mode, \n, file transmission. Send
# the server a copy of one locally modified file. filename is a file within
# the most recent directory sent with Directory; it must not contain `/'.
# If the user is operating on only some files in a directory, only those
# files need to be included. This can also be sent without Entry, if there
# is no entry for the file.
sub req_Modified
{
my ( $cmd, $data ) = @_;
my $mode = <STDIN>;
2007-07-15 02:48:42 +08:00
defined $mode
or (print "E end of file reading mode for $data\n"), return;
2006-02-22 17:50:15 +08:00
chomp $mode;
my $size = <STDIN>;
2007-07-15 02:48:42 +08:00
defined $size
or (print "E end of file reading size of $data\n"), return;
2006-02-22 17:50:15 +08:00
chomp $size;
# Grab config information
my $blocksize = 8192;
my $bytesleft = $size;
my $tmp;
# Get a filehandle/name to write it to
my ( $fh, $filename ) = tempfile( DIR => $TEMP_DIR );
# Loop over file data writing out to temporary file.
while ( $bytesleft )
{
$blocksize = $bytesleft if ( $bytesleft < $blocksize );
read STDIN, $tmp, $blocksize;
print $fh $tmp;
$bytesleft -= $blocksize;
}
2007-07-15 02:48:42 +08:00
close $fh
or (print "E failed to write temporary, $filename: $!\n"), return;
2006-02-22 17:50:15 +08:00
# Ensure we have something sensible for the file mode
if ( $mode =~ /u=(\w+)/ )
{
$mode = $1;
} else {
$mode = "rw";
}
# Save the file data in $state
$state->{entries}{$state->{directory}.$data}{modified_filename} = $filename;
$state->{entries}{$state->{directory}.$data}{modified_mode} = $mode;
2017-09-11 13:45:09 +08:00
$state->{entries}{$state->{directory}.$data}{modified_hash} = safe_pipe_capture('git','hash-object',$filename);
2006-02-22 17:50:15 +08:00
$state->{entries}{$state->{directory}.$data}{modified_hash} =~ s/\s.*$//s;
#$log->debug("req_Modified : file=$data mode=$mode size=$size");
}
# Unchanged filename \n
# Response expected: no. Tell the server that filename has not been
# modified in the checked out directory. The filename is a file within the
# most recent directory sent with Directory; it must not contain `/'.
sub req_Unchanged
{
my ( $cmd, $data ) = @_;
$state->{entries}{$state->{directory}.$data}{unchanged} = 1;
#$log->debug("req_Unchanged : $data");
}
# Argument text \n
# Response expected: no. Save argument for use in a subsequent command.
# Arguments accumulate until an argument-using command is given, at which
# point they are forgotten.
# Argumentx text \n
# Response expected: no. Append \n followed by text to the current argument
# being saved.
sub req_Argument
{
my ( $cmd, $data ) = @_;
2006-07-27 03:59:08 +08:00
# Argumentx means: append to last Argument (with a newline in front)
2006-02-22 17:50:15 +08:00
$log->debug("$cmd : $data");
2006-07-27 03:59:08 +08:00
if ( $cmd eq 'Argumentx') {
${$state->{arguments}}[$#{$state->{arguments}}] .= "\n" . $data;
} else {
push @{$state->{arguments}}, $data;
}
2006-02-22 17:50:15 +08:00
}
# expand-modules \n
# Response expected: yes. Expand the modules which are specified in the
# arguments. Returns the data in Module-expansion responses. Note that the
# server can assume that this is checkout or export, not rtag or rdiff; the
# latter do not access the working directory and thus have no need to
# expand modules on the client side. Expand may not be the best word for
# what this request does. It does not necessarily tell you all the files
# contained in a module, for example. Basically it is a way of telling you
# which working directories the server needs to know about in order to
# handle a checkout of the specified modules. For example, suppose that the
# server has a module defined by
# aliasmodule -a 1dir
# That is, one can check out aliasmodule and it will take 1dir in the
# repository and check it out to 1dir in the working directory. Now suppose
# the client already has this module checked out and is planning on using
# the co request to update it. Without using expand-modules, the client
# would have two bad choices: it could either send information about all
# working directories under the current directory, which could be
# unnecessarily slow, or it could be ignorant of the fact that aliasmodule
# stands for 1dir, and neglect to send information for 1dir, which would
# lead to incorrect operation. With expand-modules, the client would first
# ask for the module to be expanded:
sub req_expandmodules
{
my ( $cmd, $data ) = @_;
argsplit();
$log->debug("req_expandmodules : " . ( defined($data) ? $data : "[NULL]" ) );
unless ( ref $state->{arguments} eq "ARRAY" )
{
print "ok\n";
return;
}
foreach my $module ( @{$state->{arguments}} )
{
$log->debug("SEND : Module-expansion $module");
print "Module-expansion $module\n";
}
print "ok\n";
statecleanup();
}
# co \n
# Response expected: yes. Get files from the repository. This uses any
# previous Argument, Directory, Entry, or Modified requests, if they have
# been sent. Arguments to this command are module names; the client cannot
# know what directories they correspond to except by (1) just sending the
# co request, and then seeing what directory names the server sends back in
# its responses, and (2) the expand-modules request.
sub req_co
{
my ( $cmd, $data ) = @_;
argsplit("co");
2008-07-18 01:00:29 +08:00
# Provide list of modules, if -c was used.
if (exists $state->{opt}{c}) {
2017-09-11 13:45:54 +08:00
my $showref = safe_pipe_capture(qw(git show-ref --heads));
2008-07-18 01:00:29 +08:00
for my $line (split '\n', $showref) {
if ( $line =~ m% refs/heads/(.*)$% ) {
print "M $1\t$1\n";
}
}
print "ok\n";
return 1;
}
2012-10-14 13:42:31 +08:00
my $stickyInfo = { 'tag' => $state->{opt}{r},
'date' => $state->{opt}{D} };
2006-02-22 17:50:15 +08:00
my $module = $state->{args}[0];
2008-05-15 12:35:47 +08:00
$state->{module} = $module;
2006-02-22 17:50:15 +08:00
my $checkout_path = $module;
# use the user specified directory if we're given it
$checkout_path = $state->{opt}{d} if ( exists ( $state->{opt}{d} ) );
$log->debug("req_co : " . ( defined($data) ? $data : "[NULL]" ) );
$log->info("Checking out module '$module' ($state->{CVSROOT}) to '$checkout_path'");
$ENV{GIT_DIR} = $state->{CVSROOT} . "/";
# Grab a handle to the SQLite db and do any necessary updates
my $updater = GITCVS::updater->new($state->{CVSROOT}, $module, $log);
$updater->update();
2012-10-14 13:42:31 +08:00
my $headHash;
if( defined($stickyInfo) && defined($stickyInfo->{tag}) )
{
$headHash = $updater->lookupCommitRef($stickyInfo->{tag});
if( !defined($headHash) )
{
print "error 1 no such tag `$stickyInfo->{tag}'\n";
cleanupWorkTree();
exit;
}
}
2006-03-02 08:58:57 +08:00
2012-10-14 13:42:31 +08:00
$checkout_path =~ s|/$||; # get rid of trailing slashes
2006-03-02 08:58:57 +08:00
my %seendirs = ();
2006-03-03 11:38:03 +08:00
my $lastdir ='';
2006-02-22 17:50:15 +08:00
2012-10-14 13:42:31 +08:00
prepDirForOutput(
".",
$state->{CVSROOT} . "/$module",
$checkout_path,
\%seendirs,
'checkout',
$state->{dirArgs} );
foreach my $git ( @{$updater->getAnyHead($headHash)} )
2006-02-22 17:50:15 +08:00
{
# Don't want to check out deleted files
next if ( $git->{filehash} eq "deleted" );
2008-05-15 12:35:47 +08:00
my $fullName = $git->{name};
2006-02-22 17:50:15 +08:00
( $git->{name}, $git->{dir} ) = filenamesplit($git->{name});
2012-10-14 13:42:31 +08:00
unless (exists($seendirs{$git->{dir}})) {
prepDirForOutput($git->{dir}, $state->{CVSROOT} . "/$module/",
$checkout_path, \%seendirs, 'checkout',
$state->{dirArgs} );
$lastdir = $git->{dir};
$seendirs{$git->{dir}} = 1;
}
2006-03-04 12:47:29 +08:00
2006-02-22 17:50:15 +08:00
# modification time of this file
print "Mod-time $git->{modified}\n";
# print some information to the client
if ( defined ( $git->{dir} ) and $git->{dir} ne "./" )
{
2006-03-02 08:58:57 +08:00
print "M U $checkout_path/$git->{dir}$git->{name}\n";
2006-02-22 17:50:15 +08:00
} else {
2006-03-02 08:58:57 +08:00
print "M U $checkout_path/$git->{name}\n";
2006-02-22 17:50:15 +08:00
}
2006-03-02 08:58:57 +08:00
2006-03-04 12:47:29 +08:00
# instruct client we're sending a file to put in this path
print "Created $checkout_path/" . ( defined ( $git->{dir} ) and $git->{dir} ne "./" ? $git->{dir} . "/" : "" ) . "\n";
2006-02-22 17:50:15 +08:00
2006-03-04 12:47:29 +08:00
print $state->{CVSROOT} . "/$module/" . ( defined ( $git->{dir} ) and $git->{dir} ne "./" ? $git->{dir} . "/" : "" ) . "$git->{name}\n";
2006-02-22 17:50:15 +08:00
# this is an "entries" line
2008-05-15 12:35:48 +08:00
my $kopts = kopts_from_path($fullName,"sha1",$git->{filehash});
2012-10-14 13:42:31 +08:00
print "/$git->{name}/$git->{revision}//$kopts/" .
getStickyTagOrDate($stickyInfo) . "\n";
2006-02-22 17:50:15 +08:00
# permissions
print "u=$git->{mode},g=$git->{mode},o=$git->{mode}\n";
# transmit file
transmitfile($git->{filehash});
}
print "ok\n";
statecleanup();
}
2012-10-14 13:42:31 +08:00
# used by req_co and req_update to set up directories for files
# recursively handles parents
sub prepDirForOutput
{
my ($dir, $repodir, $remotedir, $seendirs, $request, $dirArgs) = @_;
my $parent = dirname($dir);
$dir =~ s|/+$||;
$repodir =~ s|/+$||;
$remotedir =~ s|/+$||;
$parent =~ s|/+$||;
if ($parent eq '.' || $parent eq './')
{
$parent = '';
}
# recurse to announce unseen parents first
if( length($parent) &&
!exists($seendirs->{$parent}) &&
( $request eq "checkout" ||
exists($dirArgs->{$parent}) ) )
{
prepDirForOutput($parent, $repodir, $remotedir,
$seendirs, $request, $dirArgs);
}
# Announce that we are going to modify at the parent level
if ($dir eq '.' || $dir eq './')
{
$dir = '';
}
if(exists($seendirs->{$dir}))
{
return;
}
$log->debug("announcedir $dir, $repodir, $remotedir" );
my($thisRemoteDir,$thisRepoDir);
if ($dir ne "")
{
$thisRepoDir="$repodir/$dir";
if($remotedir eq ".")
{
$thisRemoteDir=$dir;
}
else
{
$thisRemoteDir="$remotedir/$dir";
}
}
else
{
$thisRepoDir=$repodir;
$thisRemoteDir=$remotedir;
}
unless ( $state->{globaloptions}{-Q} || $state->{globaloptions}{-q} )
{
print "E cvs $request: Updating $thisRemoteDir\n";
}
my ($opt_r)=$state->{opt}{r};
my $stickyInfo;
if(exists($state->{opt}{A}))
{
# $stickyInfo=undef;
}
elsif( defined($opt_r) && $opt_r ne "" )
# || ( defined($state->{opt}{D}) && $state->{opt}{D} ne "" ) # TODO
{
$stickyInfo={ 'tag' => (defined($opt_r)?$opt_r:undef) };
# TODO: Convert -D value into the form 2011.04.10.04.46.57,
# similar to an entry line's sticky date, without the D prefix.
# It sometimes (always?) arrives as something more like
# '10 Apr 2011 04:46:57 -0000'...
# $stickyInfo={ 'date' => (defined($stickyDate)?$stickyDate:undef) };
}
else
{
$stickyInfo=getDirStickyInfo($state->{prependdir} . $dir);
}
my $stickyResponse;
if(defined($stickyInfo))
{
$stickyResponse = "Set-sticky $thisRemoteDir/\n" .
"$thisRepoDir/\n" .
getStickyTagOrDate($stickyInfo) . "\n";
}
else
{
$stickyResponse = "Clear-sticky $thisRemoteDir/\n" .
"$thisRepoDir/\n";
}
unless ( $state->{globaloptions}{-n} )
{
print $stickyResponse;
print "Clear-static-directory $thisRemoteDir/\n";
print "$thisRepoDir/\n";
print $stickyResponse; # yes, twice
print "Template $thisRemoteDir/\n";
print "$thisRepoDir/\n";
print "0\n";
}
$seendirs->{$dir} = 1;
# FUTURE: This would more accurately emulate CVS by sending
# another copy of sticky after processing the files in that
# directory. Or intermediate: perhaps send all sticky's for
2016-05-06 20:36:46 +08:00
# $seendirs after processing all files.
2012-10-14 13:42:31 +08:00
}
2006-02-22 17:50:15 +08:00
# update \n
# Response expected: yes. Actually do a cvs update command. This uses any
# previous Argument, Directory, Entry, or Modified requests, if they have
# been sent. The last Directory sent specifies the working directory at the
# time of the operation. The -I option is not used--files which the client
# can decide whether to ignore are not mentioned and the client sends the
# Questionable request for others.
sub req_update
{
my ( $cmd, $data ) = @_;
$log->debug("req_update : " . ( defined($data) ? $data : "[NULL]" ));
argsplit("update");
2006-03-01 15:03:58 +08:00
#
2006-04-26 14:59:28 +08:00
# It may just be a client exploring the available heads/modules
2006-03-01 15:03:58 +08:00
# in that case, list them as top level directories and leave it
# at that. Eclipse uses this technique to offer you a list of
# projects (heads in this case) to checkout.
#
if ($state->{module} eq '') {
2017-09-11 13:45:54 +08:00
my $showref = safe_pipe_capture(qw(git show-ref --heads));
2006-03-01 15:03:58 +08:00
print "E cvs update: Updating .\n";
2008-07-18 01:00:27 +08:00
for my $line (split '\n', $showref) {
if ( $line =~ m% refs/heads/(.*)$% ) {
print "E cvs update: New directory `$1'\n";
}
}
print "ok\n";
return 1;
2006-03-01 15:03:58 +08:00
}
2006-02-22 17:50:15 +08:00
# Grab a handle to the SQLite db and do any necessary updates
my $updater = GITCVS::updater->new($state->{CVSROOT}, $state->{module}, $log);
$updater->update();
2006-03-27 11:51:42 +08:00
argsfromdir($updater);
2006-02-22 17:50:15 +08:00
#$log->debug("update state : " . Dumper($state));
2012-10-14 13:42:31 +08:00
my($repoDir);
$repoDir=$state->{CVSROOT} . "/$state->{module}/$state->{prependdir}";
my %seendirs = ();
2009-12-07 19:11:44 +08:00
2006-07-09 15:44:30 +08:00
# foreach file specified on the command line ...
2012-10-14 13:42:31 +08:00
foreach my $argsFilename ( @{$state->{args}} )
2006-02-22 17:50:15 +08:00
{
2012-10-14 13:42:31 +08:00
my $filename;
$filename = filecleanup($argsFilename);
2006-02-22 17:50:15 +08:00
2006-03-27 11:51:42 +08:00
$log->debug("Processing file $filename");
2006-02-22 17:50:15 +08:00
# if we have a -C we should pretend we never saw modified stuff
if ( exists ( $state->{opt}{C} ) )
{
delete $state->{entries}{$filename}{modified_hash};
delete $state->{entries}{$filename}{modified_filename};
$state->{entries}{$filename}{unchanged} = 1;
}
2012-10-14 13:42:31 +08:00
my $stickyInfo = resolveStickyInfo($filename,
$state->{opt}{r},
$state->{opt}{D},
exists($state->{opt}{A}));
my $meta = $updater->getmeta($filename, $stickyInfo);
2006-02-22 17:50:15 +08:00
2008-03-28 06:18:12 +08:00
# If -p was given, "print" the contents of the requested revision.
if ( exists ( $state->{opt}{p} ) ) {
if ( defined ( $meta->{revision} ) ) {
$log->info("Printing '$filename' revision " . $meta->{revision});
transmitfile($meta->{filehash}, { print => 1 });
}
next;
}
2012-10-14 13:42:31 +08:00
# Directories:
prepDirForOutput(
dirname($argsFilename),
$repoDir,
".",
\%seendirs,
"update",
$state->{dirArgs} );
my $wrev = revparse($filename);
2006-10-11 06:20:43 +08:00
if ( ! defined $meta )
{
$meta = {
name => $filename,
2012-10-14 13:42:21 +08:00
revision => '0',
2006-10-11 06:20:43 +08:00
filehash => 'added'
};
2012-10-14 13:42:31 +08:00
if($wrev ne "0")
{
$meta->{filehash}='deleted';
}
2006-10-11 06:20:43 +08:00
}
2006-02-22 17:50:15 +08:00
my $oldmeta = $meta;
# If the working copy is an old revision, lets get that version too for comparison.
2012-10-14 13:42:31 +08:00
my $oldWrev=$wrev;
if(defined($oldWrev))
2006-02-22 17:50:15 +08:00
{
2012-10-14 13:42:31 +08:00
$oldWrev=~s/^-//;
if($oldWrev ne $meta->{revision})
{
$oldmeta = $updater->getmeta($filename, $oldWrev);
}
2006-02-22 17:50:15 +08:00
}
#$log->debug("Target revision is $meta->{revision}, current working revision is $wrev");
2006-03-02 13:42:01 +08:00
# Files are up to date if the working copy and repo copy have the same revision,
# and the working copy is unmodified _and_ the user hasn't specified -C
next if ( defined ( $wrev )
and defined($meta->{revision})
2012-10-14 13:42:21 +08:00
and $wrev eq $meta->{revision}
2006-03-02 13:42:01 +08:00
and $state->{entries}{$filename}{unchanged}
and not exists ( $state->{opt}{C} ) );
# If the working copy and repo copy have the same revision,
# but the working copy is modified, tell the client it's modified
if ( defined ( $wrev )
and defined($meta->{revision})
2012-10-14 13:42:21 +08:00
and $wrev eq $meta->{revision}
2012-10-14 13:42:31 +08:00
and $wrev ne "0"
2007-04-12 04:38:19 +08:00
and defined($state->{entries}{$filename}{modified_hash})
2006-03-02 13:42:01 +08:00
and not exists ( $state->{opt}{C} ) )
{
$log->info("Tell the client the file is modified");
2006-10-11 06:20:43 +08:00
print "MT text M \n";
2006-03-02 13:42:01 +08:00
print "MT fname $filename\n";
print "MT newline\n";
next;
}
2006-02-22 17:50:15 +08:00
2012-10-14 13:42:31 +08:00
if ( $meta->{filehash} eq "deleted" && $wrev ne "0" )
2006-02-22 17:50:15 +08:00
{
2012-10-14 13:42:17 +08:00
# TODO: If it has been modified in the sandbox, error out
# with the appropriate message, rather than deleting a modified
# file.
2006-03-27 11:51:42 +08:00
my ( $filepart, $dirpart ) = filenamesplit($filename,1);
2006-02-22 17:50:15 +08:00
$log->info("Removing '$filename' from working copy (no longer in the repo)");
print "E cvs update: `$filename' is no longer in the repository\n";
2006-03-27 11:51:42 +08:00
# Don't want to actually _DO_ the update if -n specified
unless ( $state->{globaloptions}{-n} ) {
print "Removed $dirpart\n";
print "$filepart\n";
}
2006-02-22 17:50:15 +08:00
}
2006-03-02 13:42:01 +08:00
elsif ( not defined ( $state->{entries}{$filename}{modified_hash} )
2006-10-11 06:20:43 +08:00
or $state->{entries}{$filename}{modified_hash} eq $oldmeta->{filehash}
or $meta->{filehash} eq 'added' )
2006-02-22 17:50:15 +08:00
{
2006-10-11 06:20:43 +08:00
# normal update, just send the new revision (either U=Update,
# or A=Add, or R=Remove)
2012-10-14 13:42:21 +08:00
if ( defined($wrev) && ($wrev=~/^-/) )
2006-10-11 06:20:43 +08:00
{
$log->info("Tell the client the file is scheduled for removal");
print "MT text R \n";
print "MT fname $filename\n";
print "MT newline\n";
next;
}
2012-10-14 13:42:21 +08:00
elsif ( (!defined($wrev) || $wrev eq '0') &&
(!defined($meta->{revision}) || $meta->{revision} eq '0') )
2006-10-11 06:20:43 +08:00
{
2007-01-22 18:56:27 +08:00
$log->info("Tell the client the file is scheduled for addition");
2006-10-11 06:20:43 +08:00
print "MT text A \n";
print "MT fname $filename\n";
print "MT newline\n";
next;
}
else {
2012-10-14 13:42:21 +08:00
$log->info("UpdatingX3 '$filename' to ".$meta->{revision});
2006-10-11 06:20:43 +08:00
print "MT +updated\n";
print "MT text U \n";
print "MT fname $filename\n";
print "MT newline\n";
print "MT -updated\n";
}
2006-02-22 17:50:15 +08:00
2006-03-27 11:51:42 +08:00
my ( $filepart, $dirpart ) = filenamesplit($filename,1);
# Don't want to actually _DO_ the update if -n specified
unless ( $state->{globaloptions}{-n} )
{
if ( defined ( $wrev ) )
{
# instruct client we're sending a file to put in this path as a replacement
print "Update-existing $dirpart\n";
$log->debug("Updating existing file 'Update-existing $dirpart'");
} else {
# instruct client we're sending a file to put in this path as a new file
$log->debug("Creating new file 'Created $dirpart'");
print "Created $dirpart\n";
}
print $state->{CVSROOT} . "/$state->{module}/$filename\n";
# this is an "entries" line
2008-05-15 12:35:48 +08:00
my $kopts = kopts_from_path($filename,"sha1",$meta->{filehash});
2012-10-14 13:42:31 +08:00
my $entriesLine = "/$filepart/$meta->{revision}//$kopts/";
$entriesLine .= getStickyTagOrDate($stickyInfo);
$log->debug($entriesLine);
print "$entriesLine\n";
2006-03-27 11:51:42 +08:00
# permissions
$log->debug("SEND : u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}");
print "u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}\n";
# transmit file
transmitfile($meta->{filehash});
}
2006-02-22 17:50:15 +08:00
} else {
2006-03-27 11:51:42 +08:00
my ( $filepart, $dirpart ) = filenamesplit($meta->{name},1);
2006-02-22 17:50:15 +08:00
2008-05-15 12:35:46 +08:00
my $mergeDir = setupTmpDir();
2006-02-22 17:50:15 +08:00
my $file_local = $filepart . ".mine";
2008-05-15 12:35:46 +08:00
my $mergedFile = "$mergeDir/$file_local";
2006-02-22 17:50:15 +08:00
system("ln","-s",$state->{entries}{$filename}{modified_filename}, $file_local);
my $file_old = $filepart . "." . $oldmeta->{revision};
2008-03-28 06:18:12 +08:00
transmitfile($oldmeta->{filehash}, { targetfile => $file_old });
2006-02-22 17:50:15 +08:00
my $file_new = $filepart . "." . $meta->{revision};
2008-03-28 06:18:12 +08:00
transmitfile($meta->{filehash}, { targetfile => $file_new });
2006-02-22 17:50:15 +08:00
# we need to merge with the local changes ( M=successful merge, C=conflict merge )
$log->info("Merging $file_local, $file_old, $file_new");
2012-10-14 13:42:21 +08:00
print "M Merging differences between $oldmeta->{revision} and $meta->{revision} into $filename\n";
2006-02-22 17:50:15 +08:00
2008-05-15 12:35:46 +08:00
$log->debug("Temporary directory for merge is $mergeDir");
2006-02-22 17:50:15 +08:00
2006-12-20 06:58:20 +08:00
my $return = system("git", "merge-file", $file_local, $file_old, $file_new);
2006-02-22 17:50:15 +08:00
$return >>= 8;
2008-05-15 12:35:46 +08:00
cleanupTmpDir();
2006-02-22 17:50:15 +08:00
if ( $return == 0 )
{
$log->info("Merged successfully");
print "M M $filename\n";
2007-03-06 17:42:24 +08:00
$log->debug("Merged $dirpart");
2006-03-27 11:51:42 +08:00
# Don't want to actually _DO_ the update if -n specified
unless ( $state->{globaloptions}{-n} )
{
2007-03-06 17:42:24 +08:00
print "Merged $dirpart\n";
2006-03-27 11:51:42 +08:00
$log->debug($state->{CVSROOT} . "/$state->{module}/$filename");
print $state->{CVSROOT} . "/$state->{module}/$filename\n";
2008-05-15 12:35:48 +08:00
my $kopts = kopts_from_path("$dirpart/$filepart",
"file",$mergedFile);
2012-10-14 13:42:21 +08:00
$log->debug("/$filepart/$meta->{revision}//$kopts/");
2012-10-14 13:42:31 +08:00
my $entriesLine="/$filepart/$meta->{revision}//$kopts/";
$entriesLine .= getStickyTagOrDate($stickyInfo);
print "$entriesLine\n";
2006-03-27 11:51:42 +08:00
}
2006-02-22 17:50:15 +08:00
}
elsif ( $return == 1 )
{
$log->info("Merged with conflicts");
2007-03-14 01:25:22 +08:00
print "E cvs update: conflicts found in $filename\n";
2006-02-22 17:50:15 +08:00
print "M C $filename\n";
2006-03-27 11:51:42 +08:00
# Don't want to actually _DO_ the update if -n specified
unless ( $state->{globaloptions}{-n} )
{
2007-03-06 17:42:24 +08:00
print "Merged $dirpart\n";
2006-03-27 11:51:42 +08:00
print $state->{CVSROOT} . "/$state->{module}/$filename\n";
2008-05-15 12:35:48 +08:00
my $kopts = kopts_from_path("$dirpart/$filepart",
"file",$mergedFile);
2012-10-14 13:42:31 +08:00
my $entriesLine = "/$filepart/$meta->{revision}/+/$kopts/";
$entriesLine .= getStickyTagOrDate($stickyInfo);
print "$entriesLine\n";
2006-03-27 11:51:42 +08:00
}
2006-02-22 17:50:15 +08:00
}
else
{
$log->warn("Merge failed");
next;
}
2006-03-27 11:51:42 +08:00
# Don't want to actually _DO_ the update if -n specified
unless ( $state->{globaloptions}{-n} )
{
# permissions
$log->debug("SEND : u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}");
print "u=$meta->{mode},g=$meta->{mode},o=$meta->{mode}\n";
# transmit file, format is single integer on a line by itself (file
# size) followed by the file contents
# TODO : we should copy files in blocks
2017-09-11 13:45:09 +08:00
my $data = safe_pipe_capture('cat', $mergedFile);
2006-03-27 11:51:42 +08:00
$log->debug("File size : " . length($data));
print length($data) . "\n";
print $data;
}
2006-02-22 17:50:15 +08:00
}
}
2012-10-14 13:42:31 +08:00
# prepDirForOutput() any other existing directories unless they already
# have the right sticky tag:
unless ( $state->{globaloptions}{n} )
{
my $dir;
foreach $dir (keys(%{$state->{dirMap}}))
{
if( ! $seendirs{$dir} &&
exists($state->{dirArgs}{$dir}) )
{
my($oldTag);
$oldTag=$state->{dirMap}{$dir}{tagspec};
unless( ( exists($state->{opt}{A}) &&
defined($oldTag) ) ||
( defined($state->{opt}{r}) &&
( !defined($oldTag) ||
$state->{opt}{r} ne $oldTag ) ) )
# TODO?: OR sticky dir is different...
{
next;
}
prepDirForOutput(
$dir,
$repoDir,
".",
\%seendirs,
'update',
$state->{dirArgs} );
}
# TODO?: Consider sending a final duplicate Sticky response
# to more closely mimic real CVS.
}
}
2006-02-22 17:50:15 +08:00
print "ok\n";
}
sub req_ci
{
my ( $cmd, $data ) = @_;
argsplit("ci");
#$log->debug("State : " . Dumper($state));
$log->info("req_ci : " . ( defined($data) ? $data : "[NULL]" ));
2010-05-15 23:06:46 +08:00
if ( $state->{method} eq 'pserver' and $state->{user} eq 'anonymous' )
2006-03-04 15:30:04 +08:00
{
2010-05-15 23:06:46 +08:00
print "error 1 anonymous user cannot commit via pserver\n";
2008-05-15 12:35:46 +08:00
cleanupWorkTree();
2006-03-04 15:30:04 +08:00
exit;
}
2006-02-22 17:50:15 +08:00
if ( -e $state->{CVSROOT} . "/index" )
{
2006-03-17 08:33:19 +08:00
$log->warn("file 'index' already exists in the git repository");
2006-02-22 17:50:15 +08:00
print "error 1 Index already exists in git repo\n";
2008-05-15 12:35:46 +08:00
cleanupWorkTree();
2006-02-22 17:50:15 +08:00
exit;
}
# Grab a handle to the SQLite db and do any necessary updates
my $updater = GITCVS::updater->new($state->{CVSROOT}, $state->{module}, $log);
$updater->update();
my @committedfiles = ();
2007-03-14 01:25:23 +08:00
my %oldmeta;
2012-10-14 13:42:31 +08:00
my $stickyInfo;
my $branchRef;
my $parenthash;
2006-02-22 17:50:15 +08:00
2006-07-09 15:44:30 +08:00
# foreach file specified on the command line ...
2006-02-22 17:50:15 +08:00
foreach my $filename ( @{$state->{args}} )
{
2006-03-27 11:51:42 +08:00
my $committedfile = $filename;
2006-02-22 17:50:15 +08:00
$filename = filecleanup($filename);
next unless ( exists $state->{entries}{$filename}{modified_filename} or not $state->{entries}{$filename}{unchanged} );
2012-10-14 13:42:31 +08:00
#####
# Figure out which branch and parenthash we are committing
# to, and setup worktree:
# should always come from entries:
my $fileStickyInfo = resolveStickyInfo($filename);
if( !defined($branchRef) )
{
$stickyInfo = $fileStickyInfo;
if( defined($stickyInfo) &&
( defined($stickyInfo->{date}) ||
!defined($stickyInfo->{tag}) ) )
{
print "error 1 cannot commit with sticky date for file `$filename'\n";
cleanupWorkTree();
exit;
}
$branchRef = "refs/heads/$state->{module}";
if ( defined($stickyInfo) && defined($stickyInfo->{tag}) )
{
$branchRef = "refs/heads/$stickyInfo->{tag}";
}
2017-09-11 13:45:09 +08:00
$parenthash = safe_pipe_capture('git', 'show-ref', '-s', $branchRef);
2012-10-14 13:42:31 +08:00
chomp $parenthash;
2020-06-23 02:04:16 +08:00
if ($parenthash !~ /^[0-9a-f]{$state->{hexsz}}$/)
2012-10-14 13:42:31 +08:00
{
if ( defined($stickyInfo) && defined($stickyInfo->{tag}) )
{
print "error 1 sticky tag `$stickyInfo->{tag}' for file `$filename' is not a branch\n";
}
else
{
print "error 1 pserver cannot find the current HEAD of module";
}
cleanupWorkTree();
exit;
}
setupWorkTree($parenthash);
$log->info("Lockless commit start, basing commit on '$work->{workDir}', index file is '$work->{index}'");
$log->info("Created index '$work->{index}' for head $state->{module} - exit status $?");
}
elsif( !refHashEqual($stickyInfo,$fileStickyInfo) )
{
#TODO: We could split the cvs commit into multiple
# git commits by distinct stickyTag values, but that
# is lowish priority.
print "error 1 Committing different files to different"
. " branches is not currently supported\n";
cleanupWorkTree();
exit;
}
#####
# Process this file:
my $meta = $updater->getmeta($filename,$stickyInfo);
2007-03-14 01:25:23 +08:00
$oldmeta{$filename} = $meta;
2006-02-22 17:50:15 +08:00
my $wrev = revparse($filename);
my ( $filepart, $dirpart ) = filenamesplit($filename);
2007-11-23 17:12:54 +08:00
# do a checkout of the file if it is part of this tree
2006-02-22 17:50:15 +08:00
if ($wrev) {
2009-09-02 17:23:10 +08:00
system('git', 'checkout-index', '-f', '-u', $filename);
2006-02-22 17:50:15 +08:00
unless ($? == 0) {
die "Error running git-checkout-index -f -u $filename : $!";
}
}
my $addflag = 0;
my $rmflag = 0;
2012-10-14 13:42:21 +08:00
$rmflag = 1 if ( defined($wrev) and ($wrev=~/^-/) );
2006-02-22 17:50:15 +08:00
$addflag = 1 unless ( -e $filename );
# Do up to date checking
2012-10-14 13:42:21 +08:00
unless ( $addflag or $wrev eq $meta->{revision} or
( $rmflag and $wrev eq "-$meta->{revision}" ) )
2006-02-22 17:50:15 +08:00
{
# fail everything if an up to date check fails
print "error 1 Up to date check failed for $filename\n";
2008-05-15 12:35:46 +08:00
cleanupWorkTree();
2006-02-22 17:50:15 +08:00
exit;
}
2006-03-27 11:51:42 +08:00
push @committedfiles, $committedfile;
2006-02-22 17:50:15 +08:00
$log->info("Committing $filename");
system("mkdir","-p",$dirpart) unless ( -d $dirpart );
unless ( $rmflag )
{
$log->debug("rename $state->{entries}{$filename}{modified_filename} $filename");
rename $state->{entries}{$filename}{modified_filename},$filename;
# Calculate modes to remove
my $invmode = "";
foreach ( qw (r w x) ) { $invmode .= $_ unless ( $state->{entries}{$filename}{modified_mode} =~ /$_/ ); }
$log->debug("chmod u+" . $state->{entries}{$filename}{modified_mode} . "-" . $invmode . " $filename");
system("chmod","u+" . $state->{entries}{$filename}{modified_mode} . "-" . $invmode, $filename);
}
if ( $rmflag )
{
$log->info("Removing file '$filename'");
unlink($filename);
2009-09-02 17:23:10 +08:00
system("git", "update-index", "--remove", $filename);
2006-02-22 17:50:15 +08:00
}
elsif ( $addflag )
{
$log->info("Adding file '$filename'");
2009-09-02 17:23:10 +08:00
system("git", "update-index", "--add", $filename);
2006-02-22 17:50:15 +08:00
} else {
2012-10-14 13:42:21 +08:00
$log->info("UpdatingX2 file '$filename'");
2009-09-02 17:23:10 +08:00
system("git", "update-index", $filename);
2006-02-22 17:50:15 +08:00
}
}
unless ( scalar(@committedfiles) > 0 )
{
print "E No files to commit\n";
print "ok\n";
2008-05-15 12:35:46 +08:00
cleanupWorkTree();
2006-02-22 17:50:15 +08:00
return;
}
2017-09-11 13:45:54 +08:00
my $treehash = safe_pipe_capture(qw(git write-tree));
2006-02-22 17:50:15 +08:00
chomp $treehash;
$log->debug("Treehash : $treehash, Parenthash : $parenthash");
# write our commit message out if we have one ...
my ( $msg_fh, $msg_filename ) = tempfile( DIR => $TEMP_DIR );
print $msg_fh $state->{opt}{m};# if ( exists ( $state->{opt}{m} ) );
2009-01-02 23:40:13 +08:00
if ( defined ( $cfg->{gitcvs}{commitmsgannotation} ) ) {
if ($cfg->{gitcvs}{commitmsgannotation} !~ /^\s*$/ ) {
print $msg_fh "\n\n".$cfg->{gitcvs}{commitmsgannotation}."\n"
}
} else {
print $msg_fh "\n\nvia git-CVS emulator\n";
}
2006-02-22 17:50:15 +08:00
close $msg_fh;
2017-09-11 13:45:09 +08:00
my $commithash = safe_pipe_capture('git', 'commit-tree', $treehash, '-p', $parenthash, '-F', $msg_filename);
2007-02-27 20:49:09 +08:00
chomp($commithash);
2006-02-22 17:50:15 +08:00
$log->info("Commit hash : $commithash");
2020-06-23 02:04:16 +08:00
unless ( $commithash =~ /[a-zA-Z0-9]{$state->{hexsz}}/ )
2006-02-22 17:50:15 +08:00
{
$log->warn("Commit failed (Invalid commit hash)");
print "error 1 Commit failed (unknown reason)\n";
2008-05-15 12:35:46 +08:00
cleanupWorkTree();
2006-02-22 17:50:15 +08:00
exit;
}
2007-11-23 17:12:54 +08:00
### Emulate git-receive-pack by running hooks/update
2012-10-14 13:42:31 +08:00
my @hook = ( $ENV{GIT_DIR}.'hooks/update', $branchRef,
2007-02-13 23:12:45 +08:00
$parenthash, $commithash );
2007-11-23 17:12:54 +08:00
if( -x $hook[0] ) {
unless( system( @hook ) == 0 )
2007-02-13 23:12:45 +08:00
{
$log->warn("Commit failed (update hook declined to update ref)");
print "error 1 Commit failed (update hook declined)\n";
2008-05-15 12:35:46 +08:00
cleanupWorkTree();
2007-02-13 23:12:45 +08:00
exit;
}
}
2007-11-23 17:12:54 +08:00
### Update the ref
2007-02-21 13:54:39 +08:00
if (system(qw(git update-ref -m), "cvsserver ci",
2012-10-14 13:42:31 +08:00
$branchRef, $commithash, $parenthash)) {
2007-02-21 13:54:39 +08:00
$log->warn("update-ref for $state->{module} failed.");
print "error 1 Cannot commit -- update first\n";
2008-05-15 12:35:46 +08:00
cleanupWorkTree();
2007-02-21 13:54:39 +08:00
exit;
}
2006-02-22 17:50:15 +08:00
2007-11-23 17:12:54 +08:00
### Emulate git-receive-pack by running hooks/post-receive
my $hook = $ENV{GIT_DIR}.'hooks/post-receive';
if( -x $hook ) {
open(my $pipe, "| $hook") || die "can't fork $!";
local $SIG{PIPE} = sub { die 'pipe broke' };
2012-10-14 13:42:31 +08:00
print $pipe "$parenthash $commithash $branchRef\n";
2007-11-23 17:12:54 +08:00
close $pipe || die "bad pipe: $! $?";
}
2009-01-30 05:58:02 +08:00
$updater->update();
2007-12-05 17:15:01 +08:00
### Then hooks/post-update
$hook = $ENV{GIT_DIR}.'hooks/post-update';
if (-x $hook) {
2012-10-14 13:42:31 +08:00
system($hook, $branchRef);
2007-12-05 17:15:01 +08:00
}
2006-07-09 15:44:30 +08:00
# foreach file specified on the command line ...
2006-02-22 17:50:15 +08:00
foreach my $filename ( @committedfiles )
{
$filename = filecleanup($filename);
2012-10-14 13:42:31 +08:00
my $meta = $updater->getmeta($filename,$stickyInfo);
2007-01-09 10:10:41 +08:00
unless (defined $meta->{revision}) {
2012-10-14 13:42:21 +08:00
$meta->{revision} = "1.1";
2007-01-09 10:10:41 +08:00
}
2006-02-22 17:50:15 +08:00
2006-03-27 11:51:42 +08:00
my ( $filepart, $dirpart ) = filenamesplit($filename, 1);
2006-02-22 17:50:15 +08:00
$log->debug("Checked-in $dirpart : $filename");
2007-03-14 01:25:23 +08:00
print "M $state->{CVSROOT}/$state->{module}/$filename,v <-- $dirpart$filepart\n";
2007-01-09 10:10:41 +08:00
if ( defined $meta->{filehash} && $meta->{filehash} eq "deleted" )
2006-02-22 17:50:15 +08:00
{
2012-10-14 13:42:21 +08:00
print "M new revision: delete; previous revision: $oldmeta{$filename}{revision}\n";
2006-02-22 17:50:15 +08:00
print "Remove-entry $dirpart\n";
print "$filename\n";
} else {
2012-10-14 13:42:21 +08:00
if ($meta->{revision} eq "1.1") {
2007-03-14 01:25:22 +08:00
print "M initial revision: 1.1\n";
} else {
2012-10-14 13:42:21 +08:00
print "M new revision: $meta->{revision}; previous revision: $oldmeta{$filename}{revision}\n";
2007-03-14 01:25:22 +08:00
}
2006-02-22 17:50:15 +08:00
print "Checked-in $dirpart\n";
print "$filename\n";
2008-05-15 12:35:48 +08:00
my $kopts = kopts_from_path($filename,"sha1",$meta->{filehash});
2012-10-14 13:42:31 +08:00
print "/$filepart/$meta->{revision}//$kopts/" .
getStickyTagOrDate($stickyInfo) . "\n";
2006-02-22 17:50:15 +08:00
}
}
2008-05-15 12:35:46 +08:00
cleanupWorkTree();
2006-02-22 17:50:15 +08:00
print "ok\n";
}
sub req_status
{
my ( $cmd, $data ) = @_;
argsplit("status");
$log->info("req_status : " . ( defined($data) ? $data : "[NULL]" ));
#$log->debug("status state : " . Dumper($state));
# Grab a handle to the SQLite db and do any necessary updates
2012-10-14 13:42:20 +08:00
my $updater;
$updater = GITCVS::updater->new($state->{CVSROOT}, $state->{module}, $log);
2006-02-22 17:50:15 +08:00
$updater->update();
2012-10-14 13:42:20 +08:00
# if no files were specified, we need to work out what files we should
# be providing status on ...
2006-03-27 11:51:42 +08:00
argsfromdir($updater);
2006-02-22 17:50:15 +08:00
2006-07-09 15:44:30 +08:00
# foreach file specified on the command line ...
2006-02-22 17:50:15 +08:00
foreach my $filename ( @{$state->{args}} )
{
$filename = filecleanup($filename);
2012-10-14 13:42:20 +08:00
if ( exists($state->{opt}{l}) &&
index($filename, '/', length($state->{prependdir})) >= 0 )
{
next;
}
2008-03-28 06:17:53 +08:00
2006-02-22 17:50:15 +08:00
my $wrev = revparse($filename);
2012-10-14 13:42:31 +08:00
my $stickyInfo = resolveStickyInfo($filename);
my $meta = $updater->getmeta($filename,$stickyInfo);
my $oldmeta = $meta;
2012-10-14 13:42:20 +08:00
# If the working copy is an old revision, lets get that
# version too for comparison.
2012-10-14 13:42:21 +08:00
if ( defined($wrev) and $wrev ne $meta->{revision} )
2006-02-22 17:50:15 +08:00
{
2012-10-14 13:42:31 +08:00
my($rmRev)=$wrev;
$rmRev=~s/^-//;
$oldmeta = $updater->getmeta($filename, $rmRev);
2006-02-22 17:50:15 +08:00
}
# TODO : All possible statuses aren't yet implemented
my $status;
2012-10-14 13:42:20 +08:00
# Files are up to date if the working copy and repo copy have
# the same revision, and the working copy is unmodified
if ( defined ( $wrev ) and defined($meta->{revision}) and
2012-10-14 13:42:21 +08:00
$wrev eq $meta->{revision} and
2012-10-14 13:42:20 +08:00
( ( $state->{entries}{$filename}{unchanged} and
( not defined ( $state->{entries}{$filename}{conflict} ) or
$state->{entries}{$filename}{conflict} !~ /^\+=/ ) ) or
( defined($state->{entries}{$filename}{modified_hash}) and
$state->{entries}{$filename}{modified_hash} eq
2012-10-14 13:42:21 +08:00
$meta->{filehash} ) ) )
2012-10-14 13:42:20 +08:00
{
2012-10-14 13:42:21 +08:00
$status = "Up-to-date"
2012-10-14 13:42:20 +08:00
}
2012-10-14 13:42:21 +08:00
# Need checkout if the working copy has a different (usually
# older) revision than the repo copy, and the working copy is
# unmodified
2012-10-14 13:42:20 +08:00
if ( defined ( $wrev ) and defined ( $meta->{revision} ) and
2012-10-14 13:42:21 +08:00
$meta->{revision} ne $wrev and
2012-10-14 13:42:20 +08:00
( $state->{entries}{$filename}{unchanged} or
( defined($state->{entries}{$filename}{modified_hash}) and
$state->{entries}{$filename}{modified_hash} eq
$oldmeta->{filehash} ) ) )
{
$status ||= "Needs Checkout";
}
# Need checkout if it exists in the repo but doesn't have a working
# copy
if ( not defined ( $wrev ) and defined ( $meta->{revision} ) )
{
$status ||= "Needs Checkout";
}
# Locally modified if working copy and repo copy have the
# same revision but there are local changes
if ( defined ( $wrev ) and defined($meta->{revision}) and
2012-10-14 13:42:21 +08:00
$wrev eq $meta->{revision} and
2012-10-14 13:42:31 +08:00
$wrev ne "0" and
2012-10-14 13:42:20 +08:00
$state->{entries}{$filename}{modified_filename} )
{
$status ||= "Locally Modified";
}
2012-10-14 13:42:21 +08:00
# Needs Merge if working copy revision is different
# (usually older) than repo copy and there are local changes
2012-10-14 13:42:20 +08:00
if ( defined ( $wrev ) and defined ( $meta->{revision} ) and
2012-10-14 13:42:21 +08:00
$meta->{revision} ne $wrev and
2012-10-14 13:42:20 +08:00
$state->{entries}{$filename}{modified_filename} )
{
$status ||= "Needs Merge";
}
if ( defined ( $state->{entries}{$filename}{revision} ) and
2012-10-14 13:42:31 +08:00
( !defined($meta->{revision}) ||
$meta->{revision} eq "0" ) )
2012-10-14 13:42:20 +08:00
{
$status ||= "Locally Added";
}
if ( defined ( $wrev ) and defined ( $meta->{revision} ) and
2012-10-14 13:42:21 +08:00
$wrev eq "-$meta->{revision}" )
2012-10-14 13:42:20 +08:00
{
$status ||= "Locally Removed";
}
if ( defined ( $state->{entries}{$filename}{conflict} ) and
$state->{entries}{$filename}{conflict} =~ /^\+=/ )
{
$status ||= "Unresolved Conflict";
}
if ( 0 )
{
$status ||= "File had conflicts on merge";
}
2006-02-22 17:50:15 +08:00
$status ||= "Unknown";
2008-03-28 06:17:42 +08:00
my ($filepart) = filenamesplit($filename);
2012-10-14 13:42:20 +08:00
print "M =======" . ( "=" x 60 ) . "\n";
2008-03-28 06:17:42 +08:00
print "M File: $filepart\tStatus: $status\n";
2006-02-22 17:50:15 +08:00
if ( defined($state->{entries}{$filename}{revision}) )
{
2012-10-14 13:42:20 +08:00
print "M Working revision:\t" .
$state->{entries}{$filename}{revision} . "\n";
2006-02-22 17:50:15 +08:00
} else {
print "M Working revision:\tNo entry for $filename\n";
}
if ( defined($meta->{revision}) )
{
2012-10-14 13:42:21 +08:00
print "M Repository revision:\t" .
2012-10-14 13:42:20 +08:00
$meta->{revision} .
"\t$state->{CVSROOT}/$state->{module}/$filename,v\n";
2012-10-14 13:42:23 +08:00
my($tagOrDate)=$state->{entries}{$filename}{tag_or_date};
my($tag)=($tagOrDate=~m/^T(.+)$/);
if( !defined($tag) )
{
$tag="(none)";
}
print "M Sticky Tag:\t\t$tag\n";
my($date)=($tagOrDate=~m/^D(.+)$/);
if( !defined($date) )
{
$date="(none)";
}
print "M Sticky Date:\t\t$date\n";
my($options)=$state->{entries}{$filename}{options};
if( $options eq "" )
{
$options="(none)";
}
print "M Sticky Options:\t\t$options\n";
2006-02-22 17:50:15 +08:00
} else {
print "M Repository revision:\tNo revision control file\n";
}
print "M\n";
}
print "ok\n";
}
sub req_diff
{
my ( $cmd, $data ) = @_;
argsplit("diff");
$log->debug("req_diff : " . ( defined($data) ? $data : "[NULL]" ));
#$log->debug("status state : " . Dumper($state));
my ($revision1, $revision2);
if ( defined ( $state->{opt}{r} ) and ref $state->{opt}{r} eq "ARRAY" )
{
$revision1 = $state->{opt}{r}[0];
$revision2 = $state->{opt}{r}[1];
} else {
$revision1 = $state->{opt}{r};
}
2012-10-14 13:42:20 +08:00
$log->debug("Diffing revisions " .
( defined($revision1) ? $revision1 : "[NULL]" ) .
" and " . ( defined($revision2) ? $revision2 : "[NULL]" ) );
2006-02-22 17:50:15 +08:00
# Grab a handle to the SQLite db and do any necessary updates
2012-10-14 13:42:20 +08:00
my $updater;
$updater = GITCVS::updater->new($state->{CVSROOT}, $state->{module}, $log);
2006-02-22 17:50:15 +08:00
$updater->update();
2012-10-14 13:42:20 +08:00
# if no files were specified, we need to work out what files we should
# be providing status on ...
2006-03-27 11:51:42 +08:00
argsfromdir($updater);
2006-02-22 17:50:15 +08:00
2012-10-14 13:42:31 +08:00
my($foundDiff);
2006-07-09 15:44:30 +08:00
# foreach file specified on the command line ...
2012-10-14 13:42:31 +08:00
foreach my $argFilename ( @{$state->{args}} )
2006-02-22 17:50:15 +08:00
{
2012-10-14 13:42:31 +08:00
my($filename) = filecleanup($argFilename);
2006-02-22 17:50:15 +08:00
my ( $fh, $file1, $file2, $meta1, $meta2, $filediff );
my $wrev = revparse($filename);
2012-10-14 13:42:31 +08:00
# Priority for revision1:
# 1. First -r (missing file: check -N)
# 2. wrev from client's Entry line
# - missing line/file: check -N
# - "0": added file not committed (empty contents for rev1)
# - Prefixed with dash (to be removed): check -N
2006-02-22 17:50:15 +08:00
if ( defined ( $revision1 ) )
{
$meta1 = $updater->getmeta($filename, $revision1);
2012-10-14 13:42:31 +08:00
}
elsif( defined($wrev) && $wrev ne "0" )
{
my($rmRev)=$wrev;
$rmRev=~s/^-//;
$meta1 = $updater->getmeta($filename, $rmRev);
}
if ( !defined($meta1) ||
$meta1->{filehash} eq "deleted" )
{
if( !exists($state->{opt}{N}) )
2006-02-22 17:50:15 +08:00
{
2012-10-14 13:42:31 +08:00
if(!defined($revision1))
{
print "E File $filename at revision $revision1 doesn't exist\n";
}
2006-02-22 17:50:15 +08:00
next;
}
2012-10-14 13:42:31 +08:00
elsif( !defined($meta1) )
{
$meta1 = {
name => $filename,
revision => '0',
filehash => 'deleted'
};
}
2006-02-22 17:50:15 +08:00
}
2012-10-14 13:42:31 +08:00
# Priority for revision2:
# 1. Second -r (missing file: check -N)
# 2. Modified file contents from client
# 3. wrev from client's Entry line
# - missing line/file: check -N
# - Prefixed with dash (to be removed): check -N
2006-02-22 17:50:15 +08:00
# if we have a second -r switch, use it too
if ( defined ( $revision2 ) )
{
$meta2 = $updater->getmeta($filename, $revision2);
}
2012-10-14 13:42:31 +08:00
elsif(defined($state->{entries}{$filename}{modified_filename}))
2006-02-22 17:50:15 +08:00
{
$file2 = $state->{entries}{$filename}{modified_filename};
2012-10-14 13:42:31 +08:00
$meta2 = {
name => $filename,
revision => '0',
filehash => 'modified'
};
2006-02-22 17:50:15 +08:00
}
2012-10-14 13:42:31 +08:00
elsif( defined($wrev) && ($wrev!~/^-/) )
2006-02-22 17:50:15 +08:00
{
2012-10-14 13:42:31 +08:00
if(!defined($revision1)) # no revision and no modifications:
{
next;
}
2006-02-22 17:50:15 +08:00
$meta2 = $updater->getmeta($filename, $wrev);
2012-10-14 13:42:31 +08:00
}
if(!defined($file2))
{
if ( !defined($meta2) ||
$meta2->{filehash} eq "deleted" )
{
if( !exists($state->{opt}{N}) )
{
if(!defined($revision2))
{
print "E File $filename at revision $revision2 doesn't exist\n";
}
next;
}
elsif( !defined($meta2) )
{
$meta2 = {
name => $filename,
revision => '0',
filehash => 'deleted'
};
}
}
2006-02-22 17:50:15 +08:00
}
2012-10-14 13:42:31 +08:00
if( $meta1->{filehash} eq $meta2->{filehash} )
2012-10-14 13:42:20 +08:00
{
2012-10-14 13:42:31 +08:00
$log->info("unchanged $filename");
2012-10-14 13:42:20 +08:00
next;
}
2006-02-22 17:50:15 +08:00
2012-10-14 13:42:31 +08:00
# Retrieve revision contents:
( undef, $file1 ) = tempfile( DIR => $TEMP_DIR, OPEN => 0 );
transmitfile($meta1->{filehash}, { targetfile => $file1 });
if(!defined($file2))
2012-10-14 13:42:20 +08:00
{
2012-10-14 13:42:31 +08:00
( undef, $file2 ) = tempfile( DIR => $TEMP_DIR, OPEN => 0 );
transmitfile($meta2->{filehash}, { targetfile => $file2 });
2012-10-14 13:42:20 +08:00
}
2006-02-22 17:50:15 +08:00
2012-10-14 13:42:31 +08:00
# Generate the actual diff:
print "M Index: $argFilename\n";
2012-10-14 13:42:20 +08:00
print "M =======" . ( "=" x 60 ) . "\n";
2006-02-22 17:50:15 +08:00
print "M RCS file: $state->{CVSROOT}/$state->{module}/$filename,v\n";
2012-10-14 13:42:31 +08:00
if ( defined ( $meta1 ) && $meta1->{revision} ne "0" )
2012-10-14 13:42:20 +08:00
{
2012-10-14 13:42:21 +08:00
print "M retrieving revision $meta1->{revision}\n"
2012-10-14 13:42:20 +08:00
}
2012-10-14 13:42:31 +08:00
if ( defined ( $meta2 ) && $meta2->{revision} ne "0" )
2012-10-14 13:42:20 +08:00
{
2012-10-14 13:42:21 +08:00
print "M retrieving revision $meta2->{revision}\n"
2012-10-14 13:42:20 +08:00
}
2006-02-22 17:50:15 +08:00
print "M diff ";
2013-10-30 16:44:43 +08:00
foreach my $opt ( sort keys %{$state->{opt}} )
2006-02-22 17:50:15 +08:00
{
if ( ref $state->{opt}{$opt} eq "ARRAY" )
{
foreach my $value ( @{$state->{opt}{$opt}} )
{
print "-$opt $value ";
}
} else {
print "-$opt ";
2012-10-14 13:42:20 +08:00
if ( defined ( $state->{opt}{$opt} ) )
{
print "$state->{opt}{$opt} "
}
2006-02-22 17:50:15 +08:00
}
}
2012-10-14 13:42:31 +08:00
print "$argFilename\n";
2006-02-22 17:50:15 +08:00
2012-10-14 13:42:20 +08:00
$log->info("Diffing $filename -r $meta1->{revision} -r " .
( $meta2->{revision} or "workingcopy" ));
2006-02-22 17:50:15 +08:00
2012-10-14 13:42:31 +08:00
# TODO: Use --label instead of -L because -L is no longer
2021-06-11 19:18:50 +08:00
# documented and may go away someday. Not sure if there are
2012-10-14 13:42:31 +08:00
# versions that only support -L, which would make this change risky?
# http://osdir.com/ml/bug-gnu-utils-gnu/2010-12/msg00060.html
# ("man diff" should actually document the best migration strategy,
# [current behavior, future changes, old compatibility issues
# or lack thereof, etc], not just stop mentioning the option...)
# TODO: Real CVS seems to include a date in the label, before
# the revision part, without the keyword "revision". The following
# has minimal changes compared to original versions of
# git-cvsserver.perl. (Mostly tab vs space after filename.)
2006-02-22 17:50:15 +08:00
2012-10-14 13:42:31 +08:00
my (@diffCmd) = ( 'diff' );
if ( exists($state->{opt}{N}) )
2006-02-22 17:50:15 +08:00
{
2012-10-14 13:42:31 +08:00
push @diffCmd,"-N";
2006-02-22 17:50:15 +08:00
}
2012-10-14 13:42:31 +08:00
if ( exists $state->{opt}{u} )
{
push @diffCmd,("-u","-L");
if( $meta1->{filehash} eq "deleted" )
{
push @diffCmd,"/dev/null";
} else {
push @diffCmd,("$argFilename\trevision $meta1->{revision}");
}
2006-02-22 17:50:15 +08:00
2012-10-14 13:42:31 +08:00
if( defined($meta2->{filehash}) )
{
if( $meta2->{filehash} eq "deleted" )
{
push @diffCmd,("-L","/dev/null");
} else {
push @diffCmd,("-L",
"$argFilename\trevision $meta2->{revision}");
}
} else {
push @diffCmd,("-L","$argFilename\tworking copy");
}
}
push @diffCmd,($file1,$file2);
if(!open(DIFF,"-|",@diffCmd))
2006-02-22 17:50:15 +08:00
{
2012-10-14 13:42:31 +08:00
$log->warn("Unable to run diff: $!");
2006-02-22 17:50:15 +08:00
}
2012-10-14 13:42:31 +08:00
my($diffLine);
while(defined($diffLine=<DIFF>))
{
print "M $diffLine";
$foundDiff=1;
}
close(DIFF);
2006-02-22 17:50:15 +08:00
}
2012-10-14 13:42:31 +08:00
if($foundDiff)
{
print "error \n";
}
else
{
print "ok\n";
}
2006-02-22 17:50:15 +08:00
}
sub req_log
{
my ( $cmd, $data ) = @_;
argsplit("log");
$log->debug("req_log : " . ( defined($data) ? $data : "[NULL]" ));
#$log->debug("log state : " . Dumper($state));
2012-10-14 13:42:21 +08:00
my ( $revFilter );
if ( defined ( $state->{opt}{r} ) )
{
$revFilter = $state->{opt}{r};
2006-02-22 17:50:15 +08:00
}
# Grab a handle to the SQLite db and do any necessary updates
2012-10-14 13:42:20 +08:00
my $updater;
$updater = GITCVS::updater->new($state->{CVSROOT}, $state->{module}, $log);
2006-02-22 17:50:15 +08:00
$updater->update();
2012-10-14 13:42:20 +08:00
# if no files were specified, we need to work out what files we
# should be providing status on ...
2006-03-27 11:51:42 +08:00
argsfromdir($updater);
2006-02-22 17:50:15 +08:00
2006-07-09 15:44:30 +08:00
# foreach file specified on the command line ...
2006-02-22 17:50:15 +08:00
foreach my $filename ( @{$state->{args}} )
{
$filename = filecleanup($filename);
my $headmeta = $updater->getmeta($filename);
2012-10-14 13:42:21 +08:00
my ($revisions,$totalrevisions) = $updater->getlog($filename,
$revFilter);
2006-02-22 17:50:15 +08:00
next unless ( scalar(@$revisions) );
print "M \n";
print "M RCS file: $state->{CVSROOT}/$state->{module}/$filename,v\n";
print "M Working file: $filename\n";
2012-10-14 13:42:21 +08:00
print "M head: $headmeta->{revision}\n";
2006-02-22 17:50:15 +08:00
print "M branch:\n";
print "M locks: strict\n";
print "M access list:\n";
print "M symbolic names:\n";
print "M keyword substitution: kv\n";
2012-10-14 13:42:20 +08:00
print "M total revisions: $totalrevisions;\tselected revisions: " .
scalar(@$revisions) . "\n";
2006-02-22 17:50:15 +08:00
print "M description:\n";
foreach my $revision ( @$revisions )
{
print "M ----------------------------\n";
2012-10-14 13:42:21 +08:00
print "M revision $revision->{revision}\n";
2006-02-22 17:50:15 +08:00
# reformat the date for log output
2012-10-14 13:42:20 +08:00
if ( $revision->{modified} =~ /(\d+)\s+(\w+)\s+(\d+)\s+(\S+)/ and
defined($DATE_LIST->{$2}) )
{
$revision->{modified} = sprintf('%04d/%02d/%02d %s',
$3, $DATE_LIST->{$2}, $1, $4 );
}
2008-03-28 06:18:35 +08:00
$revision->{author} = cvs_author($revision->{author});
2012-10-14 13:42:20 +08:00
print "M date: $revision->{modified};" .
" author: $revision->{author}; state: " .
( $revision->{filehash} eq "deleted" ? "dead" : "Exp" ) .
"; lines: +2 -3\n";
my $commitmessage;
$commitmessage = $updater->commitmessage($revision->{commithash});
2006-02-22 17:50:15 +08:00
$commitmessage =~ s/^/M /mg;
print $commitmessage . "\n";
}
2012-10-14 13:42:20 +08:00
print "M =======" . ( "=" x 70 ) . "\n";
2006-02-22 17:50:15 +08:00
}
print "ok\n";
}
sub req_annotate
{
my ( $cmd, $data ) = @_;
argsplit("annotate");
$log->info("req_annotate : " . ( defined($data) ? $data : "[NULL]" ));
#$log->debug("status state : " . Dumper($state));
# Grab a handle to the SQLite db and do any necessary updates
my $updater = GITCVS::updater->new($state->{CVSROOT}, $state->{module}, $log);
$updater->update();
# if no files were specified, we need to work out what files we should be providing annotate on ...
2006-03-27 11:51:42 +08:00
argsfromdir($updater);
2006-02-22 17:50:15 +08:00
# we'll need a temporary checkout dir
2008-05-15 12:35:46 +08:00
setupWorkTree();
2006-02-22 17:50:15 +08:00
2008-05-15 12:35:46 +08:00
$log->info("Temp checkoutdir creation successful, basing annotate session work on '$work->{workDir}', index file is '$ENV{GIT_INDEX_FILE}'");
2006-02-22 17:50:15 +08:00
2006-07-09 15:44:30 +08:00
# foreach file specified on the command line ...
2006-02-22 17:50:15 +08:00
foreach my $filename ( @{$state->{args}} )
{
$filename = filecleanup($filename);
my $meta = $updater->getmeta($filename);
next unless ( $meta->{revision} );
# get all the commits that this file was in
# in dense format -- aka skip dead revisions
my $revisions = $updater->gethistorydense($filename);
my $lastseenin = $revisions->[0][2];
# populate the temporary index based on the latest commit were we saw
# the file -- but do it cheaply without checking out any files
# TODO: if we got a revision from the client, use that instead
# to look up the commithash in sqlite (still good to default to
# the current head as we do now)
2009-09-02 17:23:10 +08:00
system("git", "read-tree", $lastseenin);
2006-02-22 17:50:15 +08:00
unless ($? == 0)
{
2008-05-15 12:35:46 +08:00
print "E error running git-read-tree $lastseenin $ENV{GIT_INDEX_FILE} $!\n";
2007-07-15 02:48:42 +08:00
return;
2006-02-22 17:50:15 +08:00
}
2008-05-15 12:35:46 +08:00
$log->info("Created index '$ENV{GIT_INDEX_FILE}' with commit $lastseenin - exit status $?");
2006-02-22 17:50:15 +08:00
# do a checkout of the file
2009-09-02 17:23:10 +08:00
system('git', 'checkout-index', '-f', '-u', $filename);
2006-02-22 17:50:15 +08:00
unless ($? == 0) {
2007-07-15 02:48:42 +08:00
print "E error running git-checkout-index -f -u $filename : $!\n";
return;
2006-02-22 17:50:15 +08:00
}
$log->info("Annotate $filename");
# Prepare a file with the commits from the linearized
# history that annotate should know about. This prevents
# git-jsannotate telling us about commits we are hiding
# from the client.
2008-05-15 12:35:46 +08:00
my $a_hints = "$work->{workDir}/.annotate_hints";
2007-07-15 02:48:42 +08:00
if (!open(ANNOTATEHINTS, '>', $a_hints)) {
print "E failed to open '$a_hints' for writing: $!\n";
return;
}
2006-02-22 17:50:15 +08:00
for (my $i=0; $i < @$revisions; $i++)
{
print ANNOTATEHINTS $revisions->[$i][2];
if ($i+1 < @$revisions) { # have we got a parent?
print ANNOTATEHINTS ' ' . $revisions->[$i+1][2];
}
print ANNOTATEHINTS "\n";
}
print ANNOTATEHINTS "\n";
2007-07-15 02:48:42 +08:00
close ANNOTATEHINTS
or (print "E failed to write $a_hints: $!\n"), return;
2006-02-22 17:50:15 +08:00
2009-09-02 17:23:10 +08:00
my @cmd = (qw(git annotate -l -S), $a_hints, $filename);
2007-07-15 02:48:42 +08:00
if (!open(ANNOTATE, "-|", @cmd)) {
print "E error invoking ". join(' ',@cmd) .": $!\n";
return;
}
2006-02-22 17:50:15 +08:00
my $metadata = {};
print "E Annotations for $filename\n";
print "E ***************\n";
while ( <ANNOTATE> )
{
2020-06-23 02:04:16 +08:00
if (m/^([a-zA-Z0-9]{$state->{hexsz}})\t\([^\)]*\)(.*)$/i)
2006-02-22 17:50:15 +08:00
{
my $commithash = $1;
my $data = $2;
unless ( defined ( $metadata->{$commithash} ) )
{
$metadata->{$commithash} = $updater->getmeta($filename, $commithash);
2008-03-28 06:18:35 +08:00
$metadata->{$commithash}{author} = cvs_author($metadata->{$commithash}{author});
2006-02-22 17:50:15 +08:00
$metadata->{$commithash}{modified} = sprintf("%02d-%s-%02d", $1, $2, $3) if ( $metadata->{$commithash}{modified} =~ /^(\d+)\s(\w+)\s\d\d(\d\d)/ );
}
2012-10-14 13:42:21 +08:00
printf("M %-7s (%-8s %10s): %s\n",
2006-02-22 17:50:15 +08:00
$metadata->{$commithash}{revision},
$metadata->{$commithash}{author},
$metadata->{$commithash}{modified},
$data
);
} else {
$log->warn("Error in annotate output! LINE: $_");
print "E Annotate error \n";
next;
}
}
close ANNOTATE;
}
# done; get out of the tempdir
2008-07-16 19:35:46 +08:00
cleanupWorkTree();
2006-02-22 17:50:15 +08:00
print "ok\n";
}
# This method takes the state->{arguments} array and produces two new arrays.
# The first is $state->{args} which is everything before the '--' argument, and
# the second is $state->{files} which is everything after it.
sub argsplit
{
$state->{args} = [];
$state->{files} = [];
$state->{opt} = {};
2007-06-17 16:31:02 +08:00
return unless( defined($state->{arguments}) and ref $state->{arguments} eq "ARRAY" );
my $type = shift;
2006-02-22 17:50:15 +08:00
if ( defined($type) )
{
my $opt = {};
$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" );
$opt = { v => 0, l => 0, R => 0 } if ( $type eq "status" );
$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" );
2012-10-14 13:42:31 +08:00
$opt = { l => 0, R => 0, k => 1, D => 1, D => 1, r => 2, N => 0 } if ( $type eq "diff" );
2006-02-22 17:50:15 +08:00
$opt = { c => 0, R => 0, l => 0, f => 0, F => 1, m => 1, r => 1 } if ( $type eq "ci" );
$opt = { k => 1, m => 1 } if ( $type eq "add" );
$opt = { f => 0, l => 0, R => 0 } if ( $type eq "remove" );
$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" );
while ( scalar ( @{$state->{arguments}} ) > 0 )
{
my $arg = shift @{$state->{arguments}};
next if ( $arg eq "--" );
next unless ( $arg =~ /\S/ );
# if the argument looks like a switch
if ( $arg =~ /^-(\w)(.*)/ )
{
# if it's a switch that takes an argument
if ( $opt->{$1} )
{
# If this switch has already been provided
if ( $opt->{$1} > 1 and exists ( $state->{opt}{$1} ) )
{
$state->{opt}{$1} = [ $state->{opt}{$1} ];
if ( length($2) > 0 )
{
push @{$state->{opt}{$1}},$2;
} else {
push @{$state->{opt}{$1}}, shift @{$state->{arguments}};
}
} else {
# if there's extra data in the arg, use that as the argument for the switch
if ( length($2) > 0 )
{
$state->{opt}{$1} = $2;
} else {
$state->{opt}{$1} = shift @{$state->{arguments}};
}
}
} else {
$state->{opt}{$1} = undef;
}
}
else
{
push @{$state->{args}}, $arg;
}
}
}
else
{
my $mode = 0;
foreach my $value ( @{$state->{arguments}} )
{
if ( $value eq "--" )
{
$mode++;
next;
}
push @{$state->{args}}, $value if ( $mode == 0 );
push @{$state->{files}}, $value if ( $mode == 1 );
}
}
}
2012-10-14 13:42:30 +08:00
# Used by argsfromdir
sub expandArg
2006-02-22 17:50:15 +08:00
{
2012-10-14 13:42:30 +08:00
my ($updater,$outNameMap,$outDirMap,$path,$isDir) = @_;
2006-02-22 17:50:15 +08:00
2012-10-14 13:42:30 +08:00
my $fullPath = filecleanup($path);
2006-03-27 11:51:42 +08:00
2012-10-14 13:42:30 +08:00
# Is it a directory?
if( defined($state->{dirMap}{$fullPath}) ||
defined($state->{dirMap}{"$fullPath/"}) )
{
# It is a directory in the user's sandbox.
$isDir=1;
2006-03-27 11:51:42 +08:00
2012-10-14 13:42:30 +08:00
if(defined($state->{entries}{$fullPath}))
{
$log->fatal("Inconsistent file/dir type");
die "Inconsistent file/dir type";
}
}
elsif(defined($state->{entries}{$fullPath}))
{
# It is a file in the user's sandbox.
$isDir=0;
}
my($revDirMap,$otherRevDirMap);
if(!defined($isDir) || $isDir)
{
# Resolve version tree for sticky tag:
# (for now we only want list of files for the version, not
# particular versions of those files: assume it is a directory
# for the moment; ignore Entry's stick tag)
# Order of precedence of sticky tags:
# -A [head]
# -r /tag/
# [file entry sticky tag, but that is only relevant to files]
# [the tag specified in dir req_Sticky]
# [the tag specified in a parent dir req_Sticky]
# [head]
# Also, -r may appear twice (for diff).
#
# FUTURE: When/if -j (merges) are supported, we also
# need to add relevant files from one or two
# versions specified with -j.
if(exists($state->{opt}{A}))
{
$revDirMap=$updater->getRevisionDirMap();
}
elsif( defined($state->{opt}{r}) and
ref $state->{opt}{r} eq "ARRAY" )
{
$revDirMap=$updater->getRevisionDirMap($state->{opt}{r}[0]);
$otherRevDirMap=$updater->getRevisionDirMap($state->{opt}{r}[1]);
}
elsif(defined($state->{opt}{r}))
{
$revDirMap=$updater->getRevisionDirMap($state->{opt}{r});
}
else
{
my($sticky)=getDirStickyInfo($fullPath);
$revDirMap=$updater->getRevisionDirMap($sticky->{tag});
}
2006-10-11 06:20:43 +08:00
2012-10-14 13:42:30 +08:00
# Is it a directory?
if( defined($revDirMap->{$fullPath}) ||
defined($otherRevDirMap->{$fullPath}) )
{
$isDir=1;
}
2006-10-11 06:20:43 +08:00
}
2012-10-14 13:42:30 +08:00
# What to do with it?
if(!$isDir)
2006-03-28 09:24:27 +08:00
{
2012-10-14 13:42:30 +08:00
$outNameMap->{$fullPath}=1;
}
else
{
$outDirMap->{$fullPath}=1;
2006-02-22 17:50:15 +08:00
2012-10-14 13:42:30 +08:00
if(defined($revDirMap->{$fullPath}))
{
addDirMapFiles($updater,$outNameMap,$outDirMap,
$revDirMap->{$fullPath});
}
if( defined($otherRevDirMap) &&
defined($otherRevDirMap->{$fullPath}) )
2006-03-28 09:24:27 +08:00
{
2012-10-14 13:42:30 +08:00
addDirMapFiles($updater,$outNameMap,$outDirMap,
$otherRevDirMap->{$fullPath});
2006-03-28 09:24:27 +08:00
}
2012-10-14 13:42:30 +08:00
}
}
2006-03-28 09:24:27 +08:00
2012-10-14 13:42:30 +08:00
# Used by argsfromdir
# Add entries from dirMap to outNameMap. Also recurse into entries
# that are subdirectories.
sub addDirMapFiles
{
my($updater,$outNameMap,$outDirMap,$dirMap)=@_;
2006-03-28 09:24:27 +08:00
2012-10-14 13:42:30 +08:00
my($fullName);
foreach $fullName (keys(%$dirMap))
{
my $cleanName=$fullName;
if(defined($state->{prependdir}))
{
if(!($cleanName=~s/^\Q$state->{prependdir}\E//))
{
$log->fatal("internal error stripping prependdir");
die "internal error stripping prependdir";
}
}
2006-03-28 09:24:27 +08:00
2012-10-14 13:42:30 +08:00
if($dirMap->{$fullName} eq "F")
{
$outNameMap->{$cleanName}=1;
}
elsif($dirMap->{$fullName} eq "D")
{
if(!$state->{opt}{l})
{
expandArg($updater,$outNameMap,$outDirMap,$cleanName,1);
}
}
else
2006-03-28 09:24:27 +08:00
{
2012-10-14 13:42:30 +08:00
$log->fatal("internal error in addDirMapFiles");
die "internal error in addDirMapFiles";
2006-03-28 09:24:27 +08:00
}
2006-02-22 17:50:15 +08:00
}
}
2012-10-14 13:42:30 +08:00
# This method replaces $state->{args} with a directory-expanded
# list of all relevant filenames (recursively unless -d), based
# on $state->{entries}, and the "current" list of files in
# each directory. "Current" files as determined by
# either the requested (-r/-A) or "req_Sticky" version of
# that directory.
# Both the input args and the new output args are relative
# to the cvs-client's CWD, although some of the internal
# computations are relative to the top of the project.
sub argsfromdir
{
my $updater = shift;
# Notes about requirements for specific callers:
# update # "standard" case (entries; a single -r/-A/default; -l)
# # Special case: -d for create missing directories.
# diff # 0 or 1 -r's: "standard" case.
# # 2 -r's: We could ignore entries (just use the two -r's),
# # but it doesn't really matter.
# annotate # "standard" case
# log # Punting: log -r has a more complex non-"standard"
# # meaning, and we don't currently try to support log'ing
# # branches at all (need a lot of work to
# # support CVS-consistent branch relative version
# # numbering).
#HERE: But we still want to expand directories. Maybe we should
# essentially force "-A".
# status # "standard", except that -r/-A/default are not possible.
# # Mostly only used to expand entries only)
#
# Don't use argsfromdir at all:
# add # Explicit arguments required. Directory args imply add
# # the directory itself, not the files in it.
# co # Obtain list directly.
# remove # HERE: TEST: MAYBE client does the recursion for us,
# # since it only makes sense to remove stuff already in
2016-02-17 10:14:58 +08:00
# # the sandbox?
2012-10-14 13:42:30 +08:00
# ci # HERE: Similar to remove...
# # Don't try to implement the confusing/weird
# # ci -r bug er.."feature".
if(scalar(@{$state->{args}})==0)
{
$state->{args} = [ "." ];
}
my %allArgs;
my %allDirs;
for my $file (@{$state->{args}})
{
expandArg($updater,\%allArgs,\%allDirs,$file);
}
# Include any entries from sandbox. Generally client won't
# send entries that shouldn't be used.
foreach my $file (keys %{$state->{entries}})
{
$allArgs{remove_prependdir($file)} = 1;
}
$state->{dirArgs} = \%allDirs;
$state->{args} = [
sort {
# Sort priority: by directory depth, then actual file name:
my @piecesA=split('/',$a);
my @piecesB=split('/',$b);
my $count=scalar(@piecesA);
my $tmp=scalar(@piecesB);
return $count<=>$tmp if($count!=$tmp);
for($tmp=0;$tmp<$count;$tmp++)
{
if($piecesA[$tmp] ne $piecesB[$tmp])
{
return $piecesA[$tmp] cmp $piecesB[$tmp]
}
}
return 0;
} keys(%allArgs) ];
}
2012-10-14 13:42:28 +08:00
## look up directory sticky tag, of either fullPath or a parent:
sub getDirStickyInfo
{
my($fullPath)=@_;
$fullPath=~s%/+$%%;
while($fullPath ne "" && !defined($state->{dirMap}{"$fullPath/"}))
{
$fullPath=~s%/?[^/]*$%%;
}
if( !defined($state->{dirMap}{"$fullPath/"}) &&
( $fullPath eq "" ||
$fullPath eq "." ) )
{
return $state->{dirMap}{""}{stickyInfo};
}
else
{
return $state->{dirMap}{"$fullPath/"}{stickyInfo};
}
}
# Resolve precedence of various ways of specifying which version of
# a file you want. Returns undef (for default head), or a ref to a hash
# that contains "tag" and/or "date" keys.
sub resolveStickyInfo
{
my($filename,$stickyTag,$stickyDate,$reset) = @_;
# Order of precedence of sticky tags:
# -A [head]
# -r /tag/
# [file entry sticky tag]
# [the tag specified in dir req_Sticky]
# [the tag specified in a parent dir req_Sticky]
# [head]
my $result;
if($reset)
{
# $result=undef;
}
elsif( defined($stickyTag) && $stickyTag ne "" )
# || ( defined($stickyDate) && $stickyDate ne "" ) # TODO
{
$result={ 'tag' => (defined($stickyTag)?$stickyTag:undef) };
# TODO: Convert -D value into the form 2011.04.10.04.46.57,
# similar to an entry line's sticky date, without the D prefix.
# It sometimes (always?) arrives as something more like
# '10 Apr 2011 04:46:57 -0000'...
# $result={ 'date' => (defined($stickyDate)?$stickyDate:undef) };
}
elsif( defined($state->{entries}{$filename}) &&
defined($state->{entries}{$filename}{tag_or_date}) &&
$state->{entries}{$filename}{tag_or_date} ne "" )
{
my($tagOrDate)=$state->{entries}{$filename}{tag_or_date};
if($tagOrDate=~/^T([^ ]+)\s*$/)
{
$result = { 'tag' => $1 };
}
elsif($tagOrDate=~/^D([0-9.]+)\s*$/)
{
$result= { 'date' => $1 };
}
else
{
die "Unknown tag_or_date format\n";
}
}
else
{
$result=getDirStickyInfo($filename);
}
return $result;
}
# Convert a stickyInfo (ref to a hash) as returned by resolveStickyInfo into
# a form appropriate for the sticky tag field of an Entries
# line (field index 5, 0-based).
sub getStickyTagOrDate
{
my($stickyInfo)=@_;
my $result;
if(defined($stickyInfo) && defined($stickyInfo->{tag}))
{
$result="T$stickyInfo->{tag}";
}
# TODO: When/if we actually pick versions by {date} properly,
# also handle it here:
# "D$stickyInfo->{date}" (example: "D2011.04.13.20.37.07").
else
{
$result="";
}
return $result;
}
2006-02-22 17:50:15 +08:00
# This method cleans up the $state variable after a command that uses arguments has run
sub statecleanup
{
$state->{files} = [];
2012-10-14 13:42:30 +08:00
$state->{dirArgs} = {};
2006-02-22 17:50:15 +08:00
$state->{args} = [];
$state->{arguments} = [];
$state->{entries} = {};
2012-10-14 13:42:28 +08:00
$state->{dirMap} = {};
2006-02-22 17:50:15 +08:00
}
2012-10-14 13:42:21 +08:00
# Return working directory CVS revision "1.X" out
2016-05-06 20:36:46 +08:00
# of the working directory "entries" state, for the given filename.
2012-10-14 13:42:21 +08:00
# This is prefixed with a dash if the file is scheduled for removal
2012-10-14 13:42:16 +08:00
# when it is committed.
2006-02-22 17:50:15 +08:00
sub revparse
{
my $filename = shift;
2012-10-14 13:42:21 +08:00
return $state->{entries}{$filename}{revision};
2006-02-22 17:50:15 +08:00
}
2008-03-28 06:18:12 +08:00
# This method takes a file hash and does a CVS "file transfer". Its
# exact behaviour depends on a second, optional hash table argument:
# - If $options->{targetfile}, dump the contents to that file;
# - If $options->{print}, use M/MT to transmit the contents one line
# at a time;
# - Otherwise, transmit the size of the file, followed by the file
# contents.
2006-02-22 17:50:15 +08:00
sub transmitfile
{
my $filehash = shift;
2008-03-28 06:18:12 +08:00
my $options = shift;
2006-02-22 17:50:15 +08:00
if ( defined ( $filehash ) and $filehash eq "deleted" )
{
$log->warn("filehash is 'deleted'");
return;
}
2020-06-23 02:04:16 +08:00
die "Need filehash" unless ( defined ( $filehash ) and $filehash =~ /^[a-zA-Z0-9]{$state->{hexsz}}$/ );
2006-02-22 17:50:15 +08:00
2017-09-11 13:45:09 +08:00
my $type = safe_pipe_capture('git', 'cat-file', '-t', $filehash);
2006-02-22 17:50:15 +08:00
chomp $type;
die ( "Invalid type '$type' (expected 'blob')" ) unless ( defined ( $type ) and $type eq "blob" );
2017-09-11 13:45:09 +08:00
my $size = safe_pipe_capture('git', 'cat-file', '-s', $filehash);
2006-02-22 17:50:15 +08:00
chomp $size;
$log->debug("transmitfile($filehash) size=$size, type=$type");
2009-09-02 17:23:10 +08:00
if ( open my $fh, '-|', "git", "cat-file", "blob", $filehash )
2006-02-22 17:50:15 +08:00
{
2008-03-28 06:18:12 +08:00
if ( defined ( $options->{targetfile} ) )
2006-02-22 17:50:15 +08:00
{
2008-03-28 06:18:12 +08:00
my $targetfile = $options->{targetfile};
2006-02-22 17:50:15 +08:00
open NEWFILE, ">", $targetfile or die("Couldn't open '$targetfile' for writing : $!");
print NEWFILE $_ while ( <$fh> );
2007-07-15 02:48:42 +08:00
close NEWFILE or die("Failed to write '$targetfile': $!");
2008-03-28 06:18:12 +08:00
} elsif ( defined ( $options->{print} ) && $options->{print} ) {
while ( <$fh> ) {
if( /\n\z/ ) {
print 'M ', $_;
} else {
print 'MT text ', $_, "\n";
}
}
2006-02-22 17:50:15 +08:00
} else {
print "$size\n";
print while ( <$fh> );
}
2007-07-15 02:48:42 +08:00
close $fh or die ("Couldn't close filehandle for transmitfile(): $!");
2006-02-22 17:50:15 +08:00
} else {
die("Couldn't execute git-cat-file");
}
}
# This method takes a file name, and returns ( $dirpart, $filepart ) which
2006-04-26 14:59:28 +08:00
# refers to the directory portion and the file portion of the filename
2006-02-22 17:50:15 +08:00
# respectively
sub filenamesplit
{
my $filename = shift;
2006-03-27 11:51:42 +08:00
my $fixforlocaldir = shift;
2006-02-22 17:50:15 +08:00
my ( $filepart, $dirpart ) = ( $filename, "." );
( $filepart, $dirpart ) = ( $2, $1 ) if ( $filename =~ /(.*)\/(.*)/ );
$dirpart .= "/";
2006-03-27 11:51:42 +08:00
if ( $fixforlocaldir )
{
$dirpart =~ s/^$state->{prependdir}//;
}
2006-02-22 17:50:15 +08:00
return ( $filepart, $dirpart );
}
2012-10-14 13:42:25 +08:00
# Cleanup various junk in filename (try to canonicalize it), and
2013-04-12 06:36:10 +08:00
# add prependdir to accommodate running CVS client from a
2012-10-14 13:42:25 +08:00
# subdirectory (so the output is relative to top directory of the project).
2006-02-22 17:50:15 +08:00
sub filecleanup
{
my $filename = shift;
return undef unless(defined($filename));
if ( $filename =~ /^\// )
{
print "E absolute filenames '$filename' not supported by server\n";
return undef;
}
2012-10-14 13:42:25 +08:00
if($filename eq ".")
{
$filename="";
}
2006-02-22 17:50:15 +08:00
$filename =~ s/^\.\///g;
2012-10-14 13:42:25 +08:00
$filename =~ s%/+%/%g;
2006-03-28 09:24:27 +08:00
$filename = $state->{prependdir} . $filename;
2012-10-14 13:42:25 +08:00
$filename =~ s%/$%%;
2006-02-22 17:50:15 +08:00
return $filename;
}
2016-05-06 20:36:46 +08:00
# Remove prependdir from the path, so that it is relative to the directory
2012-10-14 13:42:25 +08:00
# the CVS client was started from, rather than the top of the project.
# Essentially the inverse of filecleanup().
sub remove_prependdir
{
my($path) = @_;
if(defined($state->{prependdir}) && $state->{prependdir} ne "")
{
my($pre)=$state->{prependdir};
$pre=~s%/$%%;
if(!($path=~s%^\Q$pre\E/?%%))
{
$log->fatal("internal error missing prependdir");
die("internal error missing prependdir");
}
}
return $path;
}
2008-05-15 12:35:46 +08:00
sub validateGitDir
{
if( !defined($state->{CVSROOT}) )
{
print "error 1 CVSROOT not specified\n";
cleanupWorkTree();
exit;
}
if( $ENV{GIT_DIR} ne ($state->{CVSROOT} . '/') )
{
print "error 1 Internally inconsistent CVSROOT\n";
cleanupWorkTree();
exit;
}
}
# Setup working directory in a work tree with the requested version
# loaded in the index.
sub setupWorkTree
{
my ($ver) = @_;
validateGitDir();
if( ( defined($work->{state}) && $work->{state} != 1 ) ||
defined($work->{tmpDir}) )
{
$log->warn("Bad work tree state management");
print "error 1 Internal setup multiple work trees without cleanup\n";
cleanupWorkTree();
exit;
}
$work->{workDir} = tempdir ( DIR => $TEMP_DIR );
if( !defined($work->{index}) )
{
(undef, $work->{index}) = tempfile ( DIR => $TEMP_DIR, OPEN => 0 );
}
chdir $work->{workDir} or
die "Unable to chdir to $work->{workDir}\n";
$log->info("Setting up GIT_WORK_TREE as '.' in '$work->{workDir}', index file is '$work->{index}'");
$ENV{GIT_WORK_TREE} = ".";
$ENV{GIT_INDEX_FILE} = $work->{index};
$work->{state} = 2;
if($ver)
{
system("git","read-tree",$ver);
unless ($? == 0)
{
$log->warn("Error running git-read-tree");
die "Error running git-read-tree $ver in $work->{workDir} $!\n";
}
}
# else # req_annotate reads tree for each file
}
# Ensure current directory is in some kind of working directory,
# with a recent version loaded in the index.
sub ensureWorkTree
{
if( defined($work->{tmpDir}) )
{
$log->warn("Bad work tree state management [ensureWorkTree()]");
print "error 1 Internal setup multiple dirs without cleanup\n";
cleanupWorkTree();
exit;
}
if( $work->{state} )
{
return;
}
validateGitDir();
if( !defined($work->{emptyDir}) )
{
$work->{emptyDir} = tempdir ( DIR => $TEMP_DIR, OPEN => 0);
}
chdir $work->{emptyDir} or
die "Unable to chdir to $work->{emptyDir}\n";
2017-09-11 13:45:09 +08:00
my $ver = safe_pipe_capture('git', 'show-ref', '-s', "refs/heads/$state->{module}");
2008-05-15 12:35:46 +08:00
chomp $ver;
2020-06-23 02:04:16 +08:00
if ($ver !~ /^[0-9a-f]{$state->{hexsz}}$/)
2008-05-15 12:35:46 +08:00
{
$log->warn("Error from git show-ref -s refs/head$state->{module}");
print "error 1 cannot find the current HEAD of module";
cleanupWorkTree();
exit;
}
if( !defined($work->{index}) )
{
(undef, $work->{index}) = tempfile ( DIR => $TEMP_DIR, OPEN => 0 );
}
$ENV{GIT_WORK_TREE} = ".";
$ENV{GIT_INDEX_FILE} = $work->{index};
$work->{state} = 1;
system("git","read-tree",$ver);
unless ($? == 0)
{
die "Error running git-read-tree $ver $!\n";
}
}
# Cleanup working directory that is not needed any longer.
sub cleanupWorkTree
{
if( ! $work->{state} )
{
return;
}
chdir "/" or die "Unable to chdir '/'\n";
if( defined($work->{workDir}) )
{
rmtree( $work->{workDir} );
undef $work->{workDir};
}
undef $work->{state};
}
# Setup a temporary directory (not a working tree), typically for
# merging dirty state as in req_update.
sub setupTmpDir
{
$work->{tmpDir} = tempdir ( DIR => $TEMP_DIR );
chdir $work->{tmpDir} or die "Unable to chdir $work->{tmpDir}\n";
return $work->{tmpDir};
}
# Clean up a previously setupTmpDir. Restore previous work tree if
# appropriate.
sub cleanupTmpDir
{
if ( !defined($work->{tmpDir}) )
{
$log->warn("cleanup tmpdir that has not been setup");
die "Cleanup tmpDir that has not been setup\n";
}
if( defined($work->{state}) )
{
if( $work->{state} == 1 )
{
chdir $work->{emptyDir} or
die "Unable to chdir to $work->{emptyDir}\n";
}
elsif( $work->{state} == 2 )
{
chdir $work->{workDir} or
die "Unable to chdir to $work->{emptyDir}\n";
}
else
{
$log->warn("Inconsistent work dir state");
die "Inconsistent work dir state\n";
}
}
else
{
chdir "/" or die "Unable to chdir '/'\n";
}
}
2007-02-27 21:46:55 +08:00
# Given a path, this function returns a string containing the kopts
# that should go into that path's Entries line. For example, a binary
# file should get -kb.
sub kopts_from_path
{
2008-05-15 12:35:48 +08:00
my ($path, $srcType, $name) = @_;
2007-02-27 21:46:55 +08:00
2008-05-15 12:35:47 +08:00
if ( defined ( $cfg->{gitcvs}{usecrlfattr} ) and
$cfg->{gitcvs}{usecrlfattr} =~ /\s*(1|true|yes)\s*$/i )
{
2010-05-20 04:43:11 +08:00
my ($val) = check_attr( "text", $path );
if ( $val eq "unspecified" )
2008-05-15 12:35:47 +08:00
{
2010-05-20 04:43:11 +08:00
$val = check_attr( "crlf", $path );
2008-05-15 12:35:47 +08:00
}
2010-05-20 04:43:11 +08:00
if ( $val eq "unset" )
2008-05-15 12:35:47 +08:00
{
return "-kb"
}
2010-05-20 04:43:11 +08:00
elsif ( check_attr( "eol", $path ) ne "unspecified" ||
$val eq "set" || $val eq "input" )
{
return "";
}
2008-05-15 12:35:47 +08:00
else
{
$log->info("Unrecognized check_attr crlf $path : $val");
}
}
2007-02-27 21:46:55 +08:00
2008-05-15 12:35:48 +08:00
if ( defined ( $cfg->{gitcvs}{allbinary} ) )
2007-02-27 21:46:55 +08:00
{
2008-05-15 12:35:48 +08:00
if( ($cfg->{gitcvs}{allbinary} =~ /^\s*(1|true|yes)\s*$/i) )
{
return "-kb";
}
elsif( ($cfg->{gitcvs}{allbinary} =~ /^\s*guess\s*$/i) )
{
2012-10-14 13:42:15 +08:00
if( is_binary($srcType,$name) )
2008-05-15 12:35:48 +08:00
{
2012-10-14 13:42:15 +08:00
$log->debug("... as binary");
return "-kb";
2008-05-15 12:35:48 +08:00
}
else
{
2012-10-14 13:42:15 +08:00
$log->debug("... as text");
2008-05-15 12:35:48 +08:00
}
}
2007-02-27 21:46:55 +08:00
}
2008-05-15 12:35:48 +08:00
# Return "" to give no special treatment to any path
return "";
2007-02-27 21:46:55 +08:00
}
2008-05-15 12:35:47 +08:00
sub check_attr
{
my ($attr,$path) = @_;
ensureWorkTree();
if ( open my $fh, '-|', "git", "check-attr", $attr, "--", $path )
{
my $val = <$fh>;
close $fh;
$val =~ s/.*: ([^:\r\n]*)\s*$/$1/;
return $val;
}
else
{
return undef;
}
}
2008-05-15 12:35:48 +08:00
# This should have the same heuristics as convert.c:is_binary() and related.
# Note that the bare CR test is done by callers in convert.c.
sub is_binary
{
my ($srcType,$name) = @_;
$log->debug("is_binary($srcType,$name)");
# Minimize amount of interpreted code run in the inner per-character
# loop for large files, by totalling each character value and
# then analyzing the totals.
my @counts;
my $i;
for($i=0;$i<256;$i++)
{
$counts[$i]=0;
}
my $fh = open_blob_or_die($srcType,$name);
my $line;
while( defined($line=<$fh>) )
{
# Any '\0' and bare CR are considered binary.
if( $line =~ /\0|(\r[^\n])/ )
{
close($fh);
return 1;
}
# Count up each character in the line:
my $len=length($line);
for($i=0;$i<$len;$i++)
{
$counts[ord(substr($line,$i,1))]++;
}
}
close $fh;
# Don't count CR and LF as either printable/nonprintable
$counts[ord("\n")]=0;
$counts[ord("\r")]=0;
# Categorize individual character count into printable and nonprintable:
my $printable=0;
my $nonprintable=0;
for($i=0;$i<256;$i++)
{
if( $i < 32 &&
$i != ord("\b") &&
$i != ord("\t") &&
$i != 033 && # ESC
$i != 014 ) # FF
{
$nonprintable+=$counts[$i];
}
elsif( $i==127 ) # DEL
{
$nonprintable+=$counts[$i];
}
else
{
$printable+=$counts[$i];
}
}
return ($printable >> 7) < $nonprintable;
}
# Returns open file handle. Possible invocations:
# - open_blob_or_die("file",$filename);
# - open_blob_or_die("sha1",$filehash);
sub open_blob_or_die
{
my ($srcType,$name) = @_;
my ($fh);
if( $srcType eq "file" )
{
if( !open $fh,"<",$name )
{
$log->warn("Unable to open file $name: $!");
die "Unable to open file $name: $!\n";
}
}
2012-10-14 13:42:15 +08:00
elsif( $srcType eq "sha1" )
2008-05-15 12:35:48 +08:00
{
2020-06-23 02:04:16 +08:00
unless ( defined ( $name ) and $name =~ /^[a-zA-Z0-9]{$state->{hexsz}}$/ )
2008-05-15 12:35:48 +08:00
{
$log->warn("Need filehash");
die "Need filehash\n";
}
2017-09-11 13:45:09 +08:00
my $type = safe_pipe_capture('git', 'cat-file', '-t', $name);
2008-05-15 12:35:48 +08:00
chomp $type;
unless ( defined ( $type ) and $type eq "blob" )
{
$log->warn("Invalid type '$type' for '$name'");
die ( "Invalid type '$type' (expected 'blob')" )
}
2017-09-11 13:45:09 +08:00
my $size = safe_pipe_capture('git', 'cat-file', '-s', $name);
2008-05-15 12:35:48 +08:00
chomp $size;
$log->debug("open_blob_or_die($name) size=$size, type=$type");
unless( open $fh, '-|', "git", "cat-file", "blob", $name )
{
$log->warn("Unable to open sha1 $name");
die "Unable to open sha1 $name\n";
}
}
else
{
$log->warn("Unknown type of blob source: $srcType");
die "Unknown type of blob source: $srcType\n";
}
return $fh;
}
2009-01-02 23:40:14 +08:00
# Generate a CVS author name from Git author information, by taking the local
# part of the email address and replacing characters not in the Portable
# Filename Character Set (see IEEE Std 1003.1-2001, 3.276) by underscores. CVS
# Login names are Unix login names, which should be restricted to this
# character set.
2008-03-28 06:18:35 +08:00
sub cvs_author
{
my $author_line = shift;
2009-01-02 23:40:14 +08:00
(my $author) = $author_line =~ /<([^@>]*)/;
$author =~ s/[^-a-zA-Z0-9_.]/_/g;
$author =~ s/^-/_/;
2008-03-28 06:18:35 +08:00
$author;
}
2010-05-15 23:06:46 +08:00
sub descramble
{
# This table is from src/scramble.c in the CVS source
my @SHIFTS = (
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
114,120, 53, 79, 96,109, 72,108, 70, 64, 76, 67,116, 74, 68, 87,
111, 52, 75,119, 49, 34, 82, 81, 95, 65,112, 86,118,110,122,105,
41, 57, 83, 43, 46,102, 40, 89, 38,103, 45, 50, 42,123, 91, 35,
125, 55, 54, 66,124,126, 59, 47, 92, 71,115, 78, 88,107,106, 56,
36,121,117,104,101,100, 69, 73, 99, 63, 94, 93, 39, 37, 61, 48,
58,113, 32, 90, 44, 98, 60, 51, 33, 97, 62, 77, 84, 80, 85,223,
225,216,187,166,229,189,222,188,141,249,148,200,184,136,248,190,
199,170,181,204,138,232,218,183,255,234,220,247,213,203,226,193,
174,172,228,252,217,201,131,230,197,211,145,238,161,179,160,212,
207,221,254,173,202,146,224,151,140,196,205,130,135,133,143,246,
192,159,244,239,185,168,215,144,139,165,180,157,147,186,214,176,
227,231,219,169,175,156,206,198,129,164,150,210,154,177,134,127,
182,128,158,208,162,132,167,209,149,241,153,251,237,236,171,195,
243,233,253,240,194,250,191,155,142,137,245,235,163,242,178,152
);
my ($str) = @_;
2010-06-20 00:06:57 +08:00
# This should never happen, the same password format (A) has been
2010-05-15 23:06:46 +08:00
# used by CVS since the beginning of time
2010-06-20 00:06:58 +08:00
{
my $fmt = substr($str, 0, 1);
die "invalid password format `$fmt'" unless $fmt eq 'A';
}
2010-05-15 23:06:46 +08:00
my @str = unpack "C*", substr($str, 1);
my $ret = join '', map { chr $SHIFTS[$_] } @str;
return $ret;
}
2012-10-14 13:42:31 +08:00
# Test if the (deep) values of two references to a hash are the same.
sub refHashEqual
{
my($v1,$v2) = @_;
my $out;
if(!defined($v1))
{
if(!defined($v2))
{
$out=1;
}
}
elsif( !defined($v2) ||
scalar(keys(%{$v1})) != scalar(keys(%{$v2})) )
{
# $out=undef;
}
else
{
$out=1;
my $key;
foreach $key (keys(%{$v1}))
{
if( !exists($v2->{$key}) ||
defined($v1->{$key}) ne defined($v2->{$key}) ||
( defined($v1->{$key}) &&
$v1->{$key} ne $v2->{$key} ) )
{
$out=undef;
last;
}
}
}
return $out;
}
2017-09-11 13:44:24 +08:00
# an alternative to `command` that allows input to be passed as an array
# to work around shell problems with weird characters in arguments
sub safe_pipe_capture {
my @output;
if (my $pid = open my $child, '-|') {
@output = (<$child>);
close $child or die join(' ',@_).": $! $?";
} else {
exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
}
return wantarray ? @output : join('',@output);
}
2010-05-15 23:06:46 +08:00
2006-02-22 17:50:15 +08:00
package GITCVS::log;
####
#### Copyright The Open University UK - 2006.
####
#### Authors: Martyn Smith <martyn@catalyst.net.nz>
2010-10-06 03:44:08 +08:00
#### Martin Langhoff <martin@laptop.org>
2006-02-22 17:50:15 +08:00
####
####
use strict;
use warnings;
=head1 NAME
GITCVS::log
=head1 DESCRIPTION
This module provides very crude logging with a similar interface to
Log::Log4perl
=head1 METHODS
=cut
=head2 new
Creates a new log object, optionally you can specify a filename here to
2006-04-26 14:59:28 +08:00
indicate the file to log to. If no log file is specified, you can specify one
2006-02-22 17:50:15 +08:00
later with method setfile, or indicate you no longer want logging with method
nofile.
Until one of these methods is called, all log calls will buffer messages ready
to write out.
=cut
sub new
{
my $class = shift;
my $filename = shift;
my $self = {};
bless $self, $class;
if ( defined ( $filename ) )
{
open $self->{fh}, ">>", $filename or die("Couldn't open '$filename' for writing : $!");
}
return $self;
}
=head2 setfile
This methods takes a filename, and attempts to open that file as the log file.
If successful, all buffered data is written out to the file, and any further
logging is written directly to the file.
=cut
sub setfile
{
my $self = shift;
my $filename = shift;
if ( defined ( $filename ) )
{
open $self->{fh}, ">>", $filename or die("Couldn't open '$filename' for writing : $!");
}
return unless ( defined ( $self->{buffer} ) and ref $self->{buffer} eq "ARRAY" );
while ( my $line = shift @{$self->{buffer}} )
{
print {$self->{fh}} $line;
}
}
=head2 nofile
This method indicates no logging is going to be used. It flushes any entries in
the internal buffer, and sets a flag to ensure no further data is put there.
=cut
sub nofile
{
my $self = shift;
$self->{nolog} = 1;
return unless ( defined ( $self->{buffer} ) and ref $self->{buffer} eq "ARRAY" );
$self->{buffer} = [];
}
=head2 _logopen
Internal method. Returns true if the log file is open, false otherwise.
=cut
sub _logopen
{
my $self = shift;
return 1 if ( defined ( $self->{fh} ) and ref $self->{fh} eq "GLOB" );
return 0;
}
=head2 debug info warn fatal
These four methods are wrappers to _log. They provide the actual interface for
logging data.
=cut
sub debug { my $self = shift; $self->_log("debug", @_); }
sub info { my $self = shift; $self->_log("info" , @_); }
sub warn { my $self = shift; $self->_log("warn" , @_); }
sub fatal { my $self = shift; $self->_log("fatal", @_); }
=head2 _log
This is an internal method called by the logging functions. It generates a
timestamp and pushes the logged line either to file, or internal buffer.
=cut
sub _log
{
my $self = shift;
my $level = shift;
return if ( $self->{nolog} );
my @time = localtime;
my $timestring = sprintf("%4d-%02d-%02d %02d:%02d:%02d : %-5s",
$time[5] + 1900,
$time[4] + 1,
$time[3],
$time[2],
$time[1],
$time[0],
uc $level,
);
if ( $self->_logopen )
{
print {$self->{fh}} $timestring . " - " . join(" ",@_) . "\n";
} else {
push @{$self->{buffer}}, $timestring . " - " . join(" ",@_) . "\n";
}
}
=head2 DESTROY
This method simply closes the file handle if one is open
=cut
sub DESTROY
{
my $self = shift;
if ( $self->_logopen )
{
close $self->{fh};
}
}
package GITCVS::updater;
####
#### Copyright The Open University UK - 2006.
####
#### Authors: Martyn Smith <martyn@catalyst.net.nz>
2010-10-06 03:44:08 +08:00
#### Martin Langhoff <martin@laptop.org>
2006-02-22 17:50:15 +08:00
####
####
use strict;
use warnings;
use DBI;
2021-10-29 08:15:52 +08:00
our $_use_fsync;
# n.b. consider using Git.pm
sub use_fsync {
if (!defined($_use_fsync)) {
my $x = $ENV{GIT_TEST_FSYNC};
if (defined $x) {
local $ENV{GIT_CONFIG};
delete $ENV{GIT_CONFIG};
my $v = ::safe_pipe_capture('git', '-c', "test.fsync=$x",
qw(config --type=bool test.fsync));
$_use_fsync = defined($v) ? ($v eq "true\n") : 1;
}
}
$_use_fsync;
}
2006-02-22 17:50:15 +08:00
=head1 METHODS
=cut
=head2 new
=cut
sub new
{
my $class = shift;
my $config = shift;
my $module = shift;
my $log = shift;
die "Need to specify a git repository" unless ( defined($config) and -d $config );
die "Need to specify a module" unless ( defined($module) );
$class = ref($class) || $class;
my $self = {};
bless $self, $class;
2008-03-28 05:02:14 +08:00
$self->{valid_tables} = {'revision' => 1,
'revision_ix1' => 1,
'revision_ix2' => 1,
'head' => 1,
'head_ix1' => 1,
'properties' => 1,
'commitmsgs' => 1};
2006-02-22 17:50:15 +08:00
$self->{module} = $module;
$self->{git_path} = $config . "/";
$self->{log} = $log;
die "Git repo '$self->{git_path}' doesn't exist" unless ( -d $self->{git_path} );
2012-10-14 13:42:27 +08:00
# Stores full sha1's for various branch/tag names, abbreviations, etc:
$self->{commitRefCache} = {};
2007-03-19 23:56:00 +08:00
$self->{dbdriver} = $cfg->{gitcvs}{$state->{method}}{dbdriver} ||
2007-04-07 22:58:09 +08:00
$cfg->{gitcvs}{dbdriver} || "SQLite";
2007-03-19 23:56:00 +08:00
$self->{dbname} = $cfg->{gitcvs}{$state->{method}}{dbname} ||
$cfg->{gitcvs}{dbname} || "%Ggitcvs.%m.sqlite";
$self->{dbuser} = $cfg->{gitcvs}{$state->{method}}{dbuser} ||
$cfg->{gitcvs}{dbuser} || "";
$self->{dbpass} = $cfg->{gitcvs}{$state->{method}}{dbpass} ||
$cfg->{gitcvs}{dbpass} || "";
2008-03-28 05:02:14 +08:00
$self->{dbtablenameprefix} = $cfg->{gitcvs}{$state->{method}}{dbtablenameprefix} ||
$cfg->{gitcvs}{dbtablenameprefix} || "";
2007-03-19 23:56:00 +08:00
my %mapping = ( m => $module,
a => $state->{method},
u => getlogin || getpwuid($<) || $<,
G => $self->{git_path},
g => mangle_dirname($self->{git_path}),
);
$self->{dbname} =~ s/%([mauGg])/$mapping{$1}/eg;
$self->{dbuser} =~ s/%([mauGg])/$mapping{$1}/eg;
2008-03-28 05:02:14 +08:00
$self->{dbtablenameprefix} =~ s/%([mauGg])/$mapping{$1}/eg;
$self->{dbtablenameprefix} = mangle_tablename($self->{dbtablenameprefix});
2007-03-19 23:56:00 +08:00
2007-04-07 22:58:09 +08:00
die "Invalid char ':' in dbdriver" if $self->{dbdriver} =~ /:/;
die "Invalid char ';' in dbname" if $self->{dbname} =~ /;/;
$self->{dbh} = DBI->connect("dbi:$self->{dbdriver}:dbname=$self->{dbname}",
2007-03-19 23:56:00 +08:00
$self->{dbuser},
$self->{dbpass});
2007-03-19 23:56:01 +08:00
die "Error connecting to database\n" unless defined $self->{dbh};
2021-10-29 08:15:52 +08:00
if ($self->{dbdriver} eq 'SQLite' && !use_fsync()) {
$self->{dbh}->do('PRAGMA synchronous = OFF');
}
2006-02-22 17:50:15 +08:00
$self->{tables} = {};
2007-03-31 21:57:47 +08:00
foreach my $table ( keys %{$self->{dbh}->table_info(undef,undef,undef,'TABLE')->fetchall_hashref('TABLE_NAME')} )
2006-02-22 17:50:15 +08:00
{
$self->{tables}{$table} = 1;
}
# Construct the revision table if required
2012-10-14 13:42:16 +08:00
# The revision table stores an entry for each file, each time that file
# changes.
# numberOfRecords = O( numCommits * averageNumChangedFilesPerCommit )
# This is not sufficient to support "-r {commithash}" for any
# files except files that were modified by that commit (also,
# some places in the code ignore/effectively strip out -r in
# some cases, before it gets passed to getmeta()).
# The "filehash" field typically has a git blob hash, but can also
# be set to "dead" to indicate that the given version of the file
# should not exist in the sandbox.
2008-03-28 05:02:14 +08:00
unless ( $self->{tables}{$self->tablename("revision")} )
2006-02-22 17:50:15 +08:00
{
2008-03-28 05:02:14 +08:00
my $tablename = $self->tablename("revision");
my $ix1name = $self->tablename("revision_ix1");
my $ix2name = $self->tablename("revision_ix2");
2006-02-22 17:50:15 +08:00
$self->{dbh}->do("
2008-03-28 05:02:14 +08:00
CREATE TABLE $tablename (
2006-02-22 17:50:15 +08:00
name TEXT NOT NULL,
revision INTEGER NOT NULL,
filehash TEXT NOT NULL,
commithash TEXT NOT NULL,
author TEXT NOT NULL,
modified TEXT NOT NULL,
mode TEXT NOT NULL
)
");
2006-10-23 13:09:35 +08:00
$self->{dbh}->do("
2008-03-28 05:02:14 +08:00
CREATE INDEX $ix1name
ON $tablename (name,revision)
2006-10-23 13:09:35 +08:00
");
$self->{dbh}->do("
2008-03-28 05:02:14 +08:00
CREATE INDEX $ix2name
ON $tablename (name,commithash)
2006-10-23 13:09:35 +08:00
");
2006-02-22 17:50:15 +08:00
}
2006-10-23 13:09:35 +08:00
# Construct the head table if required
2012-10-14 13:42:16 +08:00
# The head table (along with the "last_commit" entry in the property
# table) is the persisted working state of the "sub update" subroutine.
# All of it's data is read entirely first, and completely recreated
# last, every time "sub update" runs.
# This is also used by "sub getmeta" when it is asked for the latest
# version of a file (as opposed to some specific version).
# Another way of thinking about it is as a single slice out of
# "revisions", giving just the most recent revision information for
# each file.
2008-03-28 05:02:14 +08:00
unless ( $self->{tables}{$self->tablename("head")} )
2006-02-22 17:50:15 +08:00
{
2008-03-28 05:02:14 +08:00
my $tablename = $self->tablename("head");
my $ix1name = $self->tablename("head_ix1");
2006-02-22 17:50:15 +08:00
$self->{dbh}->do("
2008-03-28 05:02:14 +08:00
CREATE TABLE $tablename (
2006-02-22 17:50:15 +08:00
name TEXT NOT NULL,
revision INTEGER NOT NULL,
filehash TEXT NOT NULL,
commithash TEXT NOT NULL,
author TEXT NOT NULL,
modified TEXT NOT NULL,
mode TEXT NOT NULL
)
");
2006-10-23 13:09:35 +08:00
$self->{dbh}->do("
2008-03-28 05:02:14 +08:00
CREATE INDEX $ix1name
ON $tablename (name)
2006-10-23 13:09:35 +08:00
");
2006-02-22 17:50:15 +08:00
}
# Construct the properties table if required
2012-10-14 13:42:16 +08:00
# - "last_commit" - Used by "sub update".
2008-03-28 05:02:14 +08:00
unless ( $self->{tables}{$self->tablename("properties")} )
2006-02-22 17:50:15 +08:00
{
2008-03-28 05:02:14 +08:00
my $tablename = $self->tablename("properties");
2006-02-22 17:50:15 +08:00
$self->{dbh}->do("
2008-03-28 05:02:14 +08:00
CREATE TABLE $tablename (
2006-02-22 17:50:15 +08:00
key TEXT NOT NULL PRIMARY KEY,
value TEXT
)
");
}
# Construct the commitmsgs table if required
2012-10-14 13:42:16 +08:00
# The commitmsgs table is only used for merge commits, since
# "sub update" will only keep one branch of parents. Shortlogs
# for ignored commits (i.e. not on the chosen branch) will be used
# to construct a replacement "collapsed" merge commit message,
# which will be stored in this table. See also "sub commitmessage".
2008-03-28 05:02:14 +08:00
unless ( $self->{tables}{$self->tablename("commitmsgs")} )
2006-02-22 17:50:15 +08:00
{
2008-03-28 05:02:14 +08:00
my $tablename = $self->tablename("commitmsgs");
2006-02-22 17:50:15 +08:00
$self->{dbh}->do("
2008-03-28 05:02:14 +08:00
CREATE TABLE $tablename (
2006-02-22 17:50:15 +08:00
key TEXT NOT NULL PRIMARY KEY,
value TEXT
)
");
}
return $self;
}
2008-03-28 05:02:14 +08:00
=head2 tablename
=cut
sub tablename
{
my $self = shift;
my $name = shift;
if (exists $self->{valid_tables}{$name}) {
return $self->{dbtablenameprefix} . $name;
} else {
return undef;
}
}
2006-02-22 17:50:15 +08:00
=head2 update
2012-10-14 13:42:16 +08:00
Bring the database up to date with the latest changes from
the git repository.
Internal working state is read out of the "head" table and the
"last_commit" property, then it updates "revisions" based on that, and
finally it writes the new internal state back to the "head" table
so it can be used as a starting point the next time update is called.
2006-02-22 17:50:15 +08:00
=cut
sub update
{
my $self = shift;
# first lets get the commit list
$ENV{GIT_DIR} = $self->{git_path};
2017-09-11 13:45:09 +08:00
my $commitsha1 = ::safe_pipe_capture('git', 'rev-parse', $self->{module});
2007-01-09 10:10:32 +08:00
chomp $commitsha1;
2017-09-11 13:45:09 +08:00
my $commitinfo = ::safe_pipe_capture('git', 'cat-file', 'commit', $self->{module});
2020-06-23 02:04:16 +08:00
unless ( $commitinfo =~ /tree\s+[a-zA-Z0-9]{$state->{hexsz}}/ )
2006-02-22 17:50:15 +08:00
{
die("Invalid module '$self->{module}'");
}
my $git_log;
my $lastcommit = $self->_get_prop("last_commit");
2007-01-09 10:10:32 +08:00
if (defined $lastcommit && $lastcommit eq $commitsha1) { # up-to-date
2012-10-14 13:42:31 +08:00
# invalidate the gethead cache
$self->clearCommitRefCaches();
2007-01-09 10:10:32 +08:00
return 1;
}
2006-02-22 17:50:15 +08:00
# Start exclusive lock here...
$self->{dbh}->begin_work() or die "Cannot lock database for BEGIN";
# TODO: log processing is memory bound
# if we can parse into a 2nd file that is in reverse order
# we can probably do something really efficient
2006-05-04 06:51:46 +08:00
my @git_log_params = ('--pretty', '--parents', '--topo-order');
2006-02-22 17:50:15 +08:00
if (defined $lastcommit) {
push @git_log_params, "$lastcommit..$self->{module}";
} else {
push @git_log_params, $self->{module};
}
2006-05-04 06:51:46 +08:00
# git-rev-list is the backend / plumbing version of git-log
2012-10-14 13:42:24 +08:00
open(my $gitLogPipe, '-|', 'git', 'rev-list', @git_log_params)
or die "Cannot call git-rev-list: $!";
my @commits=readCommits($gitLogPipe);
close $gitLogPipe;
2006-02-22 17:50:15 +08:00
# Now all the commits are in the @commits bucket
# ordered by time DESC. for each commit that needs processing,
# determine whether it's following the last head we've seen or if
# it's on its own branch, grab a file list, and add whatever's changed
# NOTE: $lastcommit refers to the last commit from previous run
# $lastpicked is the last commit we picked in this run
my $lastpicked;
my $head = {};
if (defined $lastcommit) {
$lastpicked = $lastcommit;
}
my $committotal = scalar(@commits);
my $commitcount = 0;
# Load the head table into $head (for cached lookups during the update process)
2012-10-14 13:42:21 +08:00
foreach my $file ( @{$self->gethead(1)} )
2006-02-22 17:50:15 +08:00
{
$head->{$file->{name}} = $file;
}
foreach my $commit ( @commits )
{
$self->{log}->debug("GITCVS::updater - Processing commit $commit->{hash} (" . (++$commitcount) . " of $committotal)");
if (defined $lastpicked)
{
if (!in_array($lastpicked, @{$commit->{parents}}))
{
# skip, we'll see this delta
# as part of a merge later
# warn "skipping off-track $commit->{hash}\n";
next;
} elsif (@{$commit->{parents}} > 1) {
# it is a merge commit, for each parent that is
2012-10-14 13:42:16 +08:00
# not $lastpicked (not given a CVS revision number),
# see if we can get a log
2006-02-22 17:50:15 +08:00
# from the merge-base to that parent to put it
# in the message as a merge summary.
my @parents = @{$commit->{parents}};
foreach my $parent (@parents) {
if ($parent eq $lastpicked) {
next;
}
2012-10-14 13:42:16 +08:00
# git-merge-base can potentially (but rarely) throw
# several candidate merge bases. let's assume
# that the first one is the best one.
2008-01-26 17:54:06 +08:00
my $base = eval {
2017-09-11 13:44:24 +08:00
::safe_pipe_capture('git', 'merge-base',
2007-07-15 02:48:42 +08:00
$lastpicked, $parent);
2008-01-26 17:54:06 +08:00
};
# The two branches may not be related at all,
# in which case merge base simply fails to find
# any, but that's Ok.
next if ($@);
2006-02-22 17:50:15 +08:00
chomp $base;
if ($base) {
my @merged;
# print "want to log between $base $parent \n";
2009-09-02 17:23:10 +08:00
open(GITLOG, '-|', 'git', 'log', '--pretty=medium', "$base..$parent")
2007-07-15 02:48:42 +08:00
or die "Cannot call git-log: $!";
2006-02-22 17:50:15 +08:00
my $mergedhash;
while (<GITLOG>) {
chomp;
if (!defined $mergedhash) {
if (m/^commit\s+(.+)$/) {
$mergedhash = $1;
} else {
next;
}
} else {
# grab the first line that looks non-rfc822
# aka has content after leading space
if (m/^\s+(\S.*)$/) {
my $title = $1;
$title = substr($title,0,100); # truncate
unshift @merged, "$mergedhash $title";
undef $mergedhash;
}
}
}
close GITLOG;
if (@merged) {
$commit->{mergemsg} = $commit->{message};
$commit->{mergemsg} .= "\nSummary of merged commits:\n\n";
foreach my $summary (@merged) {
$commit->{mergemsg} .= "\t$summary\n";
}
$commit->{mergemsg} .= "\n\n";
# print "Message for $commit->{hash} \n$commit->{mergemsg}";
}
}
}
}
}
# convert the date to CVS-happy format
2012-10-14 13:42:24 +08:00
my $cvsDate = convertToCvsDate($commit->{date});
2006-02-22 17:50:15 +08:00
if ( defined ( $lastpicked ) )
{
2009-09-02 17:23:10 +08:00
my $filepipe = open(FILELIST, '-|', 'git', 'diff-tree', '-z', '-r', $lastpicked, $commit->{hash}) or die("Cannot call git-diff-tree : $!");
2006-11-11 03:53:41 +08:00
local ($/) = "\0";
2006-02-22 17:50:15 +08:00
while ( <FILELIST> )
{
2006-11-11 03:53:41 +08:00
chomp;
2020-06-23 02:04:16 +08:00
unless ( /^:\d{6}\s+([0-7]{6})\s+[a-f0-9]{$state->{hexsz}}\s+([a-f0-9]{$state->{hexsz}})\s+(\w)$/o )
2006-02-22 17:50:15 +08:00
{
die("Couldn't process git-diff-tree line : $_");
}
2006-11-11 03:53:41 +08:00
my ($mode, $hash, $change) = ($1, $2, $3);
my $name = <FILELIST>;
chomp($name);
2006-02-22 17:50:15 +08:00
2006-11-11 03:53:41 +08:00
# $log->debug("File mode=$mode, hash=$hash, change=$change, name=$name");
2006-02-22 17:50:15 +08:00
2012-10-14 13:42:24 +08:00
my $dbMode = convertToDbMode($mode);
2006-02-22 17:50:15 +08:00
2006-11-11 03:53:41 +08:00
if ( $change eq "D" )
2006-02-22 17:50:15 +08:00
{
2006-11-11 03:53:41 +08:00
#$log->debug("DELETE $name");
$head->{$name} = {
name => $name,
revision => $head->{$name}{revision} + 1,
2006-02-22 17:50:15 +08:00
filehash => "deleted",
commithash => $commit->{hash},
2012-10-14 13:42:24 +08:00
modified => $cvsDate,
2006-02-22 17:50:15 +08:00
author => $commit->{author},
2012-10-14 13:42:24 +08:00
mode => $dbMode,
2006-02-22 17:50:15 +08:00
};
2012-10-14 13:42:24 +08:00
$self->insert_rev($name, $head->{$name}{revision}, $hash, $commit->{hash}, $cvsDate, $commit->{author}, $dbMode);
2006-02-22 17:50:15 +08:00
}
2008-03-17 03:00:21 +08:00
elsif ( $change eq "M" || $change eq "T" )
2006-02-22 17:50:15 +08:00
{
2006-11-11 03:53:41 +08:00
#$log->debug("MODIFIED $name");
$head->{$name} = {
name => $name,
revision => $head->{$name}{revision} + 1,
filehash => $hash,
2006-02-22 17:50:15 +08:00
commithash => $commit->{hash},
2012-10-14 13:42:24 +08:00
modified => $cvsDate,
2006-02-22 17:50:15 +08:00
author => $commit->{author},
2012-10-14 13:42:24 +08:00
mode => $dbMode,
2006-02-22 17:50:15 +08:00
};
2012-10-14 13:42:24 +08:00
$self->insert_rev($name, $head->{$name}{revision}, $hash, $commit->{hash}, $cvsDate, $commit->{author}, $dbMode);
2006-02-22 17:50:15 +08:00
}
2006-11-11 03:53:41 +08:00
elsif ( $change eq "A" )
2006-02-22 17:50:15 +08:00
{
2006-11-11 03:53:41 +08:00
#$log->debug("ADDED $name");
$head->{$name} = {
name => $name,
2007-05-02 08:43:14 +08:00
revision => $head->{$name}{revision} ? $head->{$name}{revision}+1 : 1,
2006-11-11 03:53:41 +08:00
filehash => $hash,
2006-02-22 17:50:15 +08:00
commithash => $commit->{hash},
2012-10-14 13:42:24 +08:00
modified => $cvsDate,
2006-02-22 17:50:15 +08:00
author => $commit->{author},
2012-10-14 13:42:24 +08:00
mode => $dbMode,
2006-02-22 17:50:15 +08:00
};
2012-10-14 13:42:24 +08:00
$self->insert_rev($name, $head->{$name}{revision}, $hash, $commit->{hash}, $cvsDate, $commit->{author}, $dbMode);
2006-02-22 17:50:15 +08:00
}
else
{
2006-11-11 03:53:41 +08:00
$log->warn("UNKNOWN FILE CHANGE mode=$mode, hash=$hash, change=$change, name=$name");
2006-02-22 17:50:15 +08:00
die;
}
}
close FILELIST;
} else {
# this is used to detect files removed from the repo
my $seen_files = {};
2009-09-02 17:23:10 +08:00
my $filepipe = open(FILELIST, '-|', 'git', 'ls-tree', '-z', '-r', $commit->{hash}) or die("Cannot call git-ls-tree : $!");
2006-11-11 03:53:41 +08:00
local $/ = "\0";
2006-02-22 17:50:15 +08:00
while ( <FILELIST> )
{
2006-11-11 03:53:41 +08:00
chomp;
unless ( /^(\d+)\s+(\w+)\s+([a-zA-Z0-9]+)\t(.*)$/o )
2006-02-22 17:50:15 +08:00
{
die("Couldn't process git-ls-tree line : $_");
}
2012-10-14 13:42:24 +08:00
my ( $mode, $git_type, $git_hash, $git_filename ) = ( $1, $2, $3, $4 );
2006-02-22 17:50:15 +08:00
$seen_files->{$git_filename} = 1;
my ( $oldhash, $oldrevision, $oldmode ) = (
$head->{$git_filename}{filehash},
$head->{$git_filename}{revision},
$head->{$git_filename}{mode}
);
2012-10-14 13:42:24 +08:00
my $dbMode = convertToDbMode($mode);
2006-02-22 17:50:15 +08:00
# unless the file exists with the same hash, we need to update it ...
2012-10-14 13:42:24 +08:00
unless ( defined($oldhash) and $oldhash eq $git_hash and defined($oldmode) and $oldmode eq $dbMode )
2006-02-22 17:50:15 +08:00
{
my $newrevision = ( $oldrevision or 0 ) + 1;
$head->{$git_filename} = {
name => $git_filename,
revision => $newrevision,
filehash => $git_hash,
commithash => $commit->{hash},
2012-10-14 13:42:24 +08:00
modified => $cvsDate,
2006-02-22 17:50:15 +08:00
author => $commit->{author},
2012-10-14 13:42:24 +08:00
mode => $dbMode,
2006-02-22 17:50:15 +08:00
};
2012-10-14 13:42:24 +08:00
$self->insert_rev($git_filename, $newrevision, $git_hash, $commit->{hash}, $cvsDate, $commit->{author}, $dbMode);
2006-02-22 17:50:15 +08:00
}
}
close FILELIST;
# Detect deleted files
2013-10-30 16:44:43 +08:00
foreach my $file ( sort keys %$head )
2006-02-22 17:50:15 +08:00
{
unless ( exists $seen_files->{$file} or $head->{$file}{filehash} eq "deleted" )
{
$head->{$file}{revision}++;
$head->{$file}{filehash} = "deleted";
$head->{$file}{commithash} = $commit->{hash};
2012-10-14 13:42:24 +08:00
$head->{$file}{modified} = $cvsDate;
2006-02-22 17:50:15 +08:00
$head->{$file}{author} = $commit->{author};
2012-10-14 13:42:24 +08:00
$self->insert_rev($file, $head->{$file}{revision}, $head->{$file}{filehash}, $commit->{hash}, $cvsDate, $commit->{author}, $head->{$file}{mode});
2006-02-22 17:50:15 +08:00
}
}
# END : "Detect deleted files"
}
if (exists $commit->{mergemsg})
{
2006-07-25 19:57:57 +08:00
$self->insert_mergelog($commit->{hash}, $commit->{mergemsg});
2006-02-22 17:50:15 +08:00
}
$lastpicked = $commit->{hash};
$self->_set_prop("last_commit", $commit->{hash});
}
2006-07-25 19:57:57 +08:00
$self->delete_head();
2013-10-30 16:44:43 +08:00
foreach my $file ( sort keys %$head )
2006-02-22 17:50:15 +08:00
{
2006-07-25 19:57:57 +08:00
$self->insert_head(
2006-02-22 17:50:15 +08:00
$file,
$head->{$file}{revision},
$head->{$file}{filehash},
$head->{$file}{commithash},
$head->{$file}{modified},
$head->{$file}{author},
$head->{$file}{mode},
);
}
# invalidate the gethead cache
2012-10-14 13:42:27 +08:00
$self->clearCommitRefCaches();
2006-02-22 17:50:15 +08:00
# Ending exclusive lock here
$self->{dbh}->commit() or die "Failed to commit changes to SQLite";
}
2012-10-14 13:42:24 +08:00
sub readCommits
{
my $pipeHandle = shift;
my @commits;
my %commit = ();
while ( <$pipeHandle> )
{
chomp;
if (m/^commit\s+(.*)$/) {
# on ^commit lines put the just seen commit in the stack
# and prime things for the next one
if (keys %commit) {
my %copy = %commit;
unshift @commits, \%copy;
%commit = ();
}
my @parents = split(m/\s+/, $1);
$commit{hash} = shift @parents;
$commit{parents} = \@parents;
} elsif (m/^(\w+?):\s+(.*)$/ && !exists($commit{message})) {
# on rfc822-like lines seen before we see any message,
# lowercase the entry and put it in the hash as key-value
$commit{lc($1)} = $2;
} else {
# message lines - skip initial empty line
# and trim whitespace
if (!exists($commit{message}) && m/^\s*$/) {
# define it to mark the end of headers
$commit{message} = '';
next;
}
s/^\s+//; s/\s+$//; # trim ws
$commit{message} .= $_ . "\n";
}
}
unshift @commits, \%commit if ( keys %commit );
return @commits;
}
sub convertToCvsDate
{
my $date = shift;
# Convert from: "git rev-list --pretty" formatted date
# Convert to: "the format specified by RFC822 as modified by RFC1123."
# Example: 26 May 1997 13:01:40 -0400
if( $date =~ /^\w+\s+(\w+)\s+(\d+)\s+(\d+:\d+:\d+)\s+(\d+)\s+([+-]\d+)$/ )
{
$date = "$2 $1 $4 $3 $5";
}
return $date;
}
sub convertToDbMode
{
my $mode = shift;
# NOTE: The CVS protocol uses a string similar "u=rw,g=rw,o=rw",
# but the database "mode" column historically (and currently)
# only stores the "rw" (for user) part of the string.
# FUTURE: It might make more sense to persist the raw
# octal mode (or perhaps the final full CVS form) instead of
# this half-converted form, but it isn't currently worth the
# backwards compatibility headaches.
2013-09-11 06:33:06 +08:00
$mode=~/^\d{3}(\d)\d\d$/;
2012-10-14 13:42:24 +08:00
my $userBits=$1;
my $dbMode = "";
$dbMode .= "r" if ( $userBits & 4 );
$dbMode .= "w" if ( $userBits & 2 );
$dbMode .= "x" if ( $userBits & 1 );
$dbMode = "rw" if ( $dbMode eq "" );
return $dbMode;
}
2006-07-25 19:57:57 +08:00
sub insert_rev
{
my $self = shift;
my $name = shift;
my $revision = shift;
my $filehash = shift;
my $commithash = shift;
my $modified = shift;
my $author = shift;
my $mode = shift;
2008-03-28 05:02:14 +08:00
my $tablename = $self->tablename("revision");
2006-07-25 19:57:57 +08:00
2008-03-28 05:02:14 +08:00
my $insert_rev = $self->{dbh}->prepare_cached("INSERT INTO $tablename (name, revision, filehash, commithash, modified, author, mode) VALUES (?,?,?,?,?,?,?)",{},1);
2006-07-25 19:57:57 +08:00
$insert_rev->execute($name, $revision, $filehash, $commithash, $modified, $author, $mode);
}
sub insert_mergelog
{
my $self = shift;
my $key = shift;
my $value = shift;
2008-03-28 05:02:14 +08:00
my $tablename = $self->tablename("commitmsgs");
2006-07-25 19:57:57 +08:00
2008-03-28 05:02:14 +08:00
my $insert_mergelog = $self->{dbh}->prepare_cached("INSERT INTO $tablename (key, value) VALUES (?,?)",{},1);
2006-07-25 19:57:57 +08:00
$insert_mergelog->execute($key, $value);
}
sub delete_head
{
my $self = shift;
2008-03-28 05:02:14 +08:00
my $tablename = $self->tablename("head");
2006-07-25 19:57:57 +08:00
2008-03-28 05:02:14 +08:00
my $delete_head = $self->{dbh}->prepare_cached("DELETE FROM $tablename",{},1);
2006-07-25 19:57:57 +08:00
$delete_head->execute();
}
sub insert_head
{
my $self = shift;
my $name = shift;
my $revision = shift;
my $filehash = shift;
my $commithash = shift;
my $modified = shift;
my $author = shift;
my $mode = shift;
2008-03-28 05:02:14 +08:00
my $tablename = $self->tablename("head");
2006-07-25 19:57:57 +08:00
2008-03-28 05:02:14 +08:00
my $insert_head = $self->{dbh}->prepare_cached("INSERT INTO $tablename (name, revision, filehash, commithash, modified, author, mode) VALUES (?,?,?,?,?,?,?)",{},1);
2006-07-25 19:57:57 +08:00
$insert_head->execute($name, $revision, $filehash, $commithash, $modified, $author, $mode);
}
2006-02-22 17:50:15 +08:00
sub _get_prop
{
my $self = shift;
my $key = shift;
2008-03-28 05:02:14 +08:00
my $tablename = $self->tablename("properties");
2006-02-22 17:50:15 +08:00
2008-03-28 05:02:14 +08:00
my $db_query = $self->{dbh}->prepare_cached("SELECT value FROM $tablename WHERE key=?",{},1);
2006-02-22 17:50:15 +08:00
$db_query->execute($key);
my ( $value ) = $db_query->fetchrow_array;
return $value;
}
sub _set_prop
{
my $self = shift;
my $key = shift;
my $value = shift;
2008-03-28 05:02:14 +08:00
my $tablename = $self->tablename("properties");
2006-02-22 17:50:15 +08:00
2008-03-28 05:02:14 +08:00
my $db_query = $self->{dbh}->prepare_cached("UPDATE $tablename SET value=? WHERE key=?",{},1);
2006-02-22 17:50:15 +08:00
$db_query->execute($value, $key);
unless ( $db_query->rows )
{
2008-03-28 05:02:14 +08:00
$db_query = $self->{dbh}->prepare_cached("INSERT INTO $tablename (key, value) VALUES (?,?)",{},1);
2006-02-22 17:50:15 +08:00
$db_query->execute($key, $value);
}
return $value;
}
=head2 gethead
=cut
sub gethead
{
my $self = shift;
2012-10-14 13:42:21 +08:00
my $intRev = shift;
2008-03-28 05:02:14 +08:00
my $tablename = $self->tablename("head");
2006-02-22 17:50:15 +08:00
return $self->{gethead_cache} if ( defined ( $self->{gethead_cache} ) );
2008-03-28 05:02:14 +08:00
my $db_query = $self->{dbh}->prepare_cached("SELECT name, filehash, mode, revision, modified, commithash, author FROM $tablename ORDER BY name ASC",{},1);
2006-02-22 17:50:15 +08:00
$db_query->execute();
my $tree = [];
while ( my $file = $db_query->fetchrow_hashref )
{
2012-10-14 13:42:21 +08:00
if(!$intRev)
{
$file->{revision} = "1.$file->{revision}"
}
2006-02-22 17:50:15 +08:00
push @$tree, $file;
}
$self->{gethead_cache} = $tree;
return $tree;
}
2012-10-14 13:42:27 +08:00
=head2 getAnyHead
Returns a reference to an array of getmeta structures, one
per file in the specified tree hash.
=cut
sub getAnyHead
{
my ($self,$hash) = @_;
if(!defined($hash))
{
return $self->gethead();
}
my @files;
{
open(my $filePipe, '-|', 'git', 'ls-tree', '-z', '-r', $hash)
or die("Cannot call git-ls-tree : $!");
local $/ = "\0";
@files=<$filePipe>;
close $filePipe;
}
my $tree=[];
my($line);
foreach $line (@files)
{
$line=~s/\0$//;
unless ( $line=~/^(\d+)\s+(\w+)\s+([a-zA-Z0-9]+)\t(.*)$/o )
{
die("Couldn't process git-ls-tree line : $_");
}
my($mode, $git_type, $git_hash, $git_filename) = ($1, $2, $3, $4);
push @$tree, $self->getMetaFromCommithash($git_filename,$hash);
}
return $tree;
}
=head2 getRevisionDirMap
A "revision dir map" contains all the plain-file filenames associated
2013-09-05 03:04:30 +08:00
with a particular revision (tree-ish), organized by directory:
2012-10-14 13:42:27 +08:00
$type = $out->{$dir}{$fullName}
The type of each is "F" (for ordinary file) or "D" (for directory,
for which the map $out->{$fullName} will also exist).
=cut
sub getRevisionDirMap
{
my ($self,$ver)=@_;
if(!defined($self->{revisionDirMapCache}))
{
$self->{revisionDirMapCache}={};
}
# Get file list (previously cached results are dependent on HEAD,
# but are early in each case):
my $cacheKey;
my (@fileList);
if( !defined($ver) || $ver eq "" )
{
$cacheKey="";
if( defined($self->{revisionDirMapCache}{$cacheKey}) )
{
return $self->{revisionDirMapCache}{$cacheKey};
}
my @head = @{$self->gethead()};
foreach my $file ( @head )
{
next if ( $file->{filehash} eq "deleted" );
push @fileList,$file->{name};
}
}
else
{
my ($hash)=$self->lookupCommitRef($ver);
if( !defined($hash) )
{
return undef;
}
$cacheKey=$hash;
if( defined($self->{revisionDirMapCache}{$cacheKey}) )
{
return $self->{revisionDirMapCache}{$cacheKey};
}
open(my $filePipe, '-|', 'git', 'ls-tree', '-z', '-r', $hash)
or die("Cannot call git-ls-tree : $!");
local $/ = "\0";
while ( <$filePipe> )
{
chomp;
unless ( /^(\d+)\s+(\w+)\s+([a-zA-Z0-9]+)\t(.*)$/o )
{
die("Couldn't process git-ls-tree line : $_");
}
my($mode, $git_type, $git_hash, $git_filename) = ($1, $2, $3, $4);
push @fileList, $git_filename;
}
close $filePipe;
}
# Convert to normalized form:
my %revMap;
my $file;
foreach $file (@fileList)
{
my($dir) = ($file=~m%^(?:(.*)/)?([^/]*)$%);
$dir='' if(!defined($dir));
# parent directories:
# ... create empty dir maps for parent dirs:
my($td)=$dir;
while(!defined($revMap{$td}))
{
$revMap{$td}={};
my($tp)=($td=~m%^(?:(.*)/)?([^/]*)$%);
$tp='' if(!defined($tp));
$td=$tp;
}
# ... add children to parent maps (now that they exist):
$td=$dir;
while($td ne "")
{
my($tp)=($td=~m%^(?:(.*)/)?([^/]*)$%);
$tp='' if(!defined($tp));
if(defined($revMap{$tp}{$td}))
{
if($revMap{$tp}{$td} ne 'D')
{
die "Weird file/directory inconsistency in $cacheKey";
}
last; # loop exit
}
$revMap{$tp}{$td}='D';
$td=$tp;
}
# file
$revMap{$dir}{$file}='F';
}
# Save in cache:
$self->{revisionDirMapCache}{$cacheKey}=\%revMap;
return $self->{revisionDirMapCache}{$cacheKey};
}
2006-02-22 17:50:15 +08:00
=head2 getlog
2012-10-14 13:42:18 +08:00
See also gethistorydense().
2006-02-22 17:50:15 +08:00
=cut
sub getlog
{
my $self = shift;
my $filename = shift;
2012-10-14 13:42:21 +08:00
my $revFilter = shift;
2008-03-28 05:02:14 +08:00
my $tablename = $self->tablename("revision");
2006-02-22 17:50:15 +08:00
2012-10-14 13:42:21 +08:00
# Filters:
# TODO: date, state, or by specific logins filters?
# TODO: Handle comma-separated list of revFilter items, each item
# can be a range [only case currently handled] or individual
# rev or branch or "branch.".
# TODO: Adjust $db_query WHERE clause based on revFilter, instead of
# manually filtering the results of the query?
my ( $minrev, $maxrev );
if( defined($revFilter) and
$state->{opt}{r} =~ /^(1.(\d+))?(::?)(1.(\d.+))?$/ )
{
my $control = $3;
$minrev = $2;
$maxrev = $5;
$minrev++ if ( defined($minrev) and $control eq "::" );
}
2008-03-28 05:02:14 +08:00
my $db_query = $self->{dbh}->prepare_cached("SELECT name, filehash, author, mode, revision, modified, commithash FROM $tablename WHERE name=? ORDER BY revision DESC",{},1);
2006-02-22 17:50:15 +08:00
$db_query->execute($filename);
2012-10-14 13:42:21 +08:00
my $totalRevs=0;
2006-02-22 17:50:15 +08:00
my $tree = [];
while ( my $file = $db_query->fetchrow_hashref )
{
2012-10-14 13:42:21 +08:00
$totalRevs++;
if( defined($minrev) and $file->{revision} < $minrev )
{
next;
}
if( defined($maxrev) and $file->{revision} > $maxrev )
{
next;
}
$file->{revision} = "1." . $file->{revision};
2006-02-22 17:50:15 +08:00
push @$tree, $file;
}
2012-10-14 13:42:21 +08:00
return ($tree,$totalRevs);
2006-02-22 17:50:15 +08:00
}
=head2 getmeta
This function takes a filename (with path) argument and returns a hashref of
metadata for that file.
2012-10-14 13:42:29 +08:00
There are several ways $revision can be specified:
- A reference to hash that contains a "tag" that is the
actual revision (one of the below). TODO: Also allow it to
specify a "date" in the hash.
- undef, to refer to the latest version on the main branch.
- Full CVS client revision number (mapped to integer in DB, without the
"1." prefix),
- Complex CVS-compatible "special" revision number for
non-linear history (see comment below)
- git commit sha1 hash
- branch or tag name
2006-02-22 17:50:15 +08:00
=cut
sub getmeta
{
my $self = shift;
my $filename = shift;
my $revision = shift;
2008-03-28 05:02:14 +08:00
my $tablename_rev = $self->tablename("revision");
my $tablename_head = $self->tablename("head");
2006-02-22 17:50:15 +08:00
2012-10-14 13:42:29 +08:00
if ( ref($revision) eq "HASH" )
2006-02-22 17:50:15 +08:00
{
2012-10-14 13:42:29 +08:00
$revision = $revision->{tag};
2006-02-22 17:50:15 +08:00
}
2012-10-14 13:42:29 +08:00
# Overview of CVS revision numbers:
#
# General CVS numbering scheme:
# - Basic mainline branch numbers: "1.1", "1.2", "1.3", etc.
# - Result of "cvs checkin -r" (possible, but not really
# recommended): "2.1", "2.2", etc
# - Branch tag: "1.2.0.n", where "1.2" is revision it was branched
# from, "0" is a magic placeholder that identifies it as a
# branch tag instead of a version tag, and n is 2 times the
# branch number off of "1.2", starting with "2".
# - Version on a branch: "1.2.n.x", where "1.2" is branch-from, "n"
# is branch number off of "1.2" (like n above), and "x" is
# the version number on the branch.
# - Branches can branch off of branches: "1.3.2.7.4.1" (even number
# of components).
# - Odd "n"s are used by "vendor branches" that result
# from "cvs import". Vendor branches have additional
# strangeness in the sense that the main rcs "head" of the main
# branch will (temporarily until first normal commit) point
# to the version on the vendor branch, rather than the actual
# main branch. (FUTURE: This may provide an opportunity
# to use "strange" revision numbers for fast-forward-merged
# branch tip when CVS client is asking for the main branch.)
#
# git-cvsserver CVS-compatible special numbering schemes:
# - Currently git-cvsserver only tries to be identical to CVS for
# simple "1.x" numbers on the "main" branch (as identified
# by the module name that was originally cvs checkout'ed).
# - The database only stores the "x" part, for historical reasons.
# But most of the rest of the cvsserver preserves
# and thinks using the full revision number.
# - To handle non-linear history, it uses a version of the form
# "2.1.1.2000.b.b.b."..., where the 2.1.1.2000 is to help uniquely
# identify this as a special revision number, and there are
# 20 b's that together encode the sha1 git commit from which
# this version of this file originated. Each b is
# the numerical value of the corresponding byte plus
# 100.
# - "plus 100" avoids "0"s, and also reduces the
2013-04-12 06:36:10 +08:00
# likelihood of a collision in the case that someone someday
2012-10-14 13:42:29 +08:00
# writes an import tool that tries to preserve original
# CVS revision numbers, and the original CVS data had done
# lots of branches off of branches and other strangeness to
# end up with a real version number that just happens to look
# like this special revision number form. Also, if needed
# there are several ways to extend/identify alternative encodings
# within the "2.1.1.2000" part if necessary.
# - Unlike real CVS revisions, you can't really reconstruct what
# relation a revision of this form has to other revisions.
# - FUTURE: TODO: Rework database somehow to make up and remember
# fully-CVS-compatible branches and branch version numbers.
my $meta;
if ( defined($revision) )
2006-02-22 17:50:15 +08:00
{
2012-10-14 13:42:29 +08:00
if ( $revision =~ /^1\.(\d+)$/ )
{
my ($intRev) = $1;
my $db_query;
$db_query = $self->{dbh}->prepare_cached(
"SELECT * FROM $tablename_rev WHERE name=? AND revision=?",
{},1);
$db_query->execute($filename, $intRev);
$meta = $db_query->fetchrow_hashref;
}
2020-06-23 02:04:16 +08:00
elsif ( $revision =~ /^2\.1\.1\.2000(\.[1-3][0-9][0-9]){$state->{rawsz}}$/ )
2012-10-14 13:42:29 +08:00
{
my ($commitHash)=($revision=~/^2\.1\.1\.2000(.*)$/);
$commitHash=~s/\.([0-9]+)/sprintf("%02x",$1-100)/eg;
2020-06-23 02:04:16 +08:00
if($commitHash=~/^[0-9a-f]{$state->{hexsz}}$/)
2012-10-14 13:42:29 +08:00
{
return $self->getMetaFromCommithash($filename,$commitHash);
}
# error recovery: fall back on head version below
print "E Failed to find $filename version=$revision or commit=$commitHash\n";
$log->warning("failed get $revision with commithash=$commitHash");
undef $revision;
}
2020-06-23 02:04:16 +08:00
elsif ( $revision =~ /^[0-9a-f]{$state->{hexsz}}$/ )
2012-10-14 13:42:29 +08:00
{
# Try DB first. This is mostly only useful for req_annotate(),
# which only calls this for stuff that should already be in
# the DB. It is fairly likely to be a waste of time
# in most other cases [unless the file happened to be
# modified in $revision specifically], but
# it is probably in the noise compared to how long
# getMetaFromCommithash() will take.
my $db_query;
$db_query = $self->{dbh}->prepare_cached(
"SELECT * FROM $tablename_rev WHERE name=? AND commithash=?",
{},1);
$db_query->execute($filename, $revision);
$meta = $db_query->fetchrow_hashref;
if(! $meta)
{
my($revCommit)=$self->lookupCommitRef($revision);
2020-06-23 02:04:16 +08:00
if($revCommit=~/^[0-9a-f]{$state->{hexsz}}$/)
2012-10-14 13:42:29 +08:00
{
return $self->getMetaFromCommithash($filename,$revCommit);
}
# error recovery: nothing found:
print "E Failed to find $filename version=$revision\n";
$log->warning("failed get $revision");
return $meta;
}
}
else
{
my($revCommit)=$self->lookupCommitRef($revision);
2020-06-23 02:04:16 +08:00
if($revCommit=~/^[0-9a-f]{$state->{hexsz}}$/)
2012-10-14 13:42:29 +08:00
{
return $self->getMetaFromCommithash($filename,$revCommit);
}
# error recovery: fall back on head version below
print "E Failed to find $filename version=$revision\n";
$log->warning("failed get $revision");
undef $revision; # Allow fallback
}
}
if(!defined($revision))
{
my $db_query;
$db_query = $self->{dbh}->prepare_cached(
"SELECT * FROM $tablename_head WHERE name=?",{},1);
2006-02-22 17:50:15 +08:00
$db_query->execute($filename);
2012-10-14 13:42:29 +08:00
$meta = $db_query->fetchrow_hashref;
2006-02-22 17:50:15 +08:00
}
2012-10-14 13:42:21 +08:00
if($meta)
{
$meta->{revision} = "1.$meta->{revision}";
}
return $meta;
2006-02-22 17:50:15 +08:00
}
2012-10-14 13:42:27 +08:00
sub getMetaFromCommithash
{
my $self = shift;
my $filename = shift;
my $revCommit = shift;
# NOTE: This function doesn't scale well (lots of forks), especially
# if you have many files that have not been modified for many commits
# (each git-rev-parse redoes a lot of work for each file
# that theoretically could be done in parallel by smarter
# graph traversal).
#
# TODO: Possible optimization strategies:
# - Solve the issue of assigning and remembering "real" CVS
# revision numbers for branches, and ensure the
# data structure can do this efficiently. Perhaps something
# similar to "git notes", and carefully structured to take
# advantage same-sha1-is-same-contents, to roll the same
# unmodified subdirectory data onto multiple commits?
# - Write and use a C tool that is like git-blame, but
# operates on multiple files with file granularity, instead
# of one file with line granularity. Cache
# most-recently-modified in $self->{commitRefCache}{$revCommit}.
# Try to be intelligent about how many files we do with
# one fork (perhaps one directory at a time, without recursion,
# and/or include directory as one line item, recurse from here
# instead of in C tool?).
# - Perhaps we could ask the DB for (filename,fileHash),
# and just guess that it is correct (that the file hadn't
# changed between $revCommit and the found commit, then
# changed back, confusing anything trying to interpret
# history). Probably need to add another index to revisions
# DB table for this.
# - NOTE: Trying to store all (commit,file) keys in DB [to
# find "lastModfiedCommit] (instead of
# just files that changed in each commit as we do now) is
# probably not practical from a disk space perspective.
# Does the file exist in $revCommit?
# TODO: Include file hash in dirmap cache.
my($dirMap)=$self->getRevisionDirMap($revCommit);
my($dir,$file)=($filename=~m%^(?:(.*)/)?([^/]*$)%);
if(!defined($dir))
{
$dir="";
}
if( !defined($dirMap->{$dir}) ||
!defined($dirMap->{$dir}{$filename}) )
{
my($fileHash)="deleted";
my($retVal)={};
$retVal->{name}=$filename;
$retVal->{filehash}=$fileHash;
# not needed and difficult to compute:
$retVal->{revision}="0"; # $revision;
$retVal->{commithash}=$revCommit;
#$retVal->{author}=$commit->{author};
#$retVal->{modified}=convertToCvsDate($commit->{date});
#$retVal->{mode}=convertToDbMode($mode);
return $retVal;
}
2017-09-11 13:44:24 +08:00
my($fileHash) = ::safe_pipe_capture("git","rev-parse","$revCommit:$filename");
2012-10-14 13:42:27 +08:00
chomp $fileHash;
2020-06-23 02:04:16 +08:00
if(!($fileHash=~/^[0-9a-f]{$state->{hexsz}}$/))
2012-10-14 13:42:27 +08:00
{
die "Invalid fileHash '$fileHash' looking up"
." '$revCommit:$filename'\n";
}
# information about most recent commit to modify $filename:
open(my $gitLogPipe, '-|', 'git', 'rev-list',
'--max-count=1', '--pretty', '--parents',
$revCommit, '--', $filename)
or die "Cannot call git-rev-list: $!";
my @commits=readCommits($gitLogPipe);
close $gitLogPipe;
if(scalar(@commits)!=1)
{
die "Can't find most recent commit changing $filename\n";
}
my($commit)=$commits[0];
if( !defined($commit) || !defined($commit->{hash}) )
{
return undef;
}
# does this (commit,file) have a real assigned CVS revision number?
my $tablename_rev = $self->tablename("revision");
my $db_query;
$db_query = $self->{dbh}->prepare_cached(
"SELECT * FROM $tablename_rev WHERE name=? AND commithash=?",
{},1);
$db_query->execute($filename, $commit->{hash});
my($meta)=$db_query->fetchrow_hashref;
if($meta)
{
$meta->{revision} = "1.$meta->{revision}";
return $meta;
}
# fall back on special revision number
my($revision)=$commit->{hash};
$revision=~s/(..)/'.' . (hex($1)+100)/eg;
$revision="2.1.1.2000$revision";
# meta data about $filename:
open(my $filePipe, '-|', 'git', 'ls-tree', '-z',
$commit->{hash}, '--', $filename)
or die("Cannot call git-ls-tree : $!");
local $/ = "\0";
my $line;
$line=<$filePipe>;
if(defined(<$filePipe>))
{
die "Expected only a single file for git-ls-tree $filename\n";
}
close $filePipe;
chomp $line;
unless ( $line=~m/^(\d+)\s+(\w+)\s+([a-zA-Z0-9]+)\t(.*)$/o )
{
die("Couldn't process git-ls-tree line : $line\n");
}
my ( $mode, $git_type, $git_hash, $git_filename ) = ( $1, $2, $3, $4 );
# save result:
my($retVal)={};
$retVal->{name}=$filename;
$retVal->{revision}=$revision;
$retVal->{filehash}=$fileHash;
$retVal->{commithash}=$revCommit;
$retVal->{author}=$commit->{author};
$retVal->{modified}=convertToCvsDate($commit->{date});
$retVal->{mode}=convertToDbMode($mode);
return $retVal;
}
=head2 lookupCommitRef
Convert tag/branch/abbreviation/etc into a commit sha1 hash. Caches
the result so looking it up again is fast.
=cut
sub lookupCommitRef
{
my $self = shift;
my $ref = shift;
my $commitHash = $self->{commitRefCache}{$ref};
if(defined($commitHash))
{
return $commitHash;
}
2017-09-11 13:44:24 +08:00
$commitHash = ::safe_pipe_capture("git","rev-parse","--verify","--quiet",
$self->unescapeRefName($ref));
2012-10-14 13:42:27 +08:00
$commitHash=~s/\s*$//;
2020-06-23 02:04:16 +08:00
if(!($commitHash=~/^[0-9a-f]{$state->{hexsz}}$/))
2012-10-14 13:42:27 +08:00
{
$commitHash=undef;
}
if( defined($commitHash) )
{
2017-09-11 13:44:24 +08:00
my $type = ::safe_pipe_capture("git","cat-file","-t",$commitHash);
2012-10-14 13:42:27 +08:00
if( ! ($type=~/^commit\s*$/ ) )
{
$commitHash=undef;
}
}
if(defined($commitHash))
{
$self->{commitRefCache}{$ref}=$commitHash;
}
return $commitHash;
}
=head2 clearCommitRefCaches
Clears cached commit cache (sha1's for various tags/abbeviations/etc),
and related caches.
=cut
sub clearCommitRefCaches
{
my $self = shift;
$self->{commitRefCache} = {};
$self->{revisionDirMapCache} = undef;
$self->{gethead_cache} = undef;
}
2006-02-22 17:50:15 +08:00
=head2 commitmessage
this function takes a commithash and returns the commit message for that commit
=cut
sub commitmessage
{
my $self = shift;
my $commithash = shift;
2008-03-28 05:02:14 +08:00
my $tablename = $self->tablename("commitmsgs");
2006-02-22 17:50:15 +08:00
2020-06-23 02:04:16 +08:00
die("Need commithash") unless ( defined($commithash) and $commithash =~ /^[a-zA-Z0-9]{$state->{hexsz}}$/ );
2006-02-22 17:50:15 +08:00
my $db_query;
2008-03-28 05:02:14 +08:00
$db_query = $self->{dbh}->prepare_cached("SELECT value FROM $tablename WHERE key=?",{},1);
2006-02-22 17:50:15 +08:00
$db_query->execute($commithash);
my ( $message ) = $db_query->fetchrow_array;
if ( defined ( $message ) )
{
$message .= " " if ( $message =~ /\n$/ );
return $message;
}
2017-09-11 13:44:24 +08:00
my @lines = ::safe_pipe_capture("git", "cat-file", "commit", $commithash);
2006-02-22 17:50:15 +08:00
shift @lines while ( $lines[0] =~ /\S/ );
$message = join("",@lines);
$message .= " " if ( $message =~ /\n$/ );
return $message;
}
=head2 gethistorydense
This function takes a filename (with path) argument and returns an arrayofarrays
containing revision,filehash,commithash ordered by revision descending.
This version of gethistory skips deleted entries -- so it is useful for annotate.
The 'dense' part is a reference to a '--dense' option available for git-rev-list
and other git tools that depend on it.
2012-10-14 13:42:18 +08:00
See also getlog().
2006-02-22 17:50:15 +08:00
=cut
sub gethistorydense
{
my $self = shift;
my $filename = shift;
2008-03-28 05:02:14 +08:00
my $tablename = $self->tablename("revision");
2006-02-22 17:50:15 +08:00
my $db_query;
2008-03-28 05:02:14 +08:00
$db_query = $self->{dbh}->prepare_cached("SELECT revision, filehash, commithash FROM $tablename WHERE name=? AND filehash!='deleted' ORDER BY revision DESC",{},1);
2006-02-22 17:50:15 +08:00
$db_query->execute($filename);
2012-10-14 13:42:21 +08:00
my $result = $db_query->fetchall_arrayref;
my $i;
for($i=0 ; $i<scalar(@$result) ; $i++)
{
$result->[$i][0]="1." . $result->[$i][0];
}
return $result;
2006-02-22 17:50:15 +08:00
}
2012-10-14 13:42:26 +08:00
=head2 escapeRefName
Apply an escape mechanism to compensate for characters that
git ref names can have that CVS tags can not.
=cut
sub escapeRefName
{
my($self,$refName)=@_;
# CVS officially only allows [-_A-Za-z0-9] in tag names (or in
# many contexts it can also be a CVS revision number).
#
# Git tags commonly use '/' and '.' as well, but also handle
# anything else just in case:
#
# = "_-s-" For '/'.
# = "_-p-" For '.'.
# = "_-u-" For underscore, in case someone wants a literal "_-" in
# a tag name.
# = "_-xx-" Where "xx" is the hexadecimal representation of the
# desired ASCII character byte. (for anything else)
if(! $refName=~/^[1-9][0-9]*(\.[1-9][0-9]*)*$/)
{
$refName=~s/_-/_-u--/g;
$refName=~s/\./_-p-/g;
$refName=~s%/%_-s-%g;
$refName=~s/[^-_a-zA-Z0-9]/sprintf("_-%02x-",$1)/eg;
}
}
=head2 unescapeRefName
Undo an escape mechanism to compensate for characters that
git ref names can have that CVS tags can not.
=cut
sub unescapeRefName
{
my($self,$refName)=@_;
# see escapeRefName() for description of escape mechanism.
$refName=~s/_-([spu]|[0-9a-f][0-9a-f])-/unescapeRefNameChar($1)/eg;
# allowed tag names
# TODO: Perhaps use git check-ref-format, with an in-process cache of
# validated names?
if( !( $refName=~m%^[^-][-a-zA-Z0-9_/.]*$% ) ||
( $refName=~m%[/.]$% ) ||
( $refName=~/\.lock$/ ) ||
( $refName=~m%\.\.|/\.|[[\\:?*~]|\@\{% ) ) # matching }
{
# Error:
$log->warn("illegal refName: $refName");
$refName=undef;
}
return $refName;
}
sub unescapeRefNameChar
{
my($char)=@_;
if($char eq "s")
{
$char="/";
}
elsif($char eq "p")
{
$char=".";
}
elsif($char eq "u")
{
$char="_";
}
elsif($char=~/^[0-9a-f][0-9a-f]$/)
{
$char=chr(hex($char));
}
else
{
# Error case: Maybe it has come straight from user, and
# wasn't supposed to be escaped? Restore it the way we got it:
$char="_-$char-";
}
return $char;
}
2006-02-22 17:50:15 +08:00
=head2 in_array()
from Array::PAT - mimics the in_array() function
found in PHP. Yuck but works for small arrays.
=cut
sub in_array
{
my ($check, @array) = @_;
my $retval = 0;
foreach my $test (@array){
if($check eq $test){
$retval = 1;
}
}
return $retval;
}
2007-03-19 23:56:00 +08:00
=head2 mangle_dirname
create a string from a directory name that is suitable to use as
part of a filename, mainly by converting all chars except \w.- to _
=cut
sub mangle_dirname {
my $dirname = shift;
return unless defined $dirname;
$dirname =~ s/[^\w.-]/_/g;
return $dirname;
}
2006-02-22 17:50:15 +08:00
2008-03-28 05:02:14 +08:00
=head2 mangle_tablename
create a string from a that is suitable to use as part of an SQL table
name, mainly by converting all chars except \w to _
=cut
sub mangle_tablename {
my $tablename = shift;
return unless defined $tablename;
$tablename =~ s/[^\w_]/_/g;
return $tablename;
}
2006-02-22 17:50:15 +08:00
1;