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