diff 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #!/usr/bin/perl
  2. # $Id: diff,v 1.2 2006/12/07 04:59:38 reed%reedloden.com Exp $
  3. # diff -- Display diff output with markup.
  4. #
  5. # Arne Georg Gleditsch <argggh@ifi.uio.no>
  6. # Per Kristian Gjermshus <pergj@ifi.uio.no>
  7. #
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. ######################################################################
  23. use lib 'lib';
  24. use SimpleParse;
  25. use LXR::Common;
  26. use LXR::Config;
  27. use DB_File;
  28. sub htmlsub {
  29. my ($s, $l) = @_;
  30. my $x = $s;
  31. while ($x =~ s#<style>(.*?)</style>##) {
  32. $l += length $1;
  33. }
  34. my @s = split(/(<[^>]*>|&[\#\w\d]+;?)/, $s);
  35. $s = '';
  36. while (@s) {
  37. my $f = substr(shift(@s), 0, $l);
  38. $l -= length($f);
  39. $s .= $f;
  40. $f = shift(@s);
  41. if ($f =~ /^&/) {
  42. if ($l > 0) {
  43. $s .= $f;
  44. $l--;
  45. }
  46. } else {
  47. $s .= $f;
  48. }
  49. }
  50. $s .= ' ' x $l;
  51. return $s;
  52. }
  53. my $file;
  54. my $file2;
  55. sub treeoption {
  56. my ($tree, $defaulttree, $novalue) = @_;
  57. my $value = " value='$tree'";
  58. my $selected;
  59. if ($tree eq $defaulttree) {
  60. $selected = ' selected=1';
  61. $value = '' if $novalue;
  62. }
  63. return " <option$selected$value>$tree</option>
  64. ";
  65. }
  66. sub cleanhtml {
  67. my $text = shift;
  68. $text =~ s/"/&quot;/g;
  69. $text =~ s/'/&#039;/g;
  70. $text =~ s/&/&amp;/g;
  71. $text =~ s/</&lt;/g;
  72. $text =~ s/>/&gt;/g;
  73. return $text;
  74. }
  75. sub printdiff {
  76. my ($virt, $file) = (cleanhtml $Path->{'virt'}, cleanhtml $Path->{'file'});
  77. my @similartrees = ();
  78. my @othertrees = ();
  79. my @treelist = @{$Conf->{'trees'}};
  80. print "<p class=desc>
  81. </p>
  82. <form name='diff' id='diff' class='beforecontent'>
  83. <label for='file'>Base file to compare:</label>
  84. <input id='file' name='file' size=60 value='$virt$file'>
  85. ";
  86. if (scalar @treelist > 0) {
  87. foreach my $othertree (@treelist) {
  88. my $otherpath = $Conf->{'treehash'}{$othertree}.'/'.$virt.'/'.$file;
  89. if (-e $otherpath) {
  90. push @similartrees, $othertree;
  91. } else {
  92. push @othertrees, $othertree;
  93. }
  94. }
  95. print "
  96. <label for='tree'>in</label>
  97. <select name='tree' id='tree'>
  98. ";
  99. foreach my $othertree (@treelist) {
  100. print treeoption($othertree, $Conf->{'treename'}, 1);
  101. }
  102. print '
  103. </select>
  104. <br>
  105. <label for="diffvar" title="Please select">Tree against which you wish to
  106. compare this file:</label>
  107. <select name="diffvar" id="diffvar">
  108. ';
  109. if (scalar @similartrees) {
  110. print " <option value=''>--(this path)--</option>
  111. ";
  112. foreach my $othertree (@similartrees) {
  113. print treeoption($othertree, $diffvar);
  114. }
  115. }
  116. if (scalar @othertrees) {
  117. print " <option value=''>--(another path)--</option>
  118. ";
  119. foreach my $othertree (@othertrees) {
  120. print treeoption($othertree, $diffvar);
  121. }
  122. }
  123. print '</select>
  124. ';
  125. }
  126. print qq{
  127. <br>
  128. <label for="file2">
  129. You can
  130. };
  131. print "also " if (scalar @similartrees > 1);
  132. print "
  133. compare against a different file:</label>
  134. <input id='file2' name='file2' size=60 value='$file2'>
  135. <br>
  136. <label for='diffw'>Ignore whitespace changes:</label>
  137. <input type=checkbox name='diffw' id='diffw' value='1'";
  138. print " checked=1" if $diffw;
  139. print '>
  140. <hr>
  141. <input type=submit>
  142. </form>
  143. ';
  144. return unless (scalar @treelist == 0) || $diffvar;
  145. my $file = $Path->{'file'};
  146. unless ($file) {
  147. print("<h3 align=center>Diff not yet supported for directories.</h3>\n");
  148. return;
  149. }
  150. my $virt = $Path->{'virt'};
  151. $origvirt = $virt . $file;
  152. $origreal = $Path->{'real'} . $file;
  153. $origval = $Conf->variable($diffvar);
  154. $Conf->variable($diffvar, $diffval);
  155. $file2 ||= $Path->{'virt'} . $file;
  156. $diffvirt = $file2;
  157. if ($diffvar) {
  158. $diffreal = $Conf->{'treehash'}{$diffvar} . $diffvirt;
  159. } else {
  160. $diffreal = $Conf->{'sourceroot'} . $diffvirt;
  161. }
  162. $Conf->variable($diffvar, $origval);
  163. &fflush;
  164. unless (-f $origreal && -f $diffreal) {
  165. print "<h1>Can not find one of the specified files</h1>
  166. ";
  167. return;
  168. }
  169. unless (-r $origreal && -r $diffreal) {
  170. print "<h1>Can not read one of the specified files</h1>
  171. ";
  172. return;
  173. }
  174. unless (open(DIFF, "-|")) {
  175. open(STDERR, ">&STDOUT");
  176. my @args = ('diff', '-U0');
  177. push @args, '-w' if $diffw;
  178. push @args, ($origreal, $diffreal);
  179. exec @args;
  180. print "*** Diff subprocess died unexpectedly: $!\n";
  181. exit;
  182. }
  183. my $diff = 1;
  184. my $diffn = 2;
  185. while (<DIFF>) {
  186. if (($os, $ol, $ns, $nl) =
  187. /@@ -(\d+)(?:,(\d+)|) \+(\d+)(?:,(\d+)|) @@/) {
  188. # this seems to be a gnu hack
  189. $os++ if $ol eq '0' && $nl eq '';
  190. $ns++ if $nl eq '0' && $ol eq '';
  191. $ol = 1 unless defined($ol);
  192. $nl = 1 unless defined($nl);
  193. $bo = $os + $ofs;
  194. # link to the next/previous change ...
  195. my ($remove, $change, $additt) = ('&lt;', '!', '&gt;');
  196. my ($removep, $changep, $addittp) = ($remove, $change, $additt);
  197. my $nextd = "<a name='d$diff' href='#d$diffn'>";
  198. my $removen = "$nextd$remove</a>";
  199. my $changen = "$nextd$change</a>";
  200. my $addittn = "$nextd$additt</a>";
  201. if ($diff > 1) {
  202. my $prevd = "<a href='#d" . ($diff - 1) . "'>";
  203. $removep = "$prevd$remove</a>";
  204. $changep = "$prevd$change</a>";
  205. $addittp = "$prevd$additt</a>";
  206. }
  207. $diff = $diffn++;
  208. $remove = $removep . $removen;
  209. $change = $changep . $changen;
  210. $additt = $addittp . $addittn;
  211. if ($ol < $nl) {
  212. $ofs += $nl - $ol;
  213. $dir = $additt;
  214. $ms = $nl - $ol;
  215. $ml = $ol;
  216. $orig{$os+$ol} = $ms;
  217. } else {
  218. $dir = $remove;
  219. $ms = $ol - $nl;
  220. $ml = $nl;
  221. $new{$ns+$nl} = $ms;
  222. }
  223. foreach (0..$ml - 1) {
  224. $chg{$bo + $_} = $change;
  225. }
  226. foreach (0..$ms - 1) {
  227. $chg{$bo + $ml + $_} = $dir;
  228. }
  229. }
  230. }
  231. close(DIFF);
  232. my ($tree1, $tree2) = ($Conf->{'treename'}, $diffvar);
  233. if ($tree1 eq $tree2) {
  234. $tree1 = $tree2 = '';
  235. } else {
  236. $tree1 = " <i>($tree1)</i>";
  237. $tree2 = " <i>($tree2)</i>";
  238. }
  239. my $path = $diffvirt;
  240. $path =~ s|([^-a-zA-Z0-9.+\@/_\r\n])|sprintf("%%%02X", ord($1))|ge;
  241. $path = qq{<a href="../$diffvar/source$path">$path$tree2</a>};
  242. print "<h1>Diff markup</h1>
  243. <h3><a href='#d1'>Differences</a> between " .
  244. &fileref("$origvirt$tree1", $origvirt, undef, "") .
  245. " and $path (<a href='diff?tree=$diffvar&diffvar=" .
  246. $Conf->{'treename'} .
  247. "&file=$diffvirt&file2=$origvirt'>reverse</a>)
  248. </h3><hr>
  249. <style>.x, .x > a { color: red; text-decoration: none } </style>";
  250. # "<table width=\"100%\" border=0 cellpadding=0><tr><td>",
  251. #
  252. # &fileref("<b>$origvirt <i>(".$Conf->vardescription($diffvar).
  253. # " $origval)</i></b>", $origvirt, undef, "$diffvar=$origval"),
  254. # "<hr></td>\n<td></td>\n<td>",
  255. #
  256. # &fileref("<b>$diffvirt <i>(".$Conf->vardescription($diffvar).
  257. # " $diffval)</i></b>",$diffvirt, undef, "$diffvar=$diffval"),
  258. # "<hr></td>\n</tr></table>\n");
  259. open(FOO, $origreal);
  260. $orig = '';
  261. &markupfile(\*FOO, $Path->{'virt'}, $Path->{'file'},
  262. sub { $orig .= shift }, 1);
  263. $len = $.+$ofs;
  264. close(FOO);
  265. $Conf->variable($diffvar, $diffval);
  266. open(FOO, $diffreal);
  267. $new = '';
  268. &markupfile(\*FOO, $Conf->mappath($Path->{'virt'}), $Path->{'file'},
  269. sub { $new .= shift }, 1);
  270. close(FOO);
  271. $Conf->variable($diffvar, $origval);
  272. $i = 1; $orig =~ s/^/"\n" x ($orig{$i++})/mge;
  273. $i = 1; $new =~ s/^/"\n" x ($new{$i++})/mge;
  274. @orig = split(/\n/, $orig);
  275. @new = split(/\n/, $new);
  276. print("<pre>");
  277. foreach $i (0..$len) {
  278. $o = &htmlsub($orig[$i], 80);
  279. $n = &htmlsub($new[$i], 999);
  280. # $n = $new[$i];
  281. # print("<tr><td><code>$o</code></td>".
  282. # "<td><font color=red> ", $chg{$i+1}, " </font></td>".
  283. # "<td><code>$n</code></td></tr>\n");
  284. print("$o <span class=x>", ($chg{$i+1} || " "), "</span> $n\n");
  285. }
  286. print("</pre>");
  287. # print("</td></tr></table>");
  288. }
  289. ($Conf, $HTTP, $Path, $head) = &init;
  290. my $tree = $HTTP->{'param'}->{'tree'};
  291. $diffvar = $HTTP->{'param'}->{'diffvar'};
  292. $diffval = $HTTP->{'param'}->{'diffval'};
  293. $diffw = $HTTP->{'param'}->{'diffw'} eq '1';
  294. $file2 = cleanhtml $HTTP->{'param'}->{'file2'};
  295. if ($file2 ne '') {
  296. if ($file2 =~ m{^//([^/]+)(/.*|$)}) {
  297. ($diffvar, $file2) = ($1, $2);
  298. } elsif ($file2 !~ m{^/}) {
  299. $file2 = '/'.$file2;
  300. }
  301. }
  302. if ($file2 ne $HTTP->{'param'}->{'file2'} || $tree) {
  303. my @tail = ();
  304. push @tail, "file=" . url_quote($Path->{'virt'}.$Path->{'file'}) if $Path->{'file'} ne '';
  305. push @tail, "file2=" . url_quote($file2) if $file2 ne '';
  306. push @tail, "diffvar=" . url_quote($diffvar) if $diffvar ne '';
  307. push @tail, "diffval=" . url_quote($diffval) if $diffval ne '';
  308. push @tail, "diffw=" . url_quote($diffw) if $diffw;
  309. my $tail = $#tail >= 0 ? '?' . join "&", @tail : '';
  310. my $newtree = $Conf->{'treename'};
  311. if ($tree ne $newtree) {
  312. my @treelist = @{$Conf->{'trees'}};
  313. foreach my $othertree (@treelist) {
  314. $newtree = $tree, last if $othertree eq $tree;
  315. }
  316. }
  317. $head .= "Refresh: 0; url=../$newtree/diff$tail
  318. ";
  319. }
  320. print "$head
  321. ";
  322. &makeheader('diff');
  323. &printdiff;
  324. &makefooter('diff');
  325. 1;