File size: 4,714 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
import { parsePatch } from 'diff/lib/patch/parse';
import { Fragment } from 'react';
import './style.scss';
const decompose = ( path ) => {
const lastSlash = path.lastIndexOf( '/' );
return lastSlash > -1 ? [ path.slice( 0, lastSlash ), path.slice( lastSlash ) ] : [ '', path ];
};
/**
* Uses a heuristic to return proper file name indicators
*
* This function lists the filenames for the left and right
* side of the diff in a single string.
*
* It searches for the longest shared prefix and returns
* whatever remains after that. If the paths are identical
* it only returns a single filename as we have detected
* that the diff compares changes to only one file.
*
* An exception is made for `a/` and `b/` prefixes often
* added by `git` and other utilities to separate the left
* from the right when looking at the contents of a single
* file over time.
* @param {Object} options deconstructed argument
* @param {string} options.oldFileName filename of left contents
* @param {string} options.newFileName filename of right contents
* @returns {window.Element} description of the file or files in the diff
*/
const filename = ( { oldFileName, newFileName } ) => {
// if we think the diff utility added a bogus
// prefix then cut it off
const isLikelyPrefixed =
'a' === oldFileName[ 0 ] &&
'/' === oldFileName[ 1 ] &&
'b' === newFileName[ 0 ] &&
'/' === newFileName[ 1 ];
const [ prev, next ] = isLikelyPrefixed
? [ oldFileName.slice( 2 ), newFileName.slice( 2 ) ]
: [ oldFileName, newFileName ];
if ( prev === next ) {
const [ base, name ] = decompose( prev );
// it's the same file, return the single name
return (
<Fragment>
{ base && <span className="diff-viewer__path-prefix">{ base }</span> }
<span className="diff-viewer__path">{ name }</span>
</Fragment>
);
}
// find the longest shared path prefix
const length = Math.max( prev.length, next.length );
for ( let i = 0, slash = 0; i < length; i++ ) {
if ( prev[ i ] === '/' && next[ i ] === '/' ) {
slash = i;
}
if ( prev[ i ] !== next[ i ] ) {
return (
<Fragment>
{ slash !== 0 && (
<span className="diff-viewer__path-prefix">{ prev.slice( 0, slash ) }</span>
) }
<span className="diff-viewer__path">{ prev.slice( slash ) }</span>
{ ' → ' }
{ slash !== 0 && (
<span className="diff-viewer__path-prefix">{ next.slice( 0, slash ) }</span>
) }
<span className="diff-viewer__path">{ next.slice( slash ) }</span>
</Fragment>
);
}
}
// otherwise we have no shared prefix
const [ prevBase, prevName ] = decompose( prev );
const [ nextBase, nextName ] = decompose( next );
return (
<Fragment>
{ prevBase && <span className="diff-viewer__path-prefix">{ prevBase }</span> }
<span className="diff-viewer__path">{ prevName }</span>
{ ' → ' }
{ nextBase && <span className="diff-viewer__path-prefix">{ nextBase }</span> }
<span className="diff-viewer__path">{ nextName }</span>
</Fragment>
);
};
export const DiffViewer = ( { diff } ) => (
<div className="diff-viewer">
{ parsePatch( diff ).map( ( file, fileIndex ) => (
<Fragment key={ fileIndex }>
<div key={ `file-${ fileIndex }` } className="diff-viewer__filename">
{ filename( file ) }
</div>
<div key={ `diff-${ fileIndex }` } className="diff-viewer__file">
<div key="left-numbers" className="diff-viewer__line-numbers">
{ file.hunks.map( ( hunk, hunkIndex ) => {
let lineOffset = 0;
return hunk.lines.map( ( line, index ) => (
<div key={ `${ hunkIndex }-${ index }` }>
{ line[ 0 ] === '+' ? '\u00a0' : hunk.oldStart + lineOffset++ }
</div>
) );
} ) }
</div>
<div key="right-numbers" className="diff-viewer__line-numbers">
{ file.hunks.map( ( hunk, hunkIndex ) => {
let lineOffset = 0;
return hunk.lines.map( ( line, index ) => (
<div key={ `${ hunkIndex }-${ index }` }>
{ line[ 0 ] === '-' ? '\u00a0' : hunk.newStart + lineOffset++ }
</div>
) );
} ) }
</div>
<div className="diff-viewer__lines">
{ file.hunks.map( ( hunk, hunkIndex ) =>
hunk.lines.map( ( line, index ) => {
const output = line.slice( 1 ).replace( /^\s*$/, '\u00a0' );
const key = `${ hunkIndex }-${ index }`;
switch ( line[ 0 ] ) {
case ' ':
return <div key={ key }>{ output }</div>;
case '-':
return <del key={ key }>{ output }</del>;
case '+':
return <ins key={ key }>{ output }</ins>;
}
} )
) }
</div>
</div>
</Fragment>
) ) }
</div>
);
export default DiffViewer;
|