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