summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/tla.pm
blob: 80c015e3cff881b8c59bd985647a73ae93bb6ca4 (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 $file=shift;
  94. my $message=shift;
  95. my $rcstoken=shift;
  96. my $user=shift;
  97. my $ipaddr=shift;
  98. my $emailuser=shift;
  99. if (defined $user) {
  100. $message="web commit by $user".(length $message ? ": $message" : "");
  101. }
  102. elsif (defined $ipaddr) {
  103. $message="web commit from $ipaddr".(length $message ? ": $message" : "");
  104. }
  105. if (-d "$config{srcdir}/{arch}") {
  106. # Check to see if the page has been changed by someone
  107. # else since rcs_prepedit was called.
  108. my ($oldrev)=$rcstoken=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
  109. my $rev=`tla tree-id $config{srcdir}`;
  110. if (defined $rev && defined $oldrev && $rev ne $oldrev) {
  111. # Merge their changes into the file that we've
  112. # changed.
  113. if (quiet_system("tla", "update", "-d",
  114. "$config{srcdir}") != 0) {
  115. warn("tla update failed\n");
  116. }
  117. }
  118. if (quiet_system("tla", "commit",
  119. "-L".IkiWiki::possibly_foolish_untaint($message),
  120. '-d', $config{srcdir}) != 0) {
  121. my $conflict=readfile("$config{srcdir}/$file");
  122. if (system("tla", "undo", "-n", "--quiet", "-d", "$config{srcdir}") != 0) {
  123. warn("tla undo failed\n");
  124. }
  125. return $conflict;
  126. }
  127. }
  128. return undef # success
  129. }
  130. sub rcs_commit_staged ($$$;$) {
  131. # Commits all staged changes. Changes can be staged using rcs_add,
  132. # rcs_remove, and rcs_rename.
  133. my ($message, $user, $ipaddr, $emailuser)=@_;
  134. error("rcs_commit_staged not implemented for tla"); # TODO
  135. }
  136. sub rcs_add ($) {
  137. my $file=shift;
  138. if (-d "$config{srcdir}/{arch}") {
  139. if (quiet_system("tla", "add", "$config{srcdir}/$file") != 0) {
  140. warn("tla add failed\n");
  141. }
  142. }
  143. }
  144. sub rcs_remove ($) {
  145. my $file = shift;
  146. error("rcs_remove not implemented for tla"); # TODO
  147. }
  148. sub rcs_rename ($$) {
  149. my ($src, $dest) = @_;
  150. error("rcs_rename not implemented for tla"); # TODO
  151. }
  152. sub rcs_recentchanges ($) {
  153. my $num=shift;
  154. my @ret;
  155. return unless -d "$config{srcdir}/{arch}";
  156. eval q{use Date::Parse};
  157. error($@) if $@;
  158. eval q{use Mail::Header};
  159. error($@) if $@;
  160. my $logs = `tla logs -d $config{srcdir}`;
  161. my @changesets = reverse split(/\n/, $logs);
  162. for (my $i=0; $i<$num && $i<$#changesets; $i++) {
  163. my ($change)=$changesets[$i]=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
  164. open(LOG, "tla cat-log -d $config{srcdir} $change|");
  165. my $head = Mail::Header->new(\*LOG);
  166. close(LOG);
  167. my $rev = $head->get("Revision");
  168. my $summ = $head->get("Summary");
  169. my $newfiles = $head->get("New-files");
  170. my $modfiles = $head->get("Modified-files");
  171. my $remfiles = $head->get("Removed-files");
  172. my $user = $head->get("Creator");
  173. my @paths = grep { !/^(.*\/)?\.arch-ids\/.*\.id$/ }
  174. split(/ /, "$newfiles $modfiles .arch-ids/fake.id");
  175. my $sdate = $head->get("Standard-date");
  176. my $when = str2time($sdate, 'UTC');
  177. my $committype = "web";
  178. if (defined $summ && $summ =~ /$config{web_commit_regexp}/) {
  179. $user = defined $2 ? "$2" : "$3";
  180. $summ = $4;
  181. }
  182. else {
  183. $committype="tla";
  184. }
  185. my @message;
  186. push @message, { line => $summ };
  187. my @pages;
  188. foreach my $file (@paths) {
  189. my $diffurl=defined $config{diffurl} ? $config{diffurl} : "";
  190. $diffurl=~s/\[\[file\]\]/$file/g;
  191. $diffurl=~s/\[\[rev\]\]/$change/g;
  192. push @pages, {
  193. page => pagename($file),
  194. diffurl => $diffurl,
  195. } if length $file;
  196. }
  197. push @ret, {
  198. rev => $change,
  199. user => $user,
  200. committype => $committype,
  201. when => $when,
  202. message => [@message],
  203. pages => [@pages],
  204. } if @pages;
  205. last if $i == $num;
  206. }
  207. return @ret;
  208. }
  209. sub rcs_diff ($) {
  210. my $rev=shift;
  211. my $logs = `tla logs -d $config{srcdir}`;
  212. my @changesets = reverse split(/\n/, $logs);
  213. my $i;
  214. for($i=0;$i<$#changesets;$i++) {
  215. last if $changesets[$i] eq $rev;
  216. }
  217. my $revminusone = $changesets[$i+1];
  218. return `tla diff -d $config{srcdir} $revminusone`;
  219. }
  220. sub rcs_getctime ($) {
  221. my $file=shift;
  222. eval q{use Date::Parse};
  223. error($@) if $@;
  224. eval q{use Mail::Header};
  225. error($@) if $@;
  226. my $logs = `tla logs -d $config{srcdir}`;
  227. my @changesets = reverse split(/\n/, $logs);
  228. my $sdate;
  229. for (my $i=0; $i<$#changesets; $i++) {
  230. my $change = $changesets[$i];
  231. open(LOG, "tla cat-log -d $config{srcdir} $change|");
  232. my $head = Mail::Header->new(\*LOG);
  233. close(LOG);
  234. $sdate = $head->get("Standard-date");
  235. my $newfiles = $head->get("New-files");
  236. my ($lastcreation) = grep {/^$file$/} split(/ /, "$newfiles");
  237. last if defined($lastcreation);
  238. }
  239. my $date=str2time($sdate, 'UTC');
  240. debug("found ctime ".localtime($date)." for $file");
  241. return $date;
  242. }
  243. sub rcs_getmtime ($) {
  244. error "rcs_getmtime is not implemented for tla\n"; # TODO
  245. }
  246. 1