15
$ENV{GIT_DIR} = $GIT_DIR;
16
17
my $LC_ALL = $ENV{LC_ALL};
18
+my $TZ = $ENV{TZ};
19
# make sure the svn binary gives consistent output between locales and TZs:
20
$ENV{TZ} = 'UTC';
21
$ENV{LC_ALL} = 'C';
28
use IO::File qw//;
29
use File::Basename qw/dirname basename/;
30
use File::Path qw/mkpath/;
30
-use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
31
+use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev pass_through/;
32
use File::Spec qw//;
33
use POSIX qw/strftime/;
34
my $sha1 = qr/[a-f\d]{40}/;
37
$_find_copies_harder, $_l, $_cp_similarity,
38
$_repack, $_repack_nr, $_repack_flags,
39
$_template, $_shared, $_no_default_regex, $_no_graft_copy,
40
+ $_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
41
$_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m);
40
-my (@_branch_from, %tree_map, %users);
42
+my (@_branch_from, %tree_map, %users, %rusers);
43
my ($_svn_co_url_revs, $_svn_pg_peg_revs);
44
my @repo_path_split_cache;
45
89
'multi-fetch' => [ \&multi_fetch,
90
'Fetch multiple trees (like git-svnimport)',
91
\%fc_opts ],
92
+ 'log' => [ \&show_log, 'Show commit logs',
93
+ { 'limit=i' => \$_limit,
94
+ 'revision|r=s' => \$_revision,
95
+ 'verbose|v' => \$_verbose,
96
+ 'incremental' => \$_incremental,
97
+ 'oneline' => \$_oneline,
98
+ 'show-commit' => \$_show_commit,
99
+ 'authors-file|A=s' => \$_authors,
100
+ } ],
101
);
102
103
my $cmd;
112
my %opts = %{$cmd{$cmd}->[2]} if (defined $cmd);
113
114
read_repo_config(\%opts);
104
-GetOptions(%opts, 'help|H|h' => \$_help,
105
- 'version|V' => \$_version,
106
- 'id|i=s' => \$GIT_SVN) or exit 1;
115
+my $rv = GetOptions(%opts, 'help|H|h' => \$_help,
116
+ 'version|V' => \$_version,
117
+ 'id|i=s' => \$GIT_SVN);
118
+exit 1 if (!$rv && $cmd ne 'log');
119
120
set_default_vals();
121
usage(0) if $_help;
185
croak "Non-SHA1: $c\n" unless $c =~ /^$sha1$/o;
186
my @commit = grep(/^git-svn-id: /,`git-cat-file commit $c`);
187
next if (!@commit); # skip merges
176
- my $id = $commit[$#commit];
177
- my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
178
- \s([a-f\d\-]+)$/x);
179
- if (!$rev || !$uuid || !$url) {
180
- # some of the original repositories I made had
181
- # indentifiers like this:
182
- ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)
183
- \@([a-f\d\-]+)/x);
184
- if (!$rev || !$uuid) {
185
- croak "Unable to extract revision or UUID from ",
186
- "$c, $id\n";
187
- }
188
+ my ($url, $rev, $uuid) = extract_metadata($commit[$#commit]);
189
+ if (!$rev || !$uuid) {
190
+ croak "Unable to extract revision or UUID from ",
191
+ "$c, $commit[$#commit]\n";
192
}
193
194
# if we merged or otherwise started elsewhere, this is
452
rec_fetch('', "$GIT_DIR/svn", @_);
453
}
454
455
+sub show_log {
456
+ my (@args) = @_;
457
+ my ($r_min, $r_max);
458
+ my $r_last = -1; # prevent dupes
459
+ rload_authors() if $_authors;
460
+ if (defined $TZ) {
461
+ $ENV{TZ} = $TZ;
462
+ } else {
463
+ delete $ENV{TZ};
464
+ }
465
+ if (defined $_revision) {
466
+ if ($_revision =~ /^(\d+):(\d+)$/) {
467
+ ($r_min, $r_max) = ($1, $2);
468
+ } elsif ($_revision =~ /^\d+$/) {
469
+ $r_min = $r_max = $_revision;
470
+ } else {
471
+ print STDERR "-r$_revision is not supported, use ",
472
+ "standard \'git log\' arguments instead\n";
473
+ exit 1;
474
+ }
475
+ }
476
+
477
+ my $pid = open(my $log,'-|');
478
+ defined $pid or croak $!;
479
+ if (!$pid) {
480
+ my @rl = (qw/git-log --abbrev-commit --pretty=raw
481
+ --default/, "remotes/$GIT_SVN");
482
+ push @rl, '--raw' if $_verbose;
483
+ exec(@rl, @args) or croak $!;
484
+ }
485
+ setup_pager();
486
+ my (@k, $c, $d);
487
+ while (<$log>) {
488
+ if (/^commit ($sha1_short)/o) {
489
+ my $cmt = $1;
490
+ if ($c && defined $c->{r} && $c->{r} != $r_last) {
491
+ $r_last = $c->{r};
492
+ process_commit($c, $r_min, $r_max, \@k) or
493
+ goto out;
494
+ }
495
+ $d = undef;
496
+ $c = { c => $cmt };
497
+ } elsif (/^author (.+) (\d+) ([\-\+]?\d+)$/) {
498
+ get_author_info($c, $1, $2, $3);
499
+ } elsif (/^(?:tree|parent|committer) /) {
500
+ # ignore
501
+ } elsif (/^:\d{6} \d{6} $sha1_short/o) {
502
+ push @{$c->{raw}}, $_;
503
+ } elsif (/^diff /) {
504
+ $d = 1;
505
+ push @{$c->{diff}}, $_;
506
+ } elsif ($d) {
507
+ push @{$c->{diff}}, $_;
508
+ } elsif (/^ (git-svn-id:.+)$/) {
509
+ my ($url, $rev, $uuid) = extract_metadata($1);
510
+ $c->{r} = $rev;
511
+ } elsif (s/^ //) {
512
+ push @{$c->{l}}, $_;
513
+ }
514
+ }
515
+ if ($c && defined $c->{r} && $c->{r} != $r_last) {
516
+ $r_last = $c->{r};
517
+ process_commit($c, $r_min, $r_max, \@k);
518
+ }
519
+ if (@k) {
520
+ my $swap = $r_max;
521
+ $r_max = $r_min;
522
+ $r_min = $swap;
523
+ process_commit($_, $r_min, $r_max) foreach reverse @k;
524
+ }
525
+out:
526
+ close $log;
527
+ print '-' x72,"\n" unless $_incremental || $_oneline;
528
+}
529
+
530
########################### utility functions #########################
531
532
sub rec_fetch {
1717
close $authors or croak $!;
1718
}
1719
1720
+sub rload_authors {
1721
+ open my $authors, '<', $_authors or die "Can't open $_authors $!\n";
1722
+ while (<$authors>) {
1723
+ chomp;
1724
+ next unless /^(\S+?)\s*=\s*(.+?)\s*<(.+)>\s*$/;
1725
+ my ($user, $name, $email) = ($1, $2, $3);
1726
+ $rusers{"$name <$email>"} = $user;
1727
+ }
1728
+ close $authors or croak $!;
1729
+}
1730
+
1731
sub svn_propget_base {
1732
my ($p, $f) = @_;
1733
$f .= '@BASE' if $_svn_pg_peg_revs;
1893
return $l_map;
1894
}
1895
1896
+sub extract_metadata {
1897
+ my $id = shift;
1898
+ my ($url, $rev, $uuid) = ($id =~ /^git-svn-id:\s(\S+?)\@(\d+)
1899
+ \s([a-f\d\-]+)$/x);
1900
+ if (!$rev || !$uuid || !$url) {
1901
+ # some of the original repositories I made had
1902
+ # indentifiers like this:
1903
+ ($rev, $uuid) = ($id =~/^git-svn-id:\s(\d+)\@([a-f\d\-]+)/);
1904
+ }
1905
+ return ($url, $rev, $uuid);
1906
+}
1907
+
1908
+sub tz_to_s_offset {
1909
+ my ($tz) = @_;
1910
+ $tz =~ s/(\d\d)$//;
1911
+ return ($1 * 60) + ($tz * 3600);
1912
+}
1913
+
1914
+sub setup_pager { # translated to Perl from pager.c
1915
+ return unless (-t *STDOUT);
1916
+ my $pager = $ENV{PAGER};
1917
+ if (!defined $pager) {
1918
+ $pager = 'less';
1919
+ } elsif (length $pager == 0 || $pager eq 'cat') {
1920
+ return;
1921
+ }
1922
+ pipe my $rfd, my $wfd or return;
1923
+ defined(my $pid = fork) or croak $!;
1924
+ if (!$pid) {
1925
+ open STDOUT, '>&', $wfd or croak $!;
1926
+ return;
1927
+ }
1928
+ open STDIN, '<&', $rfd or croak $!;
1929
+ $ENV{LESS} ||= '-S';
1930
+ exec $pager or croak "Can't run pager: $!\n";;
1931
+}
1932
+
1933
+sub get_author_info {
1934
+ my ($dest, $author, $t, $tz) = @_;
1935
+ $author =~ s/(?:^\s*|\s*$)//g;
1936
+ my $_a;
1937
+ if ($_authors) {
1938
+ $_a = $rusers{$author} || undef;
1939
+ }
1940
+ if (!$_a) {
1941
+ ($_a) = ($author =~ /<([^>]+)\@[^>]+>$/);
1942
+ }
1943
+ $dest->{t} = $t;
1944
+ $dest->{tz} = $tz;
1945
+ $dest->{a} = $_a;
1946
+ # Date::Parse isn't in the standard Perl distro :(
1947
+ if ($tz =~ s/^\+//) {
1948
+ $t += tz_to_s_offset($tz);
1949
+ } elsif ($tz =~ s/^\-//) {
1950
+ $t -= tz_to_s_offset($tz);
1951
+ }
1952
+ $dest->{t_utc} = $t;
1953
+}
1954
+
1955
+sub process_commit {
1956
+ my ($c, $r_min, $r_max, $defer) = @_;
1957
+ if (defined $r_min && defined $r_max) {
1958
+ if ($r_min == $c->{r} && $r_min == $r_max) {
1959
+ show_commit($c);
1960
+ return 0;
1961
+ }
1962
+ return 1 if $r_min == $r_max;
1963
+ if ($r_min < $r_max) {
1964
+ # we need to reverse the print order
1965
+ return 0 if (defined $_limit && --$_limit < 0);
1966
+ push @$defer, $c;
1967
+ return 1;
1968
+ }
1969
+ if ($r_min != $r_max) {
1970
+ return 1 if ($r_min < $c->{r});
1971
+ return 1 if ($r_max > $c->{r});
1972
+ }
1973
+ }
1974
+ return 0 if (defined $_limit && --$_limit < 0);
1975
+ show_commit($c);
1976
+ return 1;
1977
+}
1978
+
1979
+sub show_commit {
1980
+ my $c = shift;
1981
+ if ($_oneline) {
1982
+ my $x = "\n";
1983
+ if (my $l = $c->{l}) {
1984
+ while ($l->[0] =~ /^\s*$/) { shift @$l }
1985
+ $x = $l->[0];
1986
+ }
1987
+ $_l_fmt ||= 'A' . length($c->{r});
1988
+ print 'r',pack($_l_fmt, $c->{r}),' | ';
1989
+ print "$c->{c} | " if $_show_commit;
1990
+ print $x;
1991
+ } else {
1992
+ show_commit_normal($c);
1993
+ }
1994
+}
1995
+
1996
+sub show_commit_normal {
1997
+ my ($c) = @_;
1998
+ print '-' x72, "\nr$c->{r} | ";
1999
+ print "$c->{c} | " if $_show_commit;
2000
+ print "$c->{a} | ", strftime("%Y-%m-%d %H:%M:%S %z (%a, %d %b %Y)",
2001
+ localtime($c->{t_utc})), ' | ';
2002
+ my $nr_line = 0;
2003
+
2004
+ if (my $l = $c->{l}) {
2005
+ while ($l->[$#$l] eq "\n" && $l->[($#$l - 1)] eq "\n") {
2006
+ pop @$l;
2007
+ }
2008
+ $nr_line = scalar @$l;
2009
+ if (!$nr_line) {
2010
+ print "1 line\n\n\n";
2011
+ } else {
2012
+ if ($nr_line == 1) {
2013
+ $nr_line = '1 line';
2014
+ } else {
2015
+ $nr_line .= ' lines';
2016
+ }
2017
+ print $nr_line, "\n\n";
2018
+ print $_ foreach @$l;
2019
+ }
2020
+ } else {
2021
+ print "1 line\n\n";
2022
+
2023
+ }
2024
+ foreach my $x (qw/raw diff/) {
2025
+ if ($c->{$x}) {
2026
+ print "\n";
2027
+ print $_ foreach @{$c->{$x}}
2028
+ }
2029
+ }
2030
+}
2031
+
2032
__END__
2033
2034
Data structures: