Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Components: Migrate DiffViewer from Protect #39672

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Add DiffViewer component
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Unified Diff Viewer

Forked over from [Calypso](https://github.com/Automattic/wp-calypso/tree/b7a4a07/client/components/diff-viewer).
Originally forked over from [Calypso](https://github.com/Automattic/wp-calypso/tree/b7a4a07/client/components/diff-viewer).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is a copy of Calypso component, why aren't we consuming this via @automattic/components package?


This component renders the output of a unified diff (`git diff` or `diff -u`) in a
visual format recognizable by someone who works with `diff` and comparing files.
Expand Down Expand Up @@ -29,9 +29,9 @@ export const CommitView = ( { commitHash, description, diff } ) => (
### Additional usage information

The diff output should be the full text produced by the diff command (including newlines).
Internally this component relies on `jsdiff` to parse the output (the patch) and produce
the data structure used to display files, hunks (sections of change in the files), and
the actual lines of change and context.
Internally this component parses the output (the patch) and produces the data structure
used to display files, hunks (sections of change in the files), and the actual lines of
change and context.

```
diff --git a/circle.yml b/circle.yml
Expand Down
97 changes: 97 additions & 0 deletions projects/js-packages/components/components/diff-viewer/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { Fragment } from 'react';
import parseFilename from './parse-filename';
import parsePatch from './parse-patch';
import styles from './styles.module.scss';

const filename = ( {
oldFileName,
newFileName,
}: {
oldFileName: string;
newFileName: string;
} ): JSX.Element => {
const { prev, next } = parseFilename( oldFileName, newFileName );

if ( prev.prefix + prev.path === next.prefix + next.path ) {
return (
<Fragment>
{ prev.prefix && (
<span className={ styles[ 'diff-viewer__path-prefix' ] }>{ prev.prefix }</span>
) }
<span className={ styles[ 'diff-viewer__path' ] }>{ prev.path }</span>
</Fragment>
);
}

return (
<Fragment>
{ !! prev.prefix && (
<span className={ styles[ 'diff-viewer__path-prefix' ] }>{ prev.prefix }</span>
) }
<span className={ styles[ 'diff-viewer__path' ] }>{ prev.path }</span>
{ ' → ' }
{ !! next.prefix && (
<span className={ styles[ 'diff-viewer__path-prefix' ] }>{ next.prefix }</span>
) }
<span className={ styles[ 'diff-viewer__path' ] }>{ next.path }</span>
</Fragment>
);
};

export const DiffViewer = ( { diff } ) => (
<div className={ styles[ 'diff-viewer' ] }>
{ parsePatch( diff ).map( ( file, fileIndex ) => (
<Fragment key={ fileIndex }>
<div key={ `file-${ fileIndex }` } className={ styles[ 'diff-viewer__filename' ] }>
{ filename( file ) }
</div>
<div key={ `diff-${ fileIndex }` } className={ styles[ 'diff-viewer__file' ] }>
<div key="left-numbers" className={ styles[ '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={ styles[ '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={ styles[ '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>;

default:
return undefined;
}
} )
) }
</div>
</div>
</Fragment>
) ) }
</div>
);

export default DiffViewer;
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
type ParsedFilename = {
prefix: string;
path: string;
};

const decompose = ( path: string ): ParsedFilename => {
const lastSlash = path.lastIndexOf( '/' );

return lastSlash > -1
? { prefix: path.slice( 0, lastSlash ), path: path.slice( lastSlash ) }
: { prefix: '', path };
};

/**
* Parse the filename from a diff
*
* Uses a heuristic to return proper file name indicators
*
* 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 {string} prev - filename of left contents
* @param {string} next - filename of right contents
*
* @return {object} - parsed filename
*/
export default function (
prev: string,
next: string
): { prev: ParsedFilename; next: ParsedFilename } {
// Remove 'a/' and 'b/' prefixes if present
const isLikelyPrefixed = prev.startsWith( 'a/' ) && next.startsWith( 'b/' );
prev = isLikelyPrefixed ? prev.slice( 2 ) : prev;
next = isLikelyPrefixed ? next.slice( 2 ) : next;

if ( prev === next ) {
// Paths are identical
const { prefix, path } = decompose( prev );
return { prev: { prefix, path }, next: { prefix, path } };
}

// Find longest shared base path ending with a slash
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 {
prev: {
prefix: prev.slice( 0, slash ),
path: prev.slice( slash ),
},
next: {
prefix: next.slice( 0, slash ),
path: next.slice( slash ),
},
};
}
}

// No shared base path
return {
prev: decompose( prev ),
next: decompose( next ),
};
}
Loading
Loading