This file is indexed.

/usr/share/perl5/IkiWiki/Plugin/monotone.pm is in ikiwiki 3.20141016.4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
#!/usr/bin/perl
package IkiWiki::Plugin::monotone;

use warnings;
use strict;
use IkiWiki;
use Monotone;
use Date::Parse qw(str2time);
use Date::Format qw(time2str);
use URI::Escape q{uri_escape_utf8};

my $sha1_pattern = qr/[0-9a-fA-F]{40}/; # pattern to validate sha1sums
my $mtn_version = undef;

sub import {
	hook(type => "checkconfig", id => "monotone", call => \&checkconfig);
	hook(type => "getsetup", id => "monotone", call => \&getsetup);
	hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
	hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
	hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
	hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
	hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
	hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
	hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
	hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
	hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
	hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
	hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
}

sub checkconfig () {
	if (!defined($config{mtnrootdir})) {
		$config{mtnrootdir} = $config{srcdir};
	}
	if (! -d "$config{mtnrootdir}/_MTN") {
		error("Ikiwiki srcdir does not seem to be a Monotone workspace (or set the mtnrootdir)!");
	}
	
	my $child = open(MTN, "-|");
	if (! $child) {
		open STDERR, ">/dev/null";
		exec("mtn", "version") || error("mtn version failed to run");
	}

	while (<MTN>) {
		if (/^monotone (\d+\.\d+)(?:(?:\.\d+){0,2}|dev)? /) {
			$mtn_version=$1;
		}
	}

	close MTN || debug("mtn version exited $?");

	if (!defined($mtn_version)) {
		error("Cannot determine monotone version");
	}
	if ($mtn_version < 0.38) {
		error("Monotone version too old, is $mtn_version but required 0.38");
	}

	if (defined $config{mtn_wrapper} && length $config{mtn_wrapper}) {
		push @{$config{wrappers}}, {
			wrapper => $config{mtn_wrapper},
			wrappermode => (defined $config{mtn_wrappermode} ? $config{mtn_wrappermode} : "06755"),
		};
	}
}

sub getsetup () {
	return
		plugin => {
			safe => 0, # rcs plugin
			rebuild => undef,
			section => "rcs",
		},
		mtn_wrapper => {
			type => "string",
			example => "/srv/mtn/wiki/_MTN/ikiwiki-netsync-hook",
			description => "monotone netsync hook to generate",
			safe => 0, # file
			rebuild => 0,
		},
		mtn_wrappermode => {
			type => "string",
			example => '06755',
			description => "mode for mtn_wrapper (can safely be made suid)",
			safe => 0,
			rebuild => 0,
		},
		mtnkey => {
			type => "string",
			example => 'web@example.com',
			description => "your monotone key",
			safe => 1,
			rebuild => 0,
		},
		historyurl => {
			type => "string",
			example => "http://viewmtn.example.com/branch/head/filechanges/com.example.branch/[[file]]",
			description => "viewmtn url to show file history ([[file]] substituted)",
			safe => 1,
			rebuild => 1,
		},
		diffurl => {
			type => "string",
			example => "http://viewmtn.example.com/revision/diff/[[r1]]/with/[[r2]]/[[file]]",
			description => "viewmtn url to show a diff ([[r1]], [[r2]], and [[file]] substituted)",
			safe => 1,
			rebuild => 1,
		},
		mtnsync => {
			type => "boolean",
			example => 0,
			description => "sync on update and commit?",
			safe => 0, # paranoia
			rebuild => 0,
		},
		mtnrootdir => {
			type => "string",
			description => "path to your workspace (defaults to the srcdir; specify if the srcdir is a subdirectory of the workspace)",
			safe => 0, # path
			rebuild => 0,
		},
}

