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
);
21 hook
(type
=> "rcs", id
=> "rcs_getmtime", call
=> \
&rcs_getmtime
);
24 sub silentsystem
(@
) {
25 open(SAVED_STDOUT
, ">&STDOUT");
26 open(STDOUT
, ">/dev/null");
28 open(STDOUT
, ">&SAVED_STDOUT");
32 sub darcs_info
($$$) {
35 my $file = shift; # Relative to the repodir.
37 my $child = open(DARCS_CHANGES
, "-|");
39 exec('darcs', 'changes', '--repodir', $repodir, '--xml-output', $file) or
40 error
("failed to run 'darcs changes'");
43 # Brute force for now. :-/
44 while (<DARCS_CHANGES
>) {
45 last if /^<\/created_as
>$/;
47 ($_) = <DARCS_CHANGES
> =~ /$field=\'([^\']+)/;
48 $field eq 'hash' and s/\.gz//; # Strip away the '.gz' from 'hash'es.
59 my $child = open(DARCS_MANIFEST
, "-|");
61 exec('darcs', 'query', 'manifest', '--repodir', $repodir) or
62 error
("failed to run 'darcs query manifest'");
65 while (<DARCS_MANIFEST
>) {
66 $found = 1 if /^(\.\/)?
$file$/;
68 close(DARCS_MANIFEST
) or error
("'darcs query manifest' exited " . $?
);
74 my $file = shift; # Relative to the repodir.
75 my $repodir = $config{srcdir
};
77 return "" unless file_in_vc
($repodir, $file);
78 my $hash = darcs_info
('hash', $repodir, $file);
79 return defined $hash ?
$hash : "";
83 if (defined $config{darcs_wrapper
} && length $config{darcs_wrapper
}) {
84 push @
{$config{wrappers
}}, {
85 wrapper
=> $config{darcs_wrapper
},
86 wrappermode
=> (defined $config{darcs_wrappermode
} ?
$config{darcs_wrappermode
} : "06755"),
94 safe
=> 0, # rcs plugin
100 example
=> "/darcs/repo/_darcs/ikiwiki-wrapper",
101 description
=> "wrapper to generate (set as master repo apply hook)",
105 darcs_wrappermode
=> {
108 description
=> "mode for darcs_wrapper (can safely be made suid)",
114 example
=> "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filehistory;f=[[file]]",
115 description
=> "darcsweb url to show file history ([[file]] substituted)",
121 example
=> "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filediff;h=[[hash]];f=[[file]]",
122 description
=> "darcsweb url to show a diff ([[hash]] and [[file]] substituted)",
129 silentsystem
('darcs', "pull", "--repodir", $config{srcdir
}, "-qa")
132 sub rcs_prepedit
($) {
133 # Prepares to edit a file under revision control. Returns a token that
134 # must be passed to rcs_commit() when the file is to be commited. For us,
135 # this token the hash value of the latest patch that modifies the file,
136 # i.e. something like its current revision.
138 my $file = shift; # Relative to the repodir.
139 my $rev = darcs_rev
($file);
143 sub commitauthor
(@
) {
146 my $author="anon\@web";
147 if (defined $params{session
}) {
148 if (defined $params{session
}->param("name")) {
149 return $params{session
}->param("name").'@web';
151 elsif (defined $params{session
}->remote_addr()) {
152 return $params{session
}->remote_addr().'@web';
159 # Commit the page. Returns 'undef' on success and a version of the page
160 # with conflict markers on failure.
163 my ($file, $message, $token) =
164 ($params{file
}, $params{message
}, $params{token
});
166 # Compute if the "revision" of $file changed.
167 my $changed = darcs_rev
($file) ne $token;
169 # Yes, the following is a bit convoluted.
171 # TODO. Invent a better, non-conflicting name.
172 rename("$config{srcdir}/$file", "$config{srcdir}/$file.save") or
173 error
("failed to rename $file to $file.save: $!");
175 # Roll the repository back to $token.
177 # TODO. Can we be sure that no changes are lost? I think that
178 # we can, if we make sure that the 'darcs push' below will always
181 # We need to revert everything as 'darcs obliterate' might choke
183 # TODO: 'yes | ...' needed? Doesn't seem so.
184 silentsystem
('darcs', "revert", "--repodir", $config{srcdir
}, "--all") == 0 ||
185 error
("'darcs revert' failed");
186 # Remove all patches starting at $token.
187 my $child = open(DARCS_OBLITERATE
, "|-");
189 open(STDOUT
, ">/dev/null");
190 exec('darcs', "obliterate", "--repodir", $config{srcdir
},
191 "--match", "hash " . $token) and
192 error
("'darcs obliterate' failed");
194 1 while print DARCS_OBLITERATE
"y";
195 close(DARCS_OBLITERATE
);
196 # Restore the $token one.
197 silentsystem
('darcs', "pull", "--quiet", "--repodir", $config{srcdir
},
198 "--match", "hash " . $token, "--all") == 0 ||
199 error
("'darcs pull' failed");
201 # We're back at $token. Re-install the modified file.
202 rename("$config{srcdir}/$file.save", "$config{srcdir}/$file") or
203 error
("failed to rename $file.save to $file: $!");
206 # Record the changes.
207 my $author=commitauthor
(%params);
208 if (!defined $message || !length($message)) {
209 $message = "empty message";
211 silentsystem
('darcs', 'record', '--repodir', $config{srcdir
}, '--all',
212 '-m', $message, '--author', $author, $file) == 0 ||
213 error
("'darcs record' failed");
215 # Update the repository by pulling from the default repository, which is
217 silentsystem
('darcs', "pull", "--quiet", "--repodir", $config{srcdir
},
218 "--all") == 0 || error
("'darcs pull' failed");
220 # If this updating yields any conflicts, we'll record them now to resolve
221 # them. If nothing is recorded, there are no conflicts.
222 $token = darcs_rev
($file);
223 # TODO: Use only the first line here, i.e. only the patch name?
224 writefile
("$file.log", $config{srcdir
}, 'resolve conflicts: ' . $message);
225 silentsystem
('darcs', 'record', '--repodir', $config{srcdir
}, '--all',
226 '-m', 'resolve conflicts: ' . $message, '--author', $author, $file) == 0 ||
227 error
("'darcs record' failed");
228 my $conflicts = darcs_rev
($file) ne $token;
229 unlink("$config{srcdir}/$file.log") or
230 error
("failed to remove '$file.log'");
232 # Push the changes to the main repository.
233 silentsystem
('darcs', 'push', '--quiet', '--repodir', $config{srcdir
}, '--all') == 0 ||
234 error
("'darcs push' failed");
238 my $document = readfile
("$config{srcdir}/$file");
239 # Try to leave everything in a consistent state.
240 # TODO: 'yes | ...' needed? Doesn't seem so.
241 silentsystem
('darcs', "revert", "--repodir", $config{srcdir
}, "--all") == 0 ||
242 warn("'darcs revert' failed");
250 sub rcs_commit_staged
(@
) {
253 my $author=commitauthor
(%params);
254 if (!defined $params{message
} || !length($params{message
})) {
255 $params{message
} = "empty message";
258 silentsystem
('darcs', "record", "--repodir", $config{srcdir
},
260 "-m", $params{message
},
261 ) == 0 || error
("'darcs record' failed");
263 # Push the changes to the main repository.
264 silentsystem
('darcs', 'push', '--quiet', '--repodir', $config{srcdir
}, '--all') == 0 ||
265 error
("'darcs push' failed");
272 my $file = shift; # Relative to the repodir.
274 if(! file_in_vc
($config{srcdir
}, $file)) {
275 # Intermediate directories will be added automagically.
276 system('darcs', 'add', '--quiet', '--repodir', $config{srcdir
},
277 '--boring', $file) == 0 || error
("'darcs add' failed");
282 my $file = shift; # Relative to the repodir.
284 unlink($config{srcdir
}.'/'.$file);
287 sub rcs_rename
($$) {
288 my $a = shift; # Relative to the repodir.
289 my $b = shift; # Relative to the repodir.
291 system('darcs', 'mv', '--repodir', $config{srcdir
}, $a, $b) == 0 ||
292 error
("'darcs mv' failed");
295 sub rcs_recentchanges
($) {
299 eval q{use Date::Parse};
300 eval q{use XML::Simple};
302 my $repodir=$config{srcdir
};
304 my $child = open(LOG
, "-|");
306 $ENV{"DARCS_DONT_ESCAPE_ANYTHING"}=1;
307 exec("darcs", "changes", "--xml",
309 "--repodir", "$repodir",
311 || error
("'darcs changes' failed to run");
314 $data .= $_ while(<LOG
>);
317 my $log = XMLin
($data, ForceArray
=> 1);
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, $_ foreach (@
{$patch->{summary
}->[0]->{modify_file
}});
325 push @pages, $_ foreach (@
{$patch->{summary
}->[0]->{add_file
}});
326 push @pages, $_ foreach (@
{$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 page
=> pagename
($f),
347 next unless (scalar @pg > 0);
350 push @message, { line
=> $_ } foreach (@
{$patch->{name
}});
354 if ($patch->{author
} =~ /(.*)\@web$/) {
359 $author=$patch->{author
};
360 $committype = "darcs";
364 rev
=> $patch->{hash
},
366 committype
=> $committype,
368 message
=> [@message],
380 foreach my $line (silentsystem
("darcs", "diff", "--match", "hash ".$rev)) {
381 if (@lines || $line=~/^diff/) {
382 last if defined $maxlines && @lines == $maxlines;
383 push @lines, $line."\n";
390 return join("", @lines);
394 sub rcs_getctime
($) {
397 eval q{use Date::Parse};
398 eval q{use XML::Simple};
401 my $child = open(LOG
, "-|");
403 exec("darcs", "changes", "--xml", "--reverse",
404 "--repodir", $config{srcdir
}, $file)
405 || error
("'darcs changes $file' failed to run");
415 my $log = XMLin
($data, ForceArray
=> 1);
417 my $datestr = $log->{patch
}[0]->{local_date
};
419 if (! defined $datestr) {
420 warn "failed to get ctime for $file";
424 my $date = str2time
($datestr);
426 debug
("ctime for '$file': ". localtime($date));
431 sub rcs_getmtime
($) {
432 error
"rcs_getmtime is not implemented for darcs\n"; # TODO