9 use Date::Parse qw(str2time);
10 use Date::Format qw(time2str);
12 my $sha1_pattern = qr/[0-9a-fA-F]{40}/; # pattern to validate sha1sums
14 sub check_config() { #{{{
15 if (!defined($config{mtnrootdir})) {
16 $config{mtnrootdir} = $config{srcdir};
18 if (! -d "$config{mtnrootdir}/_MTN") {
19 error("Ikiwiki srcdir does not seem to be a Monotone workspace (or set the mtnrootdir)!");
23 or error("Cannot chdir to $config{srcdir}: $!");
25 my $child = open(MTN, "-|");
27 open STDERR, ">/dev/null";
28 exec("mtn", "version") || error("mtn version failed to run");
33 if (/^monotone (\d+\.\d+) /) {
38 close MTN || debug("mtn version exited $?");
40 if (!defined($version)) {
41 error("Cannot determine monotone version");
43 if ($version < 0.38) {
44 error("Monotone version too old, is $version but required 0.38");
49 my $sha1 = `mtn --root=$config{mtnrootdir} automate get_base_revision_id`;
51 ($sha1) = $sha1 =~ m/($sha1_pattern)/; # sha1 is untainted now
53 debug("Unable to get base revision for '$config{srcdir}'.")
59 sub get_rev_auto ($) { #{{{
62 my @results = $automator->call("get_base_revision_id");
64 my $sha1 = $results[0];
65 ($sha1) = $sha1 =~ m/($sha1_pattern)/; # sha1 is untainted now
67 debug("Unable to get base revision for '$config{srcdir}'.")
73 sub mtn_merge ($$$$) { #{{{
81 my $child = open(MTNMERGE, "-|");
83 open STDERR, ">&STDOUT";
84 exec("mtn", "--root=$config{mtnrootdir}",
85 "explicit_merge", $leftRev, $rightRev,
86 $branch, "--author", $author, "--key",
87 $config{mtnkey}) || error("mtn merge failed to run");
91 if (/^mtn.\s.merged.\s($sha1_pattern)$/) {
96 close MTNMERGE || return undef;
98 debug("merged $leftRev, $rightRev to make $mergeRev");
103 sub commit_file_to_new_rev($$$$$$$$) { #{{{
105 my $wsfilename=shift;
107 my $newFileContents=shift;
114 my ($out, $err) = $automator->call("put_file", $oldFileID, $newFileContents);
115 my ($newFileID) = ($out =~ m/^($sha1_pattern)$/);
116 error("Failed to store file data for $wsfilename in repository")
117 if (! defined $newFileID || length $newFileID != 40);
119 # get the mtn filename rather than the workspace filename
120 ($out, $err) = $automator->call("get_corresponding_path", $oldrev, $wsfilename, $oldrev);
121 my ($filename) = ($out =~ m/^file "(.*)"$/);
122 error("Couldn't find monotone repository path for file $wsfilename") if (! $filename);
123 debug("Converted ws filename of $wsfilename to repos filename of $filename");
125 # then stick in a new revision for this file
126 my $manifest = "format_version \"1\"\n\n".
127 "new_manifest [0000000000000000000000000000000000000000]\n\n".
128 "old_revision [$oldrev]\n\n".
129 "patch \"$filename\"\n".
130 " from [$oldFileID]\n".
131 " to [$newFileID]\n";
132 ($out, $err) = $automator->call("put_revision", $manifest);
133 my ($newRevID) = ($out =~ m/^($sha1_pattern)$/);
134 error("Unable to make new monotone repository revision")
135 if (! defined $newRevID || length $newRevID != 40);
136 debug("put revision: $newRevID");
138 # now we need to add certs for this revision...
139 # author, branch, changelog, date
140 $automator->call("cert", $newRevID, "author", $author);
141 $automator->call("cert", $newRevID, "branch", $branch);
142 $automator->call("cert", $newRevID, "changelog", $message);
143 $automator->call("cert", $newRevID, "date",
144 time2str("%Y-%m-%dT%T", time, "UTC"));
146 debug("Added certs for rev: $newRevID");
150 sub read_certs ($$) { #{{{
153 my @results = $automator->call("certs", $rev);
156 my $line = $results[0];
157 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) {
170 sub get_changed_files ($$) { #{{{
174 my @results = $automator->call("get_revision", $rev);
175 my $changes=$results[0];
180 while ($changes =~ m/\s*(add_file|patch|delete|rename)\s"(.*?)(?<!\\)"\n/sg) {
182 # don't add the same file multiple times
183 if (! $seen{$file}) {
192 sub rcs_update () { #{{{
195 if (defined($config{mtnsync}) && $config{mtnsync}) {
196 if (system("mtn", "--root=$config{mtnrootdir}", "sync",
197 "--quiet", "--ticker=none",
198 "--key", $config{mtnkey}) != 0) {
199 debug("monotone sync failed before update");
203 if (system("mtn", "--root=$config{mtnrootdir}", "update", "--quiet") != 0) {
204 debug("monotone update failed");
208 sub rcs_prepedit ($) { #{{{
213 # For monotone, return the revision of the file when
218 sub rcs_commit ($$$;$$) { #{{{
219 # Tries to commit the page; returns undef on _success_ and
220 # a version of the page with the rcs's conflict markers on failure.
221 # The file is relative to the srcdir.
230 $author="Web user: " . $user;
232 elsif (defined $ipaddr) {
233 $author="Web IP: " . $ipaddr;
236 $author="Web: Anonymous";
241 my ($oldrev)= $rcstoken=~ m/^($sha1_pattern)$/; # untaint
243 if (defined $rev && defined $oldrev && $rev ne $oldrev) {
244 my $automator = Monotone->new();
245 $automator->open_args("--root", $config{mtnrootdir}, "--key", $config{mtnkey});
247 # Something has been committed, has this file changed?
249 $automator->setOpts("r", $oldrev, "r", $rev);
250 ($out, $err) = $automator->call("content_diff", $file);
251 debug("Problem committing $file") if ($err ne "");
255 # Commit a revision with just this file changed off
258 # first get the contents
259 debug("File changed: forming branch");
260 my $newfile=readfile("$config{srcdir}/$file");
262 # then get the old content ID from the diff
263 if ($diff !~ m/^---\s$file\s+($sha1_pattern)$/m) {
264 error("Unable to find previous file ID for $file");
268 # get the branch we're working in
269 ($out, $err) = $automator->call("get_option", "branch");
271 error("Illegal branch name in monotone workspace") if ($out !~ m/^([-\@\w\.]+)$/);
274 # then put the new content into the DB (and record the new content ID)
275 my $newRevID = commit_file_to_new_rev($automator, $file, $oldFileID, $newfile, $oldrev, $branch, $author, $message);
279 # if we made it to here then the file has been committed... revert the local copy
280 if (system("mtn", "--root=$config{mtnrootdir}", "revert", $file) != 0) {
281 debug("Unable to revert $file after merge on conflicted commit!");
283 debug("Divergence created! Attempting auto-merge.");
285 # see if it will merge cleanly
286 $ENV{MTN_MERGE}="fail";
287 my $mergeResult = mtn_merge($newRevID, $rev, $branch, $author);
290 # push any changes so far
291 if (defined($config{mtnsync}) && $config{mtnsync}) {
292 if (system("mtn", "--root=$config{mtnrootdir}", "push", "--quiet", "--ticker=none", "--key", $config{mtnkey}) != 0) {
293 debug("monotone push failed");
297 if (defined($mergeResult)) {
298 # everything is merged - bring outselves up to date
299 if (system("mtn", "--root=$config{mtnrootdir}",
300 "update", "-r", $mergeResult) != 0) {
301 debug("Unable to update to rev $mergeResult after merge on conflicted commit!");
305 debug("Auto-merge failed. Using diff-merge to add conflict markers.");
307 $ENV{MTN_MERGE}="diffutils";
308 $ENV{MTN_MERGE_DIFFUTILS}="partial=true";
309 $mergeResult = mtn_merge($newRevID, $rev, $branch, $author);
311 $ENV{MTN_MERGE_DIFFUTILS}="";
313 if (!defined($mergeResult)) {
314 debug("Unable to insert conflict markers!");
315 error("Your commit succeeded. Unfortunately, someone else committed something to the same ".
316 "part of the wiki at the same time. Both versions are stored in the monotone repository, ".
317 "but at present the different versions cannot be reconciled through the web interface. ".
318 "Please use the non-web interface to resolve the conflicts.");
321 if (system("mtn", "--root=$config{mtnrootdir}",
322 "update", "-r", $mergeResult) != 0) {
323 debug("Unable to update to rev $mergeResult after conflict-enhanced merge on conflicted commit!");
326 # return "conflict enhanced" file to the user
327 # for cleanup note, this relies on the fact
328 # that ikiwiki seems to call rcs_prepedit()
329 # again after we return
330 return readfile("$config{srcdir}/$file");
337 # If we reached here then the file we're looking at hasn't changed
338 # since $oldrev. Commit it.
340 if (system("mtn", "--root=$config{mtnrootdir}", "commit", "--quiet",
341 "--author", $author, "--key", $config{mtnkey}, "-m",
342 possibly_foolish_untaint($message), $file) != 0) {
343 debug("Traditional commit failed! Returning data as conflict.");
344 my $conflict=readfile("$config{srcdir}/$file");
345 if (system("mtn", "--root=$config{mtnrootdir}", "revert",
346 "--quiet", $file) != 0) {
347 debug("monotone revert failed");
351 if (defined($config{mtnsync}) && $config{mtnsync}) {
352 if (system("mtn", "--root=$config{mtnrootdir}", "push",
353 "--quiet", "--ticker=none", "--key",
354 $config{mtnkey}) != 0) {
355 debug("monotone push failed");
359 return undef # success
362 sub rcs_add ($) { #{{{
367 if (system("mtn", "--root=$config{mtnrootdir}", "add", "--quiet",
369 error("Monotone add failed");
373 sub rcs_recentchanges ($) { #{{{
379 # use log --brief to get a list of revs, as this
380 # gives the results in a nice order
381 # (otherwise we'd have to do our own date sorting)
385 my $child = open(MTNLOG, "-|");
387 exec("mtn", "log", "--root=$config{mtnrootdir}", "--no-graph",
388 "--brief") || error("mtn log failed to run");
391 while (($num >= 0) and (my $line = <MTNLOG>)) {
392 if ($line =~ m/^($sha1_pattern)/) {
397 close MTNLOG || debug("mtn log exited $?");
399 my $automator = Monotone->new();
400 $automator->open(undef, $config{mtnrootdir});
403 my $rev = shift @revs;
404 # first go through and figure out the messages, etc
406 my $certs = [read_certs($automator, $rev)];
411 my (@pages, @message);
413 foreach my $cert (@$certs) {
414 if ($cert->{signature} eq "ok" &&
415 $cert->{trust} eq "trusted") {
416 if ($cert->{name} eq "author") {
417 $user = $cert->{value};
418 # detect the source of the commit
420 if ($cert->{key} eq $config{mtnkey}) {
423 $committype = "monotone";
425 } elsif ($cert->{name} eq "date") {
426 $when = str2time($cert->{value}, 'UTC');
427 } elsif ($cert->{name} eq "changelog") {
428 my $messageText = $cert->{value};
429 # split the changelog into multiple
431 foreach my $msgline (split(/\n/, $messageText)) {
432 push @message, { line => $msgline };
438 my @changed_files = get_changed_files($automator, $rev);
441 my ($out, $err) = $automator->call("parents", $rev);
442 my @parents = ($out =~ m/^($sha1_pattern)$/);
443 my $parent = $parents[0];
445 foreach $file (@changed_files) {
446 next unless length $file;
448 if (defined $config{diffurl} and (@parents == 1)) {
449 my $diffurl=$config{diffurl};
450 $diffurl=~s/\[\[r1\]\]/$parent/g;
451 $diffurl=~s/\[\[r2\]\]/$rev/g;
452 $diffurl=~s/\[\[file\]\]/$file/g;
454 page => pagename($file),
460 page => pagename($file),
468 committype => $committype,
470 message => [@message],
480 sub rcs_diff ($) { #{{{
484 sub rcs_getctime ($) { #{{{
489 my $child = open(MTNLOG, "-|");
491 exec("mtn", "log", "--root=$config{mtnrootdir}", "--no-graph",
492 "--brief", $file) || error("mtn log $file failed to run");
497 if (/^($sha1_pattern)/) {
501 close MTNLOG || debug("mtn log $file exited $?");
503 if (! defined $firstRev) {
504 debug "failed to parse mtn log for $file";
508 my $automator = Monotone->new();
509 $automator->open(undef, $config{mtnrootdir});
511 my $certs = [read_certs($automator, $firstRev)];
517 foreach my $cert (@$certs) {
518 if ($cert->{signature} eq "ok" && $cert->{trust} eq "trusted") {
519 if ($cert->{name} eq "date") {
520 $date = $cert->{value};
525 if (! defined $date) {
526 debug "failed to find date cert for revision $firstRev when looking for creation time of $file";
530 $date=str2time($date, 'UTC');
531 debug("found ctime ".localtime($date)." for $file");