summaryrefslogtreecommitdiff
path: root/IkiWiki/Rcs/tla.pm
blob: be5ec0601304d1c66c12f366c40d794975cd47fd (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. sub quiet_system (@) {
  8. # See Debian bug #385939.
  9. open (SAVEOUT, ">&STDOUT");
  10. close STDOUT;
  11. open (STDOUT, ">/dev/null");
  12. my $ret=system(@_);
  13. close STDOUT;
  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. error($@) if $@;
  77. eval q{use Mail::Header};
  78. error($@) if $@;
  79. my $logs = `tla logs -d $config{srcdir}`;
  80. my @changesets = reverse split(/\n/, $logs);
  81. for (my $i=0; $i<$num && $i<$#changesets; $i++) {
  82. my ($change)=$changesets[$i]=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
  83. open(LOG, "tla cat-log -d $config{srcdir} $change|");
  84. my $head = Mail::Header->new(\*LOG);
  85. close(LOG);
  86. my $rev = $head->get("Revision");
  87. my $summ = $head->get("Summary");
  88. my $newfiles = $head->get("New-files");
  89. my $modfiles = $head->get("Modified-files");
  90. my $remfiles = $head->get("Removed-files");
  91. my $user = $head->get("Creator");
  92. my @paths = grep { !/^(.*\/)?\.arch-ids\/.*\.id$/ }
  93. split(/ /, "$newfiles $modfiles .arch-ids/fake.id");
  94. my $sdate = $head->get("Standard-date");
  95. my $when = time - str2time($sdate, 'UTC');
  96. my $committype = "web";
  97. if (defined $summ && $summ =~ /$config{web_commit_regexp}/) {
  98. $user = defined $2 ? "$2" : "$3";
  99. $summ = $4;
  100. }
  101. else {
  102. $committype="tla";
  103. }
  104. my @message;
  105. push @message, { line => escapeHTML($summ) };
  106. my @pages;
  107. foreach my $file (@paths) {
  108. my $diffurl=$config{diffurl};
  109. $diffurl=~s/\[\[file\]\]/$file/g;
  110. $diffurl=~s/\[\[rev\]\]/$change/g;
  111. push @pages, {
  112. page => pagename($file),
  113. diffurl => $diffurl,
  114. } if length $file;
  115. }
  116. push @ret, { rev => $change,
  117. user => $user,
  118. committype => $committype,
  119. when => $when,
  120. message => [@message],
  121. pages => [@pages],
  122. } if @pages;
  123. last if $i == $num;
  124. }
  125. return @ret;
  126. }
  127. sub rcs_notify () { #{{{
  128. # FIXME: Not set
  129. if (! exists $ENV{ARCH_VERSION}) {
  130. error("ARCH_VERSION is not set, not running from tla post-commit hook, cannot send notifications");
  131. }
  132. my $rev=int(possibly_foolish_untaint($ENV{REV}));
  133. eval q{use Mail::Header};
  134. error($@) if $@;
  135. open(LOG, $ENV{"ARCH_LOG"});
  136. my $head = Mail::Header->new(\*LOG);
  137. close(LOG);
  138. my $user = $head->get("Creator");
  139. my $newfiles = $head->get("New-files");
  140. my $modfiles = $head->get("Modified-files");
  141. my $remfiles = $head->get("Removed-files");
  142. my @changed_pages = grep { !/(^.*\/)?\.arch-ids\/.*\.id$/ }
  143. split(/ /, "$newfiles $modfiles $remfiles .arch-ids/fake.id");
  144. require IkiWiki::UserInfo;
  145. send_commit_mails(
  146. sub {
  147. my $message = $head->get("Summary");
  148. if ($message =~ /$config{web_commit_regexp}/) {
  149. $user=defined $2 ? "$2" : "$3";
  150. $message=$4;
  151. }
  152. },
  153. sub {
  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. `tla diff -d $ENV{ARCH_TREE_ROOT} $revminusone`;
  162. }, $user, @changed_pages);
  163. } #}}}
  164. sub rcs_getctime ($) { #{{{
  165. my $file=shift;
  166. eval q{use Date::Parse};
  167. error($@) if $@;
  168. eval q{use Mail::Header};
  169. error($@) if $@;
  170. my $logs = `tla logs -d $config{srcdir}`;
  171. my @changesets = reverse split(/\n/, $logs);
  172. my $sdate;
  173. for (my $i=0; $i<$#changesets; $i++) {
  174. my $change = $changesets[$i];
  175. open(LOG, "tla cat-log -d $config{srcdir} $change|");
  176. my $head = Mail::Header->new(\*LOG);
  177. close(LOG);
  178. $sdate = $head->get("Standard-date");
  179. my $newfiles = $head->get("New-files");
  180. my ($lastcreation) = grep {/^$file$/} split(/ /, "$newfiles");
  181. last if defined($lastcreation);
  182. }
  183. my $date=str2time($sdate, 'UTC');
  184. debug("found ctime ".localtime($date)." for $file");
  185. return $date;
  186. } #}}}
  187. 1