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, last 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 rcs_commit ($$$;$$) {
144 # Commit the page. Returns 'undef' on success and a version of the page
145 # with conflict markers on failure.
147 my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
149 # Compute if the "revision" of $file changed.
150 my $changed = darcs_rev($file) ne $rcstoken;
152 # Yes, the following is a bit convoluted.
154 # TODO. Invent a better, non-conflicting name.
155 rename("$config{srcdir}/$file", "$config{srcdir}/$file.save") or
156 error("failed to rename $file to $file.save: $!");
158 # Roll the repository back to $rcstoken.
160 # TODO. Can we be sure that no changes are lost? I think that
161 # we can, if we make sure that the 'darcs push' below will always
164 # We need to revert everything as 'darcs obliterate' might choke
166 # TODO: 'yes | ...' needed? Doesn't seem so.
167 silentsystem('darcs', "revert", "--repodir", $config{srcdir}, "--all") == 0 ||
168 error("'darcs revert' failed");
169 # Remove all patches starting at $rcstoken.
170 my $child = open(DARCS_OBLITERATE, "|-");
172 open(STDOUT, ">/dev/null");
173 exec('darcs', "obliterate", "--repodir", $config{srcdir},
174 "--match", "hash " . $rcstoken) and
175 error("'darcs obliterate' failed");
177 1 while print DARCS_OBLITERATE "y";
178 close(DARCS_OBLITERATE);
179 # Restore the $rcstoken one.
180 silentsystem('darcs', "pull", "--quiet", "--repodir", $config{srcdir},
181 "--match", "hash " . $rcstoken, "--all") == 0 ||
182 error("'darcs pull' failed");
184 # We're back at $rcstoken. Re-install the modified file.
185 rename("$config{srcdir}/$file.save", "$config{srcdir}/$file") or
186 error("failed to rename $file.save to $file: $!");
189 # Record the changes.
192 $author = "$user\@web";
194 elsif (defined $ipaddr) {
195 $author = "$ipaddr\@web";
198 $author = "anon\@web";
200 if (!defined $message || !length($message)) {
201 $message = "empty message";
203 silentsystem('darcs', 'record', '--repodir', $config{srcdir}, '--all',
204 '-m', $message, '--author', $author, $file) == 0 ||
205 error("'darcs record' failed");
207 # Update the repository by pulling from the default repository, which is
209 silentsystem('darcs', "pull", "--quiet", "--repodir", $config{srcdir},
210 "--all") == 0 || error("'darcs pull' failed");
212 # If this updating yields any conflicts, we'll record them now to resolve
213 # them. If nothing is recorded, there are no conflicts.
214 $rcstoken = darcs_rev($file);
215 # TODO: Use only the first line here, i.e. only the patch name?
216 writefile("$file.log", $config{srcdir}, 'resolve conflicts: ' . $message);
217 silentsystem('darcs', 'record', '--repodir', $config{srcdir}, '--all',
218 '-m', 'resolve conflicts: ' . $message, '--author', $author, $file) == 0 ||
219 error("'darcs record' failed");
220 my $conflicts = darcs_rev($file) ne $rcstoken;
221 unlink("$config{srcdir}/$file.log") or
222 error("failed to remove '$file.log'");
224 # Push the changes to the main repository.
225 silentsystem('darcs', 'push', '--quiet', '--repodir', $config{srcdir}, '--all') == 0 ||
226 error("'darcs push' failed");
230 my $document = readfile("$config{srcdir}/$file");
231 # Try to leave everything in a consistent state.
232 # TODO: 'yes | ...' needed? Doesn't seem so.
233 silentsystem('darcs', "revert", "--repodir", $config{srcdir}, "--all") == 0 ||
234 warn("'darcs revert' failed");
242 sub rcs_commit_staged ($$$) {
243 my ($message, $user, $ipaddr) = @_;
247 $author = "$user\@web";
249 elsif (defined $ipaddr) {
250 $author = "$ipaddr\@web";
253 $author = "anon\@web";
255 if (!defined $message || !length($message)) {
256 $message = "empty message";
259 silentsystem('darcs', "record", "--repodir", $config{srcdir}, "-a", "-A", $author,
260 "-m", $message) == 0 || error("'darcs record' failed");
262 # Push the changes to the main repository.
263 silentsystem('darcs', 'push', '--quiet', '--repodir', $config{srcdir}, '--all') == 0 ||
264 error("'darcs push' failed");
271 my $file = shift; # Relative to the repodir.
273 if(! file_in_vc($config{srcdir}, $file)) {
274 # Intermediate directories will be added automagically.
275 system('darcs', 'add', '--quiet', '--repodir', $config{srcdir},
276 '--boring', $file) == 0 || error("'darcs add' failed");
281 my $file = shift; # Relative to the repodir.
283 unlink($config{srcdir}.'/'.$file);
286 sub rcs_rename ($$) {
287 my $a = shift; # Relative to the repodir.
288 my $b = shift; # Relative to the repodir.
290 system('darcs', 'mv', '--repodir', $config{srcdir}, $a, $b) == 0 ||
291 error("'darcs mv' failed");
294 sub rcs_recentchanges ($) {
298 eval q{use Date::Parse};
299 eval q{use XML::Simple};
301 my $repodir=$config{srcdir};
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 foreach my $patch (@{$log->{patch}}) {
319 my $date=$patch->{local_date};
320 my $hash=$patch->{hash};
321 my $when=str2time($date);
322 my (@pages, @files, @pg);
323 push @pages, $_ foreach (@{$patch->{summary}->[0]->{modify_file}});
324 push @pages, $_ foreach (@{$patch->{summary}->[0]->{add_file}});
325 push @pages, $_ foreach (@{$patch->{summary}->[0]->{remove_file}});
326 foreach my $f (@pages) {
327 $f = $f->{content} if ref $f;
328 $f =~ s,^\s+,,; $f =~ s,\s+$,,; # cut whitespace
332 foreach my $p (@{$patch->{summary}->[0]->{move}}) {
333 push @files, $p->{from};
336 foreach my $f (@files) {
337 my $d = defined $config{'diffurl'} ? $config{'diffurl'} : "";
338 $d =~ s/\[\[file\]\]/$f/go;
339 $d =~ s/\[\[hash\]\]/$hash/go;
342 page => pagename($f),
346 next unless (scalar @pg > 0);
349 push @message, { line => $_ } foreach (@{$patch->{name}});
353 if ($patch->{author} =~ /(.*)\@web$/) {
358 $author=$patch->{author};
359 $committype = "darcs";
363 rev => $patch->{hash},
365 committype => $committype,
367 message => [@message],
378 foreach my $line (silentsystem("darcs", "diff", "--match", "hash ".$rev)) {
379 if (@lines || $line=~/^diff/) {
380 push @lines, $line."\n";
387 return join("", @lines);
391 sub rcs_getctime ($) {
394 eval q{use Date::Parse};
395 eval q{use XML::Simple};
398 my $filer=substr($file, length($config{srcdir}));
401 my $child = open(LOG, "-|");
403 exec("darcs", "changes", "--xml", "--reverse",
404 "--repodir", $config{srcdir}, $filer)
405 || error("'darcs changes $filer' 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 $filer";
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