summaryrefslogtreecommitdiff
path: root/IkiWiki/Rcs/monotone.pm
blob: 992065931daf293acad0dda7814d05006d1f644a (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use IkiWiki;
  5. use Monotone;
  6. use Date::Parse qw(str2time);
  7. use Date::Format qw(time2str);
  8. package IkiWiki;
  9. my $sha1_pattern = qr/[0-9a-fA-F]{40}/; # pattern to validate sha1sums
  10. sub check_config() { #{{{
  11. if (!defined($config{mtnrootdir})) {
  12. $config{mtnrootdir} = $config{srcdir};
  13. }
  14. if (! -d "$config{mtnrootdir}/_MTN") {
  15. error("Ikiwiki srcdir does not seem to be a Monotone workspace (or set the mtnrootdir)!");
  16. }
  17. if (!defined($config{mtnmergerc})) {
  18. $config{mtnmergerc} = "$config{mtnrootdir}/_MTN/mergerc";
  19. }
  20. chdir $config{srcdir}
  21. or error("Cannot chdir to $config{srcdir}: $!");
  22. } #}}}
  23. sub get_rev () { #{{{
  24. my $sha1 = `mtn --root=$config{mtnrootdir} automate get_base_revision_id`;
  25. ($sha1) = $sha1 =~ m/($sha1_pattern)/; # sha1 is untainted now
  26. if (! $sha1) {
  27. debug("Unable to get base revision for '$config{srcdir}'.")
  28. }
  29. return $sha1;
  30. } #}}}
  31. sub get_rev_auto ($) { #{{{
  32. my $automator=shift;
  33. my @results = $automator->call("get_base_revision_id");
  34. my $sha1 = $results[0];
  35. ($sha1) = $sha1 =~ m/($sha1_pattern)/; # sha1 is untainted now
  36. if (! $sha1) {
  37. debug("Unable to get base revision for '$config{srcdir}'.")
  38. }
  39. return $sha1;
  40. } #}}}
  41. sub mtn_merge ($$$$) { #{{{
  42. my $leftRev=shift;
  43. my $rightRev=shift;
  44. my $branch=shift;
  45. my $author=shift;
  46. my $mergeRev;
  47. my $mergerc = $config{mtnmergerc};
  48. my $child = open(MTNMERGE, "-|");
  49. if (! $child) {
  50. open STDERR, ">&STDOUT";
  51. exec("mtn", "--root=$config{mtnrootdir}", "--rcfile",
  52. $mergerc, "explicit_merge", $leftRev, $rightRev,
  53. $branch, "--author", $author, "--key",
  54. $config{mtnkey}) || error("mtn merge failed to run");
  55. }
  56. while (<MTNMERGE>) {
  57. if (/^mtn.\s.merged.\s($sha1_pattern)$/) {
  58. $mergeRev=$1;
  59. }
  60. }
  61. close MTNMERGE || return undef;
  62. debug("merged $leftRev, $rightRev to make $mergeRev");
  63. return $mergeRev;
  64. } #}}}
  65. sub commit_file_to_new_rev($$$$$$$$) { #{{{
  66. my $automator=shift;
  67. my $wsfilename=shift;
  68. my $oldFileID=shift;
  69. my $newFileContents=shift;
  70. my $oldrev=shift;
  71. my $branch=shift;
  72. my $author=shift;
  73. my $message=shift;
  74. #store the file
  75. my ($out, $err) = $automator->call("put_file", $oldFileID, $newFileContents);
  76. my ($newFileID) = ($out =~ m/^($sha1_pattern)$/);
  77. error("Failed to store file data for $wsfilename in repository")
  78. if (! defined $newFileID || length $newFileID != 40);
  79. # get the mtn filename rather than the workspace filename
  80. ($out, $err) = $automator->call("get_corresponding_path", $oldrev, $wsfilename, $oldrev);
  81. my ($filename) = ($out =~ m/^file "(.*)"$/);
  82. error("Couldn't find monotone repository path for file $wsfilename") if (! $filename);
  83. debug("Converted ws filename of $wsfilename to repos filename of $filename");
  84. # then stick in a new revision for this file
  85. my $manifest = "format_version \"1\"\n\n".
  86. "new_manifest [0000000000000000000000000000000000000000]\n\n".
  87. "old_revision [$oldrev]\n\n".
  88. "patch \"$filename\"\n".
  89. " from [$oldFileID]\n".
  90. " to [$newFileID]\n";
  91. ($out, $err) = $automator->call("put_revision", $manifest);
  92. my ($newRevID) = ($out =~ m/^($sha1_pattern)$/);
  93. error("Unable to make new monotone repository revision")
  94. if (! defined $newRevID || length $newRevID != 40);
  95. debug("put revision: $newRevID");
  96. # now we need to add certs for this revision...
  97. # author, branch, changelog, date
  98. $automator->call("cert", $newRevID, "author", $author);
  99. $automator->call("cert", $newRevID, "branch", $branch);
  100. $automator->call("cert", $newRevID, "changelog", $message);
  101. $automator->call("cert", $newRevID, "date",
  102. time2str("%Y-%m-%dT%T", time, "UTC"));
  103. debug("Added certs for rev: $newRevID");
  104. return $newRevID;
  105. } #}}}
  106. sub check_mergerc () { #{{{
  107. my $mergerc = $config{mtnmergerc};
  108. if (! -r $mergerc ) {
  109. debug("$mergerc doesn't exist. Creating file with default mergers.");
  110. open (my $out, ">", $mergerc) or error("can't open $mergerc: $!");
  111. print $out <DATA>;
  112. close $out;
  113. }
  114. } #}}}
  115. sub read_certs ($$) { #{{{
  116. my $automator=shift;
  117. my $rev=shift;
  118. my @results = $automator->call("certs", $rev);
  119. my @ret;
  120. my $line = $results[0];
  121. 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) {
  122. push @ret, {
  123. key => $1,
  124. signature => $2,
  125. name => $3,
  126. value => $4,
  127. trust => $5,
  128. };
  129. }
  130. return @ret;
  131. } #}}}
  132. sub get_changed_files ($$) { #{{{
  133. my $automator=shift;
  134. my $rev=shift;
  135. my @results = $automator->call("get_revision", $rev);
  136. my $changes=$results[0];
  137. my @ret;
  138. my %seen = ();
  139. while ($changes =~ m/\s*(add_file|patch|delete|rename)\s"(.*?)(?<!\\)"\n/sg) {
  140. my $file = $2;
  141. # don't add the same file multiple times
  142. if (! $seen{$file}) {
  143. push @ret, $file;
  144. $seen{$file} = 1;
  145. }
  146. }
  147. return @ret;
  148. } #}}}
  149. sub rcs_update () { #{{{
  150. check_config();
  151. if (defined($config{mtnsync}) && $config{mtnsync}) {
  152. if (system("mtn", "--root=$config{mtnrootdir}", "sync",
  153. "--quiet", "--ticker=none",
  154. "--key", $config{mtnkey}) != 0) {
  155. debug("monotone sync failed before update");
  156. }
  157. }
  158. if (system("mtn", "--root=$config{mtnrootdir}", "update", "--quiet") != 0) {
  159. debug("monotone update failed");
  160. }
  161. } #}}}
  162. sub rcs_prepedit ($) { #{{{
  163. my $file=shift;
  164. check_config();
  165. # For monotone, return the revision of the file when
  166. # editing begins.
  167. return get_rev();
  168. } #}}}
  169. sub rcs_commit ($$$;$$) { #{{{
  170. # Tries to commit the page; returns undef on _success_ and
  171. # a version of the page with the rcs's conflict markers on failure.
  172. # The file is relative to the srcdir.
  173. my $file=shift;
  174. my $message=shift;
  175. my $rcstoken=shift;
  176. my $user=shift;
  177. my $ipaddr=shift;
  178. my $author;
  179. if (defined $user) {
  180. $author="Web user: " . $user;
  181. }
  182. elsif (defined $ipaddr) {
  183. $author="Web IP: " . $ipaddr;
  184. }
  185. else {
  186. $author="Web: Anonymous";
  187. }
  188. check_config();
  189. my ($oldrev)= $rcstoken=~ m/^($sha1_pattern)$/; # untaint
  190. my $rev = get_rev();
  191. if (defined $rev && defined $oldrev && $rev ne $oldrev) {
  192. my $automator = Monotone->new();
  193. $automator->open_args("--root", $config{mtnrootdir}, "--key", $config{mtnkey});
  194. # Something has been committed, has this file changed?
  195. my ($out, $err);
  196. #$automator->setOpts("-r", $oldrev, "-r", $rev);
  197. #my ($out, $err) = $automator->call("content_diff", $file);
  198. #debug("Problem committing $file") if ($err ne "");
  199. # FIXME: use of $file in these backticks is not wise from a
  200. # security POV. Probably safe, but should be avoided
  201. # anyway.
  202. # At the moment the backticks are used because the above call using the automate
  203. # interface was failing. When that bug in monotone is fixed, we should switch
  204. # back.
  205. my $diff = `mtn --root=$config{mtnrootdir} au content_diff -r $oldrev -r $rev $file`; # was just $out;
  206. if ($diff) {
  207. # Commit a revision with just this file changed off
  208. # the old revision.
  209. #
  210. # first get the contents
  211. debug("File changed: forming branch");
  212. my $newfile=readfile("$config{srcdir}/$file");
  213. # then get the old content ID from the diff
  214. if ($diff !~ m/^---\s$file\s+($sha1_pattern)$/m) {
  215. error("Unable to find previous file ID for $file");
  216. }
  217. my $oldFileID = $1;
  218. # get the branch we're working in
  219. ($out, $err) = $automator->call("get_option", "branch");
  220. chomp $out;
  221. error("Illegal branch name in monotone workspace") if ($out !~ m/^([-\@\w\.]+)$/);
  222. my $branch = $1;
  223. # then put the new content into the DB (and record the new content ID)
  224. my $newRevID = commit_file_to_new_rev($automator, $file, $oldFileID, $newfile, $oldrev, $branch, $author, $message);
  225. $automator->close();
  226. # if we made it to here then the file has been committed... revert the local copy
  227. if (system("mtn", "--root=$config{mtnrootdir}", "revert", $file) != 0) {
  228. debug("Unable to revert $file after merge on conflicted commit!");
  229. }
  230. debug("Divergence created! Attempting auto-merge.");
  231. check_mergerc();
  232. # see if it will merge cleanly
  233. $ENV{MTN_MERGE}="fail";
  234. my $mergeResult = mtn_merge($newRevID, $rev, $branch, $author);
  235. $ENV{MTN_MERGE}="";
  236. # push any changes so far
  237. if (defined($config{mtnsync}) && $config{mtnsync}) {
  238. if (system("mtn", "--root=$config{mtnrootdir}", "push", "--quiet", "--ticker=none", "--key", $config{mtnkey}) != 0) {
  239. debug("monotone push failed");
  240. }
  241. }
  242. if (defined($mergeResult)) {
  243. # everything is merged - bring outselves up to date
  244. if (system("mtn", "--root=$config{mtnrootdir}",
  245. "update", "-r", $mergeResult) != 0) {
  246. debug("Unable to update to rev $mergeResult after merge on conflicted commit!");
  247. }
  248. }
  249. else {
  250. debug("Auto-merge failed. Using diff-merge to add conflict markers.");
  251. $ENV{MTN_MERGE}="diffutils_force";
  252. $mergeResult = mtn_merge($newRevID, $rev, $branch, $author);
  253. $ENV{MTN_MERGE}="";
  254. if (!defined($mergeResult)) {
  255. debug("Unable to insert conflict markers!");
  256. error("Your commit succeeded. Unfortunately, someone else committed something to the same ".
  257. "part of the wiki at the same time. Both versions are stored in the monotone repository, ".
  258. "but at present the different versions cannot be reconciled through the web interface. ".
  259. "Please use the non-web interface to resolve the conflicts.");
  260. }
  261. if (system("mtn", "--root=$config{mtnrootdir}",
  262. "update", "-r", $mergeResult) != 0) {
  263. debug("Unable to update to rev $mergeResult after conflict-enhanced merge on conflicted commit!");
  264. }
  265. # return "conflict enhanced" file to the user
  266. # for cleanup note, this relies on the fact
  267. # that ikiwiki seems to call rcs_prepedit()
  268. # again after we return
  269. return readfile("$config{srcdir}/$file");
  270. }
  271. return undef;
  272. }
  273. $automator->close();
  274. }
  275. # If we reached here then the file we're looking at hasn't changed
  276. # since $oldrev. Commit it.
  277. if (system("mtn", "--root=$config{mtnrootdir}", "commit", "--quiet",
  278. "--author", $author, "--key", $config{mtnkey}, "-m",
  279. possibly_foolish_untaint($message), $file) != 0) {
  280. debug("Traditional commit failed! Returning data as conflict.");
  281. my $conflict=readfile("$config{srcdir}/$file");
  282. if (system("mtn", "--root=$config{mtnrootdir}", "revert",
  283. "--quiet", $file) != 0) {
  284. debug("monotone revert failed");
  285. }
  286. return $conflict;
  287. }
  288. if (defined($config{mtnsync}) && $config{mtnsync}) {
  289. if (system("mtn", "--root=$config{mtnrootdir}", "sync",
  290. "--quiet", "--ticker=none", "--key",
  291. $config{mtnkey}) != 0) {
  292. debug("monotone sync failed");
  293. }
  294. }
  295. return undef # success
  296. } #}}}
  297. sub rcs_add ($) { #{{{
  298. my $file=shift;
  299. check_config();
  300. if (system("mtn", "--root=$config{mtnrootdir}", "add", "--quiet",
  301. $file) != 0) {
  302. error("Monotone add failed");
  303. }
  304. } #}}}
  305. sub rcs_recentchanges ($) { #{{{
  306. my $num=shift;
  307. my @ret;
  308. check_config();
  309. # use log --brief to get a list of revs, as this
  310. # gives the results in a nice order
  311. # (otherwise we'd have to do our own date sorting)
  312. my @revs;
  313. my $child = open(MTNLOG, "-|");
  314. if (! $child) {
  315. exec("mtn", "log", "--root=$config{mtnrootdir}", "--no-graph",
  316. "--brief") || error("mtn log failed to run");
  317. }
  318. while (($num >= 0) and (my $line = <MTNLOG>)) {
  319. if ($line =~ m/^($sha1_pattern)/) {
  320. push @revs, $1;
  321. $num -= 1;
  322. }
  323. }
  324. close MTNLOG || debug("mtn log exited $?");
  325. my $automator = Monotone->new();
  326. $automator->open(undef, $config{mtnrootdir});
  327. while (@revs != 0) {
  328. my $rev = shift @revs;
  329. # first go through and figure out the messages, etc
  330. my $certs = [read_certs($automator, $rev)];
  331. my $user;
  332. my $when;
  333. my $committype;
  334. my (@pages, @message);
  335. foreach my $cert (@$certs) {
  336. if ($cert->{signature} eq "ok" &&
  337. $cert->{trust} eq "trusted") {
  338. if ($cert->{name} eq "author") {
  339. $user = $cert->{value};
  340. # detect the source of the commit
  341. # from the changelog
  342. if ($cert->{key} eq $config{mtnkey}) {
  343. $committype = "web";
  344. } else {
  345. $committype = "monotone";
  346. }
  347. } elsif ($cert->{name} eq "date") {
  348. $when = time - str2time($cert->{value}, 'UTC');
  349. } elsif ($cert->{name} eq "changelog") {
  350. my $messageText = $cert->{value};
  351. # split the changelog into multiple
  352. # lines
  353. foreach my $msgline (split(/\n/, $messageText)) {
  354. push @message, { line => $msgline };
  355. }
  356. }
  357. }
  358. }
  359. my @changed_files = get_changed_files($automator, $rev);
  360. my $file;
  361. foreach $file (@changed_files) {
  362. push @pages, {
  363. page => pagename($file),
  364. } if length $file;
  365. }
  366. push @ret, {
  367. rev => $rev,
  368. user => $user,
  369. committype => $committype,
  370. when => $when,
  371. message => [@message],
  372. pages => [@pages],
  373. } if @pages;
  374. }
  375. $automator->close();
  376. return @ret;
  377. } #}}}
  378. sub rcs_notify () { #{{{
  379. debug("The monotone rcs_notify function is currently untested. Use at own risk!");
  380. if (! exists $ENV{REV}) {
  381. error(gettext("REV is not set, not running from mtn post-commit hook, cannot send notifications"));
  382. }
  383. if ($ENV{REV} !~ m/($sha1_pattern)/) { # sha1 is untainted now
  384. error(gettext("REV is not a valid revision identifier, cannot send notifications"));
  385. }
  386. my $rev = $1;
  387. check_config();
  388. my $automator = Monotone->new();
  389. $automator->open(undef, $config{mtnrootdir});
  390. my $certs = [read_certs($automator, $rev)];
  391. my $user;
  392. my $message;
  393. my $when;
  394. foreach my $cert (@$certs) {
  395. if ($cert->{signature} eq "ok" && $cert->{trust} eq "trusted") {
  396. if ($cert->{name} eq "author") {
  397. $user = $cert->{value};
  398. } elsif ($cert->{name} eq "date") {
  399. $when = $cert->{value};
  400. } elsif ($cert->{name} eq "changelog") {
  401. $message = $cert->{value};
  402. }
  403. }
  404. }
  405. my @changed_pages = get_changed_files($automator, $rev);
  406. $automator->close();
  407. require IkiWiki::UserInfo;
  408. send_commit_mails(
  409. sub {
  410. return $message;
  411. },
  412. sub {
  413. `mtn --root=$config{mtnrootdir} au content_diff -r $rev`;
  414. },
  415. $user, @changed_pages);
  416. } #}}}
  417. sub rcs_getctime ($) { #{{{
  418. my $file=shift;
  419. check_config();
  420. my $child = open(MTNLOG, "-|");
  421. if (! $child) {
  422. exec("mtn", "log", "--root=$config{mtnrootdir}", "--no-graph",
  423. "--brief", $file) || error("mtn log $file failed to run");
  424. }
  425. my $firstRev;
  426. while (<MTNLOG>) {
  427. if (/^($sha1_pattern)/) {
  428. $firstRev=$1;
  429. }
  430. }
  431. close MTNLOG || debug("mtn log $file exited $?");
  432. if (! defined $firstRev) {
  433. debug "failed to parse mtn log for $file";
  434. return 0;
  435. }
  436. my $automator = Monotone->new();
  437. $automator->open(undef, $config{mtnrootdir});
  438. my $certs = [read_certs($automator, $firstRev)];
  439. $automator->close();
  440. my $date;
  441. foreach my $cert (@$certs) {
  442. if ($cert->{signature} eq "ok" && $cert->{trust} eq "trusted") {
  443. if ($cert->{name} eq "date") {
  444. $date = $cert->{value};
  445. }
  446. }
  447. }
  448. if (! defined $date) {
  449. debug "failed to find date cert for revision $firstRev when looking for creation time of $file";
  450. return 0;
  451. }
  452. $date=str2time($date, 'UTC');
  453. debug("found ctime ".localtime($date)." for $file");
  454. return $date;
  455. } #}}}
  456. 1
  457. # default mergerc content
  458. __DATA__
  459. function local_execute_redirected(stdin, stdout, stderr, path, ...)
  460. local pid
  461. local ret = -1
  462. io.flush();
  463. pid = spawn_redirected(stdin, stdout, stderr, path, unpack(arg))
  464. if (pid ~= -1) then ret, pid = wait(pid) end
  465. return ret
  466. end
  467. if (not execute_redirected) then -- use standard function if available
  468. execute_redirected = local_execute_redirected
  469. end
  470. if (not mergers.fail) then -- use standard merger if available
  471. mergers.fail = {
  472. cmd = function (tbl) return false end,
  473. available = function () return true end,
  474. wanted = function () return true end
  475. }
  476. end
  477. mergers.diffutils_force = {
  478. cmd = function (tbl)
  479. local ret = execute_redirected(
  480. "",
  481. tbl.outfile,
  482. "",
  483. "diff3",
  484. "--merge",
  485. "--show-overlap",
  486. "--label", string.format("[Yours]", tbl.left_path ),
  487. "--label", string.format("[Original]", tbl.anc_path ),
  488. "--label", string.format("[Theirs]", tbl.right_path),
  489. tbl.lfile,
  490. tbl.afile,
  491. tbl.rfile
  492. )
  493. if (ret > 1) then
  494. io.write(gettext("Error running GNU diffutils 3-way difference tool 'diff3'"))
  495. return false
  496. end
  497. return tbl.outfile
  498. end,
  499. available =
  500. function ()
  501. return program_exists_in_path("diff3");
  502. end,
  503. wanted =
  504. function ()
  505. return true
  506. end
  507. }
  508. EOF