clear sandbox/test commit
[ikiwiki.git] / IkiWiki / Plugin / tla.pm
blobda4385446412635d3b4843e4b439e07cd399195b
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::tla;
4 use warnings;
5 use strict;
6 use IkiWiki;
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);
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"),
33 sub getsetup () {
34 return
35 plugin => {
36 safe => 0, # rcs plugin
37 rebuild => undef,
38 section => "rcs",
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,
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,
54 historyurl => {
55 type => "string",
56 #example => "", # TODO example
57 description => "url to show file history ([[file]] substituted)",
58 safe => 1,
59 rebuild => 1,
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,
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;
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");
90 sub rcs_prepedit ($) {
91 my $file=shift;
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 : "";
101 sub rcs_commit (@) {
102 my %params=@_;
104 my ($file, $message, $rcstoken)=
105 ($params{file}, $params{message}, $params{token});
107 if (defined $params{session}) {
108 if (defined $params{session}->param("name")) {
109 $message="web commit by ".
110 $params{session}->param("name").
111 (length $message ? ": $message" : "");
113 elsif (defined $params{session}->remote_addr()) {
114 $message="web commit from ".
115 $params{session}->remote_addr().
116 (length $message ? ": $message" : "");
120 if (-d "$config{srcdir}/{arch}") {
121 # Check to see if the page has been changed by someone
122 # else since rcs_prepedit was called.
123 my ($oldrev)=$rcstoken=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
124 my $rev=`tla tree-id $config{srcdir}`;
125 if (defined $rev && defined $oldrev && $rev ne $oldrev) {
126 # Merge their changes into the file that we've
127 # changed.
128 if (quiet_system("tla", "update", "-d",
129 "$config{srcdir}") != 0) {
130 warn("tla update failed\n");
134 if (quiet_system("tla", "commit",
135 "-L".IkiWiki::possibly_foolish_untaint($message),
136 '-d', $config{srcdir}) != 0) {
137 my $conflict=readfile("$config{srcdir}/$file");
138 if (system("tla", "undo", "-n", "--quiet", "-d", "$config{srcdir}") != 0) {
139 warn("tla undo failed\n");
141 return $conflict;
144 return undef # success
147 sub rcs_commit_staged (@) {
148 # Commits all staged changes. Changes can be staged using rcs_add,
149 # rcs_remove, and rcs_rename.
150 my %params=@_;
152 error("rcs_commit_staged not implemented for tla"); # TODO
155 sub rcs_add ($) {
156 my $file=shift;
158 if (-d "$config{srcdir}/{arch}") {
159 if (quiet_system("tla", "add", "$config{srcdir}/$file") != 0) {
160 warn("tla add failed\n");
165 sub rcs_remove ($) {
166 my $file = shift;
168 error("rcs_remove not implemented for tla"); # TODO
171 sub rcs_rename ($$) {
172 my ($src, $dest) = @_;
174 error("rcs_rename not implemented for tla"); # TODO
177 sub rcs_recentchanges ($) {
178 my $num=shift;
179 my @ret;
181 return unless -d "$config{srcdir}/{arch}";
183 eval q{use Date::Parse};
184 error($@) if $@;
185 eval q{use Mail::Header};
186 error($@) if $@;
188 my $logs = `tla logs -d $config{srcdir}`;
189 my @changesets = reverse split(/\n/, $logs);
191 for (my $i=0; $i<$num && $i<$#changesets; $i++) {
192 my ($change)=$changesets[$i]=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
194 open(LOG, "tla cat-log -d $config{srcdir} $change|");
195 my $head = Mail::Header->new(\*LOG);
196 close(LOG);
198 my $rev = $head->get("Revision");
199 my $summ = $head->get("Summary");
200 my $newfiles = $head->get("New-files");
201 my $modfiles = $head->get("Modified-files");
202 my $remfiles = $head->get("Removed-files");
203 my $user = $head->get("Creator");
205 my @paths = grep { !/^(.*\/)?\.arch-ids\/.*\.id$/ }
206 split(/ /, "$newfiles $modfiles .arch-ids/fake.id");
208 my $sdate = $head->get("Standard-date");
209 my $when = str2time($sdate, 'UTC');
211 my $committype = "web";
212 if (defined $summ && $summ =~ /$config{web_commit_regexp}/) {
213 $user = defined $2 ? "$2" : "$3";
214 $summ = $4;
216 else {
217 $committype="tla";
220 my @message;
221 push @message, { line => $summ };
223 my @pages;
225 foreach my $file (@paths) {
226 my $diffurl=defined $config{diffurl} ? $config{diffurl} : "";
227 $diffurl=~s/\[\[file\]\]/$file/g;
228 $diffurl=~s/\[\[rev\]\]/$change/g;
229 push @pages, {
230 page => pagename($file),
231 diffurl => $diffurl,
232 } if length $file;
234 push @ret, {
235 rev => $change,
236 user => $user,
237 committype => $committype,
238 when => $when,
239 message => [@message],
240 pages => [@pages],
241 } if @pages;
243 last if $i == $num;
246 return @ret;
249 sub rcs_diff ($) {
250 my $rev=shift;
251 my $logs = `tla logs -d $config{srcdir}`;
252 my @changesets = reverse split(/\n/, $logs);
253 my $i;
255 for($i=0;$i<$#changesets;$i++) {
256 last if $changesets[$i] eq $rev;
259 my $revminusone = $changesets[$i+1];
260 return `tla diff -d $config{srcdir} $revminusone`;
263 sub rcs_getctime ($) {
264 my $file=shift;
265 eval q{use Date::Parse};
266 error($@) if $@;
267 eval q{use Mail::Header};
268 error($@) if $@;
270 my $logs = `tla logs -d $config{srcdir}`;
271 my @changesets = reverse split(/\n/, $logs);
272 my $sdate;
274 for (my $i=0; $i<$#changesets; $i++) {
275 my $change = $changesets[$i];
277 open(LOG, "tla cat-log -d $config{srcdir} $change|");
278 my $head = Mail::Header->new(\*LOG);
279 close(LOG);
281 $sdate = $head->get("Standard-date");
282 my $newfiles = $head->get("New-files");
284 my ($lastcreation) = grep {/^$file$/} split(/ /, "$newfiles");
285 last if defined($lastcreation);
288 my $date=str2time($sdate, 'UTC');
289 debug("found ctime ".localtime($date)." for $file");
290 return $date;
293 sub rcs_getmtime ($) {
294 error "rcs_getmtime is not implemented for tla\n"; # TODO