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