From dca6679a5442581746daf89e78ee83712a017b98 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@debian.org>
Date: Thu, 6 Jan 2011 00:15:28 +0000
Subject: [PATCH] autoindex: use add_autofile

This does cause a minor regression: index pages are now committed
individually rather than being a single commit per rebuild.

This also means the autoindex regression test needs to trigger the
autofile generation pass.
---
 IkiWiki/Plugin/autoindex.pm | 37 +++++++++++++++++++++----------------
 t/autoindex.t               | 19 +++++++++++++++++--
 2 files changed, 38 insertions(+), 18 deletions(-)

diff --git a/IkiWiki/Plugin/autoindex.pm b/IkiWiki/Plugin/autoindex.pm
index 11595e217..ba2dcb907 100644
--- a/IkiWiki/Plugin/autoindex.pm
+++ b/IkiWiki/Plugin/autoindex.pm
@@ -22,12 +22,23 @@ sub getsetup () {
 sub genindex ($) {
 	my $page=shift;
 	my $file=newpagefile($page, $config{default_pageext});
-	my $template=template("autoindex.tmpl");
-	$template->param(page => $page);
-	writefile($file, $config{srcdir}, $template->output);
-	if ($config{rcs}) {
-		IkiWiki::rcs_add($file);
-	}
+
+	add_autofile($file, "autoindex", sub {
+			my $message = sprintf(gettext("creating index page %s"),
+				$page);
+			debug($message);
+
+			my $template = template("autoindex.tmpl");
+			$template->param(page => $page);
+			writefile($file, $config{srcdir}, $template->output);
+
+			if ($config{rcs}) {
+				IkiWiki::disable_commit_hook();
+				IkiWiki::rcs_add($file);
+				IkiWiki::rcs_commit_staged(message => $message);
+				IkiWiki::enable_commit_hook();
+			}
+		});
 }
 
 sub refresh () {
@@ -66,7 +77,10 @@ sub refresh () {
 
 		chdir($origdir) || die "chdir $origdir: $!";
 	}
-	
+
+	# FIXME: some of this is probably redundant with add_autofile now, and
+	# the rest should perhaps be added to the autofile machinery
+
 	my %deleted;
 	if (ref $wikistate{autoindex}{deleted}) {
 		%deleted=%{$wikistate{autoindex}{deleted}};
@@ -109,18 +123,9 @@ sub refresh () {
 	}
 	
 	if (@needed) {
-		if ($config{rcs}) {
-			IkiWiki::disable_commit_hook();
-		}
 		foreach my $page (@needed) {
 			genindex($page);
 		}
-		if ($config{rcs}) {
-			IkiWiki::rcs_commit_staged(
-				message => gettext("automatic index generation"),
-			);
-			IkiWiki::enable_commit_hook();
-		}
 	}
 }
 
diff --git a/t/autoindex.t b/t/autoindex.t
index b1425c22e..04d668fbc 100755
--- a/t/autoindex.t
+++ b/t/autoindex.t
@@ -3,7 +3,7 @@ package IkiWiki;
 
 use warnings;
 use strict;
-use Test::More tests => 22;
+use Test::More tests => 28;
 
 BEGIN { use_ok("IkiWiki"); }
 BEGIN { use_ok("IkiWiki::Render"); }
@@ -61,6 +61,9 @@ writefile("attached/pie.jpg", "t/tmp", "I lied, this isn't a real JPEG");
 # "gone" disappeared just before this refresh pass so it still has a mtime
 $pagemtime{gone} = $pagectime{gone} = 1000000;
 
+my %pages;
+my @del;
+
 IkiWiki::Plugin::autoindex::refresh();
 
 # these pages are still on record as having been deleted, because they have
@@ -83,12 +86,24 @@ ok(! -f "t/tmp/has_internal.mdwn");
 ok(! exists $wikistate{autoindex}{deleted}{reinstated});
 ok(! -f "t/tmp/reinstated.mdwn");
 
-# needs creating
+# needs creating (deferred; part of the autofile mechanism now)
 ok(! exists $wikistate{autoindex}{deleted}{tags});
+%pages = ();
+@del = ();
+is($autofiles{"tags.mdwn"}{plugin}, "autoindex");
+IkiWiki::gen_autofile("tags.mdwn", \%pages, \@del);
+is_deeply(\%pages, {"t/tmp/tags" => 1}) || diag explain \%pages;
+is_deeply(\@del, []) || diag explain \@del;
 ok(-s "t/tmp/tags.mdwn");
 
 # needs creating because of an attachment
 ok(! exists $wikistate{autoindex}{deleted}{attached});
+%pages = ();
+@del = ();
+is($autofiles{"attached.mdwn"}{plugin}, "autoindex");
+IkiWiki::gen_autofile("attached.mdwn", \%pages, \@del);
+is_deeply(\%pages, {"t/tmp/attached" => 1}) || diag explain \%pages;
+is_deeply(\@del, []) || diag explain \@del;
 ok(-s "t/tmp/attached.mdwn");
 
 1;
-- 
2.39.5