summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/monotone.pm
blob: a591ecec56e3f099e6bf2cff5bc4a565f753a71f (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::monotone;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. use Monotone;
  7. use Date::Parse qw(str2time);
  8. use Date::Format qw(time2str);
  9. my $sha1_pattern = qr/[0-9a-fA-F]{40}/; # pattern to validate sha1sums
  10. sub import { #{{{
  11. if (exists $IkiWiki::hooks{rcs}) {
  12. error(gettext("cannot use multiple rcs plugins"));
  13. }
  14. hook(type => "checkconfig", id => "monotone", call => \&checkconfig);
  15. hook(type => "getsetup", id => "monotone", call => \&getsetup);
  16. hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
  17. hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
  18. hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
  19. hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
  20. hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
  21. hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
  22. hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
  23. hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
  24. hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
  25. hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
  26. } #}}}
  27. sub checkconfig () { #{{{
  28. if (!defined($config{mtnrootdir})) {
  29. $config{mtnrootdir} = $config{srcdir};
  30. }
  31. if (! -d "$config{mtnrootdir}/_MTN") {
  32. error("Ikiwiki srcdir does not seem to be a Monotone workspace (or set the mtnrootdir)!");
  33. }
  34. my $child = open(MTN, "-|");
  35. if (! $child) {
  36. open STDERR, ">/dev/null";
  37. exec("mtn", "version") || error("mtn version failed to run");
  38. }
  39. my $version=undef;
  40. while (<MTN>) {
  41. if (/^monotone (\d+\.\d+) /) {
  42. $version=$1;
  43. }
  44. }
  45. close MTN || debug("mtn version exited $?");
  46. if (!defined($version)) {
  47. error("Cannot determine monotone version");
  48. }
  49. if ($version < 0.38) {
  50. error("Monotone version too old, is $version but required 0.38");
  51. }
  52. if (length $config{mtn_wrapper}) {
  53. push @{$config{wrappers}}, {
  54. wrapper => $config{mtn_wrapper},
  55. wrappermode => (defined $config{mtn_wrappermode} ? $config{mtn_wrappermode} : "06755"),
  56. };
  57. }
  58. } #}}}
  59. sub getsetup () { #{{{
  60. return
  61. mtn_wrapper => {
  62. type => "string",
  63. example => "/srv/mtn/wiki/_MTN/ikiwiki-netsync-hook",
  64. description => "monotone netsync hook executable to generate",
  65. safe => 0, # file
  66. rebuild => 0,
  67. },
  68. mtn_wrappermode => {
  69. type => "string",
  70. example => '06755',
  71. description => "mode for mtn_wrapper (can safely be made suid)",
  72. safe => 0,
  73. rebuild => 0,
  74. },
  75. mtnkey => {
  76. type => "string",
  77. example => 'web@example.com',
  78. description => "your monotone key",
  79. safe => 1,
  80. rebuild => 0,
  81. },
  82. historyurl => {
  83. type => "string",
  84. example => "http://viewmtn.example.com/branch/head/filechanges/com.example.branch/[[file]]",
  85. description => "viewmtn url to show file history ([[file]] substituted)",
  86. safe => 1,
  87. rebuild => 1,
  88. },
  89. diffurl => {
  90. type => "string",
  91. example => "http://viewmtn.example.com/revision/diff/[[r1]]/with/[[r2]]/[[file]]",
  92. description => "viewmtn url to show a diff ([[r1]], [[r2]], and [[file]] substituted)",
  93. safe => 1,
  94. rebuild => 1,
  95. },
  96. mtnsync => {
  97. type => "boolean",
  98. example => 0,
  99. description => "sync on update and commit?",
  100. safe => 0, # paranoia
  101. rebuild => 0,
  102. },
  103. mtnrootdir => {
  104. type => "string",
  105. description => "path to your workspace (defaults to the srcdir; specify if the srcdir is a subdirectory of the workspace)",
  106. safe => 0, # path
  107. rebuild => 0,
  108. },
  109. } #}}}
  110. sub get_rev () { #{{{
  111. my $sha1 = `mtn --root=$config{mtnrootdir} automate get_base_revision_id`;
  112. ($sha1) = $sha1 =~ m/($sha1_pattern)/; # sha1 is untainted now
  113. if (! $sha1) {
  114. debug("Unable to get base revision for '$config{srcdir}'.")
  115. }
  116. return $sha1;
  117. } #}}}
  118. sub get_rev_auto ($) { #{{{
  119. my $automator=shift;
  120. my @results = $automator->call("get_base_revision_id");
  121. my $sha1 = $results[0];
  122. ($sha1) = $sha1 =~ m/($sha1_pattern)/; # sha1 is untainted now
  123. if (! $sha1) {
  124. debug("Unable to get base revision for '$config{srcdir}'.")
  125. }
  126. return $sha1;
  127. } #}}}
  128. sub mtn_merge ($$$$) { #{{{
  129. my $leftRev=shift;
  130. my $rightRev=shift;
  131. my $branch=shift;
  132. my $author=shift;
  133. my $mergeRev;
  134. my $child = open(MTNMERGE, "-|");
  135. if (! $child) {
  136. open STDERR, ">&STDOUT";
  137. exec("mtn", "--root=$config{mtnrootdir}",
  138. "explicit_merge", $leftRev, $rightRev,
  139. $branch, "--author", $author, "--key",
  140. $config{mtnkey}) || error("mtn merge failed to run");
  141. }
  142. while (<MTNMERGE>) {
  143. if (/^mtn.\s.merged.\s($sha1_pattern)$/) {
  144. $mergeRev=$1;
  145. }
  146. }
  147. close MTNMERGE || return undef;
  148. debug("merged $leftRev, $rightRev to make $mergeRev");
  149. return $mergeRev;
  150. } #}}}
  151. sub commit_file_to_new_rev ($$$$$$$$) { #{{{
  152. my $automator=shift;
  153. my $wsfilename=shift;
  154. my $oldFileID=shift;
  155. my $newFileContents=shift;
  156. my $oldrev=shift;
  157. my $branch=shift;
  158. my $author=shift;
  159. my $message=shift;
  160. #store the file
  161. my ($out, $err) = $automator->call("put_file", $oldFileID, $newFileContents);
  162. my ($newFileID) = ($out =~ m/^($sha1_pattern)$/);
  163. error("Failed to store file data for $wsfilename in repository")
  164. if (! defined $newFileID || length $newFileID != 40);
  165. # get the mtn filename rather than the workspace filename
  166. ($out, $err) = $automator->call("get_corresponding_path", $oldrev, $wsfilename, $oldrev);
  167. my ($filename) = ($out =~ m/^file "(.*)"$/);
  168. error("Couldn't find monotone repository path for file $wsfilename") if (! $filename);
  169. debug("Converted ws filename of $wsfilename to repos filename of $filename");
  170. # then stick in a new revision for this file
  171. my $manifest = "format_version \"1\"\n\n".
  172. "new_manifest [0000000000000000000000000000000000000000]\n\n".
  173. "old_revision [$oldrev]\n\n".
  174. "patch \"$filename\"\n".
  175. " from [$oldFileID]\n".
  176. " to [$newFileID]\n";
  177. ($out, $err) = $automator->call("put_revision", $manifest);
  178. my ($newRevID) = ($out =~ m/^($sha1_pattern)$/);
  179. error("Unable to make new monotone repository revision")
  180. if (! defined $newRevID || length $newRevID != 40);
  181. debug("put revision: $newRevID");
  182. # now we need to add certs for this revision...
  183. # author, branch, changelog, date
  184. $automator->call("cert", $newRevID, "author", $author);
  185. $automator->call("cert", $newRevID, "branch", $branch);
  186. $automator->call("cert", $newRevID, "changelog", $message);
  187. $automator->call("cert", $newRevID, "date",
  188. time2str("%Y-%m-%dT%T", time, "UTC"));
  189. debug("Added certs for rev: $newRevID");
  190. return $newRevID;
  191. } #}}}
  192. sub read_certs ($$) { #{{{
  193. my $automator=shift;
  194. my $rev=shift;
  195. my @results = $automator->call("certs", $rev);
  196. my @ret;
  197. my $line = $results[0];
  198. while ($line =~ m/\s+key\s"(.*?)"\nsignature\s"(ok|bad|unknown)"\n\s+name\s"(.*?)"\n\s+value\s"(.*?)"\n\s+trust\s"(trusted|untrusted)"\n/sg) {
  199. push @ret, {
  200. key => $1,
  201. signature => $2,
  202. name => $3,
  203. value => $4,
  204. trust => $5,
  205. };
  206. }
  207. return @ret;
  208. } #}}}
  209. sub get_changed_files ($$) { #{{{
  210. my $automator=shift;
  211. my $rev=shift;
  212. my @results = $automator->call("get_revision", $rev);
  213. my $changes=$results[0];
  214. my @ret;
  215. my %seen = ();
  216. while ($changes =~ m/\s*(add_file|patch|delete|rename)\s"(.*?)(?<!\\)"\n/sg) {
  217. my $file = $2;
  218. # don't add the same file multiple times
  219. if (! $seen{$file}) {
  220. push @ret, $file;
  221. $seen{$file} = 1;
  222. }
  223. }
  224. return @ret;
  225. } #}}}
  226. sub rcs_update () { #{{{
  227. chdir $config{srcdir}
  228. or error("Cannot chdir to $config{srcdir}: $!");
  229. if (defined($config{mtnsync}) && $config{mtnsync}) {
  230. if (system("mtn", "--root=$config{mtnrootdir}", "sync",
  231. "--quiet", "--ticker=none",
  232. "--key", $config{mtnkey}) != 0) {
  233. debug("monotone sync failed before update");
  234. }
  235. }
  236. if (system("mtn", "--root=$config{mtnrootdir}", "update", "--quiet") != 0) {
  237. debug("monotone update failed");
  238. }
  239. } #}}}
  240. sub rcs_prepedit ($) { #{{{
  241. my $file=shift;
  242. chdir $config{srcdir}
  243. or error("Cannot chdir to $config{srcdir}: $!");
  244. # For monotone, return the revision of the file when
  245. # editing begins.
  246. return get_rev();
  247. } #}}}
  248. sub rcs_commit ($$$;$$) { #{{{
  249. # Tries to commit the page; returns undef on _success_ and
  250. # a version of the page with the rcs's conflict markers on failure.
  251. # The file is relative to the srcdir.
  252. my $file=shift;
  253. my $message=shift;
  254. my $rcstoken=shift;
  255. my $user=shift;
  256. my $ipaddr=shift;
  257. my $author;
  258. if (defined $user) {
  259. $author="Web user: " . $user;
  260. }
  261. elsif (defined $ipaddr) {
  262. $author="Web IP: " . $ipaddr;
  263. }
  264. else {
  265. $author="Web: Anonymous";
  266. }
  267. chdir $config{srcdir}
  268. or error("Cannot chdir to $config{srcdir}: $!");
  269. my ($oldrev)= $rcstoken=~ m/^($sha1_pattern)$/; # untaint
  270. my $rev = get_rev();
  271. if (defined $rev && defined $oldrev && $rev ne $oldrev) {
  272. my $automator = Monotone->new();
  273. $automator->open_args("--root", $config{mtnrootdir}, "--key", $config{mtnkey});
  274. # Something has been committed, has this file changed?
  275. my ($out, $err);
  276. $automator->setOpts("r", $oldrev, "r", $rev);
  277. ($out, $err) = $automator->call("content_diff", $file);
  278. debug("Problem committing $file") if ($err ne "");
  279. my $diff = $out;
  280. if ($diff) {
  281. # Commit a revision with just this file changed off
  282. # the old revision.
  283. #
  284. # first get the contents
  285. debug("File changed: forming branch");
  286. my $newfile=readfile("$config{srcdir}/$file");
  287. # then get the old content ID from the diff
  288. if ($diff !~ m/^---\s$file\s+($sha1_pattern)$/m) {
  289. error("Unable to find previous file ID for $file");
  290. }
  291. my $oldFileID = $1;
  292. # get the branch we're working in
  293. ($out, $err) = $automator->call("get_option", "branch");
  294. chomp $out;
  295. error("Illegal branch name in monotone workspace") if ($out !~ m/^([-\@\w\.]+)$/);
  296. my $branch = $1;
  297. # then put the new content into the DB (and record the new content ID)
  298. my $newRevID = commit_file_to_new_rev($automator, $file, $oldFileID, $newfile, $oldrev, $branch, $author, $message);
  299. $automator->close();
  300. # if we made it to here then the file has been committed... revert the local copy
  301. if (system("mtn", "--root=$config{mtnrootdir}", "revert", $file) != 0) {
  302. debug("Unable to revert $file after merge on conflicted commit!");
  303. }
  304. debug("Divergence created! Attempting auto-merge.");
  305. # see if it will merge cleanly
  306. $ENV{MTN_MERGE}="fail";
  307. my $mergeResult = mtn_merge($newRevID, $rev, $branch, $author);
  308. $ENV{MTN_MERGE}="";
  309. # push any changes so far
  310. if (defined($config{mtnsync}) && $config{mtnsync}) {
  311. if (system("mtn", "--root=$config{mtnrootdir}", "push", "--quiet", "--ticker=none", "--key", $config{mtnkey}) != 0) {
  312. debug("monotone push failed");
  313. }
  314. }
  315. if (defined($mergeResult)) {
  316. # everything is merged - bring outselves up to date
  317. if (system("mtn", "--root=$config{mtnrootdir}",
  318. "update", "-r", $mergeResult) != 0) {
  319. debug("Unable to update to rev $mergeResult after merge on conflicted commit!");
  320. }
  321. }
  322. else {
  323. debug("Auto-merge failed. Using diff-merge to add conflict markers.");
  324. $ENV{MTN_MERGE}="diffutils";
  325. $ENV{MTN_MERGE_DIFFUTILS}="partial=true";
  326. $mergeResult = mtn_merge($newRevID, $rev, $branch, $author);
  327. $ENV{MTN_MERGE}="";
  328. $ENV{MTN_MERGE_DIFFUTILS}="";
  329. if (!defined($mergeResult)) {
  330. debug("Unable to insert conflict markers!");
  331. error("Your commit succeeded. Unfortunately, someone else committed something to the same ".
  332. "part of the wiki at the same time. Both versions are stored in the monotone repository, ".
  333. "but at present the different versions cannot be reconciled through the web interface. ".
  334. "Please use the non-web interface to resolve the conflicts.");
  335. }
  336. if (system("mtn", "--root=$config{mtnrootdir}",
  337. "update", "-r", $mergeResult) != 0) {
  338. debug("Unable to update to rev $mergeResult after conflict-enhanced merge on conflicted commit!");
  339. }
  340. # return "conflict enhanced" file to the user
  341. # for cleanup note, this relies on the fact
  342. # that ikiwiki seems to call rcs_prepedit()
  343. # again after we return
  344. return readfile("$config{srcdir}/$file");
  345. }
  346. return undef;
  347. }
  348. $automator->close();
  349. }
  350. # If we reached here then the file we're looking at hasn't changed
  351. # since $oldrev. Commit it.
  352. if (system("mtn", "--root=$config{mtnrootdir}", "commit", "--quiet",
  353. "--author", $author, "--key", $config{mtnkey}, "-m",
  354. IkiWiki::possibly_foolish_untaint($message), $file) != 0) {
  355. debug("Traditional commit failed! Returning data as conflict.");
  356. my $conflict=readfile("$config{srcdir}/$file");
  357. if (system("mtn", "--root=$config{mtnrootdir}", "revert",
  358. "--quiet", $file) != 0) {
  359. debug("monotone revert failed");
  360. }
  361. return $conflict;
  362. }
  363. if (defined($config{mtnsync}) && $config{mtnsync}) {
  364. if (system("mtn", "--root=$config{mtnrootdir}", "push",
  365. "--quiet", "--ticker=none", "--key",
  366. $config{mtnkey}) != 0) {
  367. debug("monotone push failed");
  368. }
  369. }
  370. return undef # success
  371. } #}}}
  372. sub rcs_commit_staged ($$$) {
  373. # Commits all staged changes. Changes can be staged using rcs_add,
  374. # rcs_remove, and rcs_rename.
  375. my ($message, $user, $ipaddr)=@_;
  376. # Note - this will also commit any spurious changes that happen to be
  377. # lying around in the working copy. There shouldn't be any, but...
  378. chdir $config{srcdir}
  379. or error("Cannot chdir to $config{srcdir}: $!");
  380. my $author;
  381. if (defined $user) {
  382. $author="Web user: " . $user;
  383. }
  384. elsif (defined $ipaddr) {
  385. $author="Web IP: " . $ipaddr;
  386. }
  387. else {
  388. $author="Web: Anonymous";
  389. }
  390. if (system("mtn", "--root=$config{mtnrootdir}", "commit", "--quiet",
  391. "--author", $author, "--key", $config{mtnkey}, "-m",
  392. IkiWiki::possibly_foolish_untaint($message)) != 0) {
  393. error("Monotone commit failed");
  394. }
  395. }
  396. sub rcs_add ($) { #{{{
  397. my $file=shift;
  398. chdir $config{srcdir}
  399. or error("Cannot chdir to $config{srcdir}: $!");
  400. if (system("mtn", "--root=$config{mtnrootdir}", "add", "--quiet",
  401. $file) != 0) {
  402. error("Monotone add failed");
  403. }
  404. } #}}}
  405. sub rcs_remove ($) { # {{{
  406. my $file = shift;
  407. chdir $config{srcdir}
  408. or error("Cannot chdir to $config{srcdir}: $!");
  409. # Note: it is difficult to undo a remove in Monotone at the moment.
  410. # Until this is fixed, it might be better to make 'rm' move things
  411. # into an attic, rather than actually remove them.
  412. # To resurrect a file, you currently add a new file with the contents
  413. # you want it to have. This loses all connectivity and automated
  414. # merging with the 'pre-delete' versions of the file.
  415. if (system("mtn", "--root=$config{mtnrootdir}", "rm", "--quiet",
  416. $file) != 0) {
  417. error("Monotone remove failed");
  418. }
  419. } #}}}
  420. sub rcs_rename ($$) { # {{{
  421. my ($src, $dest) = @_;
  422. chdir $config{srcdir}
  423. or error("Cannot chdir to $config{srcdir}: $!");
  424. if (system("mtn", "--root=$config{mtnrootdir}", "rename", "--quiet",
  425. $src, $dest) != 0) {
  426. error("Monotone rename failed");
  427. }
  428. } #}}}
  429. sub rcs_recentchanges ($) { #{{{
  430. my $num=shift;
  431. my @ret;
  432. chdir $config{srcdir}
  433. or error("Cannot chdir to $config{srcdir}: $!");
  434. # use log --brief to get a list of revs, as this
  435. # gives the results in a nice order
  436. # (otherwise we'd have to do our own date sorting)
  437. my @revs;
  438. my $child = open(MTNLOG, "-|");
  439. if (! $child) {
  440. exec("mtn", "log", "--root=$config{mtnrootdir}", "--no-graph",
  441. "--brief") || error("mtn log failed to run");
  442. }
  443. while (($num >= 0) and (my $line = <MTNLOG>)) {
  444. if ($line =~ m/^($sha1_pattern)/) {
  445. push @revs, $1;
  446. $num -= 1;
  447. }
  448. }
  449. close MTNLOG || debug("mtn log exited $?");
  450. my $automator = Monotone->new();
  451. $automator->open(undef, $config{mtnrootdir});
  452. while (@revs != 0) {
  453. my $rev = shift @revs;
  454. # first go through and figure out the messages, etc
  455. my $certs = [read_certs($automator, $rev)];
  456. my $user;
  457. my $when;
  458. my $committype;
  459. my (@pages, @message);
  460. foreach my $cert (@$certs) {
  461. if ($cert->{signature} eq "ok" &&
  462. $cert->{trust} eq "trusted") {
  463. if ($cert->{name} eq "author") {
  464. $user = $cert->{value};
  465. # detect the source of the commit
  466. # from the changelog
  467. if ($cert->{key} eq $config{mtnkey}) {
  468. $committype = "web";
  469. } else {
  470. $committype = "monotone";
  471. }
  472. } elsif ($cert->{name} eq "date") {
  473. $when = str2time($cert->{value}, 'UTC');
  474. } elsif ($cert->{name} eq "changelog") {
  475. my $messageText = $cert->{value};
  476. # split the changelog into multiple
  477. # lines
  478. foreach my $msgline (split(/\n/, $messageText)) {
  479. push @message, { line => $msgline };
  480. }
  481. }
  482. }
  483. }
  484. my @changed_files = get_changed_files($automator, $rev);
  485. my $file;
  486. my ($out, $err) = $automator->call("parents", $rev);
  487. my @parents = ($out =~ m/^($sha1_pattern)$/);
  488. my $parent = $parents[0];
  489. foreach $file (@changed_files) {
  490. next unless length $file;
  491. if (defined $config{diffurl} and (@parents == 1)) {
  492. my $diffurl=$config{diffurl};
  493. $diffurl=~s/\[\[r1\]\]/$parent/g;
  494. $diffurl=~s/\[\[r2\]\]/$rev/g;
  495. $diffurl=~s/\[\[file\]\]/$file/g;
  496. push @pages, {
  497. page => pagename($file),
  498. diffurl => $diffurl,
  499. };
  500. }
  501. else {
  502. push @pages, {
  503. page => pagename($file),
  504. }
  505. }
  506. }
  507. push @ret, {
  508. rev => $rev,
  509. user => $user,
  510. committype => $committype,
  511. when => $when,
  512. message => [@message],
  513. pages => [@pages],
  514. } if @pages;
  515. }
  516. $automator->close();
  517. return @ret;
  518. } #}}}
  519. sub rcs_diff ($) { #{{{
  520. my $rev=shift;
  521. my ($sha1) = $rev =~ /^($sha1_pattern)$/; # untaint
  522. chdir $config{srcdir}
  523. or error("Cannot chdir to $config{srcdir}: $!");
  524. my $child = open(MTNDIFF, "-|");
  525. if (! $child) {
  526. exec("mtn", "diff", "--root=$config{mtnrootdir}", "-r", "p:".$sha1, "-r", $sha1) || error("mtn diff $sha1 failed to run");
  527. }
  528. my (@lines) = <MTNDIFF>;
  529. close MTNDIFF || debug("mtn diff $sha1 exited $?");
  530. if (wantarray) {
  531. return @lines;
  532. }
  533. else {
  534. return join("", @lines);
  535. }
  536. } #}}}
  537. sub rcs_getctime ($) { #{{{
  538. my $file=shift;
  539. chdir $config{srcdir}
  540. or error("Cannot chdir to $config{srcdir}: $!");
  541. my $child = open(MTNLOG, "-|");
  542. if (! $child) {
  543. exec("mtn", "log", "--root=$config{mtnrootdir}", "--no-graph",
  544. "--brief", $file) || error("mtn log $file failed to run");
  545. }
  546. my $firstRev;
  547. while (<MTNLOG>) {
  548. if (/^($sha1_pattern)/) {
  549. $firstRev=$1;
  550. }
  551. }
  552. close MTNLOG || debug("mtn log $file exited $?");
  553. if (! defined $firstRev) {
  554. debug "failed to parse mtn log for $file";
  555. return 0;
  556. }
  557. my $automator = Monotone->new();
  558. $automator->open(undef, $config{mtnrootdir});
  559. my $certs = [read_certs($automator, $firstRev)];
  560. $automator->close();
  561. my $date;
  562. foreach my $cert (@$certs) {
  563. if ($cert->{signature} eq "ok" && $cert->{trust} eq "trusted") {
  564. if ($cert->{name} eq "date") {
  565. $date = $cert->{value};
  566. }
  567. }
  568. }
  569. if (! defined $date) {
  570. debug "failed to find date cert for revision $firstRev when looking for creation time of $file";
  571. return 0;
  572. }
  573. $date=str2time($date, 'UTC');
  574. debug("found ctime ".localtime($date)." for $file");
  575. return $date;
  576. } #}}}
  577. 1