4 use Git::SVN::Utils qw(fatal);
5 use Git qw(command command_oneline command_output_pipe command_close_pipe);
6 use POSIX qw/strftime/;
7 use constant commit_log_separator => ('-' x 72) . "\n";
8 use vars qw/$TZ $limit $color $pager $non_recursive $verbose $oneline
9 %rusers $show_commit $incremental/;
11 # Option set in git-svn
16 return 1 if defined $c->{r};
18 # big commit message got truncated by the 16k pretty buffer in rev-list
19 if ($c->{l} && $c->{l}->[-1] eq "...\n" &&
20 $c->{a_raw} =~ /\@([a-f\d\-]+)>$/) {
22 my @log = command(qw/cat-file commit/, $c->{c});
24 # shift off the headers
25 shift @log while ($log[0] ne '');
28 # TODO: make $c->{l} not have a trailing newline in the future
29 @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
31 (undef, $c->{r}, undef) = ::extract_metadata(
32 (grep(/^git-svn-id: /, @log))[-1]);
34 return defined $c->{r};
38 return $color || Git->repository->get_colorbool('color.diff');
42 my ($r_min, $r_max, @args) = @_;
44 my (@files, @log_opts);
45 foreach my $x (@args) {
46 if ($x eq '--' || @files) {
49 if (::verify_ref("$x^0")) {
57 my ($url, $rev, $uuid, $gs) = ::working_head_info($head);
60 $gs ||= Git::SVN->_new;
61 my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
63 push @cmd, '-r' unless $non_recursive;
64 push @cmd, qw/--raw --name-status/ if $verbose;
65 push @cmd, '--color' if log_use_color();
67 if (defined $r_max && $r_max == $r_min) {
68 push @cmd, '--max-count=1';
69 if (my $c = $gs->rev_map_get($r_max)) {
72 } elsif (defined $r_max) {
73 if ($r_max < $r_min) {
74 ($r_min, $r_max) = ($r_max, $r_min);
76 my (undef, $c_max) = $gs->find_rev_before($r_max, 1, $r_min);
77 my (undef, $c_min) = $gs->find_rev_after($r_min, 1, $r_max);
78 # If there are no commits in the range, both $c_max and $c_min
79 # will be undefined. If there is at least 1 commit in the
80 # range, both will be defined.
81 return () if !defined $c_min || !defined $c_max;
82 if ($c_min eq $c_max) {
83 push @cmd, '--max-count=1', $c_min;
85 push @cmd, '--boundary', "$c_min..$c_max";
88 return (@cmd, @files);
91 # adapted from pager.c
94 $ENV{GIT_PAGER_IN_USE} = 'false';
98 chomp($pager = command_oneline(qw(var GIT_PAGER)));
99 if ($pager eq 'cat') {
102 $ENV{GIT_PAGER_IN_USE} = defined($pager);
106 return unless defined $pager;
107 pipe my ($rfd, $wfd) or return;
108 defined(my $pid = fork) or fatal "Can't fork: $!";
110 open STDOUT, '>&', $wfd or
111 fatal "Can't redirect to stdout: $!";
114 open STDIN, '<&', $rfd or fatal "Can't redirect stdin: $!";
115 $ENV{LESS} ||= 'FRSX';
116 exec $pager or fatal "Can't run pager: $! ($pager)";
119 sub format_svn_date {
120 my $t = shift || time;
122 my $gmoff = Git::SVN::get_tz($t);
123 return strftime("%Y-%m-%d %H:%M:%S $gmoff (%a, %d %b %Y)", localtime($t));
128 # Date::Parse isn't in the standard Perl distro :(
129 if ($tz =~ s/^\+//) {
130 $t += tz_to_s_offset($tz);
131 } elsif ($tz =~ s/^\-//) {
132 $t -= tz_to_s_offset($tz);
137 sub set_local_timezone {
148 return ($1 * 60) + ($tz * 3600);
151 sub get_author_info {
152 my ($dest, $author, $t, $tz) = @_;
153 $author =~ s/(?:^\s*|\s*$)//g;
154 $dest->{a_raw} = $author;
157 $au = $rusers{$author} || undef;
160 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
165 $dest->{t_utc} = parse_git_date($t, $tz);
169 my ($c, $r_min, $r_max, $defer) = @_;
170 if (defined $r_min && defined $r_max) {
171 if ($r_min == $c->{r} && $r_min == $r_max) {
175 return 1 if $r_min == $r_max;
176 if ($r_min < $r_max) {
177 # we need to reverse the print order
178 return 0 if (defined $limit && --$limit < 0);
182 if ($r_min != $r_max) {
183 return 1 if ($r_min < $c->{r});
184 return 1 if ($r_max > $c->{r});
187 return 0 if (defined $limit && --$limit < 0);
197 if (my $l = $c->{l}) {
198 while ($l->[0] =~ /^\s*$/) { shift @$l }
201 $l_fmt ||= 'A' . length($c->{r});
202 print 'r',pack($l_fmt, $c->{r}),' | ';
203 print "$c->{c} | " if $show_commit;
206 show_commit_normal($c);
210 sub show_commit_changed_paths {
212 return unless $c->{changed};
213 print "Changed paths:\n", @{$c->{changed}};
216 sub show_commit_normal {
218 print commit_log_separator, "r$c->{r} | ";
219 print "$c->{c} | " if $show_commit;
220 print "$c->{a} | ", format_svn_date($c->{t_utc}), ' | ';
223 if (my $l = $c->{l}) {
224 while ($l->[$#$l] eq "\n" && $#$l > 0
225 && $l->[($#$l - 1)] eq "\n") {
228 $nr_line = scalar @$l;
230 print "1 line\n\n\n";
235 $nr_line .= ' lines';
237 print $nr_line, "\n";
238 show_commit_changed_paths($c);
240 print $_ foreach @$l;
244 show_commit_changed_paths($c);
248 foreach my $x (qw/raw stat diff/) {
251 print $_ foreach @{$c->{$x}}
259 my $r_last = -1; # prevent dupes
260 set_local_timezone();
261 if (defined $::_revision) {
262 if ($::_revision =~ /^(\d+):(\d+)$/) {
263 ($r_min, $r_max) = ($1, $2);
264 } elsif ($::_revision =~ /^\d+$/) {
265 $r_min = $r_max = $::_revision;
267 fatal "-r$::_revision is not supported, use ",
268 "standard 'git log' arguments instead";
273 @args = git_svn_log_cmd($r_min, $r_max, @args);
275 print commit_log_separator unless $incremental || $oneline;
278 my $log = command_output_pipe(@args);
280 my (@k, $c, $d, $stat);
281 my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
283 if (/^${esc_color}commit (?:- )?($::sha1_short)/o) {
285 if ($c && cmt_showable($c) && $c->{r} != $r_last) {
287 process_commit($c, $r_min, $r_max, \@k) or
292 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
293 get_author_info($c, $1, $2, $3);
294 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
296 } elsif (/^${esc_color}:\d{6} \d{6} $::sha1_short/o) {
297 push @{$c->{raw}}, $_;
298 } elsif (/^${esc_color}[ACRMDT]\t/) {
299 # we could add $SVN->{svn_path} here, but that requires
300 # remote access at the moment (repo_path_split)...
301 s#^(${esc_color})([ACRMDT])\t#$1 $2 #o;
302 push @{$c->{changed}}, $_;
303 } elsif (/^${esc_color}diff /o) {
305 push @{$c->{diff}}, $_;
307 push @{$c->{diff}}, $_;
308 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
309 $esc_color*[\+\-]*$esc_color$/x) {
311 push @{$c->{stat}}, $_;
312 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
313 push @{$c->{stat}}, $_;
315 } elsif (/^${esc_color} (git-svn-id:.+)$/o) {
316 ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
317 } elsif (s/^${esc_color} //o) {
321 if ($c && defined $c->{r} && $c->{r} != $r_last) {
323 process_commit($c, $r_min, $r_max, \@k);
326 ($r_min, $r_max) = ($r_max, $r_min);
327 process_commit($_, $r_min, $r_max) foreach reverse @k;
331 print commit_log_separator unless $incremental || $oneline;
340 my ($fh, $ctx, $rev);
343 ($fh, $ctx) = command_output_pipe('blame', @_, $path);
344 while (my $line = <$fh>) {
345 if ($line =~ /^\^?([[:xdigit:]]+)\s/) {
346 # Uncommitted edits show up as a rev ID of
347 # all zeros, which we can't look up with
350 (undef, $rev, undef) =
352 $rev = '0' if (!$rev);
356 $rev = sprintf('%-10s', $rev);
357 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
362 ($fh, $ctx) = command_output_pipe('blame', '-p', @_, 'HEAD',
367 my %dsha; #distinct sha keys
369 while (my $line = <$fh>) {
371 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
376 my $s2r = ::cmt_sha2rev_batch([keys %dsha]);
378 foreach my $line (@buffer) {
379 if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
381 $rev = '0' if (!$rev)
383 elsif ($line =~ /^author (.*)/) {
385 $authors{$rev} =~ s/\s/_/g;
387 elsif ($line =~ /^\t(.*)$/) {
388 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
392 command_close_pipe($fh, $ctx);