2 package IkiWiki::Plugin::tla;
7 use URI::Escape q{uri_escape_utf8};
10 hook(type => "checkconfig", id => "tla", call => \&checkconfig);
11 hook(type => "getsetup", id => "tla", 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);
26 if (defined $config{tla_wrapper} && length $config{tla_wrapper}) {
27 push @{$config{wrappers}}, {
28 wrapper => $config{tla_wrapper},
29 wrappermode => (defined $config{tla_wrappermode} ? $config{tla_wrappermode} : "06755"),
37 safe => 0, # rcs plugin
43 #example => "", # TODO example
44 description => "tla post-commit hook to generate",
51 description => "mode for tla_wrapper (can safely be made suid)",
57 #example => "", # TODO example
58 description => "url to show file history ([[file]] substituted)",
64 #example => "", # TODO example
65 description => "url to show a diff ([[file]] and [[rev]] substituted)",
71 sub quiet_system (@) {
72 # See Debian bug #385939.
73 open (SAVEOUT, ">&STDOUT");
75 open (STDOUT, ">/dev/null");
78 open (STDOUT, ">&SAVEOUT");
84 if (-d "$config{srcdir}/{arch}") {
85 if (quiet_system("tla", "replay", "-d", $config{srcdir}) != 0) {
86 warn("tla replay failed\n");
91 sub rcs_prepedit ($) {
94 if (-d "$config{srcdir}/{arch}") {
95 # For Arch, return the tree-id of archive when
97 my $rev=`tla tree-id $config{srcdir}`;
98 return defined $rev ? $rev : "";
105 my ($file, $message, $rcstoken)=
106 ($params{file}, $params{message}, $params{token});
108 if (defined $params{session}) {
109 if (defined $params{session}->param("name")) {
110 $message="web commit by ".
111 $params{session}->param("name").
112 (length $message ? ": $message" : "");
114 elsif (defined $params{session}->remote_addr()) {
115 $message="web commit from ".
116 $params{session}->remote_addr().
117 (length $message ? ": $message" : "");
121 if (-d "$config{srcdir}/{arch}") {
122 # Check to see if the page has been changed by someone
123 # else since rcs_prepedit was called.
124 my ($oldrev)=$rcstoken=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
125 my $rev=`tla tree-id $config{srcdir}`;
126 if (defined $rev && defined $oldrev && $rev ne $oldrev) {
127 # Merge their changes into the file that we've
129 if (quiet_system("tla", "update", "-d",
130 "$config{srcdir}") != 0) {
131 warn("tla update failed\n");
135 if (quiet_system("tla", "commit",
136 "-L".IkiWiki::possibly_foolish_untaint($message),
137 '-d', $config{srcdir}) != 0) {
138 my $conflict=readfile("$config{srcdir}/$file");
139 if (system("tla", "undo", "-n", "--quiet", "-d", "$config{srcdir}") != 0) {
140 warn("tla undo failed\n");
145 return undef # success
148 sub rcs_commit_staged (@) {
149 # Commits all staged changes. Changes can be staged using rcs_add,
150 # rcs_remove, and rcs_rename.
153 error("rcs_commit_staged not implemented for tla"); # TODO
159 if (-d "$config{srcdir}/{arch}") {
160 if (quiet_system("tla", "add", "$config{srcdir}/$file") != 0) {
161 warn("tla add failed\n");
169 error("rcs_remove not implemented for tla"); # TODO
172 sub rcs_rename ($$) {
173 my ($src, $dest) = @_;
175 error("rcs_rename not implemented for tla"); # TODO
178 sub rcs_recentchanges ($) {
182 return unless -d "$config{srcdir}/{arch}";
184 eval q{use Date::Parse};
186 eval q{use Mail::Header};
189 my $logs = `tla logs -d $config{srcdir}`;
190 my @changesets = reverse split(/\n/, $logs);
192 for (my $i=0; $i<$num && $i<$#changesets; $i++) {
193 my ($change)=$changesets[$i]=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
195 open(LOG, "tla cat-log -d $config{srcdir} $change|");
196 my $head = Mail::Header->new(\*LOG);
199 my $rev = $head->get("Revision");
200 my $summ = $head->get("Summary");
201 my $newfiles = $head->get("New-files");
202 my $modfiles = $head->get("Modified-files");
203 my $remfiles = $head->get("Removed-files");
204 my $user = $head->get("Creator");
206 my @paths = grep { !/^(.*\/)?\.arch-ids\/.*\.id$/ }
207 split(/ /, "$newfiles $modfiles .arch-ids/fake.id");
209 my $sdate = $head->get("Standard-date");
210 my $when = str2time($sdate, 'UTC');
212 my $committype = "web";
213 if (defined $summ && $summ =~ /$config{web_commit_regexp}/) {
214 $user = defined $2 ? "$2" : "$3";
222 push @message, { line => $summ };
226 foreach my $file (@paths) {
227 my $diffurl=defined $config{diffurl} ? $config{diffurl} : "";
228 my $efile = uri_escape_utf8($file);
229 $diffurl=~s/\[\[file\]\]/$efile/g;
230 $diffurl=~s/\[\[rev\]\]/$change/g;
232 page => pagename($file),
239 committype => $committype,
241 message => [@message],
253 my $logs = `tla logs -d $config{srcdir}`;
254 my @changesets = reverse split(/\n/, $logs);
257 for($i=0;$i<$#changesets;$i++) {
258 last if $changesets[$i] eq $rev;
261 my $revminusone = $changesets[$i+1];
262 return `tla diff -d $config{srcdir} $revminusone`;
265 sub rcs_getctime ($) {
267 eval q{use Date::Parse};
269 eval q{use Mail::Header};
272 my $logs = `tla logs -d $config{srcdir}`;
273 my @changesets = reverse split(/\n/, $logs);
276 for (my $i=0; $i<$#changesets; $i++) {
277 my $change = $changesets[$i];
279 open(LOG, "tla cat-log -d $config{srcdir} $change|");
280 my $head = Mail::Header->new(\*LOG);
283 $sdate = $head->get("Standard-date");
284 my $newfiles = $head->get("New-files");
286 my ($lastcreation) = grep {/^$file$/} split(/ /, "$newfiles");
287 last if defined($lastcreation);
290 my $date=str2time($sdate, 'UTC');
291 debug("found ctime ".localtime($date)." for $file");
295 sub rcs_getmtime ($) {
296 error "rcs_getmtime is not implemented for tla\n"; # TODO