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