sub get_rev () {
	my $sha1 = `mtn --root=$config{mtnrootdir} automate get_base_revision_id`;

	($sha1) = $sha1 =~ m/($sha1_pattern)/; # sha1 is untainted now
	if (! $sha1) {
		debug("Unable to get base revision for '$config{srcdir}'.")
	}

	return $sha1;
}

sub get_rev_auto ($) {
	my $automator=shift;

	my @results = $automator->call("get_base_revision_id");

	my $sha1 = $results[0];
	($sha1) = $sha1 =~ m/($sha1_pattern)/; # sha1 is untainted now
	if (! $sha1) {
		debug("Unable to get base revision for '$config{srcdir}'.")
	}

	return $sha1;
}

sub mtn_merge ($$$$) {
	my $leftRev=shift;
	my $rightRev=shift;
	my $branch=shift;
	my $author=shift;
    
	my $mergeRev;

	my $child = open(MTNMERGE, "-|");
	if (! $child) {
		open STDERR, ">&STDOUT";
		exec("mtn", "--root=$config{mtnrootdir}",
		     "explicit_merge", $leftRev, $rightRev,
		     $branch, "--author", $author, "--key", 
		     $config{mtnkey}) || error("mtn merge failed to run");
	}

	while (<MTNMERGE>) {
		if (/^mtn.\s.merged.\s($sha1_pattern)$/) {
			$mergeRev=$1;
		}
	}
	
	close MTNMERGE || return undef;

	debug("merged $leftRev, $rightRev to make $mergeRev");

	return $mergeRev;
}

sub commit_file_to_new_rev ($$$$$$$$) {
	my $automator=shift;
	my $wsfilename=shift;
	my $oldFileID=shift;
	my $newFileContents=shift;
	my $oldrev=shift;
	my $branch=shift;
	my $author=shift;
	my $message=shift;
	
	#store the file
	my ($out, $err) = $automator->call("put_file", $oldFileID, $newFileContents);
	my ($newFileID) = ($out =~ m/^($sha1_pattern)$/);
	error("Failed to store file data for $wsfilename in repository")
		if (! defined $newFileID || length $newFileID != 40);

	# get the mtn filename rather than the workspace filename
	($out, $err) = $automator->call("get_corresponding_path", $oldrev, $wsfilename, $oldrev);
	my ($filename) = ($out =~ m/^file "(.*)"$/);
	error("Couldn't find monotone repository path for file $wsfilename") if (! $filename);
	debug("Converted ws filename of $wsfilename to repos filename of $filename");

	# then stick in a new revision for this file
	my $manifest = "format_version \"1\"\n\n".
	               "new_manifest [0000000000000000000000000000000000000000]\n\n".
	               "old_revision [$oldrev]\n\n".
	               "patch \"$filename\"\n".
	               " from [$oldFileID]\n".
	               "   to [$newFileID]\n";
	($out, $err) = $automator->call("put_revision", $manifest);
	my ($newRevID) = ($out =~ m/^($sha1_pattern)$/);
	error("Unable to make new monotone repository revision")
		if (! defined $newRevID || length $newRevID != 40);
	debug("put revision: $newRevID");
	
	# now we need to add certs for this revision...
	# author, branch, changelog, date
	$automator->call("cert", $newRevID, "author", $author);
	$automator->call("cert", $newRevID, "branch", $branch);
	$automator->call("cert", $newRevID, "changelog", $message);
	$automator->call("cert", $newRevID, "date",
		time2str("%Y-%m-%dT%T", time, "UTC"));
	
	debug("Added certs for rev: $newRevID");
	return $newRevID;
}

sub read_certs ($$) {
	my $automator=shift;
	my $rev=shift;
	my @results = $automator->call("certs", $rev);
	my @ret;

	my $line = $results[0];
	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) {
		push @ret, {
			key => $1,
			signature => $2,
			name => $3,
			value => $4,
			trust => $5,
		};
	}

	return @ret;
}

