summaryrefslogtreecommitdiff
path: root/IkiWiki/Rcs/SVN.pm
blob: dd74a05773fec68d477c94aca0666892ec74615b (plain)
  1. #!/usr/bin/perl
  2. # For subversion support.
  3. use warnings;
  4. use strict;
  5. package IkiWiki;
  6. my $svn_log_infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
  7. sub svn_info ($$) { #{{{
  8. my $field=shift;
  9. my $file=shift;
  10. my $info=`LANG=C svn info $file`;
  11. my ($ret)=$info=~/^$field: (.*)$/m;
  12. return $ret;
  13. } #}}}
  14. sub rcs_update () { #{{{
  15. if (-d "$config{srcdir}/.svn") {
  16. if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
  17. warn("svn update failed\n");
  18. }
  19. }
  20. } #}}}
  21. sub rcs_prepedit ($) { #{{{
  22. # Prepares to edit a file under revision control. Returns a token
  23. # that must be passed into rcs_commit when the file is ready
  24. # for committing.
  25. # The file is relative to the srcdir.
  26. my $file=shift;
  27. if (-d "$config{srcdir}/.svn") {
  28. # For subversion, return the revision of the file when
  29. # editing begins.
  30. my $rev=svn_info("Revision", "$config{srcdir}/$file");
  31. return defined $rev ? $rev : "";
  32. }
  33. } #}}}
  34. sub rcs_commit ($$$) { #{{{
  35. # Tries to commit the page; returns undef on _success_ and
  36. # a version of the page with the rcs's conflict markers on failure.
  37. # The file is relative to the srcdir.
  38. my $file=shift;
  39. my $message=shift;
  40. my $rcstoken=shift;
  41. if (-d "$config{srcdir}/.svn") {
  42. # Check to see if the page has been changed by someone
  43. # else since rcs_prepedit was called.
  44. my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
  45. my $rev=svn_info("Revision", "$config{srcdir}/$file");
  46. if (defined $rev && defined $oldrev && $rev != $oldrev) {
  47. # Merge their changes into the file that we've
  48. # changed.
  49. chdir($config{srcdir}); # svn merge wants to be here
  50. if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
  51. "$config{srcdir}/$file") != 0) {
  52. warn("svn merge -r$oldrev:$rev failed\n");
  53. }
  54. }
  55. if (system("svn", "commit", "--quiet", "-m",
  56. possibly_foolish_untaint($message),
  57. "$config{srcdir}") != 0) {
  58. my $conflict=readfile("$config{srcdir}/$file");
  59. if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
  60. warn("svn revert failed\n");
  61. }
  62. return $conflict;
  63. }
  64. }
  65. return undef # success
  66. } #}}}
  67. sub rcs_add ($) { #{{{
  68. # filename is relative to the root of the srcdir
  69. my $file=shift;
  70. if (-d "$config{srcdir}/.svn") {
  71. my $parent=dirname($file);
  72. while (! -d "$config{srcdir}/$parent/.svn") {
  73. $file=$parent;
  74. $parent=dirname($file);
  75. }
  76. if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
  77. warn("svn add failed\n");
  78. }
  79. }
  80. } #}}}
  81. sub rcs_recentchanges ($) { #{{{
  82. my $num=shift;
  83. my @ret;
  84. eval q{use CGI 'escapeHTML'};
  85. eval q{use Date::Parse};
  86. eval q{use Time::Duration};
  87. if (-d "$config{srcdir}/.svn") {
  88. my $svn_url=svn_info("URL", $config{srcdir});
  89. my $div=qr/^--------------------+$/;
  90. my $state='start';
  91. my ($rev, $user, $when, @pages, @message);
  92. foreach (`LANG=C svn log -v '$svn_url'`) {
  93. chomp;
  94. if ($state eq 'start' && /$div/) {
  95. $state='header';
  96. }
  97. elsif ($state eq 'header' && /$svn_log_infoline/) {
  98. $rev=$1;
  99. $user=$2;
  100. $when=concise(ago(time - str2time($3)));
  101. }
  102. elsif ($state eq 'header' && /^\s+[A-Z]+\s+\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/) {
  103. my $file=$1;
  104. my $diffurl=$config{diffurl};
  105. $diffurl=~s/\[\[file\]\]/$file/g;
  106. $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
  107. $diffurl=~s/\[\[r2\]\]/$rev/g;
  108. push @pages, {
  109. link => htmllink("", pagename($file), 1),
  110. diffurl => $diffurl,
  111. } if length $file;
  112. }
  113. elsif ($state eq 'header' && /^$/) {
  114. $state='body';
  115. }
  116. elsif ($state eq 'body' && /$div/) {
  117. my $committype="web";
  118. if (defined $message[0] &&
  119. $message[0]->{line}=~/^web commit by (\w+):?(.*)/) {
  120. $user="$1";
  121. $message[0]->{line}=$2;
  122. }
  123. else {
  124. $committype="svn";
  125. }
  126. push @ret, { rev => $rev,
  127. user => htmllink("", $user, 1),
  128. committype => $committype,
  129. when => $when, message => [@message],
  130. pages => [@pages],
  131. } if @pages;
  132. return @ret if @ret >= $num;
  133. $state='header';
  134. $rev=$user=$when=undef;
  135. @pages=@message=();
  136. }
  137. elsif ($state eq 'body') {
  138. push @message, {line => escapeHTML($_)},
  139. }
  140. }
  141. }
  142. return @ret;
  143. } #}}}
  144. sub rcs_notify () { #{{{
  145. if (! exists $ENV{REV}) {
  146. error("REV is not set, not running from svn post-commit hook, cannot send notifications");
  147. }
  148. my @changed_pages;
  149. foreach my $change (`svnlook changed $config{svnrepo} -r $ENV{REV}`) {
  150. chomp;
  151. if (/^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
  152. push @changed_pages, $1;
  153. }
  154. }
  155. require IkiWiki::UserInfo;
  156. my @email_recipients=page_subscribers(@changed_pages);
  157. if (@email_recipients) {
  158. eval q{use Mail::Sendmail};
  159. # TODO: if a commit spans multiple pages, this will send
  160. # subscribers a diff that might contain pages they did not
  161. # sign up for. Should separate the diff per page and
  162. # reassemble into one mail with just the pages subscribed to.
  163. my $body=`LANG=C svnlook diff $config{svnrepo} -r $ENV{REV} --no-diff-deleted`;
  164. foreach my $email (@email_recipients) {
  165. sendmail(
  166. To => $email,
  167. From => "$config{wikiname} <$config{adminemail}>",
  168. Subject => "$config{wikiname} $ENV{REV} update notification",
  169. Message => $body,
  170. ) or error("Failed to send update notification mail");
  171. }
  172. }
  173. } #}}}
  174. sub rcs_getctime () { #{{{
  175. eval q{use Date::Parse};
  176. foreach my $page (keys %pagectime) {
  177. my $file="$config{srcdir}/$pagesources{$page}";
  178. next unless -e $file;
  179. my $child = open(SVNLOG, "-|");
  180. if (! $child) {
  181. exec("svn", "log", $file) || error("svn log $file failed to run");
  182. }
  183. my $date;
  184. while (<SVNLOG>) {
  185. if (/$svn_log_infoline/) {
  186. $date=$3;
  187. }
  188. }
  189. close SVNLOG || warn "svn log $file exited $?";
  190. if (! defined $date) {
  191. warn "failed to parse svn log for $file\n";
  192. next;
  193. }
  194. $pagectime{$page}=$date=str2time($date);
  195. debug("found ctime ".localtime($date)." for $page");
  196. }
  197. } #}}}
  198. 1