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