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