Imported Upstream version 1.8.0
[platform/upstream/git.git] / perl / Git / SVN / Log.pm
1 package Git::SVN::Log;
2 use strict;
3 use warnings;
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/;
10
11 # Option set in git-svn
12 our $_git_format;
13
14 sub cmt_showable {
15         my ($c) = @_;
16         return 1 if defined $c->{r};
17
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\-]+)>$/) {
21                 @{$c->{l}} = ();
22                 my @log = command(qw/cat-file commit/, $c->{c});
23
24                 # shift off the headers
25                 shift @log while ($log[0] ne '');
26                 shift @log;
27
28                 # TODO: make $c->{l} not have a trailing newline in the future
29                 @{$c->{l}} = map { "$_\n" } grep !/^git-svn-id: /, @log;
30
31                 (undef, $c->{r}, undef) = ::extract_metadata(
32                                 (grep(/^git-svn-id: /, @log))[-1]);
33         }
34         return defined $c->{r};
35 }
36
37 sub log_use_color {
38         return $color || Git->repository->get_colorbool('color.diff');
39 }
40
41 sub git_svn_log_cmd {
42         my ($r_min, $r_max, @args) = @_;
43         my $head = 'HEAD';
44         my (@files, @log_opts);
45         foreach my $x (@args) {
46                 if ($x eq '--' || @files) {
47                         push @files, $x;
48                 } else {
49                         if (::verify_ref("$x^0")) {
50                                 $head = $x;
51                         } else {
52                                 push @log_opts, $x;
53                         }
54                 }
55         }
56
57         my ($url, $rev, $uuid, $gs) = ::working_head_info($head);
58
59         require Git::SVN;
60         $gs ||= Git::SVN->_new;
61         my @cmd = (qw/log --abbrev-commit --pretty=raw --default/,
62                    $gs->refname);
63         push @cmd, '-r' unless $non_recursive;
64         push @cmd, qw/--raw --name-status/ if $verbose;
65         push @cmd, '--color' if log_use_color();
66         push @cmd, @log_opts;
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)) {
70                         push @cmd, $c;
71                 }
72         } elsif (defined $r_max) {
73                 if ($r_max < $r_min) {
74                         ($r_min, $r_max) = ($r_max, $r_min);
75                 }
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;
84                 } else {
85                         push @cmd, '--boundary', "$c_min..$c_max";
86                 }
87         }
88         return (@cmd, @files);
89 }
90
91 # adapted from pager.c
92 sub config_pager {
93         if (! -t *STDOUT) {
94                 $ENV{GIT_PAGER_IN_USE} = 'false';
95                 $pager = undef;
96                 return;
97         }
98         chomp($pager = command_oneline(qw(var GIT_PAGER)));
99         if ($pager eq 'cat') {
100                 $pager = undef;
101         }
102         $ENV{GIT_PAGER_IN_USE} = defined($pager);
103 }
104
105 sub run_pager {
106         return unless defined $pager;
107         pipe my ($rfd, $wfd) or return;
108         defined(my $pid = fork) or fatal "Can't fork: $!";
109         if (!$pid) {
110                 open STDOUT, '>&', $wfd or
111                                      fatal "Can't redirect to stdout: $!";
112                 return;
113         }
114         open STDIN, '<&', $rfd or fatal "Can't redirect stdin: $!";
115         $ENV{LESS} ||= 'FRSX';
116         exec $pager or fatal "Can't run pager: $! ($pager)";
117 }
118
119 sub format_svn_date {
120         my $t = shift || time;
121         require Git::SVN;
122         my $gmoff = Git::SVN::get_tz($t);
123         return strftime("%Y-%m-%d %H:%M:%S $gmoff (%a, %d %b %Y)", localtime($t));
124 }
125
126 sub parse_git_date {
127         my ($t, $tz) = @_;
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);
133         }
134         return $t;
135 }
136
137 sub set_local_timezone {
138         if (defined $TZ) {
139                 $ENV{TZ} = $TZ;
140         } else {
141                 delete $ENV{TZ};
142         }
143 }
144
145 sub tz_to_s_offset {
146         my ($tz) = @_;
147         $tz =~ s/(\d\d)$//;
148         return ($1 * 60) + ($tz * 3600);
149 }
150
151 sub get_author_info {
152         my ($dest, $author, $t, $tz) = @_;
153         $author =~ s/(?:^\s*|\s*$)//g;
154         $dest->{a_raw} = $author;
155         my $au;
156         if ($::_authors) {
157                 $au = $rusers{$author} || undef;
158         }
159         if (!$au) {
160                 ($au) = ($author =~ /<([^>]+)\@[^>]+>$/);
161         }
162         $dest->{t} = $t;
163         $dest->{tz} = $tz;
164         $dest->{a} = $au;
165         $dest->{t_utc} = parse_git_date($t, $tz);
166 }
167
168 sub process_commit {
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) {
172                         show_commit($c);
173                         return 0;
174                 }
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);
179                         push @$defer, $c;
180                         return 1;
181                 }
182                 if ($r_min != $r_max) {
183                         return 1 if ($r_min < $c->{r});
184                         return 1 if ($r_max > $c->{r});
185                 }
186         }
187         return 0 if (defined $limit && --$limit < 0);
188         show_commit($c);
189         return 1;
190 }
191
192 my $l_fmt;
193 sub show_commit {
194         my $c = shift;
195         if ($oneline) {
196                 my $x = "\n";
197                 if (my $l = $c->{l}) {
198                         while ($l->[0] =~ /^\s*$/) { shift @$l }
199                         $x = $l->[0];
200                 }
201                 $l_fmt ||= 'A' . length($c->{r});
202                 print 'r',pack($l_fmt, $c->{r}),' | ';
203                 print "$c->{c} | " if $show_commit;
204                 print $x;
205         } else {
206                 show_commit_normal($c);
207         }
208 }
209
210 sub show_commit_changed_paths {
211         my ($c) = @_;
212         return unless $c->{changed};
213         print "Changed paths:\n", @{$c->{changed}};
214 }
215
216 sub show_commit_normal {
217         my ($c) = @_;
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}), ' | ';
221         my $nr_line = 0;
222
223         if (my $l = $c->{l}) {
224                 while ($l->[$#$l] eq "\n" && $#$l > 0
225                                           && $l->[($#$l - 1)] eq "\n") {
226                         pop @$l;
227                 }
228                 $nr_line = scalar @$l;
229                 if (!$nr_line) {
230                         print "1 line\n\n\n";
231                 } else {
232                         if ($nr_line == 1) {
233                                 $nr_line = '1 line';
234                         } else {
235                                 $nr_line .= ' lines';
236                         }
237                         print $nr_line, "\n";
238                         show_commit_changed_paths($c);
239                         print "\n";
240                         print $_ foreach @$l;
241                 }
242         } else {
243                 print "1 line\n";
244                 show_commit_changed_paths($c);
245                 print "\n";
246
247         }
248         foreach my $x (qw/raw stat diff/) {
249                 if ($c->{$x}) {
250                         print "\n";
251                         print $_ foreach @{$c->{$x}}
252                 }
253         }
254 }
255
256 sub cmd_show_log {
257         my (@args) = @_;
258         my ($r_min, $r_max);
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;
266                 } else {
267                         fatal "-r$::_revision is not supported, use ",
268                                 "standard 'git log' arguments instead";
269                 }
270         }
271
272         config_pager();
273         @args = git_svn_log_cmd($r_min, $r_max, @args);
274         if (!@args) {
275                 print commit_log_separator unless $incremental || $oneline;
276                 return;
277         }
278         my $log = command_output_pipe(@args);
279         run_pager();
280         my (@k, $c, $d, $stat);
281         my $esc_color = qr/(?:\033\[(?:(?:\d+;)*\d*)?m)*/;
282         while (<$log>) {
283                 if (/^${esc_color}commit (?:- )?($::sha1_short)/o) {
284                         my $cmt = $1;
285                         if ($c && cmt_showable($c) && $c->{r} != $r_last) {
286                                 $r_last = $c->{r};
287                                 process_commit($c, $r_min, $r_max, \@k) or
288                                                                 goto out;
289                         }
290                         $d = undef;
291                         $c = { c => $cmt };
292                 } elsif (/^${esc_color}author (.+) (\d+) ([\-\+]?\d+)$/o) {
293                         get_author_info($c, $1, $2, $3);
294                 } elsif (/^${esc_color}(?:tree|parent|committer) /o) {
295                         # ignore
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) {
304                         $d = 1;
305                         push @{$c->{diff}}, $_;
306                 } elsif ($d) {
307                         push @{$c->{diff}}, $_;
308                 } elsif (/^\ .+\ \|\s*\d+\ $esc_color[\+\-]*
309                           $esc_color*[\+\-]*$esc_color$/x) {
310                         $stat = 1;
311                         push @{$c->{stat}}, $_;
312                 } elsif ($stat && /^ \d+ files changed, \d+ insertions/) {
313                         push @{$c->{stat}}, $_;
314                         $stat = undef;
315                 } elsif (/^${esc_color}    (git-svn-id:.+)$/o) {
316                         ($c->{url}, $c->{r}, undef) = ::extract_metadata($1);
317                 } elsif (s/^${esc_color}    //o) {
318                         push @{$c->{l}}, $_;
319                 }
320         }
321         if ($c && defined $c->{r} && $c->{r} != $r_last) {
322                 $r_last = $c->{r};
323                 process_commit($c, $r_min, $r_max, \@k);
324         }
325         if (@k) {
326                 ($r_min, $r_max) = ($r_max, $r_min);
327                 process_commit($_, $r_min, $r_max) foreach reverse @k;
328         }
329 out:
330         close $log;
331         print commit_log_separator unless $incremental || $oneline;
332 }
333
334 sub cmd_blame {
335         my $path = pop;
336
337         config_pager();
338         run_pager();
339
340         my ($fh, $ctx, $rev);
341
342         if ($_git_format) {
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
348                                 # cmt_metadata
349                                 if ($1 !~ /^0+$/) {
350                                         (undef, $rev, undef) =
351                                                 ::cmt_metadata($1);
352                                         $rev = '0' if (!$rev);
353                                 } else {
354                                         $rev = '0';
355                                 }
356                                 $rev = sprintf('%-10s', $rev);
357                                 $line =~ s/^\^?[[:xdigit:]]+(\s)/$rev$1/;
358                         }
359                         print $line;
360                 }
361         } else {
362                 ($fh, $ctx) = command_output_pipe('blame', '-p', @_, 'HEAD',
363                                                   '--', $path);
364                 my ($sha1);
365                 my %authors;
366                 my @buffer;
367                 my %dsha; #distinct sha keys
368
369                 while (my $line = <$fh>) {
370                         push @buffer, $line;
371                         if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
372                                 $dsha{$1} = 1;
373                         }
374                 }
375
376                 my $s2r = ::cmt_sha2rev_batch([keys %dsha]);
377
378                 foreach my $line (@buffer) {
379                         if ($line =~ /^([[:xdigit:]]{40})\s\d+\s\d+/) {
380                                 $rev = $s2r->{$1};
381                                 $rev = '0' if (!$rev)
382                         }
383                         elsif ($line =~ /^author (.*)/) {
384                                 $authors{$rev} = $1;
385                                 $authors{$rev} =~ s/\s/_/g;
386                         }
387                         elsif ($line =~ /^\t(.*)$/) {
388                                 printf("%6s %10s %s\n", $rev, $authors{$rev}, $1);
389                         }
390                 }
391         }
392         command_close_pipe($fh, $ctx);
393 }
394
395 1;