summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/darcs.pm
blob: 27ea6b6dd69988f817c6618fc243e6df51413815 (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. # Internal functions
  54. sub silentsystem (@) {
  55. open(SAVED_STDOUT, ">&STDOUT");
  56. open(STDOUT, ">/dev/null");
  57. my $ret = system @_;
  58. open(STDOUT, ">&SAVED_STDOUT");
  59. return $ret;
  60. }
  61. sub darcs_info ($$$) {
  62. my $field = shift;
  63. my $repodir = shift;
  64. my $file = shift; # Relative to the repodir.
  65. my $child = open(DARCS_CHANGES, "-|");
  66. if (! $child) {
  67. exec('darcs', 'changes', '--repodir', $repodir, '--xml-output', $file) or
  68. error("failed to run 'darcs changes'");
  69. }
  70. # Brute force for now. :-/
  71. while (<DARCS_CHANGES>) {
  72. last if /^<\/created_as>$/;
  73. }
  74. ($_) = <DARCS_CHANGES> =~ /$field=\'([^\']+)/;
  75. $field eq 'hash' and s/\.gz//; # Strip away the '.gz' from 'hash'es.
  76. close(DARCS_CHANGES) or error("'darcs changes' exited " . $?);
  77. return $_;
  78. }
  79. sub darcs_rev($) {
  80. my $file = shift; # Relative to the repodir.
  81. my $repodir = $config{srcdir};
  82. my $child = open(DARCS_MANIFEST, "-|");
  83. if (! $child) {
  84. exec('darcs', 'query', 'manifest', '--repodir', $repodir) or
  85. error("failed to run 'darcs query manifest'");
  86. }
  87. my $found=0;
  88. while (<DARCS_MANIFEST>) {
  89. $found = 1, last if /^$file$/;
  90. }
  91. return "" if (! $found);
  92. close(DARCS_MANIFEST) or error("'darcs query manifest' exited " . $?);
  93. my $hash = darcs_info('hash', $repodir, $file);
  94. return defined $hash ? $hash : "";
  95. }
  96. # Exported functions.
  97. sub checkconfig() {
  98. if (defined $config{darcs_wrapper} && length $config{darcs_wrapper}) {
  99. push @{$config{wrappers}}, {
  100. wrapper => $config{darcs_wrapper},
  101. wrappermode => (defined $config{darcs_wrappermode} ? $config{darcs_wrappermode} : "06755"),
  102. };
  103. }
  104. }
  105. sub getsetup() {
  106. return
  107. plugin => {
  108. safe => 0, # rcs plugin
  109. rebuild => undef,
  110. },
  111. darcs_wrapper => {
  112. type => "string",
  113. example => "/darcs/repo/_darcs/ikiwiki-wrapper",
  114. description => "wrapper to generate (set as master repo apply hook)",
  115. safe => 0, # file
  116. rebuild => 0,
  117. },
  118. darcs_wrappermode => {
  119. type => "string",
  120. example => '06755',
  121. description => "mode for darcs_wrapper (can safely be made suid)",
  122. safe => 0,
  123. rebuild => 0,
  124. },
  125. historyurl => {
  126. type => "string",
  127. example => "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filehistory;f=[[file]]",
  128. description => "darcsweb url to show file history ([[file]] substituted)",
  129. safe => 1,
  130. rebuild => 1,
  131. },
  132. diffurl => {
  133. type => "string",
  134. example => "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filediff;h=[[hash]];f=[[file]]",
  135. description => "darcsweb url to show a diff ([[hash]] and [[file]] substituted)",
  136. safe => 1,
  137. rebuild => 1,
  138. },
  139. }
  140. sub rcs_update () {
  141. silentsystem('darcs', "pull", "--repodir", $config{srcdir}, "-qa")
  142. }
  143. sub rcs_prepedit ($) {
  144. # Prepares to edit a file under revision control. Returns a token that
  145. # must be passed to rcs_commit() when the file is to be commited. For us,
  146. # this token the hash value of the latest patch that modifies the file,
  147. # i.e. something like its current revision. If the file is not yet added
  148. # to the repository, we return TODO: the empty string.
  149. my $file = shift; # Relative to the repodir.
  150. my $rev = darcs_rev($file);
  151. return $rev;
  152. }
  153. sub rcs_commit ($$$;$$) {
  154. # Commit the page. Returns 'undef' on success and a version of the page
  155. # with conflict markers on failure.
  156. my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
  157. # Compute if the "revision" of $file changed.
  158. my $changed = darcs_rev($file) ne $rcstoken;
  159. # Yes, the following is a bit convoluted.
  160. if ($changed) {
  161. # TODO. Invent a better, non-conflicting name.
  162. rename("$config{srcdir}/$file", "$config{srcdir}/$file.save") or
  163. error("failed to rename $file to $file.save: $!");
  164. # Roll the repository back to $rcstoken.
  165. # TODO. Can we be sure that no changes are lost? I think that
  166. # we can, if we make sure that the 'darcs push' below will always
  167. # succeed.
  168. # We need to revert everything as 'darcs obliterate' might choke
  169. # otherwise.
  170. # TODO: 'yes | ...' needed? Doesn't seem so.
  171. silentsystem('darcs', "revert", "--repodir", $config{srcdir}, "--all") and
  172. error("'darcs revert' failed");
  173. # Remove all patches starting at $rcstoken.
  174. my $child = open(DARCS_OBLITERATE, "|-");
  175. if (! $child) {
  176. open(STDOUT, ">/dev/null");
  177. exec('darcs', "obliterate", "--repodir", $config{srcdir},
  178. "--match", "hash " . $rcstoken) and
  179. error("'darcs obliterate' failed");
  180. }
  181. while (print DARCS_OBLITERATE "y") {
  182. ;
  183. }
  184. close(DARCS_OBLITERATE);
  185. # Restore the $rcstoken one.
  186. silentsystem('darcs', "pull", "--quiet", "--repodir", $config{srcdir},
  187. "--match", "hash " . $rcstoken, "--all") and
  188. error("'darcs pull' failed");
  189. # We're back at $rcstoken. Re-install the modified file.
  190. rename("$config{srcdir}/$file.save", "$config{srcdir}/$file") or
  191. error("failed to rename $file.save to $file: $!");
  192. }
  193. # Record the changes.
  194. my $author;
  195. if (defined $user) {
  196. $author = "$user\@web";
  197. } elsif (defined $ipaddr) {
  198. $author = "$ipaddr\@web";
  199. } else {
  200. $author = "anon\@web";
  201. }
  202. if (!defined $message || !length($message)) {
  203. $message = "empty message";
  204. }
  205. silentsystem('darcs', 'record', '--repodir', $config{srcdir}, '--all',
  206. '-m', $message, '--author', $author, $file) and
  207. error("'darcs record' failed");
  208. # Update the repository by pulling from the default repository, which is
  209. # master repository.
  210. silentsystem('darcs', "pull", "--quiet", "--repodir", $config{srcdir},
  211. "--all") and error("'darcs pull' failed");
  212. # If this updating yields any conflicts, we'll record them now to resolve
  213. # them. If nothing is recorded, there are no conflicts.
  214. $rcstoken = darcs_rev($file);
  215. # TODO: Use only the first line here, i.e. only the patch name?
  216. writefile("$file.log", $config{srcdir}, 'resolve conflicts: ' . $message);
  217. silentsystem('darcs', 'record', '--repodir', $config{srcdir}, '--all',
  218. '-m', 'resolve conflicts: ' . $message, '--author', $author, $file) and
  219. error("'darcs record' failed");
  220. my $conflicts = darcs_rev($file) ne $rcstoken;
  221. unlink("$config{srcdir}/$file.log") or
  222. error("failed to remove '$file.log'");
  223. # Push the changes to the main repository.
  224. silentsystem('darcs', 'push', '--quiet', '--repodir', $config{srcdir}, '--all')
  225. and error("'darcs push' failed");
  226. # TODO: darcs send?
  227. if ($conflicts) {
  228. my $document = readfile("$config{srcdir}/$file");
  229. # Try to leave everything in a consistent state.
  230. # TODO: 'yes | ...' needed? Doesn't seem so.
  231. silentsystem('darcs', "revert", "--repodir", $config{srcdir}, "--all") and
  232. warn("'darcs revert' failed");
  233. return $document;
  234. } else {
  235. return undef;
  236. }
  237. }
  238. sub rcs_commit_staged($$$) {
  239. my ($message, $user, $ipaddr) = @_;
  240. my $author;
  241. if (defined $user) {
  242. $author = "$user\@web";
  243. } elsif (defined $ipaddr) {
  244. $author = "$ipaddr\@web";
  245. } else {
  246. $author = "anon\@web";
  247. }
  248. if (!defined $message || !length($message)) {
  249. $message = "empty message";
  250. }
  251. silentsystem('darcs', "record", "--repodir", $config{srcdir}, "-a", "-A", $author,
  252. "-m", $message) and error("'darcs record' failed");
  253. # Push the changes to the main repository.
  254. silentsystem('darcs', 'push', '--quiet', '--repodir', $config{srcdir}, '--all')
  255. and error("'darcs push' failed");
  256. # TODO: darcs send?
  257. return undef;
  258. }
  259. sub rcs_add ($) {
  260. my $file = shift; # Relative to the repodir.
  261. # Intermediate directories will be added automagically.
  262. system('darcs', 'add', '--quiet', '--repodir', $config{srcdir},
  263. '--boring', $file) and error("'darcs add' failed");
  264. }
  265. sub rcs_remove ($) {
  266. my $file = shift; # Relative to the repodir.
  267. unlink($config{srcdir}.'/'.$file);
  268. }
  269. sub rcs_rename ($$) {
  270. my $a = shift; # Relative to the repodir.
  271. my $b = shift; # Relative to the repodir.
  272. system('darcs', 'mv', '--repodir', $config{srcdir}, $a, $b)
  273. and error("'darcs mv' failed");
  274. }
  275. sub rcs_recentchanges ($) {
  276. my $num=shift;
  277. my @ret;
  278. eval q{use Date::Parse};
  279. eval q{use XML::Simple};
  280. my $repodir=$config{srcdir};
  281. debug("darcs recent changes: $num");
  282. my $child = open(LOG, "-|");
  283. if (! $child) {
  284. $ENV{"DARCS_DONT_ESCAPE_ANYTHING"}=1;
  285. exec("darcs", "changes", "--xml",
  286. "--summary",
  287. "--repodir", "$repodir",
  288. "--last", "$num")
  289. || error("'darcs changes' failed to run");
  290. }
  291. my $data;
  292. $data .= $_ while(<LOG>);
  293. close LOG;
  294. my $log = XMLin($data, ForceArray => 1);
  295. debug("parsing recent changes...");
  296. foreach my $patch (@{$log->{patch}}) {
  297. my $date=$patch->{local_date};
  298. my $hash=$patch->{hash};
  299. my $when=str2time($date);
  300. my (@pages, @files, @pg);
  301. push @pages, $_ for (@{$patch->{summary}->[0]->{modify_file}});
  302. push @pages, $_ for (@{$patch->{summary}->[0]->{add_file}});
  303. push @pages, $_ for (@{$patch->{summary}->[0]->{remove_file}});
  304. for (@pages) {
  305. my $f = $_;
  306. $f = $_->{content} if (ref $_);
  307. $f =~ s,^\s+,,; $f =~ s,\s+$,,; # cut whitespace
  308. push @files, $f;
  309. }
  310. for (@{$patch->{summary}->[0]->{move}}) {
  311. my $p = $_;
  312. push @files, $p->{from};
  313. }
  314. for (@files) {
  315. my $f = $_;
  316. my $d = defined $config{'diffurl'} ? $config{'diffurl'} : "";
  317. $d =~ s/\[\[file\]\]/$f/go;
  318. $d =~ s/\[\[hash\]\]/$hash/go;
  319. debug("file: $f");
  320. debug("diffurl: $d");
  321. push @pg, {
  322. page => pagename($f),
  323. diffurl => $d,
  324. };
  325. }
  326. next unless (scalar @pg > 0);
  327. debug("recent change: " . $patch->{name}[0] . " ("
  328. . scalar @pg . " changes)");
  329. my @message;
  330. push @message, { line => $_ } for (@{$patch->{name}});
  331. my $committype;
  332. if ($patch->{author} =~ /\@web$/) {
  333. $committype = "web";
  334. } else {
  335. $committype = "darcs";
  336. }
  337. push @ret, {
  338. rev => $patch->{hash},
  339. user => $patch->{author},
  340. committype => $committype,
  341. when => $when,
  342. message => [@message],
  343. pages => [@pg],
  344. };
  345. }
  346. return @ret;
  347. }
  348. sub rcs_diff ($) {
  349. my $rev=shift;
  350. my @lines;
  351. foreach my $line (silentsystem("darcs", "diff", "--match", "hash ".$rev)) {
  352. if (@lines || $line=~/^diff/) {
  353. push @lines, $line."\n";
  354. }
  355. }
  356. if (wantarray) {
  357. return @lines;
  358. }
  359. else {
  360. return join("", @lines);
  361. }
  362. }
  363. sub rcs_getctime ($) {
  364. my $file=shift;
  365. eval q{use Date::Parse};
  366. eval q{use XML::Simple};
  367. local $/=undef;
  368. # Sigh... doing things the hard way again
  369. my $repodir=$config{srcdir};
  370. my $filer=substr($file, length($repodir));
  371. $filer =~ s:^[/]+::;
  372. my $child = open(LOG, "-|");
  373. if (! $child) {
  374. exec("darcs", "changes", "--xml", "--reverse",
  375. "--repodir", "$repodir", "$filer")
  376. || error("'darcs changes $filer' failed to run");
  377. }
  378. my $data;
  379. $data .= $_ while(<LOG>);
  380. close LOG;
  381. my $log = XMLin($data, ForceArray => 1);
  382. my $datestr=$log->{patch}[0]->{local_date};
  383. if (! defined $datestr) {
  384. warn "failed to get ctime for $filer";
  385. return 0;
  386. }
  387. my $date=str2time($datestr);
  388. #debug("found ctime ".localtime($date)." for $filer");
  389. return $date;
  390. }
  391. 1
  392. # vim: ts=4 sw=4 noet