sub get_changed_files ($$) {
	my $automator=shift;
	my $rev=shift;
	
	my @results = $automator->call("get_revision", $rev);
	my $changes=$results[0];

	my @ret;
	my %seen = ();

	# we need to strip off the relative path to the source dir
	# because monotone outputs all file paths absolute according
	# to the workspace root
	my $rel_src_dir = $config{'srcdir'};
	$rel_src_dir =~ s/^\Q$config{'mtnrootdir'}\E\/?//;
	$rel_src_dir .= "/" if length $rel_src_dir;

	while ($changes =~ m/\s*(add_file|patch|delete|rename)\s"(.*?)(?<!\\)"\n/sg) {
		my $file = $2;
		# ignore all file changes outside the source dir
		next unless $file =~ m/^\Q$rel_src_dir\E/;
		$file =~ s/^\Q$rel_src_dir\E//;
        
		# don't add the same file multiple times
		if (! $seen{$file}) {
			push @ret, $file;
			$seen{$file} = 1;
		}
	}
	
	return @ret;
}

sub rcs_update () {
	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	if (defined($config{mtnsync}) && $config{mtnsync}) {
		if (system("mtn", "--root=$config{mtnrootdir}", "sync",
		           "--quiet", "--ticker=none", 
		           "--key", $config{mtnkey}) != 0) {
			debug("monotone sync failed before update");
		}
	}

	if (system("mtn", "--root=$config{mtnrootdir}", "update", "--quiet") != 0) {
		debug("monotone update failed");
	}
}

sub rcs_prepedit ($) {
	my $file=shift;

	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	# For monotone, return the revision of the file when
	# editing begins.
	return get_rev();
}

sub commitauthor (@) {
	my %params=@_;

	if (defined $params{session}) {
		if (defined $params{session}->param("name")) {
			return "Web user: " . $params{session}->param("name");
		}
		elsif (defined $params{session}->remote_addr()) {
			return "Web IP: " . $params{session}->remote_addr();
		}
	}
	return "Web: Anonymous";
}


