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