2 package IkiWiki::Plugin::darcs;
9 hook(type => "checkconfig", id => "darcs", call => \&checkconfig);
10 hook(type => "getsetup", id => "darcs", call => \&getsetup);
11 hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
12 hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
13 hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
14 hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
15 hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
16 hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
17 hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
18 hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
19 hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
20 hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
23 sub silentsystem (@) {
24 open(SAVED_STDOUT, ">&STDOUT");
25 open(STDOUT, ">/dev/null");
27 open(STDOUT, ">&SAVED_STDOUT");
31 sub darcs_info ($$$) {
34 my $file = shift; # Relative to the repodir.
36 my $child = open(DARCS_CHANGES, "-|");
38 exec('darcs', 'changes', '--repodir', $repodir, '--xml-output', $file) or
39 error("failed to run 'darcs changes'");
42 # Brute force for now. :-/
43 while (<DARCS_CHANGES>) {
44 last if /^<\/created_as>$/;
46 ($_) = <DARCS_CHANGES> =~ /$field=\'([^\']+)/;
47 $field eq 'hash' and s/\.gz//; # Strip away the '.gz' from 'hash'es.
58 my $child = open(DARCS_MANIFEST, "-|");
60 exec('darcs', 'query', 'manifest', '--repodir', $repodir) or
61 error("failed to run 'darcs query manifest'");
64 while (<DARCS_MANIFEST>) {
65 $found = 1, last if /^(\.\/)?$file$/;
67 close(DARCS_MANIFEST) or error("'darcs query manifest' exited " . $?);
73 my $file = shift; # Relative to the repodir.
74 my $repodir = $config{srcdir};
76 return "" if (! file_in_vc($repodir, $file));
77 my $hash = darcs_info('hash', $repodir, $file);
78 return defined $hash ? $hash : "";
82 if (defined $config{darcs_wrapper} && length $config{darcs_wrapper}) {
83 push @{$config{wrappers}}, {
84 wrapper => $config{darcs_wrapper},
85 wrappermode => (defined $config{darcs_wrappermode} ? $config{darcs_wrappermode} : "06755"),
93 safe => 0, # rcs plugin
98 example => "/darcs/repo/_darcs/ikiwiki-wrapper",
99 description => "wrapper to generate (set as master repo apply hook)",
103 darcs_wrappermode => {
106 description => "mode for darcs_wrapper (can safely be made suid)",
112 example => "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filehistory;f=[[file]]",
113 description => "darcsweb url to show file history ([[file]] substituted)",
119 example => "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filediff;h=[[hash]];f=[[file]]",
120 description => "darcsweb url to show a diff ([[hash]] and [[file]] substituted)",
127 silentsystem('darcs', "pull", "--repodir", $config{srcdir}, "-qa")
130 sub rcs_prepedit ($) {
131 # Prepares to edit a file under revision control. Returns a token that
132 # must be passed to rcs_commit() when the file is to be commited. For us,
133 # this token the hash value of the latest patch that modifies the file,
134 # i.e. something like its current revision.
136 my $file = shift; # Relative to the repodir.
137 my $rev = darcs_rev($file);
141 sub rcs_commit ($$$;$$) {
142 # Commit the page. Returns 'undef' on success and a version of the page
143 # with conflict markers on failure.
145 my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
147 # Compute if the "revision" of $file changed.
148 my $changed = darcs_rev($file) ne $rcstoken;
150 # Yes, the following is a bit convoluted.
152 # TODO. Invent a better, non-conflicting name.
153 rename("$config{srcdir}/$file", "$config{srcdir}/$file.save") or
154 error("failed to rename $file to $file.save: $!");
156 # Roll the repository back to $rcstoken.
158 # TODO. Can we be sure that no changes are lost? I think that
159 # we can, if we make sure that the 'darcs push' below will always
162 # We need to revert everything as 'darcs obliterate' might choke
164 # TODO: 'yes | ...' needed? Doesn't seem so.
165 silentsystem('darcs', "revert", "--repodir", $config{srcdir}, "--all") == 0 ||
166 error("'darcs revert' failed");
167 # Remove all patches starting at $rcstoken.
168 my $child = open(DARCS_OBLITERATE, "|-");
170 open(STDOUT, ">/dev/null");
171 exec('darcs', "obliterate", "--repodir", $config{srcdir},
172 "--match", "hash " . $rcstoken) and
173 error("'darcs obliterate' failed");
175 1 while print DARCS_OBLITERATE "y";
176 close(DARCS_OBLITERATE);
177 # Restore the $rcstoken one.
178 silentsystem('darcs', "pull", "--quiet", "--repodir", $config{srcdir},
179 "--match", "hash " . $rcstoken, "--all") == 0 ||
180 error("'darcs pull' failed");
182 # We're back at $rcstoken. Re-install the modified file.
183 rename("$config{srcdir}/$file.save", "$config{srcdir}/$file") or
184 error("failed to rename $file.save to $file: $!");
187 # Record the changes.
190 $author = "$user\@web";
192 elsif (defined $ipaddr) {
193 $author = "$ipaddr\@web";
196 $author = "anon\@web";
198 if (!defined $message || !length($message)) {
199 $message = "empty message";
201 silentsystem('darcs', 'record', '--repodir', $config{srcdir}, '--all',
202 '-m', $message, '--author', $author, $file) == 0 ||
203 error("'darcs record' failed");
205 # Update the repository by pulling from the default repository, which is
207 silentsystem('darcs', "pull", "--quiet", "--repodir", $config{srcdir},
208 "--all") == 0 || error("'darcs pull' failed");
210 # If this updating yields any conflicts, we'll record them now to resolve
211 # them. If nothing is recorded, there are no conflicts.
212 $rcstoken = darcs_rev($file);
213 # TODO: Use only the first line here, i.e. only the patch name?
214 writefile("$file.log", $config{srcdir}, 'resolve conflicts: ' . $message);
215 silentsystem('darcs', 'record', '--repodir', $config{srcdir}, '--all',
216 '-m', 'resolve conflicts: ' . $message, '--author', $author, $file) == 0 ||
217 error("'darcs record' failed");
218 my $conflicts = darcs_rev($file) ne $rcstoken;
219 unlink("$config{srcdir}/$file.log") or
220 error("failed to remove '$file.log'");
222 # Push the changes to the main repository.
223 silentsystem('darcs', 'push', '--quiet', '--repodir', $config{srcdir}, '--all') == 0 ||
224 error("'darcs push' failed");
228 my $document = readfile("$config{srcdir}/$file");
229 # Try to leave everything in a consistent state.
230 # TODO: 'yes | ...' needed? Doesn't seem so.
231 silentsystem('darcs', "revert", "--repodir", $config{srcdir}, "--all") == 0 ||
232 warn("'darcs revert' failed");
240 sub rcs_commit_staged($$$) {
241 my ($message, $user, $ipaddr) = @_;
245 $author = "$user\@web";
247 elsif (defined $ipaddr) {
248 $author = "$ipaddr\@web";
251 $author = "anon\@web";
253 if (!defined $message || !length($message)) {
254 $message = "empty message";
257 silentsystem('darcs', "record", "--repodir", $config{srcdir}, "-a", "-A", $author,
258 "-m", $message) == 0 || error("'darcs record' failed");
260 # Push the changes to the main repository.
261 silentsystem('darcs', 'push', '--quiet', '--repodir', $config{srcdir}, '--all') == 0 ||
262 error("'darcs push' failed");
269 my $file = shift; # Relative to the repodir.
271 if(! file_in_vc($config{srcdir}, $file)) {
272 # Intermediate directories will be added automagically.
273 system('darcs', 'add', '--quiet', '--repodir', $config{srcdir},
274 '--boring', $file) == 0 || error("'darcs add' failed");
279 my $file = shift; # Relative to the repodir.
281 unlink($config{srcdir}.'/'.$file);
284 sub rcs_rename ($$) {
285 my $a = shift; # Relative to the repodir.
286 my $b = shift; # Relative to the repodir.
288 system('darcs', 'mv', '--repodir', $config{srcdir}, $a, $b) == 0 ||
289 error("'darcs mv' failed");
292 sub rcs_recentchanges ($) {
296 eval q{use Date::Parse};
297 eval q{use XML::Simple};
299 my $repodir=$config{srcdir};
301 debug("darcs recent changes: $num");
303 my $child = open(LOG, "-|");
305 $ENV{"DARCS_DONT_ESCAPE_ANYTHING"}=1;
306 exec("darcs", "changes", "--xml",
308 "--repodir", "$repodir",
310 || error("'darcs changes' failed to run");
313 $data .= $_ while(<LOG>);
316 my $log = XMLin($data, ForceArray => 1);
318 debug("parsing recent changes...");
319 foreach my $patch (@{$log->{patch}}) {
320 my $date=$patch->{local_date};
321 my $hash=$patch->{hash};
322 my $when=str2time($date);
323 my (@pages, @files, @pg);
324 push @pages, $_ for (@{$patch->{summary}->[0]->{modify_file}});
325 push @pages, $_ for (@{$patch->{summary}->[0]->{add_file}});
326 push @pages, $_ for (@{$patch->{summary}->[0]->{remove_file}});
327 foreach my $f (@pages) {
328 $f = $f->{content} if ref $f;
329 $f =~ s,^\s+,,; $f =~ s,\s+$,,; # cut whitespace
333 foreach my $p (@{$patch->{summary}->[0]->{move}}) {
334 push @files, $p->{from};
337 foreach my $f (@files) {
338 my $d = defined $config{'diffurl'} ? $config{'diffurl'} : "";
339 $d =~ s/\[\[file\]\]/$f/go;
340 $d =~ s/\[\[hash\]\]/$hash/go;
343 debug("diffurl: $d");
345 page => pagename($f),
349 next unless (scalar @pg > 0);
350 debug("recent change: " . $patch->{name}[0] . " ("
351 . scalar @pg . " changes)");
354 push @message, { line => $_ } foreach (@{$patch->{name}});
357 if ($patch->{author} =~ /\@web$/) {
361 $committype = "darcs";
365 rev => $patch->{hash},
366 user => $patch->{author},
367 committype => $committype,
369 message => [@message],
380 foreach my $line (silentsystem("darcs", "diff", "--match", "hash ".$rev)) {
381 if (@lines || $line=~/^diff/) {
382 push @lines, $line."\n";
389 return join("", @lines);
393 sub rcs_getctime ($) {
396 eval q{use Date::Parse};
397 eval q{use XML::Simple};
400 # Sigh... doing things the hard way again
401 my $repodir=$config{srcdir};
403 my $filer=substr($file, length($repodir));
406 my $child = open(LOG, "-|");
408 exec("darcs", "changes", "--xml", "--reverse",
409 "--repodir", "$repodir", "$filer")
410 || error("'darcs changes $filer' failed to run");
420 my $log = XMLin($data, ForceArray => 1);
422 my $datestr = $log->{patch}[0]->{local_date};
424 if (! defined $datestr) {
425 warn "failed to get ctime for $filer";
429 my $date = str2time($datestr);
431 debug("ctime for '$file': ". localtime($date));