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