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