4 use Git::SVN::Utils qw(fatal);
10 use POSIX qw/strftime/;
11 use constant commit_log_separator => ('-' x 72) . "\n";
12 use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
13 %rusers $show_commit $incremental/;
15 # Option set in git-svn
20 return 1 if defined $c->{r};
22 # big commit message got truncated by the 16k pretty buffer in rev-list
23 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
24 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
26 my @log = command(qw/cat-file commit/, $c->{c});
28 # shift off the headers
29 shift @log while ($log[0] ne '');
32 # TODO: make $c->{l} not have a trailing newline in the future
33 @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
35 (undef, $c->{r}, undef) = ::extract_metadata(
36 (grep(/^git-svn-id: /, @log))[-1]);
38 return defined $c->{r};
42 return $color || Git->repository->get_colorbool('color.diff');
46 my ($r_min, $r_max, @args) = @_;
48 my (@files, @log_opts);
49 foreach my $x (@args) {
50 if ($x eq '--' || @files) {
53 if (::verify_ref("$x^0")) {
61 my ($url, $rev, $uuid, $gs) = ::working_head_info($head);
64 $gs ||= Git::SVN->_new;
65 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
67 push @cmd, '-r' unless $non_recursive;
68 push @cmd, qw/--raw --name-status/ if $verbose;
69 push @cmd, '--color' if log_use_color();
71 if (defined $r_max && $r_max == $r_min) {
72 push @cmd, '--max-count=1';
73 if (my $c = $gs->rev_map_get($r_max)) {
76 } elsif (defined $r_max) {
77 if ($r_max < $r_min) {
78 ($r_min, $r_max) = ($r_max, $r_min);
80 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
81 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
82 # If there are no commits in the range, both $c_max and $c_min
83 # will be undefined. If there is at least 1 commit in the
84 # range, both will be defined.
85 return () if !defined $c_min || !defined $c_max;
86 if ($c_min eq $c_max) {
87 push @cmd, '--max-count=1', $c_min;
89 push @cmd, '--boundary', "$c_min..$c_max";
92 return (@cmd, @files);
95 # adapted from pager.c
98 $ENV{GIT_PAGER_IN_USE} = 'false';
102 chomp($pager = command_oneline(qw(var GIT_PAGER)));
103 if ($pager eq 'cat') {
106 $ENV{GIT_PAGER_IN_USE} = defined($pager);
110 return unless defined $pager;
111 pipe my ($rfd, $wfd) or return;
112 defined(my $pid = fork) or fatal "Can't fork: $!";
114 open STDOUT, '>&', $wfd or
115 fatal "Can't redirect to stdout: $!";
118 open STDIN, '<&', $rfd or fatal "Can't redirect stdin: $!";
119 $ENV{LESS} ||= 'FRSX';
120 exec $pager or fatal "Can't run pager: $! ($pager)";
123 sub format_svn_date {
124 my $t = shift || time;
126 my $gmoff = get_tz_offset($t);
127 return strftime("%Y-%m-%d %H:%M:%S $gmoff (%a, %d %b %Y)", localtime($t));
132 # Date::Parse isn't in the standard Perl distro :(
133 if ($tz =~ s/^\+//) {
134 $t += tz_to_s_offset($tz);
135 } elsif ($tz =~ s/^\-//) {
136 $t -= tz_to_s_offset($tz);
141 sub set_local_timezone {
152 return ($1 * 60) + ($tz * 3600);
155 sub get_author_info {
156 my ($dest, $author, $t, $tz) = @_;
157 $author =~ s/(?:^\s*|\s*$)//g;
158 $dest->{a_raw} = $author;
161 $au = $rusers{$author} || undef;
164 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
169 $dest->{t_utc} = parse_git_date($t, $tz);
173 my ($c, $r_min, $r_max, $defer) = @_;
174 if (defined $r_min && defined $r_max) {
175 if ($r_min == $c->{r} && $r_min == $r_max) {
179 return 1 if $r_min == $r_max;
180 if ($r_min < $r_max) {
181 # we need to reverse the print order
182 return 0 if (defined $limit && --$limit < 0);
186 if ($r_min != $r_max) {
187 return 1 if ($r_min < $c->{r});
188 return 1 if ($r_max > $c->{r});
191 return 0 if (defined $limit && --$limit < 0);
201 if (my $l = $c->{l}) {
202 while ($l->[0] =~ /^\s*$/) { shift @$l }
205 $l_fmt ||= 'A' . length($c->{r});
206 print 'r',pack($l_fmt, $c->{r}),' | ';
207 print "$c->{c} | " if $show_commit;
210 show_commit_normal($c);
214 sub show_commit_changed_paths {
216 return unless $c->{changed};
217 print "Changed paths:\n", @{$c->{changed}};
220 sub show_commit_normal {
222 print commit_log_separator, "r$c->{r} | ";
223 print "$c->{c} | " if $show_commit;
224 print "$c->{a} | ", format_svn_date($c->{t_utc}), ' | ';
227 if (my $l = $c->{l}) {
228 while ($l->[$#$l] eq "\n" && $#$l > 0
229 && $l->[($#$l - 1)] eq "\n") {
232 $nr_line = scalar @$l;
234 print "1 line\n\n\n";
239 $nr_line .= ' lines';
241 print $nr_line, "\n";
242 show_commit_changed_paths($c);
244 print $_ foreach @$l;
248 show_commit_changed_paths($c);
252 foreach my $x (qw/raw stat diff/) {
255 print $_ foreach @{$c->{$x}}
263 my $r_last = -1; # prevent dupes
264 set_local_timezone();
265 if (defined $::_revision) {
266 if ($::_revision =~ /^(\d+):(\d+)$/) {
267 ($r_min, $r_max) = ($1, $2);
268 } elsif ($::_revision =~ /^\d+$/) {
269 $r_min = $r_max = $::_revision;
271 fatal "-r$::_revision is not supported, use ",
272 "standard 'git log' arguments instead";
277 @args = git_svn_log_cmd($r_min, $r_max, @args);
279 print commit_log_separator unless $incremental || $oneline;
282 my $log = command_output_pipe(@args);
284 my (@k, $c, $d, $stat);
285 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
287 if (/^${esc_color}commit (?:- )?($::sha1_short)/o) {
289 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
291 process_commit($c, $r_min, $r_max, \@k) or
296 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
297 get_author_info($c, $1, $2, $3);
298 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
300 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
301 push @{$c->{raw}}, $_;
302 } elsif (/^${esc_color}[ACRMDT]\t/) {
303 # we could add $SVN->{svn_path} here, but that requires
304 # remote access at the moment (repo_path_split)...
305 s#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
306 push @{$c->{changed}}, $_;
307 } elsif (/^${esc_color}diff /o) {
309 push @{$c->{diff}}, $_;
311 push @{$c->{diff}}, $_;
312 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
313 $esc_color*[\+\-]*$esc_color$/x) {
315 push @{$c->{stat}}, $_;
316 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
317 push @{$c->{stat}}, $_;
319 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
320 ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
321 } elsif (s/^${esc_color} //o) {
325 if ($c && defined $c->{r} && $c->{r} != $r_last) {
327 process_commit($c, $r_min, $r_max, \@k);
330 ($r_min, $r_max) = ($r_max, $r_min);
331 process_commit($_, $r_min, $r_max) foreach reverse @k;
335 print commit_log_separator unless $incremental || $oneline;
344 my ($fh, $ctx, $rev);
347 ($fh, $ctx) = command_output_pipe('blame', @_, $path);
348 while (my $line = <$fh>) {
349 if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
350 # Uncommitted edits show up as a rev ID of
351 # all zeros, which we can't look up with
354 (undef, $rev, undef) =
356 $rev = '0' if (!$rev);
360 $rev = sprintf('%-10s', $rev);
361 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
366 ($fh, $ctx) = command_output_pipe('blame', '-p', @_, 'HEAD',
371 my %dsha; #distinct sha keys
373 while (my $line = <$fh>) {
375 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
380 my $s2r = ::cmt_sha2rev_batch([keys %dsha]);
382 foreach my $line (@buffer) {
383 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
385 $rev = '0' if (!$rev)
387 elsif ($line =~ /^author (.*)/) {
389 $authors{$rev} =~ s/\s/_/g;
391 elsif ($line =~ /^\t(.*)$/) {
392 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
396 command_close_pipe($fh, $ctx);