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