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