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