summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/cvs.pm
blob: 360d9724950c866c7cd4e5e6ca99053124d45b99 (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. return unless cvs_is_controlling;
  176. if (defined $user) {
  177. $message="web commit by $user".(length $message ? ": $message" : "");
  178. }
  179. elsif (defined $ipaddr) {
  180. $message="web commit from $ipaddr".(length $message ? ": $message" : "");
  181. }
  182. # Check to see if the page has been changed by someone
  183. # else since rcs_prepedit was called.
  184. my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
  185. my $rev=cvs_info("Repository revision", "$config{srcdir}/$file");
  186. if (defined $rev && defined $oldrev && $rev != $oldrev) {
  187. # Merge their changes into the file that we've
  188. # changed.
  189. cvs_runcvs('update', $file) ||
  190. warn("cvs merge from $oldrev to $rev failed\n");
  191. }
  192. if (! cvs_runcvs('commit', '-m',
  193. IkiWiki::possibly_foolish_untaint $message)) {
  194. my $conflict=readfile("$config{srcdir}/$file");
  195. cvs_runcvs('update', '-C', $file) ||
  196. warn("cvs revert failed\n");
  197. return $conflict;
  198. }
  199. return undef # success
  200. }
  201. sub rcs_commit_staged ($$$) {
  202. # Commits all staged changes. Changes can be staged using rcs_add,
  203. # rcs_remove, and rcs_rename.
  204. my ($message, $user, $ipaddr)=@_;
  205. if (defined $user) {
  206. $message="web commit by $user".(length $message ? ": $message" : "");
  207. }
  208. elsif (defined $ipaddr) {
  209. $message="web commit from $ipaddr".(length $message ? ": $message" : "");
  210. }
  211. if (! cvs_runcvs('commit', '-m',
  212. IkiWiki::possibly_foolish_untaint $message)) {
  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. local $CWD = $config{srcdir};
  380. # diff output is unavoidably preceded by the cvsps PatchSet entry
  381. my @cvsps = `env TZ=UTC cvsps -q --cvs-direct -z 30 -g -s $rev`;
  382. my $blank_lines_seen = 0;
  383. while (my $line = shift @cvsps) {
  384. $blank_lines_seen++ if ($line =~ /^$/);
  385. last if $blank_lines_seen == 2;
  386. }
  387. if (wantarray) {
  388. return @cvsps;
  389. }
  390. else {
  391. return join("", @cvsps);
  392. }
  393. }
  394. sub rcs_getctime ($) {
  395. my $file=shift;
  396. my $cvs_log_infoline=qr/^date: (.+);\s+author/;
  397. open CVSLOG, "cvs -Q log -r1.1 '$file' |"
  398. || error "couldn't get cvs log output: $!\n";
  399. my $date;
  400. while (<CVSLOG>) {
  401. if (/$cvs_log_infoline/) {
  402. $date=$1;
  403. }
  404. }
  405. close CVSLOG || warn "cvs log $file exited $?";
  406. if (! defined $date) {
  407. warn "failed to parse cvs log for $file\n";
  408. return 0;
  409. }
  410. eval q{use Date::Parse};
  411. error($@) if $@;
  412. $date=str2time($date, 'UTC');
  413. debug("found ctime ".localtime($date)." for $file");
  414. return $date;
  415. }
  416. sub rcs_getmtime ($) {
  417. error "rcs_getmtime is not implemented for cvs\n"; # TODO
  418. }
  419. 1