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 (