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