summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/cvs.pm
blob: a9fe162a1b7957e4f10e16163bea303b36936b7c (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::cvs;
  3. # Copyright (c) 2009 Amitai Schlair
  4. # All rights reserved.
  5. #
  6. # This code is derived from software contributed to ikiwiki
  7. # by Amitai Schlair.
  8. #
  9. # Redistribution and use in source and binary forms, with or without
  10. # modification, are permitted provided that the following conditions
  11. # are met:
  12. # 1. Redistributions of source code must retain the above copyright
  13. # notice, this list of conditions and the following disclaimer.
  14. # 2. Redistributions in binary form must reproduce the above copyright
  15. # notice, this list of conditions and the following disclaimer in the
  16. # documentation and/or other materials provided with the distribution.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY IKIWIKI AND CONTRIBUTORS ``AS IS''
  19. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  20. # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  21. # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION
  22. # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  25. # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  28. # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. # SUCH DAMAGE.
  30. use warnings;
  31. use strict;
  32. use IkiWiki;
  33. use File::chdir;
  34. sub import {
  35. hook(type => "genwrapper", id => "cvs", call => \&genwrapper);
  36. hook(type => "checkconfig", id => "cvs", call => \&checkconfig);
  37. hook(type => "getsetup", id => "cvs", call => \&getsetup);
  38. hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
  39. hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
  40. hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
  41. hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
  42. hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
  43. hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
  44. hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
  45. hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
  46. hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
  47. hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
  48. hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
  49. }
  50. sub genwrapper () {
  51. return <<EOF;
  52. {
  53. int j;
  54. for (j = 1; j < argc; j++)
  55. if (strstr(argv[j], "New directory") != NULL)
  56. exit(0);
  57. }
  58. EOF
  59. }
  60. sub checkconfig () {
  61. if (! defined $config{cvspath}) {
  62. $config{cvspath}="ikiwiki";
  63. }
  64. if (exists $config{cvspath}) {
  65. # code depends on the path not having extraneous slashes
  66. $config{cvspath}=~tr#/#/#s;
  67. $config{cvspath}=~s/\/$//;
  68. $config{cvspath}=~s/^\///;
  69. }
  70. if (defined $config{cvs_wrapper} && length $config{cvs_wrapper}) {
  71. push @{$config{wrappers}}, {
  72. wrapper => $config{cvs_wrapper},
  73. wrappermode => (defined $config{cvs_wrappermode} ? $config{cvs_wrappermode} : "04755"),
  74. };
  75. }
  76. }
  77. sub getsetup () {
  78. return
  79. plugin => {
  80. safe => 0, # rcs plugin
  81. rebuild => undef,
  82. section => "rcs",
  83. },
  84. cvsrepo => {
  85. type => "string",
  86. example => "/cvs/wikirepo",
  87. description => "cvs repository location",
  88. safe => 0, # path
  89. rebuild => 0,
  90. },
  91. cvspath => {
  92. type => "string",
  93. example => "ikiwiki",
  94. description => "path inside repository where the wiki is located",
  95. safe => 0, # paranoia
  96. rebuild => 0,
  97. },
  98. cvs_wrapper => {
  99. type => "string",
  100. example => "/cvs/wikirepo/CVSROOT/post-commit",
  101. description => "cvs post-commit hook to generate (triggered by CVSROOT/loginfo entry)",
  102. safe => 0, # file
  103. rebuild => 0,
  104. },
  105. cvs_wrappermode => {
  106. type => "string",
  107. example => '04755',
  108. description => "mode for cvs_wrapper (can safely be made suid)",
  109. safe => 0,
  110. rebuild => 0,
  111. },
  112. historyurl => {
  113. type => "string",
  114. example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]]",
  115. description => "cvsweb url to show file history ([[file]] substituted)",
  116. safe => 1,
  117. rebuild => 1,
  118. },
  119. diffurl => {
  120. type => "string",
  121. example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]].diff?r1=text&amp;tr1=[[r1]]&amp;r2=text&amp;tr2=[[r2]]",
  122. description => "cvsweb url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
  123. safe => 1,
  124. rebuild => 1,
  125. },
  126. }
  127. sub cvs_info ($$) {
  128. my $field=shift;
  129. my $file=shift;
  130. local $CWD = $config{srcdir};
  131. my $info=`cvs status $file`;
  132. my ($ret)=$info=~/^\s*$field:\s*(\S+)/m;
  133. return $ret;
  134. }
  135. sub cvs_runcvs(@) {
  136. my @cmd = @_;
  137. unshift @cmd, 'cvs', '-Q';
  138. local $CWD = $config{srcdir};
  139. open(my $savedout, ">&STDOUT");
  140. open(STDOUT, ">", "/dev/null");
  141. my $ret = system(@cmd);
  142. open(STDOUT, ">&", $savedout);
  143. return ($ret == 0) ? 1 : 0;
  144. }
  145. sub cvs_is_controlling {
  146. my $dir=shift;
  147. $dir=$config{srcdir} unless defined($dir);
  148. return (-d "$dir/CVS") ? 1 : 0;
  149. }
  150. sub rcs_update () {
  151. return unless cvs_is_controlling;
  152. cvs_runcvs('update', '-dP');
  153. }
  154. sub rcs_prepedit ($) {
  155. # Prepares to edit a file under revision control. Returns a token
  156. # that must be passed into rcs_commit when the file is ready
  157. # for committing.
  158. # The file is relative to the srcdir.
  159. my $file=shift;
  160. return unless cvs_is_controlling;
  161. # For cvs, return the revision of the file when
  162. # editing begins.
  163. my $rev=cvs_info("Repository revision", "$file");
  164. return defined $rev ? $rev : "";
  165. }
  166. sub rcs_commit ($$$;$$$) {
  167. # Tries to commit the page; returns undef on _success_ and
  168. # a version of the page with the rcs's conflict markers on failure.
  169. # The file is relative to the srcdir.
  170. my $file=shift;
  171. my $message=shift;
  172. my $rcstoken=shift;
  173. my $user=shift;
  174. my $ipaddr=shift;
  175. my $emailuser=shift;
  176. return unless cvs_is_controlling;
  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. # Check to see if the page has been changed by someone
  184. # else since rcs_prepedit was called.
  185. my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
  186. my $rev=cvs_info("Repository revision", "$config{srcdir}/$file");
  187. if (defined $rev && defined $oldrev && $rev != $oldrev) {
  188. # Merge their changes into the file that we've
  189. # changed.
  190. cvs_runcvs('update', $file) ||
  191. warn("cvs merge from $oldrev to $rev failed\n");
  192. }
  193. if (! cvs_runcvs('commit', '-m',
  194. IkiWiki::possibly_foolish_untaint $message)) {
  195. my $conflict=readfile("$config{srcdir}/$file");
  196. cvs_runcvs('update', '-C', $file) ||
  197. warn("cvs revert failed\n");
  198. return $conflict;
  199. }
  200. return undef # success
  201. }
  202. sub rcs_commit_staged ($$$;$) {
  203. # Commits all staged changes. Changes can be staged using rcs_add,
  204. # rcs_remove, and rcs_rename.
  205. my ($message, $user, $ipaddr, $emailuser)=@_;
  206. if (defined $user) {
  207. $message="web commit by $user".(length $message ? ": $message" : "");
  208. }
  209. elsif (defined $ipaddr) {
  210. $message="web commit from $ipaddr".(length $message ? ": $message" : "");
  211. }
  212. if (! cvs_runcvs('commit', '-m',
  213. IkiWiki::possibly_foolish_untaint $message)) {
  214. warn "cvs staged commit failed\n";
  215. return 1; # failure
  216. }
  217. return undef # success
  218. }
  219. sub rcs_add ($) {
  220. # filename is relative to the root of the srcdir
  221. my $file=shift;
  222. my $parent=IkiWiki::dirname($file);
  223. my @files_to_add = ($file);
  224. eval q{use File::MimeInfo};
  225. error($@) if $@;
  226. until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
  227. push @files_to_add, $parent;
  228. $parent = IkiWiki::dirname($parent);
  229. }
  230. while ($file = pop @files_to_add) {
  231. if (@files_to_add == 0) {
  232. # file
  233. my $filemime = File::MimeInfo::default($file);
  234. if (defined($filemime) && $filemime eq 'text/plain') {
  235. cvs_runcvs('add', $file) ||
  236. warn("cvs add $file failed\n");
  237. }
  238. else {
  239. cvs_runcvs('add', '-kb', $file) ||
  240. warn("cvs add binary $file failed\n");
  241. }
  242. }
  243. else {
  244. # directory
  245. cvs_runcvs('add', $file) ||
  246. warn("cvs add $file failed\n");
  247. }
  248. }
  249. }
  250. sub rcs_remove ($) {
  251. # filename is relative to the root of the srcdir
  252. my $file=shift;
  253. return unless cvs_is_controlling;
  254. cvs_runcvs('rm', '-f', $file) ||
  255. warn("cvs rm $file failed\n");
  256. }
  257. sub rcs_rename ($$) {
  258. # filenames relative to the root of the srcdir
  259. my ($src, $dest)=@_;
  260. return unless cvs_is_controlling;
  261. local $CWD = $config{srcdir};
  262. if (system("mv", "$src", "$dest") != 0) {
  263. warn("filesystem rename failed\n");
  264. }
  265. rcs_add($dest);
  266. rcs_remove($src);
  267. }
  268. sub rcs_recentchanges ($) {
  269. my $num = shift;
  270. my @ret;
  271. return unless cvs_is_controlling;
  272. eval q{use Date::Parse};
  273. error($@) if $@;
  274. local $CWD = $config{srcdir};
  275. # There's no cvsps option to get the last N changesets.
  276. # Write full output to a temp file and read backwards.
  277. eval q{use File::Temp qw/tempfile/};
  278. error($@) if $@;
  279. eval q{use File::ReadBackwards};
  280. error($@) if $@;
  281. my ($tmphandle, $tmpfile) = tempfile();
  282. system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
  283. if ($? == -1) {
  284. error "couldn't run cvsps: $!\n";
  285. }
  286. elsif (($? >> 8) != 0) {
  287. error "cvsps exited " . ($? >> 8) . ": $!\n";
  288. }
  289. tie(*SPSVC, 'File::ReadBackwards', $tmpfile)
  290. || error "couldn't open $tmpfile for read: $!\n";
  291. while (my $line = <SPSVC>) {
  292. $line =~ /^$/ || error "expected blank line, got $line";
  293. my ($rev, $user, $committype, $when);
  294. my (@message, @pages);
  295. # We're reading backwards.
  296. # Forwards, an entry looks like so:
  297. # ---------------------
  298. # PatchSet $rev
  299. # Date: $when
  300. # Author: $user (or user CGI runs as, for web commits)
  301. # Branch: branch
  302. # Tag: tag
  303. # Log:
  304. # @message_lines
  305. # Members:
  306. # @pages (and revisions)
  307. #
  308. while ($line = <SPSVC>) {
  309. last if ($line =~ /^Members:/);
  310. for ($line) {
  311. s/^\s+//;
  312. s/\s+$//;
  313. }
  314. my ($page, $revs) = split(/:/, $line);
  315. my ($oldrev, $newrev) = split(/->/, $revs);
  316. $oldrev =~ s/INITIAL/0/;
  317. $newrev =~ s/\(DEAD\)//;
  318. my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
  319. $diffurl=~s/\[\[file\]\]/$page/g;
  320. $diffurl=~s/\[\[r1\]\]/$oldrev/g;
  321. $diffurl=~s/\[\[r2\]\]/$newrev/g;
  322. unshift @pages, {
  323. page => pagename($page),
  324. diffurl => $diffurl,
  325. } if length $page;
  326. }
  327. while ($line = <SPSVC>) {
  328. last if ($line =~ /^Log:$/);
  329. chomp $line;
  330. unshift @message, { line => $line };
  331. }
  332. $committype = "web";
  333. if (defined $message[0] &&
  334. $message[0]->{line}=~/$config{web_commit_regexp}/) {
  335. $user=defined $2 ? "$2" : "$3";
  336. $message[0]->{line}=$4;
  337. }
  338. else {
  339. $committype="cvs";
  340. }
  341. $line = <SPSVC>; # Tag
  342. $line = <SPSVC>; # Branch
  343. $line = <SPSVC>;
  344. if ($line =~ /^Author: (.*)$/) {
  345. $user = $1 unless defined $user && length $user;
  346. }
  347. else {
  348. error "expected Author, got $line";
  349. }
  350. $line = <SPSVC>;
  351. if ($line =~ /^Date: (.*)$/) {
  352. $when = str2time($1, 'UTC');
  353. }
  354. else {
  355. error "expected Date, got $line";
  356. }
  357. $line = <SPSVC>;
  358. if ($line =~ /^PatchSet (.*)$/) {
  359. $rev = $1;
  360. }
  361. else {
  362. error "expected PatchSet, got $line";
  363. }
  364. $line = <SPSVC>; # ---------------------
  365. push @ret, {
  366. rev => $rev,
  367. user => $user,
  368. committype => $committype,
  369. when => $when,
  370. message => [@message],
  371. pages => [@pages],
  372. } if @pages;
  373. last if @ret >= $num;
  374. }
  375. unlink($tmpfile) || error "couldn't unlink $tmpfile: $!\n";
  376. return @ret;
  377. }
  378. sub rcs_diff ($) {
  379. my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
  380. local $CWD = $config{srcdir};
  381. # diff output is unavoidably preceded by the cvsps PatchSet entry
  382. my @cvsps = `env TZ=UTC cvsps -q --cvs-direct -z 30 -g -s $rev`;
  383. my $blank_lines_seen = 0;
  384. while (my $line = shift @cvsps) {
  385. $blank_lines_seen++ if ($line =~ /^$/);
  386. last if $blank_lines_seen == 2;
  387. }
  388. if (wantarray) {
  389. return @cvsps;
  390. }
  391. else {
  392. return join("", @cvsps);
  393. }
  394. }
  395. sub rcs_getctime ($) {
  396. my $file=shift;
  397. my $cvs_log_infoline=qr/^date: (.+);\s+author/;
  398. open CVSLOG, "cvs -Q log -r1.1 '$file' |"
  399. || error "couldn't get cvs log output: $!\n";
  400. my $date;
  401. while (<CVSLOG>) {
  402. if (/$cvs_log_infoline/) {
  403. $date=$1;
  404. }
  405. }
  406. close CVSLOG || warn "cvs log $file exited $?";
  407. if (! defined $date) {
  408. warn "failed to parse cvs log for $file\n";
  409. return 0;
  410. }
  411. eval q{use Date::Parse};
  412. error($@) if $@;
  413. $date=str2time($date, 'UTC');
  414. debug("found ctime ".localtime($date)." for $file");
  415. return $date;
  416. }
  417. sub rcs_getmtime ($) {
  418. error "rcs_getmtime is not implemented for cvs\n"; # TODO
  419. }
  420. 1