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