summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/cvs.pm
blob: 71566d212ba71ea3b8c2296ffeb46c0101bea92d (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 commitmessage (@) {
  167. my %params=@_;
  168. if (defined $params{session}) {
  169. if (defined $params{session}->param("name")) {
  170. return "web commit by ".
  171. $params{session}->param("name").
  172. (length $params{message} ? ": $params{message}" : "");
  173. }
  174. elsif (defined $params{session}->remote_addr()) {
  175. return "web commit from ".
  176. $params{session}->remote_addr().
  177. (length $params{message} ? ": $params{message}" : "");
  178. }
  179. }
  180. return $params{message};
  181. }
  182. sub rcs_commit (@) {
  183. # Tries to commit the page; returns undef on _success_ and
  184. # a version of the page with the rcs's conflict markers on failure.
  185. # The file is relative to the srcdir.
  186. my %params=@_;
  187. return unless cvs_is_controlling;
  188. # Check to see if the page has been changed by someone
  189. # else since rcs_prepedit was called.
  190. my ($oldrev)=$params{token}=~/^([0-9]+)$/; # untaint
  191. my $rev=cvs_info("Repository revision", "$config{srcdir}/$params{file}");
  192. if (defined $rev && defined $oldrev && $rev != $oldrev) {
  193. # Merge their changes into the file that we've
  194. # changed.
  195. cvs_runcvs('update', $params{file}) ||
  196. warn("cvs merge from $oldrev to $rev failed\n");
  197. }
  198. if (! cvs_runcvs('commit', '-m',
  199. IkiWiki::possibly_foolish_untaint(commitmessage(%params)))) {
  200. my $conflict=readfile("$config{srcdir}/$params{file}");
  201. cvs_runcvs('update', '-C', $params{file}) ||
  202. warn("cvs revert failed\n");
  203. return $conflict;
  204. }
  205. return undef # success
  206. }
  207. sub rcs_commit_staged (@) {
  208. # Commits all staged changes. Changes can be staged using rcs_add,
  209. # rcs_remove, and rcs_rename.
  210. my %params=@_;
  211. if (! cvs_runcvs('commit', '-m',
  212. IkiWiki::possibly_foolish_untaint(commitmessage(%params)))) {
  213. warn "cvs staged commit failed\n";
  214. return 1; # failure
  215. }
  216. return undef # success
  217. }
  218. sub rcs_add ($) {
  219. # filename is relative to the root of the srcdir
  220. my $file=shift;
  221. my $parent=IkiWiki::dirname($file);
  222. my @files_to_add = ($file);
  223. eval q{use File::MimeInfo};
  224. error($@) if $@;
  225. until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
  226. push @files_to_add, $parent;
  227. $parent = IkiWiki::dirname($parent);
  228. }
  229. while ($file = pop @files_to_add) {
  230. if (@files_to_add == 0) {
  231. # file
  232. my $filemime = File::MimeInfo::default($file);
  233. if (defined($filemime) && $filemime eq 'text/plain') {
  234. cvs_runcvs('add', $file) ||
  235. warn("cvs add $file failed\n");
  236. }
  237. else {
  238. cvs_runcvs('add', '-kb', $file) ||
  239. warn("cvs add binary $file failed\n");
  240. }
  241. }
  242. else {
  243. # directory
  244. cvs_runcvs('add', $file) ||
  245. warn("cvs add $file failed\n");
  246. }
  247. }
  248. }
  249. sub rcs_remove ($) {
  250. # filename is relative to the root of the srcdir
  251. my $file=shift;
  252. return unless cvs_is_controlling;
  253. cvs_runcvs('rm', '-f', $file) ||
  254. warn("cvs rm $file failed\n");
  255. }
  256. sub rcs_rename ($$) {
  257. # filenames relative to the root of the srcdir
  258. my ($src, $dest)=@_;
  259. return unless cvs_is_controlling;
  260. local $CWD = $config{srcdir};
  261. if (system("mv", "$src", "$dest") != 0) {
  262. warn("filesystem rename failed\n");
  263. }
  264. rcs_add($dest);
  265. rcs_remove($src);
  266. }
  267. sub rcs_recentchanges ($) {
  268. my $num = shift;
  269. my @ret;
  270. return unless cvs_is_controlling;
  271. eval q{use Date::Parse};
  272. error($@) if $@;
  273. local $CWD = $config{srcdir};
  274. # There's no cvsps option to get the last N changesets.
  275. # Write full output to a temp file and read backwards.
  276. eval q{use File::Temp qw/tempfile/};
  277. error($@) if $@;
  278. eval q{use File::ReadBackwards};
  279. error($@) if $@;
  280. my ($tmphandle, $tmpfile) = tempfile();
  281. system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
  282. if ($? == -1) {
  283. error "couldn't run cvsps: $!\n";
  284. }
  285. elsif (($? >> 8) != 0) {
  286. error "cvsps exited " . ($? >> 8) . ": $!\n";
  287. }
  288. tie(*SPSVC, 'File::ReadBackwards', $tmpfile)
  289. || error "couldn't open $tmpfile for read: $!\n";
  290. while (my $line = <SPSVC>) {
  291. $line =~ /^$/ || error "expected blank line, got $line";
  292. my ($rev, $user, $committype, $when);
  293. my (@message, @pages);
  294. # We're reading backwards.
  295. # Forwards, an entry looks like so:
  296. # ---------------------
  297. # PatchSet $rev
  298. # Date: $when
  299. # Author: $user (or user CGI runs as, for web commits)
  300. # Branch: branch
  301. # Tag: tag
  302. # Log:
  303. # @message_lines
  304. # Members:
  305. # @pages (and revisions)
  306. #
  307. while ($line = <SPSVC>) {
  308. last if ($line =~ /^Members:/);
  309. for ($line) {
  310. s/^\s+//;
  311. s/\s+$//;
  312. }
  313. my ($page, $revs) = split(/:/, $line);
  314. my ($oldrev, $newrev) = split(/->/, $revs);
  315. $oldrev =~ s/INITIAL/0/;
  316. $newrev =~ s/\(DEAD\)//;
  317. my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
  318. $diffurl=~s/\[\[file\]\]/$page/g;
  319. $diffurl=~s/\[\[r1\]\]/$oldrev/g;
  320. $diffurl=~s/\[\[r2\]\]/$newrev/g;
  321. unshift @pages, {
  322. page => pagename($page),
  323. diffurl => $diffurl,
  324. } if length $page;
  325. }
  326. while ($line = <SPSVC>) {
  327. last if ($line =~ /^Log:$/);
  328. chomp $line;
  329. unshift @message, { line => $line };
  330. }
  331. $committype = "web";
  332. if (defined $message[0] &&
  333. $message[0]->{line}=~/$config{web_commit_regexp}/) {
  334. $user=defined $2 ? "$2" : "$3";
  335. $message[0]->{line}=$4;
  336. }
  337. else {
  338. $committype="cvs";
  339. }
  340. $line = <SPSVC>; # Tag
  341. $line = <SPSVC>; # Branch
  342. $line = <SPSVC>;
  343. if ($line =~ /^Author: (.*)$/) {
  344. $user = $1 unless defined $user && length $user;
  345. }
  346. else {
  347. error "expected Author, got $line";
  348. }
  349. $line = <SPSVC>;
  350. if ($line =~ /^Date: (.*)$/) {
  351. $when = str2time($1, 'UTC');
  352. }
  353. else {
  354. error "expected Date, got $line";
  355. }
  356. $line = <SPSVC>;
  357. if ($line =~ /^PatchSet (.*)$/) {
  358. $rev = $1;
  359. }
  360. else {
  361. error "expected PatchSet, got $line";
  362. }
  363. $line = <SPSVC>; # ---------------------
  364. push @ret, {
  365. rev => $rev,
  366. user => $user,
  367. committype => $committype,
  368. when => $when,
  369. message => [@message],
  370. pages => [@pages],
  371. } if @pages;
  372. last if @ret >= $num;
  373. }
  374. unlink($tmpfile) || error "couldn't unlink $tmpfile: $!\n";
  375. return @ret;
  376. }
  377. sub rcs_diff ($;$) {
  378. my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
  379. my $maxlines=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. local $CWD = $config{srcdir};
  398. my $cvs_log_infoline=qr/^date: (.+);\s+author/;
  399. open CVSLOG, "cvs -Q log -r1.1 '$file' |"
  400. || error "couldn't get cvs log output: $!\n";
  401. my $date;
  402. while (<CVSLOG>) {
  403. if (/$cvs_log_infoline/) {
  404. $date=$1;
  405. }
  406. }
  407. close CVSLOG || warn "cvs log $file exited $?";
  408. if (! defined $date) {
  409. warn "failed to parse cvs log for $file\n";
  410. return 0;
  411. }
  412. eval q{use Date::Parse};
  413. error($@) if $@;
  414. $date=str2time($date, 'UTC');
  415. debug("found ctime ".localtime($date)." for $file");
  416. return $date;
  417. }
  418. sub rcs_getmtime ($) {
  419. error "rcs_getmtime is not implemented for cvs\n"; # TODO
  420. }
  421. 1