sub rcs_commit (@) {
	# Tries to commit the page; returns undef on _success_ and
	# a version of the page with the rcs's conflict markers on failure.
	# The file is relative to the srcdir.
	my %params=@_;

	my $author=IkiWiki::possibly_foolish_untaint(commitauthor(%params)),

	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	my ($oldrev) = $params{token} =~ m/^($sha1_pattern)$/; # untaint
	my $rev = get_rev();
	if (defined $rev && defined $oldrev && $rev ne $oldrev) {
		my $automator = Monotone->new();
		$automator->open_args("--root", $config{mtnrootdir}, "--key", $config{mtnkey});

		# Something has been committed, has this file changed?
		my ($out, $err);
		$automator->setOpts("r", $oldrev, "r", $rev);
		($out, $err) = $automator->call("content_diff", $params{file});
		debug("Problem committing $params{file}") if ($err ne "");
		my $diff = $out;
		
		if ($diff) {
			# Commit a revision with just this file changed off
			# the old revision.
			#
			# first get the contents
			debug("File changed: forming branch");
			my $newfile=readfile("$config{srcdir}/$params{file}");
			
			# then get the old content ID from the diff
			if ($diff !~ m/^---\s$params{file}\s+($sha1_pattern)$/m) {
				error("Unable to find previous file ID for $params{file}");
			}
			my $oldFileID = $1;

			# get the branch we're working in
			($out, $err) = $automator->call("get_option", "branch");
			chomp $out;
			error("Illegal branch name in monotone workspace") if ($out !~ m/^([-\@\w\.]+)$/);
			my $branch = $1;

			# then put the new content into the DB (and record the new content ID)
			my $newRevID = commit_file_to_new_rev($automator, $params{file}, $oldFileID, $newfile, $oldrev, $branch, $author, $params{message});

			$automator->close();

			# if we made it to here then the file has been committed... revert the local copy
			if (system("mtn", "--root=$config{mtnrootdir}", "revert", $params{file}) != 0) {
				debug("Unable to revert $params{file} after merge on conflicted commit!");
			}
			debug("Divergence created! Attempting auto-merge.");

			# see if it will merge cleanly
			$ENV{MTN_MERGE}="fail";
			my $mergeResult = mtn_merge($newRevID, $rev, $branch, $author);
			$ENV{MTN_MERGE}="";

			# push any changes so far
			if (defined($config{mtnsync}) && $config{mtnsync}) {
				if (system("mtn", "--root=$config{mtnrootdir}", "push", "--quiet", "--ticker=none", "--key", $config{mtnkey}) != 0) {
					debug("monotone push failed");
				}
			}
			
			if (defined($mergeResult)) {
				# everything is merged - bring outselves up to date
				if (system("mtn", "--root=$config{mtnrootdir}",
				           "update", "-r", $mergeResult) != 0) {
					debug("Unable to update to rev $mergeResult after merge on conflicted commit!");
				}
			}
			else {
				debug("Auto-merge failed.  Using diff-merge to add conflict markers.");
				
				$ENV{MTN_MERGE}="diffutils";
				$ENV{MTN_MERGE_DIFFUTILS}="partial=true";
				$mergeResult = mtn_merge($newRevID, $rev, $branch, $author);
				$ENV{MTN_MERGE}="";
				$ENV{MTN_MERGE_DIFFUTILS}="";
				
				if (!defined($mergeResult)) {
					debug("Unable to insert conflict markers!");
					error("Your commit succeeded. Unfortunately, someone else committed something to the same ".
				 		"part of the wiki at the same time. Both versions are stored in the monotone repository, ".
						"but at present the different versions cannot be reconciled through the web interface. ".
						"Please use the non-web interface to resolve the conflicts.");
				}
				
				if (system("mtn", "--root=$config{mtnrootdir}",
				           "update", "-r", $mergeResult) != 0) {
					debug("Unable to update to rev $mergeResult after conflict-enhanced merge on conflicted commit!");
				}
				
				# return "conflict enhanced" file to the user
				# for cleanup note, this relies on the fact
				# that ikiwiki seems to call rcs_prepedit()
				# again after we return
				return readfile("$config{srcdir}/$params{file}");
			}
			return undef;
		}
		$automator->close();
	}

	# If we reached here then the file we're looking at hasn't changed
	# since $oldrev. Commit it.

	if (system("mtn", "--root=$config{mtnrootdir}", "commit", "--quiet",
	           "--author", $author, "--key", $config{mtnkey}, "-m",
	           IkiWiki::possibly_foolish_untaint($params{message}),
	           $params{file}) != 0) {
		debug("Traditional commit failed! Returning data as conflict.");
		my $conflict=readfile("$config{srcdir}/$params{file}");
		if (system("mtn", "--root=$config{mtnrootdir}", "revert",
		           "--quiet", $params{file}) != 0) {
			debug("monotone revert failed");
		}
		return $conflict;
	}
	if (defined($config{mtnsync}) && $config{mtnsync}) {
		if (system("mtn", "--root=$config{mtnrootdir}", "push",
		           "--quiet", "--ticker=none", "--key",
		           $config{mtnkey}) != 0) {
			debug("monotone push failed");
		}
	}

	return undef # success
}

sub rcs_commit_staged (@) {
	# Commits all staged changes. Changes can be staged using rcs_add,
	# rcs_remove, and rcs_rename.
	my %params=@_;

	# Note - this will also commit any spurious changes that happen to be
	# lying around in the working copy.  There shouldn't be any, but...
	
	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	if (system("mtn", "--root=$config{mtnrootdir}", "commit", "--quiet",
	           "--author", IkiWiki::possibly_foolish_untaint(commitauthor(%params)),
	           "--key", $config{mtnkey}, "-m",
		   IkiWiki::possibly_foolish_untaint($params{message})) != 0) {
		error("Monotone commit failed");
	}
}

