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