summaryrefslogtreecommitdiff
path: root/IkiWiki/Rcs/tla.pm
blob: c9b64dbe5e09f4556126b5f1026c75a53fe22dc3 (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use IkiWiki;
  5. use POSIX qw(setlocale LC_CTYPE);
  6. package IkiWiki;
  7. my $tla_webcommit=qr/^web commit (by (\w+)|from (\d+\.\d+\.\d+\.\d+)):?(.*)/;
  8. sub quiet_system (@) {
  9. # See Debian bug #385939.
  10. open (SAVEOUT, ">&STDOUT");
  11. close STDOUT;
  12. open (STDOUT, ">/dev/null");
  13. my $ret=system(@_);
  14. close STDOUT;
  15. open (STDOUT, ">&SAVEOUT");
  16. close SAVEOUT;
  17. return $ret;
  18. }
  19. sub rcs_update () { #{{{
  20. if (-d "$config{srcdir}/{arch}") {
  21. if (quiet_system("tla", "replay", "-d", $config{srcdir}) != 0) {
  22. warn("tla replay failed\n");
  23. }
  24. }
  25. } #}}}
  26. sub rcs_prepedit ($) { #{{{
  27. my $file=shift;
  28. if (-d "$config{srcdir}/{arch}") {
  29. # For Arch, return the tree-id of archive when
  30. # editing begins.
  31. my $rev=`tla tree-id $config{srcdir}`;
  32. return defined $rev ? $rev : "";
  33. }
  34. } #}}}
  35. sub rcs_commit ($$$) { #{{{
  36. my $file=shift;
  37. my $message=shift;
  38. my $rcstoken=shift;
  39. if (-d "$config{srcdir}/{arch}") {
  40. # Check to see if the page has been changed by someone
  41. # else since rcs_prepedit was called.
  42. my ($oldrev)=$rcstoken=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
  43. my $rev=`tla tree-id $config{srcdir}`;
  44. if (defined $rev && defined $oldrev && $rev ne $oldrev) {
  45. # Merge their changes into the file that we've
  46. # changed.
  47. if (quiet_system("tla", "update", "-d",
  48. "$config{srcdir}") != 0) {
  49. warn("tla update failed\n");
  50. }
  51. }
  52. if (quiet_system("tla", "commit",
  53. "-L".possibly_foolish_untaint($message),
  54. '-d', $config{srcdir}) != 0) {
  55. my $conflict=readfile("$config{srcdir}/$file");
  56. if (system("tla", "undo", "-n", "--quiet", "-d", "$config{srcdir}") != 0) {
  57. warn("tla undo failed\n");
  58. }
  59. return $conflict;
  60. }
  61. }
  62. return undef # success
  63. } #}}}
  64. sub rcs_add ($) { #{{{
  65. my $file=shift;
  66. if (-d "$config{srcdir}/{arch}") {
  67. if (quiet_system("tla", "add", "$config{srcdir}/$file") != 0) {
  68. warn("tla add failed\n");
  69. }
  70. }
  71. } #}}}
  72. sub rcs_recentchanges ($) {
  73. my $num=shift;
  74. my @ret;
  75. return unless -d "$config{srcdir}/{arch}";
  76. eval q{use Date::Parse};
  77. eval q{use Mail::Header};
  78. my $logs = `tla logs -d $config{srcdir}`;
  79. my @changesets = reverse split(/\n/, $logs);
  80. for (my $i=0; $i<$num && $i<$#changesets; $i++) {
  81. my ($change)=$changesets[$i]=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
  82. open(LOG, "tla cat-log -d $config{srcdir} $change|");
  83. my $head = Mail::Header->new(\*LOG);
  84. close(LOG);
  85. my $rev = $head->get("Revision");
  86. my $summ = $head->get("Summary");
  87. my $newfiles = $head->get("New-files");
  88. my $modfiles = $head->get("Modified-files");
  89. my $remfiles = $head->get("Removed-files");
  90. my $user = $head->get("Creator");
  91. my @paths = grep { !/^(.*\/)?\.arch-ids\/.*\.id$/ }
  92. split(/ /, "$newfiles $modfiles .arch-ids/fake.id");
  93. my $sdate = $head->get("Standard-date");
  94. my $when = time - str2time($sdate, 'UTC');
  95. my $committype = "web";
  96. if (defined $summ && $summ =~ /$tla_webcommit/) {
  97. $user = defined $2 ? "$2" : "$3";
  98. $summ = $4;
  99. }
  100. else {
  101. $committype="tla";
  102. }
  103. my @message;
  104. push @message, { line => escapeHTML($summ) };
  105. my @pages;
  106. foreach my $file (@paths) {
  107. my $diffurl=$config{diffurl};
  108. $diffurl=~s/\[\[file\]\]/$file/g;
  109. $diffurl=~s/\[\[rev\]\]/$change/g;
  110. push @pages, {
  111. page => pagename($file),
  112. diffurl => $diffurl,
  113. } if length $file;
  114. }
  115. push @ret, { rev => $change,
  116. user => $user,
  117. committype => $committype,
  118. when => $when,
  119. message => [@message],
  120. pages => [@pages],
  121. } if @pages;
  122. last if $i == $num;
  123. }
  124. return @ret;
  125. }
  126. sub rcs_notify () { #{{{
  127. # FIXME: Not set
  128. if (! exists $ENV{ARCH_VERSION}) {
  129. error("ARCH_VERSION is not set, not running from tla post-commit hook, cannot send notifications");
  130. }
  131. my $rev=int(possibly_foolish_untaint($ENV{REV}));
  132. eval q{use Mail::Header};
  133. open(LOG, $ENV{"ARCH_LOG"});
  134. my $head = Mail::Header->new(\*LOG);
  135. close(LOG);
  136. my $message = $head->get("Summary");
  137. my $user = $head->get("Creator");
  138. my $newfiles = $head->get("New-files");
  139. my $modfiles = $head->get("Modified-files");
  140. my $remfiles = $head->get("Removed-files");
  141. my @changed_pages = grep { !/(^.*\/)?\.arch-ids\/.*\.id$/ }
  142. split(/ /, "$newfiles $modfiles $remfiles .arch-ids/fake.id");
  143. if ($message =~ /$tla_webcommit/) {
  144. $user=defined $2 ? "$2" : "$3";
  145. $message=$4;
  146. }
  147. require IkiWiki::UserInfo;
  148. my @email_recipients=commit_notify_list($user, @changed_pages);
  149. if (@email_recipients) {
  150. # TODO: if a commit spans multiple pages, this will send
  151. # subscribers a diff that might contain pages they did not
  152. # sign up for. Should separate the diff per page and
  153. # reassemble into one mail with just the pages subscribed to.
  154. my $logs = `tla logs -d $config{srcdir}`;
  155. my @changesets = reverse split(/\n/, $logs);
  156. my $i;
  157. for($i=0;$i<$#changesets;$i++) {
  158. last if $changesets[$i] eq $rev;
  159. }
  160. my $revminusone = $changesets[$i+1];
  161. my $diff=`tla diff -d $ENV{ARCH_TREE_ROOT} $revminusone`;
  162. my $subject="$config{wikiname} update of ";
  163. if (@changed_pages > 2) {
  164. $subject.="$changed_pages[0] $changed_pages[1] etc";
  165. }
  166. else {
  167. $subject.=join(" ", @changed_pages);
  168. }
  169. $subject.=" by $user";
  170. my $template=template("notifymail.tmpl");
  171. $template->param(
  172. wikiname => $config{wikiname},
  173. diff => $diff,
  174. user => $user,
  175. message => $message,
  176. );
  177. eval q{use Mail::Sendmail};
  178. foreach my $email (@email_recipients) {
  179. sendmail(
  180. To => $email,
  181. From => "$config{wikiname} <$config{adminemail}>",
  182. Subject => $subject,
  183. Message => $template->output,
  184. ) or error("Failed to send update notification mail");
  185. }
  186. }
  187. } #}}}
  188. sub rcs_getctime ($) { #{{{
  189. my $file=shift;
  190. eval q{use Date::Parse};
  191. eval q{use Mail::Header};
  192. my $logs = `tla logs -d $config{srcdir}`;
  193. my @changesets = reverse split(/\n/, $logs);
  194. my $sdate;
  195. for (my $i=0; $i<$#changesets; $i++) {
  196. my $change = $changesets[$i];
  197. open(LOG, "tla cat-log -d $config{srcdir} $change|");
  198. my $head = Mail::Header->new(\*LOG);
  199. close(LOG);
  200. $sdate = $head->get("Standard-date");
  201. my $newfiles = $head->get("New-files");
  202. my ($lastcreation) = grep {/^$file$/} split(/ /, "$newfiles");
  203. last if defined($lastcreation);
  204. }
  205. my $date=str2time($sdate, 'UTC');
  206. debug("found ctime ".localtime($date)." for $file");
  207. return $date;
  208. } #}}}
  209. 1