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