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