]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/git.pm
comments: pass COMMENTOPENID to templates
[ikiwiki.git] / IkiWiki / Plugin / git.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::git;
3
4 use warnings;
5 use strict;
6 use IkiWiki;
7 use Encode;
8 use open qw{:utf8 :std};
9
10 my $sha1_pattern     = qr/[0-9a-fA-F]{40}/; # pattern to validate Git sha1sums
11 my $dummy_commit_msg = 'dummy commit';      # message to skip in recent changes
12 my $no_chdir=0;
13
14 sub import {
15         hook(type => "checkconfig", id => "git", call => \&checkconfig);
16         hook(type => "getsetup", id => "git", call => \&getsetup);
17         hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
18         hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
19         hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
20         hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
21         hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
22         hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
23         hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
24         hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
25         hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
26         hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
27         hook(type => "rcs", id => "rcs_receive", call => \&rcs_receive);
28 }
29
30 sub checkconfig () {
31         if (! defined $config{gitorigin_branch}) {
32                 $config{gitorigin_branch}="origin";
33         }
34         if (! defined $config{gitmaster_branch}) {
35                 $config{gitmaster_branch}="master";
36         }
37         if (defined $config{git_wrapper} &&
38             length $config{git_wrapper}) {
39                 push @{$config{wrappers}}, {
40                         wrapper => $config{git_wrapper},
41                         wrappermode => (defined $config{git_wrappermode} ? $config{git_wrappermode} : "06755"),
42                 };
43         }
44         if (defined $config{git_test_receive_wrapper} &&
45             length $config{git_test_receive_wrapper}) {
46                 push @{$config{wrappers}}, {
47                         test_receive => 1,
48                         wrapper => $config{git_test_receive_wrapper},
49                         wrappermode => (defined $config{git_wrappermode} ? $config{git_wrappermode} : "06755"),
50                 };
51         }
52 }
53
54 sub getsetup () {
55         return
56                 plugin => {
57                         safe => 0, # rcs plugin
58                         rebuild => undef,
59                 },
60                 git_wrapper => {
61                         type => "string",
62                         example => "/git/wiki.git/hooks/post-update",
63                         description => "git hook to generate",
64                         safe => 0, # file
65                         rebuild => 0,
66                 },
67                 git_wrappermode => {
68                         type => "string",
69                         example => '06755',
70                         description => "mode for git_wrapper (can safely be made suid)",
71                         safe => 0,
72                         rebuild => 0,
73                 },
74                 git_test_receive_wrapper => {
75                         type => "string",
76                         example => "/git/wiki.git/hooks/pre-receive",
77                         description => "git pre-receive hook to generate",
78                         safe => 0, # file
79                         rebuild => 0,
80                 },
81                 untrusted_committers => {
82                         type => "string",
83                         example => [],
84                         description => "unix users whose commits should be checked by the pre-receive hook",
85                         safe => 0,
86                         rebuild => 0,
87                 },
88                 historyurl => {
89                         type => "string",
90                         example => "http://git.example.com/gitweb.cgi?p=wiki.git;a=history;f=[[file]]",
91                         description => "gitweb url to show file history ([[file]] substituted)",
92                         safe => 1,
93                         rebuild => 1,
94                 },
95                 diffurl => {
96                         type => "string",
97                         example => "http://git.example.com/gitweb.cgi?p=wiki.git;a=blobdiff;h=[[sha1_to]];hp=[[sha1_from]];hb=[[sha1_parent]];f=[[file]]",
98                         description => "gitweb url to show a diff ([[sha1_to]], [[sha1_from]], [[sha1_parent]], [[sha1_commit]] and [[file]] substituted)",
99                         safe => 1,
100                         rebuild => 1,
101                 },
102                 gitorigin_branch => {
103                         type => "string",
104                         example => "origin",
105                         description => "where to pull and push changes (set to empty string to disable)",
106                         safe => 0, # paranoia
107                         rebuild => 0,
108                 },
109                 gitmaster_branch => {
110                         type => "string",
111                         example => "master",
112                         description => "branch that the wiki is stored in",
113                         safe => 0, # paranoia
114                         rebuild => 0,
115                 },
116 }
117
118 sub safe_git (&@) {
119         # Start a child process safely without resorting /bin/sh.
120         # Return command output or success state (in scalar context).
121
122         my ($error_handler, @cmdline) = @_;
123
124         my $pid = open my $OUT, "-|";
125
126         error("Cannot fork: $!") if !defined $pid;
127
128         if (!$pid) {
129                 # In child.
130                 # Git commands want to be in wc.
131                 if (! $no_chdir) {
132                         chdir $config{srcdir}
133                             or error("Cannot chdir to $config{srcdir}: $!");
134                 }
135                 exec @cmdline or error("Cannot exec '@cmdline': $!");
136         }
137         # In parent.
138
139         my @lines;
140         while (<$OUT>) {
141                 chomp;
142                 push @lines, $_;
143         }
144
145         close $OUT;
146
147         $error_handler->("'@cmdline' failed: $!") if $? && $error_handler;
148
149         return wantarray ? @lines : ($? == 0);
150 }
151 # Convenient wrappers.
152 sub run_or_die ($@) { safe_git(\&error, @_) }
153 sub run_or_cry ($@) { safe_git(sub { warn @_ },  @_) }
154 sub run_or_non ($@) { safe_git(undef,            @_) }
155
156
157 sub merge_past ($$$) {
158         # Unlike with Subversion, Git cannot make a 'svn merge -rN:M file'.
159         # Git merge commands work with the committed changes, except in the
160         # implicit case of '-m' of git checkout(1).  So we should invent a
161         # kludge here.  In principle, we need to create a throw-away branch
162         # in preparing for the merge itself.  Since branches are cheap (and
163         # branching is fast), this shouldn't cost high.
164         #
165         # The main problem is the presence of _uncommitted_ local changes.  One
166         # possible approach to get rid of this situation could be that we first
167         # make a temporary commit in the master branch and later restore the
168         # initial state (this is possible since Git has the ability to undo a
169         # commit, i.e. 'git reset --soft HEAD^').  The method can be summarized
170         # as follows:
171         #
172         #       - create a diff of HEAD:current-sha1
173         #       - dummy commit
174         #       - create a dummy branch and switch to it
175         #       - rewind to past (reset --hard to the current-sha1)
176         #       - apply the diff and commit
177         #       - switch to master and do the merge with the dummy branch
178         #       - make a soft reset (undo the last commit of master)
179         #
180         # The above method has some drawbacks: (1) it needs a redundant commit
181         # just to get rid of local changes, (2) somewhat slow because of the
182         # required system forks.  Until someone points a more straight method
183         # (which I would be grateful) I have implemented an alternative method.
184         # In this approach, we hide all the modified files from Git by renaming
185         # them (using the 'rename' builtin) and later restore those files in
186         # the throw-away branch (that is, we put the files themselves instead
187         # of applying a patch).
188
189         my ($sha1, $file, $message) = @_;
190
191         my @undo;      # undo stack for cleanup in case of an error
192         my $conflict;  # file content with conflict markers
193
194         eval {
195                 # Hide local changes from Git by renaming the modified file.
196                 # Relative paths must be converted to absolute for renaming.
197                 my ($target, $hidden) = (
198                     "$config{srcdir}/${file}", "$config{srcdir}/${file}.${sha1}"
199                 );
200                 rename($target, $hidden)
201                     or error("rename '$target' to '$hidden' failed: $!");
202                 # Ensure to restore the renamed file on error.
203                 push @undo, sub {
204                         return if ! -e "$hidden"; # already renamed
205                         rename($hidden, $target)
206                             or warn "rename '$hidden' to '$target' failed: $!";
207                 };
208
209                 my $branch = "throw_away_${sha1}"; # supposed to be unique
210
211                 # Create a throw-away branch and rewind backward.
212                 push @undo, sub { run_or_cry('git', 'branch', '-D', $branch) };
213                 run_or_die('git', 'branch', $branch, $sha1);
214
215                 # Switch to throw-away branch for the merge operation.
216                 push @undo, sub {
217                         if (!run_or_cry('git', 'checkout', $config{gitmaster_branch})) {
218                                 run_or_cry('git', 'checkout','-f',$config{gitmaster_branch});
219                         }
220                 };
221                 run_or_die('git', 'checkout', $branch);
222
223                 # Put the modified file in _this_ branch.
224                 rename($hidden, $target)
225                     or error("rename '$hidden' to '$target' failed: $!");
226
227                 # _Silently_ commit all modifications in the current branch.
228                 run_or_non('git', 'commit', '-m', $message, '-a');
229                 # ... and re-switch to master.
230                 run_or_die('git', 'checkout', $config{gitmaster_branch});
231
232                 # Attempt to merge without complaining.
233                 if (!run_or_non('git', 'pull', '--no-commit', '.', $branch)) {
234                         $conflict = readfile($target);
235                         run_or_die('git', 'reset', '--hard');
236                 }
237         };
238         my $failure = $@;
239
240         # Process undo stack (in reverse order).  By policy cleanup
241         # actions should normally print a warning on failure.
242         while (my $handle = pop @undo) {
243                 $handle->();
244         }
245
246         error("Git merge failed!\n$failure\n") if $failure;
247
248         return $conflict;
249 }
250
251 sub parse_diff_tree ($@) {
252         # Parse the raw diff tree chunk and return the info hash.
253         # See git-diff-tree(1) for the syntax.
254
255         my ($prefix, $dt_ref) = @_;
256
257         # End of stream?
258         return if !defined @{ $dt_ref } ||
259                   !defined @{ $dt_ref }[0] || !length @{ $dt_ref }[0];
260
261         my %ci;
262         # Header line.
263         while (my $line = shift @{ $dt_ref }) {
264                 return if $line !~ m/^(.+) ($sha1_pattern)/;
265
266                 my $sha1 = $2;
267                 $ci{'sha1'} = $sha1;
268                 last;
269         }
270
271         # Identification lines for the commit.
272         while (my $line = shift @{ $dt_ref }) {
273                 # Regexps are semi-stolen from gitweb.cgi.
274                 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
275                         $ci{'tree'} = $1;
276                 }
277                 elsif ($line =~ m/^parent ([0-9a-fA-F]{40})$/) {
278                         # XXX: collecting in reverse order
279                         push @{ $ci{'parents'} }, $1;
280                 }
281                 elsif ($line =~ m/^(author|committer) (.*) ([0-9]+) (.*)$/) {
282                         my ($who, $name, $epoch, $tz) =
283                            ($1,   $2,    $3,     $4 );
284
285                         $ci{  $who          } = $name;
286                         $ci{ "${who}_epoch" } = $epoch;
287                         $ci{ "${who}_tz"    } = $tz;
288
289                         if ($name =~ m/^[^<]+\s+<([^@>]+)/) {
290                                 $ci{"${who}_username"} = $1;
291                         }
292                         elsif ($name =~ m/^([^<]+)\s+<>$/) {
293                                 $ci{"${who}_username"} = $1;
294                         }
295                         else {
296                                 $ci{"${who}_username"} = $name;
297                         }
298                 }
299                 elsif ($line =~ m/^$/) {
300                         # Trailing empty line signals next section.
301                         last;
302                 }
303         }
304
305         debug("No 'tree' seen in diff-tree output") if !defined $ci{'tree'};
306         
307         if (defined $ci{'parents'}) {
308                 $ci{'parent'} = @{ $ci{'parents'} }[0];
309         }
310         else {
311                 $ci{'parent'} = 0 x 40;
312         }
313
314         # Commit message (optional).
315         while ($dt_ref->[0] =~ /^    /) {
316                 my $line = shift @{ $dt_ref };
317                 $line =~ s/^    //;
318                 push @{ $ci{'comment'} }, $line;
319         }
320         shift @{ $dt_ref } if $dt_ref->[0] =~ /^$/;
321
322         # Modified files.
323         while (my $line = shift @{ $dt_ref }) {
324                 if ($line =~ m{^
325                         (:+)       # number of parents
326                         ([^\t]+)\t # modes, sha1, status
327                         (.*)       # file names
328                 $}xo) {
329                         my $num_parents = length $1;
330                         my @tmp = split(" ", $2);
331                         my ($file, $file_to) = split("\t", $3);
332                         my @mode_from = splice(@tmp, 0, $num_parents);
333                         my $mode_to = shift(@tmp);
334                         my @sha1_from = splice(@tmp, 0, $num_parents);
335                         my $sha1_to = shift(@tmp);
336                         my $status = shift(@tmp);
337
338                         # git does not output utf-8 filenames, but instead
339                         # double-quotes them with the utf-8 characters
340                         # escaped as \nnn\nnn.
341                         if ($file =~ m/^"(.*)"$/) {
342                                 ($file=$1) =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
343                         }
344                         $file =~ s/^\Q$prefix\E//;
345                         if (length $file) {
346                                 push @{ $ci{'details'} }, {
347                                         'file'      => decode("utf8", $file),
348                                         'sha1_from' => $sha1_from[0],
349                                         'sha1_to'   => $sha1_to,
350                                         'mode_from' => $mode_from[0],
351                                         'mode_to'   => $mode_to,
352                                         'status'    => $status,
353                                 };
354                         }
355                         next;
356                 };
357                 last;
358         }
359
360         return \%ci;
361 }
362
363 sub git_commit_info ($;$) {
364         # Return an array of commit info hashes of num commits
365         # starting from the given sha1sum.
366         my ($sha1, $num) = @_;
367
368         my @opts;
369         push @opts, "--max-count=$num" if defined $num;
370
371         my @raw_lines = run_or_die('git', 'log', @opts,
372                 '--pretty=raw', '--raw', '--abbrev=40', '--always', '-c',
373                 '-r', $sha1, '--', '.');
374         my ($prefix) = run_or_die('git', 'rev-parse', '--show-prefix');
375
376         my @ci;
377         while (my $parsed = parse_diff_tree(($prefix or ""), \@raw_lines)) {
378                 push @ci, $parsed;
379         }
380
381         warn "Cannot parse commit info for '$sha1' commit" if !@ci;
382
383         return wantarray ? @ci : $ci[0];
384 }
385
386 sub git_sha1 (;$) {
387         # Return head sha1sum (of given file).
388         my $file = shift || q{--};
389
390         # Ignore error since a non-existing file might be given.
391         my ($sha1) = run_or_non('git', 'rev-list', '--max-count=1', 'HEAD',
392                 '--', $file);
393         if ($sha1) {
394                 ($sha1) = $sha1 =~ m/($sha1_pattern)/; # sha1 is untainted now
395         } else { debug("Empty sha1sum for '$file'.") }
396         return defined $sha1 ? $sha1 : q{};
397 }
398
399 sub rcs_update () {
400         # Update working directory.
401
402         if (length $config{gitorigin_branch}) {
403                 run_or_cry('git', 'pull', $config{gitorigin_branch});
404         }
405 }
406
407 sub rcs_prepedit ($) {
408         # Return the commit sha1sum of the file when editing begins.
409         # This will be later used in rcs_commit if a merge is required.
410         my ($file) = @_;
411
412         return git_sha1($file);
413 }
414
415 sub rcs_commit ($$$;$$) {
416         # Try to commit the page; returns undef on _success_ and
417         # a version of the page with the rcs's conflict markers on
418         # failure.
419
420         my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
421
422         # Check to see if the page has been changed by someone else since
423         # rcs_prepedit was called.
424         my $cur    = git_sha1($file);
425         my ($prev) = $rcstoken =~ /^($sha1_pattern)$/; # untaint
426
427         if (defined $cur && defined $prev && $cur ne $prev) {
428                 my $conflict = merge_past($prev, $file, $dummy_commit_msg);
429                 return $conflict if defined $conflict;
430         }
431
432         rcs_add($file); 
433         return rcs_commit_staged($message, $user, $ipaddr);
434 }
435
436 sub rcs_commit_staged ($$$) {
437         # Commits all staged changes. Changes can be staged using rcs_add,
438         # rcs_remove, and rcs_rename.
439         my ($message, $user, $ipaddr)=@_;
440
441         # Set the commit author and email to the web committer.
442         my %env=%ENV;
443         if (defined $user || defined $ipaddr) {
444                 my $u=defined $user ? $user : $ipaddr;
445                 $ENV{GIT_AUTHOR_NAME}=$u;
446                 $ENV{GIT_AUTHOR_EMAIL}="$u\@web";
447         }
448
449         $message = IkiWiki::possibly_foolish_untaint($message);
450         my @opts;
451         if ($message !~ /\S/) {
452                 # Force git to allow empty commit messages.
453                 # (If this version of git supports it.)
454                 my ($version)=`git --version` =~ /git version (.*)/;
455                 if ($version ge "1.5.4") {
456                         push @opts, '--cleanup=verbatim';
457                 }
458                 else {
459                         $message.=".";
460                 }
461         }
462         push @opts, '-q';
463         # git commit returns non-zero if file has not been really changed.
464         # so we should ignore its exit status (hence run_or_non).
465         if (run_or_non('git', 'commit', @opts, '-m', $message)) {
466                 if (length $config{gitorigin_branch}) {
467                         run_or_cry('git', 'push', $config{gitorigin_branch});
468                 }
469         }
470         
471         %ENV=%env;
472         return undef; # success
473 }
474
475 sub rcs_add ($) {
476         # Add file to archive.
477
478         my ($file) = @_;
479
480         run_or_cry('git', 'add', $file);
481 }
482
483 sub rcs_remove ($) {
484         # Remove file from archive.
485
486         my ($file) = @_;
487
488         run_or_cry('git', 'rm', '-f', $file);
489 }
490
491 sub rcs_rename ($$) {
492         my ($src, $dest) = @_;
493
494         run_or_cry('git', 'mv', '-f', $src, $dest);
495 }
496
497 sub rcs_recentchanges ($) {
498         # List of recent changes.
499
500         my ($num) = @_;
501
502         eval q{use Date::Parse};
503         error($@) if $@;
504
505         my @rets;
506         foreach my $ci (git_commit_info('HEAD', $num || 1)) {
507                 # Skip redundant commits.
508                 next if ($ci->{'comment'} && @{$ci->{'comment'}}[0] eq $dummy_commit_msg);
509
510                 my ($sha1, $when) = (
511                         $ci->{'sha1'},
512                         $ci->{'author_epoch'}
513                 );
514
515                 my @pages;
516                 foreach my $detail (@{ $ci->{'details'} }) {
517                         my $file = $detail->{'file'};
518
519                         my $diffurl = defined $config{'diffurl'} ? $config{'diffurl'} : "";
520                         $diffurl =~ s/\[\[file\]\]/$file/go;
521                         $diffurl =~ s/\[\[sha1_parent\]\]/$ci->{'parent'}/go;
522                         $diffurl =~ s/\[\[sha1_from\]\]/$detail->{'sha1_from'}/go;
523                         $diffurl =~ s/\[\[sha1_to\]\]/$detail->{'sha1_to'}/go;
524                         $diffurl =~ s/\[\[sha1_commit\]\]/$sha1/go;
525
526                         push @pages, {
527                                 page => pagename($file),
528                                 diffurl => $diffurl,
529                         };
530                 }
531
532                 my @messages;
533                 my $pastblank=0;
534                 foreach my $line (@{$ci->{'comment'}}) {
535                         $pastblank=1 if $line eq '';
536                         next if $pastblank && $line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i;
537                         push @messages, { line => $line };
538                 }
539
540                 my $user=$ci->{'author_username'};
541                 my $web_commit = ($ci->{'author'} =~ /\@web>/);
542                 
543                 # compatability code for old web commit messages
544                 if (! $web_commit &&
545                       defined $messages[0] &&
546                       $messages[0]->{line} =~ m/$config{web_commit_regexp}/) {
547                         $user = defined $2 ? "$2" : "$3";
548                         $messages[0]->{line} = $4;
549                         $web_commit=1;
550                 }
551
552                 push @rets, {
553                         rev        => $sha1,
554                         user       => $user,
555                         committype => $web_commit ? "web" : "git",
556                         when       => $when,
557                         message    => [@messages],
558                         pages      => [@pages],
559                 } if @pages;
560
561                 last if @rets >= $num;
562         }
563
564         return @rets;
565 }
566
567 sub rcs_diff ($) {
568         my $rev=shift;
569         my ($sha1) = $rev =~ /^($sha1_pattern)$/; # untaint
570         my @lines;
571         foreach my $line (run_or_non("git", "show", $sha1)) {
572                 if (@lines || $line=~/^diff --git/) {
573                         push @lines, $line."\n";
574                 }
575         }
576         if (wantarray) {
577                 return @lines;
578         }
579         else {
580                 return join("", @lines);
581         }
582 }
583
584 sub rcs_getctime ($) {
585         my $file=shift;
586         # Remove srcdir prefix
587         $file =~ s/^\Q$config{srcdir}\E\/?//;
588
589         my $sha1  = git_sha1($file);
590         my $ci    = git_commit_info($sha1, 1);
591         my $ctime = $ci->{'author_epoch'};
592         debug("ctime for '$file': ". localtime($ctime));
593
594         return $ctime;
595 }
596
597 sub rcs_receive () {
598         # The wiki may not be the only thing in the git repo.
599         # Determine if it is in a subdirectory by examining the srcdir,
600         # and its parents, looking for the .git directory.
601         my $subdir="";
602         my $dir=$config{srcdir};
603         while (! -d "$dir/.git") {
604                 $subdir=IkiWiki::basename($dir)."/".$subdir;
605                 $dir=IkiWiki::dirname($dir);
606                 if (! length $dir) {
607                         error("cannot determine root of git repo");
608                 }
609         }
610
611         my @rets;
612         while (<>) {
613                 chomp;
614                 my ($oldrev, $newrev, $refname) = split(' ', $_, 3);
615                 
616                 # only allow changes to gitmaster_branch
617                 if ($refname !~ /^refs\/heads\/\Q$config{gitmaster_branch}\E$/) {
618                         error sprintf(gettext("you are not allowed to change %s"), $refname);
619                 }
620                 
621                 # Avoid chdir when running git here, because the changes
622                 # are in the master git repo, not the srcdir repo.
623                 # The pre-recieve hook already puts us in the right place.
624                 $no_chdir=1;
625                 my @changes=git_commit_info($oldrev."..".$newrev);
626                 $no_chdir=0;
627
628                 foreach my $ci (@changes) {
629                         foreach my $detail (@{ $ci->{'details'} }) {
630                                 my $file = $detail->{'file'};
631
632                                 # check that all changed files are in the
633                                 # subdir
634                                 if (length $subdir &&
635                                     ! ($file =~ s/^\Q$subdir\E//)) {
636                                         error sprintf(gettext("you are not allowed to change %s"), $file);
637                                 }
638
639                                 my ($action, $mode, $path);
640                                 if ($detail->{'status'} =~ /^[M]+\d*$/) {
641                                         $action="change";
642                                         $mode=$detail->{'mode_to'};
643                                 }
644                                 elsif ($detail->{'status'} =~ /^[AM]+\d*$/) {
645                                         $action="add";
646                                         $mode=$detail->{'mode_to'};
647                                 }
648                                 elsif ($detail->{'status'} =~ /^[DAM]+\d*/) {
649                                         $action="remove";
650                                         $mode=$detail->{'mode_from'};
651                                 }
652                                 else {
653                                         error "unknown status ".$detail->{'status'};
654                                 }
655                                 
656                                 # test that the file mode is ok
657                                 if ($mode !~ /^100[64][64][64]$/) {
658                                         error sprintf(gettext("you cannot act on a file with mode %s"), $mode);
659                                 }
660                                 if ($action eq "change") {
661                                         if ($detail->{'mode_from'} ne $detail->{'mode_to'}) {
662                                                 error gettext("you are not allowed to change file modes");
663                                         }
664                                 }
665                                 
666                                 # extract attachment to temp file
667                                 if (($action eq 'add' || $action eq 'change') &&
668                                      ! pagetype($file)) {
669                                         eval q{use File::Temp};
670                                         die $@ if $@;
671                                         my $fh;
672                                         ($fh, $path)=File::Temp::tempfile("XXXXXXXXXX", UNLINK => 1);
673                                         if (system("git show ".$detail->{sha1_to}." > '$path'") != 0) {
674                                                 error("failed writing temp file");
675                                         }
676                                 }
677
678                                 push @rets, {
679                                         file => $file,
680                                         action => $action,
681                                         path => $path,
682                                 };
683                         }
684                 }
685         }
686
687         return reverse @rets;
688 }
689
690 1