git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Adam Roben <aroben@apple.com>
To: git@vger.kernel.org
Cc: Adam Roben <aroben@apple.com>, Eric Wong <normalperson@yhbt.net>
Subject: [PATCH 11/11] git-svn: Speed up fetch
Date: Wed, 23 Apr 2008 15:17:53 -0400	[thread overview]
Message-ID: <1208978273-98146-12-git-send-email-aroben@apple.com> (raw)
In-Reply-To: <1208978273-98146-11-git-send-email-aroben@apple.com>

We were spending a lot of time forking/execing git-cat-file and
git-hash-object. We now maintain a global Git repository object in order to use
Git.pm's more efficient hash_and_insert_object and cat_blob methods.

Signed-off-by: Adam Roben <aroben@apple.com>
---
 git-svn.perl |   42 ++++++++++++++++++++----------------------
 1 files changed, 20 insertions(+), 22 deletions(-)

diff --git a/git-svn.perl b/git-svn.perl
index b864b54..5ef9d23 100755
--- a/git-svn.perl
+++ b/git-svn.perl
@@ -4,7 +4,7 @@
 use warnings;
 use strict;
 use vars qw/	$AUTHOR $VERSION
-		$sha1 $sha1_short $_revision
+		$sha1 $sha1_short $_revision $_repository
 		$_q $_authors %users/;
 $AUTHOR = 'Eric Wong <normalperson@yhbt.net>';
 $VERSION = '@@GIT_VERSION@@';
@@ -220,6 +220,7 @@ unless ($cmd && $cmd =~ /(?:clone|init|multi-init)$/) {
 		}
 		$ENV{GIT_DIR} = $git_dir;
 	}
+	$_repository = Git->repository(Repository => $ENV{GIT_DIR});
 }
 
 my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
@@ -301,6 +302,7 @@ sub do_git_init_db {
 			}
 		}
 		command_noisy(@init_db);
+		$_repository = Git->repository(Repository => ".git");
 	}
 	my $set;
 	my $pfx = "svn-remote.$Git::SVN::default_repo_id";
@@ -317,6 +319,7 @@ sub init_subdir {
 	mkpath([$repo_path]) unless -d $repo_path;
 	chdir $repo_path or die "Couldn't chdir to $repo_path: $!\n";
 	$ENV{GIT_DIR} = '.git';
+	$_repository = Git->repository(Repository => $ENV{GIT_DIR});
 }
 
 sub cmd_clone {
@@ -3010,6 +3013,7 @@ use vars qw/@ISA/;
 use strict;
 use warnings;
 use Carp qw/croak/;
+use File::Temp qw/tempfile/;
 use IO::File qw//;
 
 # file baton members: path, mode_a, mode_b, pool, fh, blob, base
@@ -3165,14 +3169,9 @@ sub apply_textdelta {
 	my $base = IO::File->new_tmpfile;
 	$base->autoflush(1);
 	if ($fb->{blob}) {
-		defined (my $pid = fork) or croak $!;
-		if (!$pid) {
-			open STDOUT, '>&', $base or croak $!;
-			print STDOUT 'link ' if ($fb->{mode_a} == 120000);
-			exec qw/git-cat-file blob/, $fb->{blob} or croak $!;
-		}
-		waitpid $pid, 0;
-		croak $? if $?;
+		print $base 'link ' if ($fb->{mode_a} == 120000);
+		my $size = $::_repository->cat_blob($fb->{blob}, $base);
+		die "Failed to read object $fb->{blob}" unless $size;
 
 		if (defined $exp) {
 			seek $base, 0, 0 or croak $!;
@@ -3213,14 +3212,18 @@ sub close_file {
 				sysseek($fh, 0, 0) or croak $!;
 			}
 		}
-		defined(my $pid = open my $out,'-|') or die "Can't fork: $!\n";
-		if (!$pid) {
-			open STDIN, '<&', $fh or croak $!;
-			exec qw/git-hash-object -w --stdin/ or croak $!;
+
+		my ($tmp_fh, $tmp_filename) = File::Temp::tempfile(UNLINK => 1);
+		my $result;
+		while ($result = sysread($fh, my $string, 1024)) {
+			syswrite($tmp_fh, $string, $result);
 		}
-		chomp($hash = do { local $/; <$out> });
-		close $out or croak $!;
+		defined $result or croak $!;
+		close $tmp_fh or croak $!;
+
 		close $fh or croak $!;
+
+		$hash = $::_repository->hash_and_insert_object($tmp_filename);
 		$hash =~ /^[a-f\d]{40}$/ or die "not a sha1: $hash\n";
 		close $fb->{base} or croak $!;
 	} else {
@@ -3546,13 +3549,8 @@ sub chg_file {
 	} elsif ($m->{mode_a} =~ /^120/ && $m->{mode_b} !~ /^120/) {
 		$self->change_file_prop($fbat,'svn:special',undef);
 	}
-	defined(my $pid = fork) or croak $!;
-	if (!$pid) {
-		open STDOUT, '>&', $fh or croak $!;
-		exec qw/git-cat-file blob/, $m->{sha1_b} or croak $!;
-	}
-	waitpid $pid, 0;
-	croak $? if $?;
+	my $size = $::_repository->cat_blob($m->{sha1_b}, $fh);
+	croak "Failed to read object $m->{sha1_b}" unless $size;
 	$fh->flush == 0 or croak $!;
 	seek $fh, 0, 0 or croak $!;
 
-- 
1.5.5.1.152.g9aeb7

  reply	other threads:[~2008-04-23 19:19 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-04-23 19:17 Speed up git-svn fetch Adam Roben
2008-04-23 19:17 ` [PATCH 01/11] Add tests for git cat-file Adam Roben
2008-04-23 19:17   ` [PATCH 02/11] git-cat-file: Small refactor of cmd_cat_file Adam Roben
2008-04-23 19:17     ` [PATCH 03/11] git-cat-file: Make option parsing a little more flexible Adam Roben
2008-04-23 19:17       ` [PATCH 04/11] git-cat-file: Add --batch-check option Adam Roben
2008-04-23 19:17         ` [PATCH 05/11] git-cat-file: Add --batch option Adam Roben
2008-04-23 19:17           ` [PATCH 06/11] Move git-hash-object tests from t5303 to t1007 Adam Roben
2008-04-23 19:17             ` [PATCH 07/11] Add more tests for git hash-object Adam Roben
2008-04-23 19:17               ` [PATCH 08/11] git-hash-object: Add --stdin-paths option Adam Roben
2008-04-23 19:17                 ` [PATCH 09/11] Git.pm: Add command_bidi_pipe and command_close_bidi_pipe Adam Roben
2008-04-23 19:17                   ` [PATCH 10/11] Git.pm: Add hash_and_insert_object and cat_blob Adam Roben
2008-04-23 19:17                     ` Adam Roben [this message]
2008-04-25 18:04       ` [PATCH 03/11] git-cat-file: Make option parsing a little more flexible Junio C Hamano
2008-04-25  6:56   ` [PATCH 01/11] Add tests for git cat-file Eric Wong
2008-04-25 18:06     ` Junio C Hamano
2008-04-25 18:03   ` Junio C Hamano
2008-05-06  6:41     ` Junio C Hamano
2008-04-23 19:19 ` Speed up git-svn fetch Adam Roben
2008-04-25  7:15   ` Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1208978273-98146-12-git-send-email-aroben@apple.com \
    --to=aroben@apple.com \
    --cc=git@vger.kernel.org \
    --cc=normalperson@yhbt.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).