sub rcs_add ($) {
	my $file=shift;

	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	if (system("mtn", "--root=$config{mtnrootdir}", "add", "--quiet",
	           $file) != 0) {
		error("Monotone add failed");
	}
}

sub rcs_remove ($) {
	my $file = shift;

	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	# Note: it is difficult to undo a remove in Monotone at the moment.
	# Until this is fixed, it might be better to make 'rm' move things
	# into an attic, rather than actually remove them.
	# To resurrect a file, you currently add a new file with the contents
	# you want it to have.  This loses all connectivity and automated
	# merging with the 'pre-delete' versions of the file.

	if (system("mtn", "--root=$config{mtnrootdir}", "rm", "--quiet",
	           $file) != 0) {
		error("Monotone remove failed");
	}
}

sub rcs_rename ($$) {
	my ($src, $dest) = @_;

	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	if (system("mtn", "--root=$config{mtnrootdir}", "rename", "--quiet",
	           $src, $dest) != 0) {
		error("Monotone rename failed");
	}
}

sub rcs_recentchanges ($) {
	my $num=shift;
	my @ret;

	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	# use log --brief to get a list of revs, as this
	# gives the results in a nice order
	# (otherwise we'd have to do our own date sorting)

	my @revs;

	my $child = open(MTNLOG, "-|");
	if (! $child) {
		exec("mtn", "log", "--root=$config{mtnrootdir}", "--no-graph",
		     "--brief", "--last=$num") || error("mtn log failed to run");
	}

	while (my $line = <MTNLOG>) {
		if ($line =~ m/^($sha1_pattern)/) {
			push @revs, $1;
		}
	}
	close MTNLOG || debug("mtn log exited $?");

	my $automator = Monotone->new();
	$automator->open(undef, $config{mtnrootdir});

	while (@revs != 0) {
		my $rev = shift @revs;
		# first go through and figure out the messages, etc

		my $certs = [read_certs($automator, $rev)];
		
		my $user;
		my $when;
		my $committype;
		my (@pages, @message);
		
		foreach my $cert (@$certs) {
			if ($cert->{signature} eq "ok" &&
			    $cert->{trust} eq "trusted") {
				if ($cert->{name} eq "author") {
					$user = $cert->{value};
				 	# detect the source of the commit
					# from the changelog
					if ($cert->{key} eq $config{mtnkey}) {
						$committype = "web";
					}
					else {
						$committype = "mtn";
					}
				} elsif ($cert->{name} eq "date") {
					$when = str2time($cert->{value}, 'UTC');
				} elsif ($cert->{name} eq "changelog") {
					my $messageText = $cert->{value};
					# split the changelog into multiple
					# lines
					foreach my $msgline (split(/\n/, $messageText)) {
						push @message, { line => $msgline };
					}
				}
			}
		}
		
		my @changed_files = get_changed_files($automator, $rev);
		
		my ($out, $err) = $automator->call("parents", $rev);
		my @parents = ($out =~ m/^($sha1_pattern)$/);
		my $parent = $parents[0];

		foreach my $file (@changed_files) {
			next unless length $file;
			
			if (defined $config{diffurl} and (@parents == 1)) {
				my $diffurl=$config{diffurl};
				$diffurl=~s/\[\[r1\]\]/$parent/g;
				$diffurl=~s/\[\[r2\]\]/$rev/g;
				my $efile = uri_escape_utf8($file);
				$diffurl=~s/\[\[file\]\]/$efile/g;
				push @pages, {
					page => pagename($file),
					diffurl => $diffurl,
				};
			}
			else {
				push @pages, {
					page => pagename($file),
				}
			}
		}
		
		push @ret, {
			rev => $rev,
			user => $user,
			committype => $committype,
			when => $when,
			message => [@message],
			pages => [@pages],
		} if @pages;
	}

	$automator->close();

	return @ret;
}

