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