@matheusbsbs / matheus-bagliano / commits / e73aefe4fd
Fork 0

cvsimport: cleanup commit function

This change attempts to clean up the commit function to make it a bit easier to read (or at least the first half of it). It also improves robustness and performance. Specifically: - report get_headref errors on opening ref unless the error is ENOENT - use regex to check for sha1 instead of length - use lexically scoped filehandles which get cleaned up automagically - check for error on both 'print' and 'close' (since output is buffered) - avoid "fork, do some perl, then exec" in commit(). It's not necessary, and we probably end up COW'ing parts of the perl process. Plus the code is much smaller because we can use open2() - avoid calling strftime over and over (mainly a readability cleanup) Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <junkio@cox.net>

Jeff King committed May 23, 2006 at 03:27 UTC e73aefe4fdba0d161d9878642c69b40d83a0204c
1 file changed +64 -86
git-cvsimport.perl
+64 -86
@@ -23,7 +23,7 @@ use File::Basename qw(basename dirname);
23 use Time::Local;
24 use IO::Socket;
25 use IO::Pipe;
26 -use POSIX qw(strftime dup2);
26 +use POSIX qw(strftime dup2 :errno_h);
27 use IPC::Open2;
28
29 $SIG{'PIPE'}="IGNORE";
@@ -429,22 +429,25 @@ sub getwd() {
429 return $pwd;
430 }
431
432 +sub is_sha1 {
433 + my $s = shift;
434 + return $s =~ /^[a-f0-9]{40}$/;
435 +}
436
433 -sub get_headref($$) {
437 +sub get_headref ($$) {
438 my $name = shift;
439 my $git_dir = shift;
436 - my $sha;
440
438 - if (open(C,"$git_dir/refs/heads/$name")) {
439 - chomp($sha = <C>);
440 - close(C);
441 - length($sha) == 40
442 - or die "Cannot get head id for $name ($sha): $!\n";
441 + my $f = "$git_dir/refs/heads/$name";
442 + if(open(my $fh, $f)) {
443 + chomp(my $r = <$fh>);
444 + is_sha1($r) or die "Cannot get head id for $name ($r): $!";
445 + return $r;
446 }
444 - return $sha;
447 + die "unable to open $f: $!" unless $! == POSIX::ENOENT;
448 + return undef;
449 }
450
447 -
451 -d $git_tree
452 or mkdir($git_tree,0777)
453 or die "Could not create $git_tree: $!";
@@ -561,90 +564,67 @@ unless($pid) {
564
565 my $state = 0;
566
564 -my($patchset,$date,$author_name,$author_email,$branch,$ancestor,$tag,$logmsg);
565 -my(@old,@new,@skipped);
566 -sub commit {
567 - my $pid;
568 -
567 +sub update_index (\@\@) {
568 + my $old = shift;
569 + my $new = shift;
570 open(my $fh, '|-', qw(git-update-index -z --index-info))
571 or die "unable to open git-update-index: $!";
572 print $fh
573 (map { "0 0000000000000000000000000000000000000000\t$_\0" }
573 - @old),
574 + @$old),
575 (map { '100' . sprintf('%o', $_->[0]) . " $_->[1]\t$_->[2]\0" }
575 - @new)
576 + @$new)
577 or die "unable to write to git-update-index: $!";
578 close $fh
579 or die "unable to write to git-update-index: $!";
580 $? and die "git-update-index reported error: $?";
580 - @old = @new = ();
581 +}
582
582 - $pid = open(C,"-|");
583 - die "Cannot fork: $!" unless defined $pid;
584 - unless($pid) {
585 - exec("git-write-tree");
586 - die "Cannot exec git-write-tree: $!\n";
587 - }
588 - chomp(my $tree = <C>);
589 - length($tree) == 40
590 - or die "Cannot get tree id ($tree): $!\n";
591 - close(C)
583 +sub write_tree () {
584 + open(my $fh, '-|', qw(git-write-tree))
585 + or die "unable to open git-write-tree: $!";
586 + chomp(my $tree = <$fh>);
587 + is_sha1($tree)
588 + or die "Cannot get tree id ($tree): $!";
589 + close($fh)
590 or die "Error running git-write-tree: $?\n";
591 print "Tree ID $tree\n" if $opt_v;
592 + return $tree;
593 +}
594
595 - my $parent = "";
596 - if(open(C,"$git_dir/refs/heads/$last_branch")) {
597 - chomp($parent = <C>);
598 - close(C);
599 - length($parent) == 40
600 - or die "Cannot get parent id ($parent): $!\n";
601 - print "Parent ID $parent\n" if $opt_v;
602 - }
603 -
604 - my $pr = IO::Pipe->new() or die "Cannot open pipe: $!\n";
605 - my $pw = IO::Pipe->new() or die "Cannot open pipe: $!\n";
606 - $pid = fork();
607 - die "Fork: $!\n" unless defined $pid;
608 - unless($pid) {
609 - $pr->writer();
610 - $pw->reader();
611 - open(OUT,">&STDOUT");
612 - dup2($pw->fileno(),0);
613 - dup2($pr->fileno(),1);
614 - $pr->close();
615 - $pw->close();
616 -
617 - my @par = ();
618 - @par = ("-p",$parent) if $parent;
619 -
620 - # loose detection of merges
621 - # based on the commit msg
622 - foreach my $rx (@mergerx) {
623 - if ($logmsg =~ $rx) {
624 - my $mparent = $1;
625 - if ($mparent eq 'HEAD') { $mparent = $opt_o };
626 - if ( -e "$git_dir/refs/heads/$mparent") {
627 - $mparent = get_headref($mparent, $git_dir);
628 - push @par, '-p', $mparent;
629 - print OUT "Merge parent branch: $mparent\n" if $opt_v;
630 - }
631 - }
595 +my($patchset,$date,$author_name,$author_email,$branch,$ancestor,$tag,$logmsg);
596 +my(@old,@new,@skipped);
597 +sub commit {
598 + update_index(@old, @new);
599 + @old = @new = ();
600 + my $tree = write_tree();
601 + my $parent = get_headref($last_branch, $git_dir);
602 + print "Parent ID " . ($parent ? $parent : "(empty)") . "\n" if $opt_v;
603 +
604 + my @commit_args;
605 + push @commit_args, ("-p", $parent) if $parent;
606 +
607 + # loose detection of merges
608 + # based on the commit msg
609 + foreach my $rx (@mergerx) {
610 + next unless $logmsg =~ $rx && $1;
611 + my $mparent = $1 eq 'HEAD' ? $opt_o : $1;
612 + if(my $sha1 = get_headref($mparent, $git_dir)) {
613 + push @commit_args, '-p', $mparent;
614 + print "Merge parent branch: $mparent\n" if $opt_v;
615 }
633 -
634 - exec("env",
635 - "GIT_AUTHOR_NAME=$author_name",
636 - "GIT_AUTHOR_EMAIL=$author_email",
637 - "GIT_AUTHOR_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
638 - "GIT_COMMITTER_NAME=$author_name",
639 - "GIT_COMMITTER_EMAIL=$author_email",
640 - "GIT_COMMITTER_DATE=".strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date)),
641 - "git-commit-tree", $tree,@par);
642 - die "Cannot exec git-commit-tree: $!\n";
643 -
644 - close OUT;
616 }
646 - $pw->writer();
647 - $pr->reader();
617 +
618 + my $commit_date = strftime("+0000 %Y-%m-%d %H:%M:%S",gmtime($date));
619 + my $pid = open2(my $commit_read, my $commit_write,
620 + 'env',
621 + "GIT_AUTHOR_NAME=$author_name",
622 + "GIT_AUTHOR_EMAIL=$author_email",
623 + "GIT_AUTHOR_DATE=$commit_date",
624 + "GIT_COMMITTER_NAME=$author_name",
625 + "GIT_COMMITTER_EMAIL=$author_email",
626 + "GIT_COMMITTER_DATE=$commit_date",
627 + 'git-commit-tree', $tree, @commit_args);
628
629 # compatibility with git2cvs
630 substr($logmsg,32767) = "" if length($logmsg) > 32767;
@@ -656,16 +636,14 @@ sub commit {
636 @skipped = ();
637 }
638
659 - print $pw "$logmsg\n"
639 + print($commit_write "$logmsg\n") && close($commit_write)
640 or die "Error writing to git-commit-tree: $!\n";
661 - $pw->close();
641
663 - print "Committed patch $patchset ($branch ".strftime("%Y-%m-%d %H:%M:%S",gmtime($date)).")\n" if $opt_v;
664 - chomp(my $cid = <$pr>);
665 - length($cid) == 40
666 - or die "Cannot get commit id ($cid): $!\n";
642 + print "Committed patch $patchset ($branch $commit_date)\n" if $opt_v;
643 + chomp(my $cid = <$commit_read>);
644 + is_sha1($cid) or die "Cannot get commit id ($cid): $!\n";
645 print "Commit ID $cid\n" if $opt_v;
668 - $pr->close();
646 + close($commit_read);
647
648 waitpid($pid,0);
649 die "Error running git-commit-tree: $?\n" if $?;