summaryrefslogtreecommitdiff
path: root/IkiWiki/Rcs/svn.pm
blob: b5f5fb445832d65e4115831fc885823ceb687a5d (plain)
  1. #!/usr/bin/perl
  2. # For subversion support.
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. package IkiWiki;
  7. my $svn_webcommit=qr/^web commit (by (\w+)|from (\d+\.\d+\.\d+\.\d+)):?(.*)/;
  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",
  57. "--encoding", "UTF-8", "-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. return unless -d "$config{srcdir}/.svn";
  87. eval q{use CGI 'escapeHTML'};
  88. eval q{use Date::Parse};
  89. eval q{use Time::Duration};
  90. eval q{use XML::SAX};
  91. eval q{use XML::Simple};
  92. # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
  93. my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
  94. do {
  95. $XML::Simple::PREFERRED_PARSER = pop @parsers;
  96. } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
  97. # --limit is only supported on Subversion 1.2.0+
  98. my $svn_version=`svn --version -q`;
  99. my $svn_limit='';
  100. $svn_limit="--limit $num"
  101. if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
  102. my $svn_url=svn_info("URL", $config{srcdir});
  103. my $xml = XMLin(scalar `svn $svn_limit --xml -v log '$svn_url'`,
  104. ForceArray => [ 'logentry', 'path' ],
  105. GroupTags => { paths => 'path' },
  106. KeyAttr => { path => 'content' },
  107. );
  108. foreach my $logentry (@{$xml->{logentry}}) {
  109. my (@pages, @message);
  110. my $rev = $logentry->{revision};
  111. my $user = $logentry->{author};
  112. my $date = $logentry->{date};
  113. $date =~ s/T/ /;
  114. $date =~ s/\.\d+Z$//;
  115. my $when=concise(ago(time - str2time($date, 'UTC')));
  116. foreach my $msgline (split(/\n/, $logentry->{msg})) {
  117. push @message, { line => escapeHTML($msgline) };
  118. }
  119. my $committype="web";
  120. if (defined $message[0] &&
  121. $message[0]->{line}=~/$svn_webcommit/) {
  122. $user=defined $2 ? "$2" : "$3";
  123. $message[0]->{line}=$4;
  124. }
  125. else {
  126. $committype="svn";
  127. }
  128. foreach (keys %{$logentry->{paths}}) {
  129. next unless /^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
  130. my $file=$1;
  131. my $diffurl=$config{diffurl};
  132. $diffurl=~s/\[\[file\]\]/$file/g;
  133. $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
  134. $diffurl=~s/\[\[r2\]\]/$rev/g;
  135. push @pages, {
  136. link => htmllink("", "", pagename($file), 1),
  137. diffurl => $diffurl,
  138. } if length $file;
  139. }
  140. push @ret, { rev => $rev,
  141. user => htmllink("", "", $user, 1),
  142. committype => $committype,
  143. when => $when,
  144. message => [@message],
  145. pages => [@pages],
  146. } if @pages;
  147. return @ret if @ret >= $num;
  148. }
  149. return @ret;
  150. } #}}}
  151. sub rcs_notify () { #{{{
  152. if (! exists $ENV{REV}) {
  153. error("REV is not set, not running from svn post-commit hook, cannot send notifications");
  154. }
  155. my $rev=int(possibly_foolish_untaint($ENV{REV}));
  156. my $user=`svnlook author $config{svnrepo} -r $rev`;
  157. chomp $user;
  158. my $message=`svnlook log $config{svnrepo} -r $rev`;
  159. if ($message=~/$svn_webcommit/) {
  160. $user=defined $2 ? "$2" : "$3";
  161. $message=$4;
  162. }
  163. my @changed_pages;
  164. foreach my $change (`svnlook changed $config{svnrepo} -r $rev`) {
  165. chomp $change;
  166. if ($change =~ /^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
  167. push @changed_pages, $1;
  168. }
  169. }
  170. require IkiWiki::UserInfo;
  171. my @email_recipients=commit_notify_list($user, @changed_pages);
  172. if (@email_recipients) {
  173. # TODO: if a commit spans multiple pages, this will send
  174. # subscribers a diff that might contain pages they did not
  175. # sign up for. Should separate the diff per page and
  176. # reassemble into one mail with just the pages subscribed to.
  177. my $diff=`svnlook diff $config{svnrepo} -r $rev --no-diff-deleted`;
  178. my $subject="$config{wikiname} update of ";
  179. if (@changed_pages > 2) {
  180. $subject.="$changed_pages[0] $changed_pages[1] etc";
  181. }
  182. else {
  183. $subject.=join(" ", @changed_pages);
  184. }
  185. $subject.=" by $user";
  186. my $template=template("notifymail.tmpl");
  187. $template->param(
  188. wikiname => $config{wikiname},
  189. diff => $diff,
  190. user => $user,
  191. message => $message,
  192. );
  193. eval q{use Mail::Sendmail};
  194. foreach my $email (@email_recipients) {
  195. sendmail(
  196. To => $email,
  197. From => "$config{wikiname} <$config{adminemail}>",
  198. Subject => $subject,
  199. Message => $template->output,
  200. ) or error("Failed to send update notification mail");
  201. }
  202. }
  203. } #}}}
  204. sub rcs_getctime ($) { #{{{
  205. my $file=shift;
  206. eval q{use Date::Parse};
  207. my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
  208. my $child = open(SVNLOG, "-|");
  209. if (! $child) {
  210. exec("svn", "log", $file) || error("svn log $file failed to run");
  211. }
  212. my $date;
  213. while (<SVNLOG>) {
  214. if (/$svn_log_infoline/) {
  215. $date=$1;
  216. }
  217. }
  218. close SVNLOG || warn "svn log $file exited $?";
  219. if (! defined $date) {
  220. warn "failed to parse svn log for $file\n";
  221. return 0;
  222. }
  223. $date=str2time($date);
  224. debug("found ctime ".localtime($date)." for $file");
  225. return $date;
  226. } #}}}
  227. 1