summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/darcs.pm
blob: 0957ef0efd9dff6197e04f097b3471798e90066e (plain)
  1. # Support for the darcs rcs, <URL:http://darcs.net/>.
  2. # Copyright (C) 2006 Thomas Schwinge <tschwinge@gnu.org>
  3. # 2007 Benjamin A'Lee <bma@bmalee.eu>
  4. # Tuomo Valkonen <tuomov@iki.fi>
  5. # 2008 Simon Michael <simon@joyful.com>
  6. # Petr Ročkai <me@mornfall.net>
  7. # Sven M. Hallberg <pesco@khjk.org>
  8. #
  9. # This program is free software; you can redistribute it and/or modify it
  10. # under the terms of the GNU General Public License as published by the
  11. # Free Software Foundation; either version 2 of the License, or (at your
  12. # option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful, but
  15. # WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. # General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. # History (see http://ikiwiki.info/todo/darcs/):
  23. #
  24. # * Thomas Schwinge wrote the original file, implementing only rcs_commit.
  25. # * Benjamin A'Lee contributed an alternative implementation.
  26. # * Tuomo Valkonen contributed rcs_getctime and stub rcs_recentchanges.
  27. # * Simon Michael contributed multiple changes.
  28. # * Petr Ročkai fixed rcs_recentchanges and added caching to rcs_getctime.
  29. # * Sven M. Hallberg merged the above and added missing features.
  30. # We're guaranteed to be the only instance of ikiwiki running at a given
  31. # time. It is essential that only ikiwiki is working on a particular
  32. # repository. That means one instance of ikiwiki and it also means that
  33. # you must not 'darcs push' into this repository, as this might create
  34. # race conditions, as I understand it.
  35. package IkiWiki::Plugin::darcs;
  36. use warnings;
  37. use strict;
  38. use IkiWiki;
  39. sub import {
  40. hook(type => "checkconfig", id => "darcs", call => \&checkconfig);
  41. hook(type => "getsetup", id => "darcs", call => \&getsetup);
  42. hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
  43. hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
  44. hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
  45. hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
  46. hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
  47. hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
  48. hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
  49. hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
  50. hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
  51. hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
  52. }
  53. # Which darcs executable to use.
  54. my $darcs = ($ENV{DARCS} or 'darcs');
  55. # Internal functions
  56. our %cache;
  57. sub loadcache () {
  58. my $repodir=$config{srcdir};
  59. if (!defined %cache) {
  60. my $f = "$repodir/.ikiwiki.ctimes";
  61. if (-s "$f") {
  62. my $VAR1;
  63. my $x = `cat "$f"`;
  64. $x =~ /^(.*)$/sm; # untaint
  65. eval "$1";
  66. %cache = %$VAR1;
  67. }
  68. }
  69. }
  70. END {
  71. my $repodir=$config{srcdir};
  72. if (defined %cache) {
  73. debug("dumping ctime cache to $repodir/.ikiwiki.ctimes");
  74. open CACHE, ">$repodir/.ikiwiki.ctimes";
  75. print CACHE Dumper(\%cache);
  76. close CACHE;
  77. }
  78. }
  79. sub silentsystem (@) {
  80. open(SAVED_STDOUT, ">&STDOUT");
  81. open(STDOUT, ">/dev/null");
  82. my $ret = system @_;
  83. open(STDOUT, ">&SAVED_STDOUT");
  84. return $ret;
  85. }
  86. sub darcs_info ($$$) {
  87. my $field = shift;
  88. my $repodir = shift;
  89. my $file = shift; # Relative to the repodir.
  90. my $child = open(DARCS_CHANGES, "-|");
  91. if (! $child) {
  92. exec($darcs, 'changes', '--repodir', $repodir, '--xml-output', $file) or
  93. error("failed to run 'darcs changes'");
  94. }
  95. # Brute force for now. :-/
  96. while (<DARCS_CHANGES>) {
  97. last if /^<\/created_as>$/;
  98. }
  99. ($_) = <DARCS_CHANGES> =~ /$field=\'([^\']+)/;
  100. $field eq 'hash' and s/\.gz//; # Strip away the '.gz' from 'hash'es.
  101. close(DARCS_CHANGES) or error("'darcs changes' exited " . $?);
  102. return $_;
  103. }
  104. sub darcs_rev($) {
  105. my $file = shift; # Relative to the repodir.
  106. my $repodir = $config{srcdir};
  107. my $child = open(DARCS_MANIFEST, "-|");
  108. if (! $child) {
  109. exec($darcs, 'query', 'manifest', '--repodir', $repodir) or
  110. error("failed to run 'darcs query manifest'");
  111. }
  112. my $found=0;
  113. while (<DARCS_MANIFEST>) {
  114. $found = 1, last if /^$file$/;
  115. }
  116. return "" if (! $found);
  117. close(DARCS_MANIFEST) or error("'darcs query manifest' exited " . $?);
  118. my $hash = darcs_info('hash', $repodir, $file);
  119. return defined $hash ? $hash : "";
  120. }
  121. # Exported functions.
  122. sub checkconfig() {
  123. if (defined $config{darcs_wrapper} && length $config{darcs_wrapper}) {
  124. push @{$config{wrappers}}, {
  125. wrapper => $config{darcs_wrapper},
  126. wrappermode => (defined $config{darcs_wrappermode} ? $config{darcs_wrappermode} : "06755"),
  127. };
  128. }
  129. }
  130. sub getsetup() {
  131. return
  132. plugin => {
  133. safe => 0, # rcs plugin
  134. rebuild => undef,
  135. },
  136. darcs_wrapper => {
  137. type => "string",
  138. example => "/darcs/repo/_darcs/ikiwiki-wrapper",
  139. description => "wrapper to generate (set as master repo apply hook)",
  140. safe => 0, # file
  141. rebuild => 0,
  142. },
  143. darcs_wrappermode => {
  144. type => "string",
  145. example => '06755',
  146. description => "mode for darcs_wrapper (can safely be made suid)",
  147. safe => 0,
  148. rebuild => 0,
  149. },
  150. historyurl => {
  151. type => "string",
  152. example => "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filehistory;f=[[file]]",
  153. description => "darcsweb url to show file history ([[file]] substituted)",
  154. safe => 1,
  155. rebuild => 1,
  156. },
  157. diffurl => {
  158. type => "string",
  159. example => "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filediff;h=[[hash]];f=[[file]]",
  160. description => "darcsweb url to show a diff ([[hash]] and [[file]] substituted)",
  161. safe => 1,
  162. rebuild => 1,
  163. },
  164. }
  165. sub rcs_update () {
  166. silentsystem($darcs, "pull", "--repodir", $config{srcdir}, "-qa")
  167. }
  168. sub rcs_prepedit ($) {
  169. # Prepares to edit a file under revision control. Returns a token that
  170. # must be passed to rcs_commit() when the file is to be commited. For us,
  171. # this token the hash value of the latest patch that modifies the file,
  172. # i.e. something like its current revision. If the file is not yet added
  173. # to the repository, we return TODO: the empty string.
  174. my $file = shift; # Relative to the repodir.
  175. my $rev = darcs_rev($file);
  176. return $rev;
  177. }
  178. sub rcs_commit ($$$;$$) {
  179. # Commit the page. Returns 'undef' on success and a version of the page
  180. # with conflict markers on failure.
  181. my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
  182. # Compute if the "revision" of $file changed.
  183. my $changed = darcs_rev($file) ne $rcstoken;
  184. # Yes, the following is a bit convoluted.
  185. if ($changed) {
  186. # TODO. Invent a better, non-conflicting name.
  187. rename("$config{srcdir}/$file", "$config{srcdir}/$file.save") or
  188. error("failed to rename $file to $file.save: $!");
  189. # Roll the repository back to $rcstoken.
  190. # TODO. Can we be sure that no changes are lost? I think that
  191. # we can, if we make sure that the 'darcs push' below will always
  192. # succeed.
  193. # We need to revert everything as 'darcs obliterate' might choke
  194. # otherwise.
  195. # TODO: 'yes | ...' needed? Doesn't seem so.
  196. silentsystem($darcs, "revert", "--repodir", $config{srcdir}, "--all") and
  197. error("'darcs revert' failed");
  198. # Remove all patches starting at $rcstoken.
  199. my $child = open(DARCS_OBLITERATE, "|-");
  200. if (! $child) {
  201. open(STDOUT, ">/dev/null");
  202. exec($darcs, "obliterate", "--repodir", $config{srcdir},
  203. "--match", "hash " . $rcstoken) and
  204. error("'darcs obliterate' failed");
  205. }
  206. while (print DARCS_OBLITERATE "y") {
  207. ;
  208. }
  209. close(DARCS_OBLITERATE);
  210. # Restore the $rcstoken one.
  211. silentsystem($darcs, "pull", "--quiet", "--repodir", $config{srcdir},
  212. "--match", "hash " . $rcstoken, "--all") and
  213. error("'darcs pull' failed");
  214. # We're back at $rcstoken. Re-install the modified file.
  215. rename("$config{srcdir}/$file.save", "$config{srcdir}/$file") or
  216. error("failed to rename $file.save to $file: $!");
  217. }
  218. # Record the changes.
  219. my $author;
  220. if (defined $user) {
  221. $author = "$user\@web";
  222. } elsif (defined $ipaddr) {
  223. $author = "$ipaddr\@web";
  224. } else {
  225. $author = "anon\@web";
  226. }
  227. if (!defined $message || !length($message)) {
  228. $message = "empty message";
  229. }
  230. silentsystem($darcs, 'record', '--repodir', $config{srcdir}, '--all',
  231. '-m', $message, '--author', $author, $file) and
  232. error("'darcs record' failed");
  233. # Update the repository by pulling from the default repository, which is
  234. # master repository.
  235. silentsystem($darcs, "pull", "--quiet", "--repodir", $config{srcdir},
  236. "--all") and error("'darcs pull' failed");
  237. # If this updating yields any conflicts, we'll record them now to resolve
  238. # them. If nothing is recorded, there are no conflicts.
  239. $rcstoken = darcs_rev($file);
  240. # TODO: Use only the first line here, i.e. only the patch name?
  241. writefile("$file.log", $config{srcdir}, 'resolve conflicts: ' . $message);
  242. silentsystem($darcs, 'record', '--repodir', $config{srcdir}, '--all',
  243. '-m', 'resolve conflicts: ' . $message, '--author', $author, $file) and
  244. error("'darcs record' failed");
  245. my $conflicts = darcs_rev($file) ne $rcstoken;
  246. unlink("$config{srcdir}/$file.log") or
  247. error("failed to remove '$file.log'");
  248. # Push the changes to the main repository.
  249. silentsystem($darcs, 'push', '--quiet', '--repodir', $config{srcdir}, '--all')
  250. and error("'darcs push' failed");
  251. # TODO: darcs send?
  252. if ($conflicts) {
  253. my $document = readfile("$config{srcdir}/$file");
  254. # Try to leave everything in a consistent state.
  255. # TODO: 'yes | ...' needed? Doesn't seem so.
  256. silentsystem($darcs, "revert", "--repodir", $config{srcdir}, "--all") and
  257. warn("'darcs revert' failed");
  258. return $document;
  259. } else {
  260. return undef;
  261. }
  262. }
  263. sub rcs_commit_staged($$$) {
  264. my ($message, $user, $ipaddr) = @_;
  265. my $author;
  266. if (defined $user) {
  267. $author = "$user\@web";
  268. } elsif (defined $ipaddr) {
  269. $author = "$ipaddr\@web";
  270. } else {
  271. $author = "anon\@web";
  272. }
  273. if (!defined $message || !length($message)) {
  274. $message = "empty message";
  275. }
  276. silentsystem($darcs, "record", "--repodir", $config{srcdir}, "-a", "-A", $author,
  277. "-m", $message) and error("'darcs record' failed");
  278. # Push the changes to the main repository.
  279. silentsystem($darcs, 'push', '--quiet', '--repodir', $config{srcdir}, '--all')
  280. and error("'darcs push' failed");
  281. # TODO: darcs send?
  282. return undef;
  283. }
  284. sub rcs_add ($) {
  285. my $file = shift; # Relative to the repodir.
  286. # Intermediate directories will be added automagically.
  287. system($darcs, 'add', '--quiet', '--repodir', $config{srcdir},
  288. '--boring', $file) and error("'darcs add' failed");
  289. }
  290. sub rcs_remove ($) {
  291. my $file = shift; # Relative to the repodir.
  292. system('rm', $config{srcdir}.'/'.$file)
  293. and error("'rm' failed");
  294. }
  295. sub rcs_rename ($$) {
  296. my $a = shift; # Relative to the repodir.
  297. my $b = shift; # Relative to the repodir.
  298. system($darcs, 'mv', '--repodir', $config{srcdir}, $a, $b)
  299. and error("'darcs mv' failed");
  300. }
  301. sub rcs_recentchanges ($) {
  302. my $num=shift;
  303. my @ret;
  304. eval q{use Date::Parse};
  305. eval q{use XML::Simple};
  306. my $repodir=$config{srcdir};
  307. debug("darcs recent changes: $num");
  308. my $child = open(LOG, "-|");
  309. if (! $child) {
  310. $ENV{"DARCS_DONT_ESCAPE_ANYTHING"}=1;
  311. exec("darcs", "changes", "--xml",
  312. "--summary",
  313. "--repodir", "$repodir",
  314. "--last", "$num")
  315. || error("'darcs changes' failed to run");
  316. }
  317. my $data;
  318. $data .= $_ while(<LOG>);
  319. close LOG;
  320. my $log = XMLin($data, ForceArray => 1);
  321. debug("parsing recent changes...");
  322. foreach my $patch (@{$log->{patch}}) {
  323. my $date=$patch->{local_date};
  324. my $hash=$patch->{hash};
  325. my $when=str2time($date);
  326. my (@pages, @files, @pg);
  327. push @pages, $_ for (@{$patch->{summary}->[0]->{modify_file}});
  328. push @pages, $_ for (@{$patch->{summary}->[0]->{add_file}});
  329. push @pages, $_ for (@{$patch->{summary}->[0]->{remove_file}});
  330. for (@pages) {
  331. my $f = $_;
  332. $f = $_->{content} if (ref $_);
  333. $f =~ s,^\s+,,; $f =~ s,\s+$,,; # cut whitespace
  334. push @files, $f;
  335. }
  336. for (@{$patch->{summary}->[0]->{move}}) {
  337. my $p = $_;
  338. push @files, $p->{from};
  339. }
  340. for (@files) {
  341. my $f = $_;
  342. my $d = defined $config{'diffurl'} ? $config{'diffurl'} : "";
  343. $d =~ s/\[\[file\]\]/$f/go;
  344. $d =~ s/\[\[hash\]\]/$hash/go;
  345. debug("file: $f");
  346. debug("diffurl: $d");
  347. push @pg, {
  348. page => pagename($f),
  349. diffurl => $d,
  350. };
  351. }
  352. next unless (scalar @pg > 0);
  353. debug("recent change: " . $patch->{name}[0] . " ("
  354. . scalar @pg . " changes)");
  355. my @message;
  356. push @message, { line => $_ } for (@{$patch->{name}});
  357. my $committype;
  358. if ($patch->{author} =~ /\@web$/) {
  359. $committype = "web";
  360. } else {
  361. $committype = "darcs";
  362. }
  363. push @ret, {
  364. rev => $patch->{hash},
  365. user => $patch->{author},
  366. committype => $committype,
  367. when => $when,
  368. message => [@message],
  369. pages => [@pg],
  370. };
  371. }
  372. return @ret;
  373. }
  374. sub rcs_diff ($) {
  375. my $rev=shift;
  376. my @lines;
  377. foreach my $line (silentsystem("darcs", "diff", "--match", "hash ".$rev)) {
  378. if (@lines || $line=~/^diff/) {
  379. push @lines, $line."\n";
  380. }
  381. }
  382. if (wantarray) {
  383. return @lines;
  384. }
  385. else {
  386. return join("", @lines);
  387. }
  388. }
  389. sub rcs_getctime ($) {
  390. my $file=shift;
  391. eval q{use Date::Parse};
  392. eval q{use XML::Simple};
  393. local $/=undef;
  394. # Sigh... doing things the hard way again
  395. my $repodir=$config{srcdir};
  396. &loadcache;
  397. my $filer=substr($file, length($repodir));
  398. $filer =~ s:^[/]+::;
  399. if (defined $cache{$filer}) {
  400. #debug("taking cached ctime ".localtime($cache{$filer})." for $filer");
  401. return $cache{$filer};
  402. }
  403. my $child = open(LOG, "-|");
  404. if (! $child) {
  405. exec("darcs", "changes", "--xml", "--reverse",
  406. "--repodir", "$repodir", "$filer")
  407. || error("'darcs changes $filer' failed to run");
  408. }
  409. my $data;
  410. $data .= $_ while(<LOG>);
  411. close LOG;
  412. my $log = XMLin($data, ForceArray => 1);
  413. my $datestr=$log->{patch}[0]->{local_date};
  414. if (! defined $datestr) {
  415. warn "failed to get ctime for $filer";
  416. $cache{$filer} = 0;
  417. return 0;
  418. }
  419. my $date=str2time($datestr);
  420. #debug("found ctime ".localtime($date)." for $filer");
  421. $cache{$filer} = $date;
  422. return $date;
  423. }
  424. 1