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