API: rcs_commit and rcs_commit_staged are passed a new parameter
[ikiwiki] / IkiWiki / Plugin / tla.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::tla;
3
4 use warnings;
5 use strict;
6 use IkiWiki;
7
8 sub import {
9         hook(type => "checkconfig", id => "tla", call => \&checkconfig);
10         hook(type => "getsetup", id => "tla", 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 checkconfig () {
25         if (defined $config{tla_wrapper} && length $config{tla_wrapper}) {
26                 push @{$config{wrappers}}, {
27                         wrapper => $config{tla_wrapper},
28                         wrappermode => (defined $config{tla_wrappermode} ? $config{tla_wrappermode} : "06755"),
29                 };
30         }
31 }
32
33 sub getsetup () {
34         return
35                 plugin => {
36                         safe => 0, # rcs plugin
37                         rebuild => undef,
38                         section => "rcs",
39                 },
40                 tla_wrapper => {
41                         type => "string",
42                         #example => "", # TODO example
43                         description => "tla post-commit hook to generate",
44                         safe => 0, # file
45                         rebuild => 0,
46                 },
47                 tla_wrappermode => {
48                         type => "string",
49                         example => '06755',
50                         description => "mode for tla_wrapper (can safely be made suid)",
51                         safe => 0,
52                         rebuild => 0,
53                 },
54                 historyurl => {
55                         type => "string",
56                         #example => "", # TODO example
57                         description => "url to show file history ([[file]] substituted)",
58                         safe => 1,
59                         rebuild => 1,
60                 },
61                 diffurl => {
62                         type => "string",
63                         #example => "", # TODO example
64                         description => "url to show a diff ([[file]] and [[rev]] substituted)",
65                         safe => 1,
66                         rebuild => 1,
67                 },
68 }
69
70 sub quiet_system (@) {
71         # See Debian bug #385939.
72         open (SAVEOUT, ">&STDOUT");
73         close STDOUT;
74         open (STDOUT, ">/dev/null");
75         my $ret=system(@_);
76         close STDOUT;
77         open (STDOUT, ">&SAVEOUT");
78         close SAVEOUT;
79         return $ret;
80 }
81
82 sub rcs_update () {
83         if (-d "$config{srcdir}/{arch}") {
84                 if (quiet_system("tla", "replay", "-d", $config{srcdir}) != 0) {
85                         warn("tla replay failed\n");
86                 }
87         }
88 }
89
90 sub rcs_prepedit ($) {
91         my $file=shift;
92
93         if (-d "$config{srcdir}/{arch}") {
94                 # For Arch, return the tree-id of archive when
95                 # editing begins.
96                 my $rev=`tla tree-id $config{srcdir}`;
97                 return defined $rev ? $rev : "";
98         }
99 }
100
101 sub rcs_commit ($$$;$$$) {
102         my $file=shift;
103         my $message=shift;
104         my $rcstoken=shift;
105         my $user=shift;
106         my $ipaddr=shift;
107         my $emailuser=shift;
108
109         if (defined $user) {
110                 $message="web commit by $user".(length $message ? ": $message" : "");
111         }
112         elsif (defined $ipaddr) {
113                 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
114         }
115
116         if (-d "$config{srcdir}/{arch}") {
117                 # Check to see if the page has been changed by someone
118                 # else since rcs_prepedit was called.
119                 my ($oldrev)=$rcstoken=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
120                 my $rev=`tla tree-id $config{srcdir}`;
121                 if (defined $rev && defined $oldrev && $rev ne $oldrev) {
122                         # Merge their changes into the file that we've
123                         # changed.
124                         if (quiet_system("tla", "update", "-d",
125                                    "$config{srcdir}") != 0) {
126                                 warn("tla update failed\n");
127                         }
128                 }
129
130                 if (quiet_system("tla", "commit",
131                            "-L".IkiWiki::possibly_foolish_untaint($message),
132                            '-d', $config{srcdir}) != 0) {
133                         my $conflict=readfile("$config{srcdir}/$file");
134                         if (system("tla", "undo", "-n", "--quiet", "-d", "$config{srcdir}") != 0) {
135                                 warn("tla undo failed\n");
136                         }
137                         return $conflict;
138                 }
139         }
140         return undef # success
141 }
142
143 sub rcs_commit_staged ($$$;$) {
144         # Commits all staged changes. Changes can be staged using rcs_add,
145         # rcs_remove, and rcs_rename.
146         my ($message, $user, $ipaddr, $emailuser)=@_;
147         
148         error("rcs_commit_staged not implemented for tla"); # TODO
149 }
150
151 sub rcs_add ($) {
152         my $file=shift;
153
154         if (-d "$config{srcdir}/{arch}") {
155                 if (quiet_system("tla", "add", "$config{srcdir}/$file") != 0) {
156                         warn("tla add failed\n");
157                 }
158         }
159 }
160
161 sub rcs_remove ($) {
162         my $file = shift;
163
164         error("rcs_remove not implemented for tla"); # TODO
165 }
166
167 sub rcs_rename ($$) {
168         my ($src, $dest) = @_;
169
170         error("rcs_rename not implemented for tla"); # TODO
171 }
172
173 sub rcs_recentchanges ($) {
174         my $num=shift;
175         my @ret;
176
177         return unless -d "$config{srcdir}/{arch}";
178
179         eval q{use Date::Parse};
180         error($@) if $@;
181         eval q{use Mail::Header};
182         error($@) if $@;
183
184         my $logs = `tla logs -d $config{srcdir}`;
185         my @changesets = reverse split(/\n/, $logs);
186
187         for (my $i=0; $i<$num && $i<$#changesets; $i++) {
188                 my ($change)=$changesets[$i]=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
189
190                 open(LOG, "tla cat-log -d $config{srcdir} $change|");
191                 my $head = Mail::Header->new(\*LOG);
192                 close(LOG);
193
194                 my $rev = $head->get("Revision");
195                 my $summ = $head->get("Summary");
196                 my $newfiles = $head->get("New-files");
197                 my $modfiles = $head->get("Modified-files");
198                 my $remfiles = $head->get("Removed-files");
199                 my $user = $head->get("Creator");
200
201                 my @paths = grep { !/^(.*\/)?\.arch-ids\/.*\.id$/ }
202                         split(/ /, "$newfiles $modfiles .arch-ids/fake.id");
203
204                 my $sdate = $head->get("Standard-date");
205                 my $when = str2time($sdate, 'UTC');
206
207                 my $committype = "web";
208                 if (defined $summ && $summ =~ /$config{web_commit_regexp}/) {
209                         $user = defined $2 ? "$2" : "$3";
210                         $summ = $4;
211                 }
212                 else {
213                         $committype="tla";
214                 }
215
216                 my @message;
217                 push @message, { line => $summ };
218
219                 my @pages;
220
221                 foreach my $file (@paths) {
222                         my $diffurl=defined $config{diffurl} ? $config{diffurl} : "";
223                         $diffurl=~s/\[\[file\]\]/$file/g;
224                         $diffurl=~s/\[\[rev\]\]/$change/g;
225                         push @pages, {
226                                 page => pagename($file),
227                                 diffurl => $diffurl,
228                         } if length $file;
229                 }
230                 push @ret, {
231                         rev => $change,
232                         user => $user,
233                         committype => $committype,
234                         when => $when,
235                         message => [@message],
236                         pages => [@pages],
237                 } if @pages;
238
239                 last if $i == $num;
240         }
241
242         return @ret;
243 }
244
245 sub rcs_diff ($) {
246         my $rev=shift;
247         my $logs = `tla logs -d $config{srcdir}`;
248         my @changesets = reverse split(/\n/, $logs);
249         my $i;
250
251         for($i=0;$i<$#changesets;$i++) {
252                 last if $changesets[$i] eq $rev;
253         }
254
255         my $revminusone = $changesets[$i+1];
256         return `tla diff -d $config{srcdir} $revminusone`;
257 }
258
259 sub rcs_getctime ($) {
260         my $file=shift;
261         eval q{use Date::Parse};
262         error($@) if $@;
263         eval q{use Mail::Header};
264         error($@) if $@;
265
266         my $logs = `tla logs -d $config{srcdir}`;
267         my @changesets = reverse split(/\n/, $logs);
268         my $sdate;
269
270         for (my $i=0; $i<$#changesets; $i++) {
271                 my $change = $changesets[$i];
272
273                 open(LOG, "tla cat-log -d $config{srcdir} $change|");
274                 my $head = Mail::Header->new(\*LOG);
275                 close(LOG);
276
277                 $sdate = $head->get("Standard-date");
278                 my $newfiles = $head->get("New-files");
279
280                 my ($lastcreation) = grep {/^$file$/} split(/ /, "$newfiles");
281                 last if defined($lastcreation);
282         }
283
284         my $date=str2time($sdate, 'UTC');
285         debug("found ctime ".localtime($date)." for $file");
286         return $date;
287 }
288
289 sub rcs_getmtime ($) {
290         error "rcs_getmtime is not implemented for tla\n"; # TODO
291 }
292
293 1