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