Code covered by the BSD License  

Highlights from
CVS Differences Report

from CVS Differences Report by Malcolm Wood
Shows the modifications to files in a CVS sandbox

cvsdiff(directory)
function cvsdiff(directory)
%CVSDIFF Shows formatted CVS diff for the specified directory
%
% cvsdiff  % use pwd
% cvsdiff(directory)
%
% Author: Malcolm Wood, The MathWorks, 1/3/2006

% Copyright 2006-2010 The MathWorks, Inc.

if nargin<1
    directory = pwd;
end

olddir = pwd;
cd(directory);

% If the CVS executable is not on the path, you'll need to
% edit this line to specify its location
[a,b] =  system('cvs diff'); %#ok (unused output)

% Unfortunately, it's not at all easy to tell from either return value
% whether the call succeeded or not.  Continue as if it did.

cd(olddir);

% Separate the output into a cell array of strings, with each line in a
% separate cell.
lines = textscan(b,'%s','Delimiter',char(10));
lines = lines{1};

% Find indices of lines which start with the string "Index"
ind = strmatch('Index:',lines);

reportfile = [tempname 'cvsdiff.html'];

f = fopen(reportfile,'w');
if f==-1
    error('Failed to open file for writing');
end

fprintf(f,'<html>\n<head><title>CVS Differences</title></head>\n<body>\n');
fprintf(f,'<h3>CVS Differences</h3>\n');

for i=1:numel(ind)
    start = ind(i);
    fn = lines{start}; % file name
    fn = fn(7:end);
    fprintf(f,'<h4>File: %s</h4>\n<p><pre>',fn);

    start = start + 5;

    if i<numel(ind)
        finish = ind(i+1) - 1;
    else
        finish = numel(lines);
    end
    block = lines(start:finish);
    % Remove lines beginning "cvs server:"
    ignorelines = strncmp(block,'cvs server:',11);
    block(ignorelines) = [];
    
    % Remove lines beginning "cvs diff:"
    ignorelines = strncmp(block,'cvs diff:',9);
    block(ignorelines) = [];
    
    for k=1:numel(block)
        block{k} = i_escape(block{k});
    end
    
    % Turn lines beginning "> " green
    addlines = find(strncmp(block,'&gt; ',4)); % already escaped
    for k=1:numel(addlines)
        block{addlines(k)} = sprintf('<font color="##007000">%s</font>',block{addlines(k)});
    end
    
    % Turn lines beginning "< " red
    addlines = find(strncmp(block,'&lt; ',4));
    for k=1:numel(addlines)
        block{addlines(k)} = sprintf('<font color="##700000">%s</font>',block{addlines(k)});
    end
    
    
    fprintf(f,'%s\n',block{:});
    fprintf(f,'</pre></p>\n\n');
end

fprintf(f,'</body></html>');

fclose(f);

fprintf(1,'CVS differences written in %s\n',reportfile); % command window output
web(reportfile,'-helpbrowser');

%%%%%%%%%%%%%%%%%%%%%%%%
function str = i_escape( str )
% escape sequences for HTML docs

% deal with special cases
str = strrep( str, '&', '&amp;' );
str = strrep( str, '<', '&lt;' );
str = strrep( str, '>', '&gt;' );
str = strrep( str, '"', '&quot;' );

% deal with out-of-range characters, replace with &#384; type stuff
outchars = unique( sort( str( str > 127 ) ) );
outchars = [outchars unique( sort( str( str < 32 ) ) )];

for ii=1:length( outchars )
   str = strrep( str, outchars(ii), sprintf( '&#%d;', outchars(ii) ) );
end

Contact us at files@mathworks.com