]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/darcs.pm
Merge branch 'master' into staging
[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         my $child = open(LOG, "-|");
302         if (! $child) {
303                 $ENV{"DARCS_DONT_ESCAPE_ANYTHING"}=1;
304                 exec("darcs", "changes", "--xml", 
305                         "--summary",
306                          "--repodir", "$repodir",
307                          "--last", "$num")
308                 || error("'darcs changes' failed to run");
309         }
310         my $data;
311         $data .= $_ while(<LOG>);
312         close LOG;
313
314         my $log = XMLin($data, ForceArray => 1);
315
316         foreach my $patch (@{$log->{patch}}) {
317                 my $date=$patch->{local_date};
318                 my $hash=$patch->{hash};
319                 my $when=str2time($date);
320                 my (@pages, @files, @pg);
321                 push @pages, $_ for (@{$patch->{summary}->[0]->{modify_file}});
322                 push @pages, $_ for (@{$patch->{summary}->[0]->{add_file}});
323                 push @pages, $_ for (@{$patch->{summary}->[0]->{remove_file}});
324                 foreach my $f (@pages) {
325                         $f = $f->{content} if ref $f;
326                         $f =~ s,^\s+,,; $f =~ s,\s+$,,; # cut whitespace
327
328                         push @files, $f;
329                 }
330                 foreach my $p (@{$patch->{summary}->[0]->{move}}) {
331                         push @files, $p->{from};
332                 }
333
334                 foreach my $f (@files) {
335                         my $d = defined $config{'diffurl'} ? $config{'diffurl'} : "";
336                         $d =~ s/\[\[file\]\]/$f/go;
337                         $d =~ s/\[\[hash\]\]/$hash/go;
338
339                         push @pg, {
340                                 page => pagename($f),
341                                 diffurl => $d,
342                         };
343                 }
344                 next unless (scalar @pg > 0);
345
346                 my @message;
347                 push @message, { line => $_ } foreach (@{$patch->{name}});
348
349                 my $committype;
350                 my $author;
351                 if ($patch->{author} =~ /(.*)\@web$/) {
352                         $author = $1;
353                         $committype = "web";
354                 }
355                 else {
356                         $author=$patch->{author};
357                         $committype = "darcs";
358                 }
359
360                 push @ret, {
361                         rev => $patch->{hash},
362                         user => $author,
363                         committype => $committype,
364                         when => $when, 
365                         message => [@message],
366                         pages => [@pg],
367                 };
368         }
369
370         return @ret;
371 }
372
373 sub rcs_diff ($) {
374         my $rev=shift;
375         my @lines;
376         foreach my $line (silentsystem("darcs", "diff", "--match", "hash ".$rev)) {
377                 if (@lines || $line=~/^diff/) {
378                         push @lines, $line."\n";
379                 }
380         }
381         if (wantarray) {
382                 return @lines;
383         }
384         else {
385                 return join("", @lines);
386         }
387 }
388
389 sub rcs_getctime ($) {
390         my $file=shift;
391
392         eval q{use Date::Parse};
393         eval q{use XML::Simple};
394         local $/=undef;
395
396         my $filer=substr($file, length($config{srcdir}));
397         $filer =~ s:^[/]+::;
398
399         my $child = open(LOG, "-|");
400         if (! $child) {
401                 exec("darcs", "changes", "--xml", "--reverse",
402                         "--repodir", $config{srcdir}, $filer)
403                 || error("'darcs changes $filer' 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 $filer";
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 1