summaryrefslogtreecommitdiff
path: root/IkiWiki/Rcs/tla.pm
blob: 7254eb1a7f5cb6a87b4ea6b3d6aa18e34275c387 (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. my $user=shift;
  39. my $ipaddr=shift;
  40. if (defined $user) {
  41. $message="web commit by $user".(length $message ? ": $message" : "");
  42. }
  43. elsif (defined $ipaddr) {
  44. $message="web commit from $ipaddr".(length $message ? ": $message" : "");
  45. }
  46. if (-d "$config{srcdir}/{arch}") {
  47. # Check to see if the page has been changed by someone
  48. # else since rcs_prepedit was called.
  49. my ($oldrev)=$rcstoken=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
  50. my $rev=`tla tree-id $config{srcdir}`;
  51. if (defined $rev && defined $oldrev && $rev ne $oldrev) {
  52. # Merge their changes into the file that we've
  53. # changed.
  54. if (quiet_system("tla", "update", "-d",
  55. "$config{srcdir}") != 0) {
  56. warn("tla update failed\n");
  57. }
  58. }
  59. if (quiet_system("tla", "commit",
  60. "-L".possibly_foolish_untaint($message),
  61. '-d', $config{srcdir}) != 0) {
  62. my $conflict=readfile("$config{srcdir}/$file");
  63. if (system("tla", "undo", "-n", "--quiet", "-d", "$config{srcdir}") != 0) {
  64. warn("tla undo failed\n");
  65. }
  66. return $conflict;
  67. }
  68. }
  69. return undef # success
  70. } #}}}
  71. sub rcs_add ($) { #{{{
  72. my $file=shift;
  73. if (-d "$config{srcdir}/{arch}") {
  74. if (quiet_system("tla", "add", "$config{srcdir}/$file") != 0) {
  75. warn("tla add failed\n");
  76. }
  77. }
  78. } #}}}
  79. sub rcs_recentchanges ($) {
  80. my $num=shift;
  81. my @ret;
  82. return unless -d "$config{srcdir}/{arch}";
  83. eval q{use Date::Parse};
  84. error($@) if $@;
  85. eval q{use Mail::Header};
  86. error($@) if $@;
  87. my $logs = `tla logs -d $config{srcdir}`;
  88. my @changesets = reverse split(/\n/, $logs);
  89. for (my $i=0; $i<$num && $i<$#changesets; $i++) {
  90. my ($change)=$changesets[$i]=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
  91. open(LOG, "tla cat-log -d $config{srcdir} $change|");
  92. my $head = Mail::Header->new(\*LOG);
  93. close(LOG);
  94. my $rev = $head->get("Revision");
  95. my $summ = $head->get("Summary");
  96. my $newfiles = $head->get("New-files");
  97. my $modfiles = $head->get("Modified-files");
  98. my $remfiles = $head->get("Removed-files");
  99. my $user = $head->get("Creator");
  100. my @paths = grep { !/^(.*\/)?\.arch-ids\/.*\.id$/ }
  101. split(/ /, "$newfiles $modfiles .arch-ids/fake.id");
  102. my $sdate = $head->get("Standard-date");
  103. my $when = time - str2time($sdate, 'UTC');
  104. my $committype = "web";
  105. if (defined $summ && $summ =~ /$config{web_commit_regexp}/) {
  106. $user = defined $2 ? "$2" : "$3";
  107. $summ = $4;
  108. }
  109. else {
  110. $committype="tla";
  111. }
  112. my @message;
  113. push @message, { line => escapeHTML($summ) };
  114. my @pages;
  115. foreach my $file (@paths) {
  116. my $diffurl=$config{diffurl};
  117. $diffurl=~s/\[\[file\]\]/$file/g;
  118. $diffurl=~s/\[\[rev\]\]/$change/g;
  119. push @pages, {
  120. page => pagename($file),
  121. diffurl => $diffurl,
  122. } if length $file;
  123. }
  124. push @ret, { rev => $change,
  125. user => $user,
  126. committype => $committype,
  127. when => $when,
  128. message => [@message],
  129. pages => [@pages],
  130. } if @pages;
  131. last if $i == $num;
  132. }
  133. return @ret;
  134. }
  135. sub rcs_notify () { #{{{
  136. # FIXME: Not set
  137. if (! exists $ENV{ARCH_VERSION}) {
  138. error("ARCH_VERSION is not set, not running from tla post-commit hook, cannot send notifications");
  139. }
  140. my $rev=int(possibly_foolish_untaint($ENV{REV}));
  141. eval q{use Mail::Header};
  142. error($@) if $@;
  143. open(LOG, $ENV{"ARCH_LOG"});
  144. my $head = Mail::Header->new(\*LOG);
  145. close(LOG);
  146. my $user = $head->get("Creator");
  147. my $newfiles = $head->get("New-files");
  148. my $modfiles = $head->get("Modified-files");
  149. my $remfiles = $head->get("Removed-files");
  150. my @changed_pages = grep { !/(^.*\/)?\.arch-ids\/.*\.id$/ }
  151. split(/ /, "$newfiles $modfiles $remfiles .arch-ids/fake.id");
  152. require IkiWiki::UserInfo;
  153. send_commit_mails(
  154. sub {
  155. my $message = $head->get("Summary");
  156. if ($message =~ /$config{web_commit_regexp}/) {
  157. $user=defined $2 ? "$2" : "$3";
  158. $message=$4;
  159. }
  160. },
  161. sub {
  162. my $logs = `tla logs -d $config{srcdir}`;
  163. my @changesets = reverse split(/\n/, $logs);
  164. my $i;
  165. for($i=0;$i<$#changesets;$i++) {
  166. last if $changesets[$i] eq $rev;
  167. }
  168. my $revminusone = $changesets[$i+1];
  169. `tla diff -d $ENV{ARCH_TREE_ROOT} $revminusone`;
  170. }, $user, @changed_pages);
  171. } #}}}
  172. sub rcs_getctime ($) { #{{{
  173. my $file=shift;
  174. eval q{use Date::Parse};
  175. error($@) if $@;
  176. eval q{use Mail::Header};
  177. error($@) if $@;
  178. my $logs = `tla logs -d $config{srcdir}`;
  179. my @changesets = reverse split(/\n/, $logs);
  180. my $sdate;
  181. for (my $i=0; $i<$#changesets; $i++) {
  182. my $change = $changesets[$i];
  183. open(LOG, "tla cat-log -d $config{srcdir} $change|");
  184. my $head = Mail::Header->new(\*LOG);
  185. close(LOG);
  186. $sdate = $head->get("Standard-date");
  187. my $newfiles = $head->get("New-files");
  188. my ($lastcreation) = grep {/^$file$/} split(/ /, "$newfiles");
  189. last if defined($lastcreation);
  190. }
  191. my $date=str2time($sdate, 'UTC');
  192. debug("found ctime ".localtime($date)." for $file");
  193. return $date;
  194. } #}}}
  195. 1