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