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 "" unless 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
99 example => "/darcs/repo/_darcs/ikiwiki-wrapper",
100 description => "wrapper to generate (set as master repo apply hook)",
104 darcs_wrappermode => {
107 description => "mode for darcs_wrapper (can safely be made suid)",
113 example => "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filehistory;f=[[file]]",
114 description => "darcsweb url to show file history ([[file]] substituted)",
120 example => "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filediff;h=[[hash]];f=[[file]]",
121 description => "darcsweb url to show a diff ([[hash]] and [[file]] substituted)",
128 silentsystem('darcs', "pull", "--repodir", $config{srcdir}, "-qa")
131 sub rcs_prepedit ($) {
132 # Prepares to edit a file under revision control. Returns a token that
133 # must be passed to rcs_commit() when the file is to be commited. For us,
134 # this token the hash value of the latest patch that modifies the file,
135 # i.e. something like its current revision.
137 my $file = shift; # Relative to the repodir.
138 my $rev = darcs_rev($file);
142 sub rcs_commit ($$$;$$) {
143 # Commit the page. Returns 'undef' on success and a version of the page
144 # with conflict markers on failure.
146 my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
148 # Compute if the "revision" of $file changed.
149 my $changed = darcs_rev($file) ne $rcstoken;
151 # Yes, the following is a bit convoluted.
153 # TODO. Invent a better, non-conflicting name.
154 rename("$config{srcdir}/$file", "$config{srcdir}/$file.save") or
155 error("failed to rename $file to $file.save: $!");
157 # Roll the repository back to $rcstoken.
159 # TODO. Can we be sure that no changes are lost? I think that
160 # we can, if we make sure that the 'darcs push' below will always
163 # We need to revert everything as 'darcs obliterate' might choke
165 # TODO: 'yes | ...' needed? Doesn't seem so.
166 silentsystem('darcs', "revert", "--repodir", $config{srcdir}, "--all") == 0 ||
167 error("'darcs revert' failed");
168 # Remove all patches starting at $rcstoken.
169 my $child = open(DARCS_OBLITERATE, "|-");
171 open(STDOUT, ">/dev/null");
172 exec('darcs', "obliterate", "--repodir", $config{srcdir},
173 "--match", "hash " . $rcstoken) and
174 error("'darcs obliterate' failed");
176 1 while print DARCS_OBLITERATE "y";
177 close(DARCS_OBLITERATE);
178 # Restore the $rcstoken one.
179 silentsystem('darcs', "pull", "--quiet", "--repodir", $config{srcdir},
180 "--match", "hash " . $rcstoken, "--all") == 0 ||
181 error("'darcs pull' failed");
183 # We're back at $rcstoken. Re-install the modified file.
184 rename("$config{srcdir}/$file.save", "$config{srcdir}/$file") or
185 error("failed to rename $file.save to $file: $!");
188 # Record the changes.
191 $author = "$user\@web";
193 elsif (defined $ipaddr) {
194 $author = "$ipaddr\@web";
197 $author = "anon\@web";
199 if (!defined $message || !length($message)) {
200 $message = "empty message";
202 silentsystem('darcs', 'record', '--repodir', $config{srcdir}, '--all',
203 '-m', $message, '--author', $author, $file) == 0 ||
204 error("'darcs record' failed");
206 # Update the repository by pulling from the default repository, which is
208 silentsystem('darcs', "pull", "--quiet", "--repodir", $config{srcdir},
209 "--all") == 0 || error("'darcs pull' failed");
211 # If this updating yields any conflicts, we'll record them now to resolve
212 # them. If nothing is recorded, there are no conflicts.
213 $rcstoken = darcs_rev($file);
214 # TODO: Use only the first line here, i.e. only the patch name?
215 writefile("$file.log", $config{srcdir}, 'resolve conflicts: ' . $message);
216 silentsystem('darcs', 'record', '--repodir', $config{srcdir}, '--all',
217 '-m', 'resolve conflicts: ' . $message, '--author', $author, $file) == 0 ||
218 error("'darcs record' failed");
219 my $conflicts = darcs_rev($file) ne $rcstoken;
220 unlink("$config{srcdir}/$file.log") or
221 error("failed to remove '$file.log'");
223 # Push the changes to the main repository.
224 silentsystem('darcs', 'push', '--quiet', '--repodir', $config{srcdir}, '--all') == 0 ||
225 error("'darcs push' failed");
229 my $document = readfile("$config{srcdir}/$file");
230 # Try to leave everything in a consistent state.
231 # TODO: 'yes | ...' needed? Doesn't seem so.
232 silentsystem('darcs', "revert", "--repodir", $config{srcdir}, "--all") == 0 ||
233 warn("'darcs revert' failed");
241 sub rcs_commit_staged($$$) {
242 my ($message, $user, $ipaddr) = @_;
246 $author = "$user\@web";
248 elsif (defined $ipaddr) {
249 $author = "$ipaddr\@web";
252 $author = "anon\@web";
254 if (!defined $message || !length($message)) {
255 $message = "empty message";
258 silentsystem('darcs', "record", "--repodir", $config{srcdir}, "-a", "-A", $author,
259 "-m", $message) == 0 || error("'darcs record' failed");
261 # Push the changes to the main repository.
262 silentsystem('darcs', 'push', '--quiet', '--repodir', $config{srcdir}, '--all') == 0 ||
263 error("'darcs push' failed");
270 my $file = shift; # Relative to the repodir.
272 if(! file_in_vc($config{srcdir}, $file)) {
273 # Intermediate directories will be added automagically.
274 system('darcs', 'add', '--quiet', '--repodir', $config{srcdir},
275 '--boring', $file) == 0 || error("'darcs add' failed");
280 my $file = shift; # Relative to the repodir.
282 unlink($config{srcdir}.'/'.$file);
285 sub rcs_rename ($$) {
286 my $a = shift; # Relative to the repodir.
287 my $b = shift; # Relative to the repodir.
289 system('darcs', 'mv', '--repodir', $config{srcdir}, $a, $b) == 0 ||
290 error("'darcs mv' failed");
293 sub rcs_recentchanges ($) {
297 eval q{use Date::Parse};
298 eval q{use XML::Simple};
300 my $repodir=$config{srcdir};
302 my $child = open(LOG, "-|");
304 $ENV{"DARCS_DONT_ESCAPE_ANYTHING"}=1;
305 exec("darcs", "changes", "--xml",
307 "--repodir", "$repodir",
309 || error("'darcs changes' failed to run");
312 $data .= $_ while(<LOG>);
315 my $log = XMLin($data, ForceArray => 1);
317 foreach my $patch (@{$log->{patch}}) {
318 my $date=$patch->{local_date};
319 my $hash=$patch->{hash};
320 my $when=str2time($date);
321 my (@pages, @files, @pg);
322 push @pages, $_ foreach (@{$patch->{summary}->[0]->{modify_file}});
323 push @pages, $_ foreach (@{$patch->{summary}->[0]->{add_file}});
324 push @pages, $_ foreach (@{$patch->{summary}->[0]->{remove_file}});
325 foreach my $f (@pages) {
326 $f = $f->{content} if ref $f;
327 $f =~ s,^\s+,,; $f =~ s,\s+$,,; # cut whitespace
331 foreach my $p (@{$patch->{summary}->[0]->{move}}) {
332 push @files, $p->{from};
335 foreach my $f (@files) {
336 my $d = defined $config{'diffurl'} ? $config{'diffurl'} : "";
337 $d =~ s/\[\[file\]\]/$f/go;
338 $d =~ s/\[\[hash\]\]/$hash/go;
341 page => pagename($f),
345 next unless (scalar @pg > 0);
348 push @message, { line => $_ } foreach (@{$patch->{name}});
352 if ($patch->{author} =~ /(.*)\@web$/) {
357 $author=$patch->{author};
358 $committype = "darcs";
362 rev => $patch->{hash},
364 committype => $committype,
366 message => [@message],
377 foreach my $line (silentsystem("darcs", "diff", "--match", "hash ".$rev)) {
378 if (@lines || $line=~/^diff/) {
379 push @lines, $line."\n";
386 return join("", @lines);
390 sub rcs_getctime ($) {
393 eval q{use Date::Parse};
394 eval q{use XML::Simple};
397 my $filer=substr($file, length($config{srcdir}));
400 my $child = open(LOG, "-|");
402 exec("darcs", "changes", "--xml", "--reverse",
403 "--repodir", $config{srcdir}, $filer)
404 || error("'darcs changes $filer' failed to run");
414 my $log = XMLin($data, ForceArray => 1);
416 my $datestr = $log->{patch}[0]->{local_date};
418 if (! defined $datestr) {
419 warn "failed to get ctime for $filer";
423 my $date = str2time($datestr);
425 debug("ctime for '$file': ". localtime($date));