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