sub rcs_diff ($;$) {
	my $rev=shift;
	my $maxlines=shift;
	my ($sha1) = $rev =~ /^($sha1_pattern)$/; # untaint
	
	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	my $child = open(MTNDIFF, "-|");
	if (! $child) {
		exec("mtn", "diff", "--root=$config{mtnrootdir}", "-r", "p:".$sha1, "-r", $sha1) || error("mtn diff $sha1 failed to run");
	}

	my @lines;
	while (my $line=<MTNDIFF>) {
		last if defined $maxlines && @lines == $maxlines;
		push @lines, $line;
	}

	close MTNDIFF || debug("mtn diff $sha1 exited $?");

	if (wantarray) {
		return @lines;
	}
	else {
		return join("", @lines);
	}
}

sub rcs_getctime ($) {
	my $file=shift;

	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	my $child = open(MTNLOG, "-|");
	if (! $child) {
		exec("mtn", "log", "--root=$config{mtnrootdir}", "--no-graph",
		     "--brief", $file) || error("mtn log $file failed to run");
	}

	my $prevRev;
	my $firstRev;
	while (<MTNLOG>) {
		if (/^($sha1_pattern)/) {
			$prevRev=$firstRev;
			$firstRev=$1;
		}
	}
	close MTNLOG || debug("mtn log $file exited $?");

	if (! defined $firstRev) {
		debug "failed to parse mtn log for $file";
		return 0;
	}

	my $automator = Monotone->new();
	$automator->open(undef, $config{mtnrootdir});

	# mtn 0.48 has a bug that makes it list the creation of parent
	# directories as last (first) log entry...  So when we're dealing
	# with that version, let's check that the file we're looking for
	# is actually part of the last (first) revision.  Otherwise, pick
	# the one before (after) that one.
	if ($mtn_version == 0.48) {
		my $changes = [get_changed_files($automator, $firstRev)];
		if (! exists {map { $_ => 1 } @$changes}->{$file}) {
			$firstRev = $prevRev;
		}
	}
	my $certs = [read_certs($automator, $firstRev)];

	$automator->close();

	my $date;

	foreach my $cert (@$certs) {
		if ($cert->{signature} eq "ok" && $cert->{trust} eq "trusted") {
			if ($cert->{name} eq "date") {
				$date = $cert->{value};
			}
		}
	}

	if (! defined $date) {
		debug "failed to find date cert for revision $firstRev when looking for creation time of $file";
		return 0;
	}

	$date=str2time($date, 'UTC');
	debug("found ctime ".localtime($date)." for $file");
	return $date;
}

sub rcs_getmtime ($) {
	my $file=shift;

	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	my $child = open(MTNLOG, "-|");
	if (! $child) {
		exec("mtn", "log", "--root=$config{mtnrootdir}", "--no-graph",
		     "--brief", $file) || error("mtn log $file failed to run");
	}

	my $lastRev = "";
	while (<MTNLOG>) {
		if (/^($sha1_pattern)/ && $lastRev eq "") {
			$lastRev=$1;
		}
	}
	close MTNLOG || debug("mtn log $file exited $?");

	if (! defined $lastRev) {
		debug "failed to parse mtn log for $file";
		return 0;
	}

	my $automator = Monotone->new();
	$automator->open(undef, $config{mtnrootdir});

	my $certs = [read_certs($automator, $lastRev)];

	$automator->close();

	my $date;

	foreach my $cert (@$certs) {
		if ($cert->{signature} eq "ok" && $cert->{trust} eq "trusted") {
			if ($cert->{name} eq "date") {
				$date = $cert->{value};
			}
		}
	}

	if (! defined $date) {
		debug "failed to find date cert for revision $lastRev when looking for creation time of $file";
		return 0;
	}

	$date=str2time($date, 'UTC');
	debug("found mtime ".localtime($date)." for $file");
	return $date;
}

1