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