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