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