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