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