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