git-svn: extract reusable code into utility functions
Extacted canonicalize_path() in the main package. Created new Git::SVN::Util package with an md5sum() function. A new package was created so that Digest::MD5 did not have to be loaded in the main package. Replaced code in the SVN::Git::Editor and SVN::Git::Fetcher packages with calls to md5sum(). Extracted the format_svn_date(), parse_git_date() and set_local_timezone() functions within the Git::SVN::Log package. Signed-off-by: David D. Kilzer <ddkilzer@kilzer.net> Acked-by: Eric Wong <normalperson@yhbt.net>
David D. Kilzer committed
Nov 20, 2007 at 22:43 UTC
b2b3ada7fc46f6e43f065718cd64940811cd50b3
1 file changed
+64
-32
git-svn.perl
+64
-32
@@ -48,7 +48,8 @@ BEGIN {
48
foreach (qw/command command_oneline command_noisy command_output_pipe
49
command_input_pipe command_close_pipe/) {
50
for my $package ( qw(SVN::Git::Editor SVN::Git::Fetcher
51
- Git::SVN::Migration Git::SVN::Log Git::SVN),
51
+ Git::SVN::Migration Git::SVN::Log Git::SVN
52
+ Git::SVN::Util),
53
__PACKAGE__) {
54
*{"${package}::$_"} = \&{"Git::$_"};
55
}
@@ -583,6 +584,17 @@ sub cmd_create_ignore {
584
});
585
}
586
587
+sub canonicalize_path {
588
+ my ($path) = @_;
589
+ # File::Spec->canonpath doesn't collapse x/../y into y (for a
590
+ # good reason), so let's do this manually.
591
+ $path =~ s#/+#/#g;
592
+ $path =~ s#/\.(?:/|$)#/#g;
593
+ $path =~ s#/[^/]+/\.\.##g;
594
+ $path =~ s#/$##g;
595
+ return $path;
596
+}
597
+
598
# get_svnprops(PATH)
599
# ------------------
600
# Helper for cmd_propget and cmd_proplist below.
@@ -600,12 +612,7 @@ sub get_svnprops {
612
613
# canonicalize the path (otherwise libsvn will abort or fail to
614
# find the file)
603
- # File::Spec->canonpath doesn't collapse x/../y into y (for a
604
- # good reason), so let's do this manually.
605
- $path =~ s#/+#/#g;
606
- $path =~ s#/\.(?:/|$)#/#g;
607
- $path =~ s#/[^/]+/\.\.##g;
608
- $path =~ s#/$##g;
615
+ $path = canonicalize_path($path);
616
617
my $r = (defined $_revision ? $_revision : $gs->ra->get_latest_revnum);
618
my $props;
@@ -1043,6 +1050,27 @@ sub linearize_history {
1050
(\@linear_refs, \%parents);
1051
}
1052
1053
+package Git::SVN::Util;
1054
+use strict;
1055
+use warnings;
1056
+use Digest::MD5;
1057
+
1058
+sub md5sum {
1059
+ my $arg = shift;
1060
+ my $ref = ref $arg;
1061
+ my $md5 = Digest::MD5->new();
1062
+ if ($ref eq 'GLOB' || $ref eq 'IO::File') {
1063
+ $md5->addfile($arg) or croak $!;
1064
+ } elsif ($ref eq 'SCALAR') {
1065
+ $md5->add($$arg) or croak $!;
1066
+ } elsif (!$ref) {
1067
+ $md5->add($arg) or croak $!;
1068
+ } else {
1069
+ ::fatal "Can't provide MD5 hash for unknown ref type: '", $ref, "'";
1070
+ }
1071
+ return $md5->hexdigest();
1072
+}
1073
+
1074
package Git::SVN;
1075
use strict;
1076
use warnings;
@@ -2610,7 +2638,6 @@ use strict;
2638
use warnings;
2639
use Carp qw/croak/;
2640
use IO::File qw//;
2613
-use Digest::MD5;
2641
2642
# file baton members: path, mode_a, mode_b, pool, fh, blob, base
2643
sub new {
@@ -2762,9 +2789,7 @@ sub apply_textdelta {
2789
2790
if (defined $exp) {
2791
seek $base, 0, 0 or croak $!;
2765
- my $md5 = Digest::MD5->new;
2766
- $md5->addfile($base);
2767
- my $got = $md5->hexdigest;
2792
+ my $got = Git::SVN::Util::md5sum($base);
2793
die "Checksum mismatch: $fb->{path} $fb->{blob}\n",
2794
"expected: $exp\n",
2795
" got: $got\n" if ($got ne $exp);
@@ -2783,9 +2808,7 @@ sub close_file {
2808
if (my $fh = $fb->{fh}) {
2809
if (defined $exp) {
2810
seek($fh, 0, 0) or croak $!;
2786
- my $md5 = Digest::MD5->new;
2787
- $md5->addfile($fh);
2788
- my $got = $md5->hexdigest;
2811
+ my $got = Git::SVN::Util::md5sum($fh);
2812
if ($got ne $exp) {
2813
die "Checksum mismatch: $path\n",
2814
"expected: $exp\n got: $got\n";
@@ -2837,7 +2860,6 @@ use strict;
2860
use warnings;
2861
use Carp qw/croak/;
2862
use IO::File;
2840
-use Digest::MD5;
2863
2864
sub new {
2865
my ($class, $opts) = @_;
@@ -3141,11 +3163,9 @@ sub chg_file {
3163
$fh->flush == 0 or croak $!;
3164
seek $fh, 0, 0 or croak $!;
3165
3144
- my $md5 = Digest::MD5->new;
3145
- $md5->addfile($fh) or croak $!;
3166
+ my $exp = Git::SVN::Util::md5sum($fh);
3167
seek $fh, 0, 0 or croak $!;
3168
3148
- my $exp = $md5->hexdigest;
3169
my $pool = SVN::Pool->new;
3170
my $atd = $self->apply_textdelta($fbat, undef, $pool);
3171
my $got = SVN::TxDelta::send_stream($fh, @$atd, $pool);
@@ -3859,6 +3879,29 @@ sub run_pager {
3879
exec $pager or ::fatal "Can't run pager: $! ($pager)";
3880
}
3881
3882
+sub format_svn_date {
3883
+ return strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)", localtime(shift));
3884
+}
3885
+
3886
+sub parse_git_date {
3887
+ my ($t, $tz) = @_;
3888
+ # Date::Parse isn't in the standard Perl distro :(
3889
+ if ($tz =~ s/^\+//) {
3890
+ $t += tz_to_s_offset($tz);
3891
+ } elsif ($tz =~ s/^\-//) {
3892
+ $t -= tz_to_s_offset($tz);
3893
+ }
3894
+ return $t;
3895
+}
3896
+
3897
+sub set_local_timezone {
3898
+ if (defined $TZ) {
3899
+ $ENV{TZ} = $TZ;
3900
+ } else {
3901
+ delete $ENV{TZ};
3902
+ }
3903
+}
3904
+
3905
sub tz_to_s_offset {
3906
my ($tz) = @_;
3907
$tz =~ s/(\d\d)$//;
@@ -3879,13 +3922,7 @@ sub get_author_info {
3922
$dest->{t} = $t;
3923
$dest->{tz} = $tz;
3924
$dest->{a} = $au;
3882
- # Date::Parse isn't in the standard Perl distro :(
3883
- if ($tz =~ s/^\+//) {
3884
- $t += tz_to_s_offset($tz);
3885
- } elsif ($tz =~ s/^\-//) {
3886
- $t -= tz_to_s_offset($tz);
3887
- }
3888
- $dest->{t_utc} = $t;
3925
+ $dest->{t_utc} = parse_git_date($t, $tz);
3926
}
3927
3928
sub process_commit {
@@ -3939,8 +3976,7 @@ sub show_commit_normal {
3976
my ($c) = @_;
3977
print commit_log_separator, "r$c->{r} | ";
3978
print "$c->{c} | " if $show_commit;
3942
- print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
3943
- localtime($c->{t_utc})), ' | ';
3979
+ print "$c->{a} | ", format_svn_date($c->{t_utc}), ' | ';
3980
my $nr_line = 0;
3981
3982
if (my $l = $c->{l}) {
@@ -3980,11 +4016,7 @@ sub cmd_show_log {
4016
my (@args) = @_;
4017
my ($r_min, $r_max);
4018
my $r_last = -1; # prevent dupes
3983
- if (defined $TZ) {
3984
- $ENV{TZ} = $TZ;
3985
- } else {
3986
- delete $ENV{TZ};
3987
- }
4019
+ set_local_timezone();
4020
if (defined $::_revision) {
4021
if ($::_revision =~ /^(\d+):(\d+)$/) {
4022
($r_min, $r_max) = ($1, $2);