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