summaryrefslogtreecommitdiff
path: root/IkiWiki/Rcs/svn.pm
blob: 597295d44c44220a7a66999767d961d854e5cfec (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 Time::Duration;
  114. use XML::SAX;
  115. use XML::Simple;
  116. };
  117. error($@) if $@;
  118. # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
  119. my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
  120. do {
  121. $XML::Simple::PREFERRED_PARSER = pop @parsers;
  122. } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
  123. # --limit is only supported on Subversion 1.2.0+
  124. my $svn_version=`svn --version -q`;
  125. my $svn_limit='';
  126. $svn_limit="--limit $num"
  127. if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
  128. my $svn_url=svn_info("URL", $config{srcdir});
  129. my $xml = XMLin(scalar `svn $svn_limit --xml -v log '$svn_url'`,
  130. ForceArray => [ 'logentry', 'path' ],
  131. GroupTags => { paths => 'path' },
  132. KeyAttr => { path => 'content' },
  133. );
  134. foreach my $logentry (@{$xml->{logentry}}) {
  135. my (@pages, @message);
  136. my $rev = $logentry->{revision};
  137. my $user = $logentry->{author};
  138. my $when=time - str2time($logentry->{date}, 'UTC');
  139. foreach my $msgline (split(/\n/, $logentry->{msg})) {
  140. push @message, { line => $msgline };
  141. }
  142. my $committype="web";
  143. if (defined $message[0] &&
  144. $message[0]->{line}=~/$config{web_commit_regexp}/) {
  145. $user=defined $2 ? "$2" : "$3";
  146. $message[0]->{line}=$4;
  147. }
  148. else {
  149. $committype="svn";
  150. }
  151. foreach (keys %{$logentry->{paths}}) {
  152. next unless /^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
  153. my $file=$1;
  154. my $diffurl=$config{diffurl};
  155. $diffurl=~s/\[\[file\]\]/$file/g;
  156. $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
  157. $diffurl=~s/\[\[r2\]\]/$rev/g;
  158. push @pages, {
  159. page => pagename($file),
  160. diffurl => $diffurl,
  161. } if length $file;
  162. }
  163. push @ret, { rev => $rev,
  164. user => $user,
  165. committype => $committype,
  166. when => $when,
  167. message => [@message],
  168. pages => [@pages],
  169. } if @pages;
  170. return @ret if @ret >= $num;
  171. }
  172. return @ret;
  173. } #}}}
  174. sub rcs_notify () { #{{{
  175. if (! exists $ENV{REV}) {
  176. error("REV is not set, not running from svn post-commit hook, cannot send notifications");
  177. }
  178. my $rev=int(possibly_foolish_untaint($ENV{REV}));
  179. my $user=`svnlook author $config{svnrepo} -r $rev`;
  180. chomp $user;
  181. my $message=`svnlook log $config{svnrepo} -r $rev`;
  182. if ($message=~/$config{web_commit_regexp}/) {
  183. $user=defined $2 ? "$2" : "$3";
  184. $message=$4;
  185. }
  186. my @changed_pages;
  187. foreach my $change (`svnlook changed $config{svnrepo} -r $rev`) {
  188. chomp $change;
  189. if ($change =~ /^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
  190. push @changed_pages, $1;
  191. }
  192. }
  193. require IkiWiki::UserInfo;
  194. send_commit_mails(
  195. sub {
  196. return $message;
  197. },
  198. sub {
  199. `svnlook diff $config{svnrepo} -r $rev --no-diff-deleted`;
  200. }, $user, @changed_pages);
  201. } #}}}
  202. sub rcs_getctime ($) { #{{{
  203. my $file=shift;
  204. my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
  205. my $child = open(SVNLOG, "-|");
  206. if (! $child) {
  207. exec("svn", "log", $file) || error("svn log $file failed to run");
  208. }
  209. my $date;
  210. while (<SVNLOG>) {
  211. if (/$svn_log_infoline/) {
  212. $date=$1;
  213. }
  214. }
  215. close SVNLOG || warn "svn log $file exited $?";
  216. if (! defined $date) {
  217. warn "failed to parse svn log for $file\n";
  218. return 0;
  219. }
  220. eval q{use Date::Parse};
  221. error($@) if $@;
  222. $date=str2time($date);
  223. debug("found ctime ".localtime($date)." for $file");
  224. return $date;
  225. } #}}}
  226. 1