]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/darcs.pm
dfd193b9c1a66a1ee59f03bc15e8fa51f5c1098d
[ikiwiki.git] / IkiWiki / Plugin / darcs.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::darcs;
3
4 use warnings;
5 use strict;
6 use IkiWiki;
7
8 sub import {
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 }
22
23 sub silentsystem (@) {
24         open(SAVED_STDOUT, ">&STDOUT");
25         open(STDOUT, ">/dev/null");
26         my $ret = system @_;
27         open(STDOUT, ">&SAVED_STDOUT");
28         return $ret;
29 }
30
31 sub darcs_info ($$$) {
32         my $field = shift;
33         my $repodir = shift;
34         my $file = shift; # Relative to the repodir.
35
36         my $child = open(DARCS_CHANGES, "-|");
37         if (! $child) {
38                 exec('darcs', 'changes', '--repodir', $repodir, '--xml-output', $file) or
39                         error("failed to run 'darcs changes'");
40         }
41
42         # Brute force for now.  :-/
43         while (<DARCS_CHANGES>) {
44                 last if /^<\/created_as>$/;
45         }
46         ($_) = <DARCS_CHANGES> =~ /$field=\'([^\']+)/;
47         $field eq 'hash' and s/\.gz//; # Strip away the '.gz' from 'hash'es.
48
49         close(DARCS_CHANGES);
50
51         return $_;
52 }
53
54 sub file_in_vc($$) {
55         my $repodir = shift;
56         my $file = shift;
57
58         my $child = open(DARCS_MANIFEST, "-|");
59         if (! $child) {
60                 exec('darcs', 'query', 'manifest', '--repodir', $repodir) or
61                         error("failed to run 'darcs query manifest'");
62         }
63         my $found=0;
64         while (<DARCS_MANIFEST>) {
65                 $found = 1, last if /^(\.\/)?$file$/;
66         }
67         close(DARCS_MANIFEST) or error("'darcs query manifest' exited " . $?);
68
69         return $found;
70 }
71
72 sub darcs_rev($) {
73         my $file = shift; # Relative to the repodir.
74         my $repodir = $config{srcdir};
75
76         return "" if (! file_in_vc($repodir, $file));
77         my $hash = darcs_info('hash', $repodir, $file);
78         return defined $hash ? $hash : "";
79 }
80
81 sub checkconfig() {
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"),
86                 };
87         }
88 }
89
90 sub getsetup() {
91         return
92                 plugin => {
93                         safe => 0, # rcs plugin
94                         rebuild => undef,
95                 },
96                 darcs_wrapper => {
97                         type => "string",
98                         example => "/darcs/repo/_darcs/ikiwiki-wrapper",
99                         description => "wrapper to generate (set as master repo apply hook)",
100                         safe => 0, # file
101                         rebuild => 0,
102                 },
103                 darcs_wrappermode => {
104                         type => "string",
105                         example => '06755',
106                         description => "mode for darcs_wrapper (can safely be made suid)",
107                         safe => 0,
108                         rebuild => 0,
109                 },
110                 historyurl => {
111                         type => "string",
112                         example => "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filehistory;f=[[file]]",
113                         description => "darcsweb url to show file history ([[file]] substituted)",
114                         safe => 1,
115                         rebuild => 1,
116                 },
117                 diffurl => {
118                         type => "string",
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)",
121                         safe => 1,
122                         rebuild => 1,
123                 },
124 }
125
126 sub rcs_update () {
127         silentsystem('darcs', "pull", "--repodir", $config{srcdir}, "-qa")
128 }
129
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.
135
136         my $file = shift; # Relative to the repodir.
137         my $rev = darcs_rev($file);
138         return $rev;
139 }
140
141 sub rcs_commit ($$$;$$) {
142         # Commit the page.  Returns 'undef' on success and a version of the page
143         # with conflict markers on failure.
144
145         my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
146
147         # Compute if the "revision" of $file changed.
148         my $changed = darcs_rev($file) ne $rcstoken;
149
150         # Yes, the following is a bit convoluted.
151         if ($changed) {
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: $!");
155
156                 # Roll the repository back to $rcstoken.
157
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
160                 # succeed.
161         
162                 # We need to revert everything as 'darcs obliterate' might choke
163                 # otherwise.
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, "|-");
169                 if (! $child) {
170                         open(STDOUT, ">/dev/null");
171                         exec('darcs', "obliterate", "--repodir", $config{srcdir},
172                            "--match", "hash " . $rcstoken) and
173                            error("'darcs obliterate' failed");
174                 }
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");
181         
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: $!");
185         }
186
187         # Record the changes.
188         my $author;
189         if (defined $user) {
190                 $author = "$user\@web";
191         }
192         elsif (defined $ipaddr) {
193                 $author = "$ipaddr\@web";
194         }
195         else {
196                 $author = "anon\@web";
197         }
198         if (!defined $message || !length($message)) {
199                 $message = "empty message";
200         }
201         silentsystem('darcs', 'record', '--repodir', $config{srcdir}, '--all',
202                         '-m', $message, '--author', $author, $file) == 0 ||
203                 error("'darcs record' failed");
204
205         # Update the repository by pulling from the default repository, which is
206         # master repository.
207         silentsystem('darcs', "pull", "--quiet", "--repodir", $config{srcdir},
208                 "--all") !=0 || error("'darcs pull' failed");
209
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'");
221
222         # Push the changes to the main repository.
223         silentsystem('darcs', 'push', '--quiet', '--repodir', $config{srcdir}, '--all') == 0 ||
224                 error("'darcs push' failed");
225         # TODO: darcs send?
226
227         if ($conflicts) {
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");
233                 return $document;
234         }
235         else {
236                 return undef;
237         }
238 }
239
240 sub rcs_commit_staged($$$) {
241         my ($message, $user, $ipaddr) = @_;
242
243         my $author;
244         if (defined $user) {
245                 $author = "$user\@web";
246         }
247         elsif (defined $ipaddr) {
248                 $author = "$ipaddr\@web";
249         }
250         else {
251                 $author = "anon\@web";
252         }
253         if (!defined $message || !length($message)) {
254                 $message = "empty message";
255         }
256
257         silentsystem('darcs', "record", "--repodir", $config{srcdir}, "-a", "-A", $author,
258                 "-m", $message) == 0 || error("'darcs record' failed");
259
260         # Push the changes to the main repository.
261         silentsystem('darcs', 'push', '--quiet', '--repodir', $config{srcdir}, '--all') == 0 ||
262                 error("'darcs push' failed");
263         # TODO: darcs send?
264
265         return undef;
266 }
267
268 sub rcs_add ($) {
269         my $file = shift; # Relative to the repodir.
270
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");
275         }
276 }
277
278 sub rcs_remove ($) {
279         my $file = shift; # Relative to the repodir.
280
281         unlink($config{srcdir}.'/'.$file);
282 }
283
284 sub rcs_rename ($$) {
285         my $a = shift; # Relative to the repodir.
286         my $b = shift; # Relative to the repodir.
287
288         system('darcs', 'mv', '--repodir', $config{srcdir}, $a, $b) == 0 ||
289                 error("'darcs mv' failed");
290 }
291
292 sub rcs_recentchanges ($) {
293         my $num=shift;
294         my @ret;
295
296         eval q{use Date::Parse};
297         eval q{use XML::Simple};
298
299         my $repodir=$config{srcdir};
300
301         debug("darcs recent changes: $num");
302
303         my $child = open(LOG, "-|");
304         if (! $child) {
305                 $ENV{"DARCS_DONT_ESCAPE_ANYTHING"}=1;
306                 exec("darcs", "changes", "--xml", 
307                         "--summary",
308                          "--repodir", "$repodir",
309                          "--last", "$num")
310                 || error("'darcs changes' failed to run");
311         }
312         my $data;
313         $data .= $_ while(<LOG>);
314         close LOG;
315
316         my $log = XMLin($data, ForceArray => 1);
317
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
330
331                         push @files, $f;
332                 }
333                 foreach my $p (@{$patch->{summary}->[0]->{move}}) {
334                         push @files, $p->{from};
335                 }
336
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;
341
342                         debug("file: $f");
343                         debug("diffurl: $d");
344                         push @pg, {
345                                 page => pagename($f),
346                                 diffurl => $d,
347                         };
348                 }
349                 next unless (scalar @pg > 0);
350                 debug("recent change: " . $patch->{name}[0] . " ("
351                         . scalar @pg . " changes)");
352
353                 my @message;
354                 push @message, { line => $_ } foreach (@{$patch->{name}});
355
356                 my $committype;
357                 if ($patch->{author} =~ /\@web$/) {
358                         $committype = "web";
359                 }
360                 else {
361                         $committype = "darcs";
362                 }
363
364                 push @ret, {
365                         rev => $patch->{hash},
366                         user => $patch->{author},
367                         committype => $committype,
368                         when => $when, 
369                         message => [@message],
370                         pages => [@pg],
371                 };
372         }
373
374         return @ret;
375 }
376
377 sub rcs_diff ($) {
378         my $rev=shift;
379         my @lines;
380         foreach my $line (silentsystem("darcs", "diff", "--match", "hash ".$rev)) {
381                 if (@lines || $line=~/^diff/) {
382                         push @lines, $line."\n";
383                 }
384         }
385         if (wantarray) {
386                 return @lines;
387         }
388         else {
389                 return join("", @lines);
390         }
391 }
392
393 sub rcs_getctime ($) {
394         my $file=shift;
395
396         eval q{use Date::Parse};
397         eval q{use XML::Simple};
398         local $/=undef;
399
400         # Sigh... doing things the hard way again
401         my $repodir=$config{srcdir};
402
403         my $filer=substr($file, length($repodir));
404         $filer =~ s:^[/]+::;
405
406         my $child = open(LOG, "-|");
407         if (! $child) {
408                 exec("darcs", "changes", "--xml", "--reverse",
409                         "--repodir", "$repodir", "$filer")
410                 || error("'darcs changes $filer' failed to run");
411         }
412
413         my $data;
414         {
415                 local $/=undef;
416                 $data = <LOG>;
417         }
418         close LOG;
419
420         my $log = XMLin($data, ForceArray => 1);
421
422         my $datestr = $log->{patch}[0]->{local_date};
423
424         if (! defined $datestr) {
425                 warn "failed to get ctime for $filer";
426                 return 0;
427         }
428
429         my $date = str2time($datestr);
430         
431         debug("ctime for '$file': ". localtime($date));
432
433         return $date;
434 }
435
436 1