2 package IkiWiki::Plugin::darcs;
6 use URI::Escape q{uri_escape_utf8};
10 hook(type => "checkconfig", id => "darcs", call => \&checkconfig);
11 hook(type => "getsetup", id => "darcs", call => \&getsetup);
12 hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
13 hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
14 hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
15 hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
16 hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
17 hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
18 hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
19 hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
20 hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
21 hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
22 hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
25 sub silentsystem (@) {
26 open(SAVED_STDOUT, ">&STDOUT");
27 open(STDOUT, ">/dev/null");
29 open(STDOUT, ">&SAVED_STDOUT");
33 sub darcs_info ($$$) {
36 my $file = shift; # Relative to the repodir.
38 my $child = open(DARCS_CHANGES, "-|");
40 exec('darcs', 'changes', '--repodir', $repodir, '--xml-output', $file) or
41 error("failed to run 'darcs changes'");
44 # Brute force for now. :-/
45 while (<DARCS_CHANGES>) {
46 last if /^<\/created_as>$/;
48 ($_) = <DARCS_CHANGES> =~ /$field=\'([^\']+)/;
49 $field eq 'hash' and s/\.gz//; # Strip away the '.gz' from 'hash'es.
60 my $child = open(DARCS_MANIFEST, "-|");
62 exec('darcs', 'query', 'manifest', '--repodir', $repodir) or
63 error("failed to run 'darcs query manifest'");
66 while (<DARCS_MANIFEST>) {
67 $found = 1 if /^(\.\/)?$file$/;
69 close(DARCS_MANIFEST) or error("'darcs query manifest' exited " . $?);
75 my $file = shift; # Relative to the repodir.
76 my $repodir = $config{srcdir};
78 return "" unless file_in_vc($repodir, $file);
79 my $hash = darcs_info('hash', $repodir, $file);
80 return defined $hash ? $hash : "";
84 if (defined $config{darcs_wrapper} && length $config{darcs_wrapper}) {
85 push @{$config{wrappers}}, {
86 wrapper => $config{darcs_wrapper},
87 wrappermode => (defined $config{darcs_wrappermode} ? $config{darcs_wrappermode} : "06755"),
95 safe => 0, # rcs plugin
101 example => "/darcs/repo/_darcs/ikiwiki-wrapper",
102 description => "wrapper to generate (set as master repo apply hook)",
106 darcs_wrappermode => {
109 description => "mode for darcs_wrapper (can safely be made suid)",
115 example => "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filehistory;f=[[file]]",
116 description => "darcsweb url to show file history ([[file]] substituted)",
122 example => "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filediff;h=[[hash]];f=[[file]]",
123 description => "darcsweb url to show a diff ([[hash]] and [[file]] substituted)",
130 silentsystem('darcs', "pull", "--repodir", $config{srcdir}, "-qa")
133 sub rcs_prepedit ($) {
134 # Prepares to edit a file under revision control. Returns a token that
135 # must be passed to rcs_commit() when the file is to be commited. For us,
136 # this token the hash value of the latest patch that modifies the file,
137 # i.e. something like its current revision.
139 my $file = shift; # Relative to the repodir.
140 my $rev = darcs_rev($file);
144 sub commitauthor (@) {
147 my $author="anon\@web";
148 if (defined $params{session}) {
149 if (defined $params{session}->param("name")) {
150 return $params{session}->param("name").'@web';
152 elsif (defined $params{session}->remote_addr()) {
153 return $params{session}->remote_addr().'@web';
160 # Commit the page. Returns 'undef' on success and a version of the page
161 # with conflict markers on failure.
164 my ($file, $message, $token) =
165 ($params{file}, $params{message}, $params{token});
167 # Compute if the "revision" of $file changed.
168 my $changed = darcs_rev($file) ne $token;
170 # Yes, the following is a bit convoluted.
172 # TODO. Invent a better, non-conflicting name.
173 rename("$config{srcdir}/$file", "$config{srcdir}/$file.save") or
174 error("failed to rename $file to $file.save: $!");
176 # Roll the repository back to $token.
178 # TODO. Can we be sure that no changes are lost? I think that
179 # we can, if we make sure that the 'darcs push' below will always
182 # We need to revert everything as 'darcs obliterate' might choke
184 # TODO: 'yes | ...' needed? Doesn't seem so.
185 silentsystem('darcs', "revert", "--repodir", $config{srcdir}, "--all") == 0 ||
186 error("'darcs revert' failed");
187 # Remove all patches starting at $token.
188 my $child = open(DARCS_OBLITERATE, "|-");
190 open(STDOUT, ">/dev/null");
191 exec('darcs', "obliterate", "--repodir", $config{srcdir},
192 "--match", "hash " . $token) and
193 error("'darcs obliterate' failed");
195 1 while print DARCS_OBLITERATE "y";
196 close(DARCS_OBLITERATE);
197 # Restore the $token one.
198 silentsystem('darcs', "pull", "--quiet", "--repodir", $config{srcdir},
199 "--match", "hash " . $token, "--all") == 0 ||
200 error("'darcs pull' failed");
202 # We're back at $token. Re-install the modified file.
203 rename("$config{srcdir}/$file.save", "$config{srcdir}/$file") or
204 error("failed to rename $file.save to $file: $!");
207 # Record the changes.
208 my $author=commitauthor(%params);
209 if (!defined $message || !length($message)) {
210 $message = "empty message";
212 silentsystem('darcs', 'record', '--repodir', $config{srcdir}, '--all',
213 '-m', $message, '--author', $author, $file) == 0 ||
214 error("'darcs record' failed");
216 # Update the repository by pulling from the default repository, which is
218 silentsystem('darcs', "pull", "--quiet", "--repodir", $config{srcdir},
219 "--all") == 0 || error("'darcs pull' failed");
221 # If this updating yields any conflicts, we'll record them now to resolve
222 # them. If nothing is recorded, there are no conflicts.
223 $token = darcs_rev($file);
224 # TODO: Use only the first line here, i.e. only the patch name?
225 writefile("$file.log", $config{srcdir}, 'resolve conflicts: ' . $message);
226 silentsystem('darcs', 'record', '--repodir', $config{srcdir}, '--all',
227 '-m', 'resolve conflicts: ' . $message, '--author', $author, $file) == 0 ||
228 error("'darcs record' failed");
229 my $conflicts = darcs_rev($file) ne $token;
230 unlink("$config{srcdir}/$file.log") or
231 error("failed to remove '$file.log'");
233 # Push the changes to the main repository.
234 silentsystem('darcs', 'push', '--quiet', '--repodir', $config{srcdir}, '--all') == 0 ||
235 error("'darcs push' failed");
239 my $document = readfile("$config{srcdir}/$file");
240 # Try to leave everything in a consistent state.
241 # TODO: 'yes | ...' needed? Doesn't seem so.
242 silentsystem('darcs', "revert", "--repodir", $config{srcdir}, "--all") == 0 ||
243 warn("'darcs revert' failed");
251 sub rcs_commit_staged (@) {
254 my $author=commitauthor(%params);
255 if (!defined $params{message} || !length($params{message})) {
256 $params{message} = "empty message";
259 silentsystem('darcs', "record", "--repodir", $config{srcdir},
261 "-m", $params{message},
262 ) == 0 || error("'darcs record' failed");
264 # Push the changes to the main repository.
265 silentsystem('darcs', 'push', '--quiet', '--repodir', $config{srcdir}, '--all') == 0 ||
266 error("'darcs push' failed");
273 my $file = shift; # Relative to the repodir.
275 if(! file_in_vc($config{srcdir}, $file)) {
276 # Intermediate directories will be added automagically.
277 system('darcs', 'add', '--quiet', '--repodir', $config{srcdir},
278 '--boring', $file) == 0 || error("'darcs add' failed");
283 my $file = shift; # Relative to the repodir.
285 unlink($config{srcdir}.'/'.$file);
288 sub rcs_rename ($$) {
289 my $a = shift; # Relative to the repodir.
290 my $b = shift; # Relative to the repodir.
292 system('darcs', 'mv', '--repodir', $config{srcdir}, $a, $b) == 0 ||
293 error("'darcs mv' failed");
296 sub rcs_recentchanges ($) {
300 eval q{use Date::Parse};
301 eval q{use XML::Simple};
303 my $repodir=$config{srcdir};
305 my $child = open(LOG, "-|");
307 $ENV{"DARCS_DONT_ESCAPE_ANYTHING"}=1;
308 exec("darcs", "changes", "--xml",
310 "--repodir", "$repodir",
312 || error("'darcs changes' failed to run");
315 $data .= $_ while(<LOG>);
318 my $log = XMLin($data, ForceArray => 1);
320 foreach my $patch (@{$log->{patch}}) {
321 my $date=$patch->{local_date};
322 my $hash=$patch->{hash};
323 my $when=str2time($date);
324 my (@pages, @files, @pg);
325 push @pages, $_ foreach (@{$patch->{summary}->[0]->{modify_file}});
326 push @pages, $_ foreach (@{$patch->{summary}->[0]->{add_file}});
327 push @pages, $_ foreach (@{$patch->{summary}->[0]->{remove_file}});
328 foreach my $f (@pages) {
329 $f = $f->{content} if ref $f;
330 $f =~ s,^\s+,,; $f =~ s,\s+$,,; # cut whitespace
334 foreach my $p (@{$patch->{summary}->[0]->{move}}) {
335 push @files, $p->{from};
338 foreach my $f (@files) {
339 my $d = defined $config{'diffurl'} ? $config{'diffurl'} : "";
340 my $ef = uri_escape_utf8($f);
341 $d =~ s/\[\[file\]\]/$ef/go;
342 $d =~ s/\[\[hash\]\]/$hash/go;
345 page => pagename($f),
349 next unless (scalar @pg > 0);
352 push @message, { line => $_ } foreach (@{$patch->{name}});
356 if ($patch->{author} =~ /(.*)\@web$/) {
361 $author=$patch->{author};
362 $committype = "darcs";
366 rev => $patch->{hash},
368 committype => $committype,
370 message => [@message],
382 my $repodir=$config{srcdir};
383 foreach my $line (`darcs diff --repodir $repodir --match 'hash $rev'`) {
384 if (@lines || $line=~/^diff/) {
385 last if defined $maxlines && @lines == $maxlines;
386 push @lines, $line."\n";
393 return join("", @lines);
397 sub rcs_getctime ($) {
400 eval q{use Date::Parse};
401 eval q{use XML::Simple};
404 my $child = open(LOG, "-|");
406 exec("darcs", "changes", "--xml", "--reverse",
407 "--repodir", $config{srcdir}, $file)
408 || error("'darcs changes $file' failed to run");
418 my $log = XMLin($data, ForceArray => 1);
420 my $datestr = $log->{patch}[0]->{local_date};
422 if (! defined $datestr) {
423 warn "failed to get ctime for $file";
427 my $date = str2time($datestr);
429 debug("ctime for '$file': ". localtime($date));
434 sub rcs_getmtime ($) {
435 error "rcs_getmtime is not implemented for darcs\n"; # TODO