Skip to content

Commit 7273df3

Browse files
authored
Display diff between any two commits for a single file (#1117)
* Display any two commits diff for single file Fixes #985 * Add integration test
1 parent 2f2865c commit 7273df3

File tree

2 files changed

+95
-38
lines changed

2 files changed

+95
-38
lines changed

src/components/GitPanel.tsx

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -504,47 +504,56 @@ export class GitPanel extends React.Component<IGitPanelProps, IGitPanelState> {
504504
challengerCommit={this.state.challengerCommit}
505505
onSelectForCompare={commit => async event => {
506506
event.stopPropagation();
507-
this.setState({ referenceCommit: commit });
507+
this.setState({ referenceCommit: commit }, () => {
508+
this._openSingleFileComparison(
509+
event as React.MouseEvent<HTMLLIElement, MouseEvent>
510+
);
511+
});
508512
}}
509513
onCompareWithSelected={commit => async event => {
510514
event.stopPropagation();
511-
this.setState({ challengerCommit: commit });
515+
this.setState({ challengerCommit: commit }, () => {
516+
this._openSingleFileComparison(
517+
event as React.MouseEvent<HTMLLIElement, MouseEvent>
518+
);
519+
});
512520
}}
513521
/>
514-
{(this.state.referenceCommit || this.state.challengerCommit) && (
515-
<CommitComparisonBox
516-
header={this.props.trans.__(
517-
'Compare %1 and %2',
518-
this.state.referenceCommit
519-
? this.state.referenceCommit.commit.substring(0, 7)
520-
: '...',
521-
this.state.challengerCommit
522-
? this.state.challengerCommit.commit.substring(0, 7)
523-
: '...'
524-
)}
525-
referenceCommit={this.state.referenceCommit}
526-
challengerCommit={this.state.challengerCommit}
527-
commands={this.props.commands}
528-
model={this.props.model}
529-
logger={this.props.logger}
530-
trans={this.props.trans}
531-
onClose={event => {
532-
event.stopPropagation();
533-
this.setState({
534-
referenceCommit: null,
535-
challengerCommit: null
536-
});
537-
}}
538-
onOpenDiff={
539-
this.state.referenceCommit && this.state.challengerCommit
540-
? openFileDiff(this.props.commands)(
541-
this.state.challengerCommit,
542-
this.state.referenceCommit
543-
)
544-
: undefined
545-
}
546-
/>
547-
)}
522+
{this.props.model.selectedHistoryFile === null &&
523+
(this.state.referenceCommit || this.state.challengerCommit) && (
524+
<CommitComparisonBox
525+
header={this.props.trans.__(
526+
'Compare %1 and %2',
527+
this.state.referenceCommit
528+
? this.state.referenceCommit.commit.substring(0, 7)
529+
: '...',
530+
this.state.challengerCommit
531+
? this.state.challengerCommit.commit.substring(0, 7)
532+
: '...'
533+
)}
534+
referenceCommit={this.state.referenceCommit}
535+
challengerCommit={this.state.challengerCommit}
536+
commands={this.props.commands}
537+
model={this.props.model}
538+
logger={this.props.logger}
539+
trans={this.props.trans}
540+
onClose={event => {
541+
event.stopPropagation();
542+
this.setState({
543+
referenceCommit: null,
544+
challengerCommit: null
545+
});
546+
}}
547+
onOpenDiff={
548+
this.state.referenceCommit && this.state.challengerCommit
549+
? openFileDiff(this.props.commands)(
550+
this.state.challengerCommit,
551+
this.state.referenceCommit
552+
)
553+
: undefined
554+
}
555+
/>
556+
)}
548557
</React.Fragment>
549558
);
550559
}
@@ -851,4 +860,25 @@ export class GitPanel extends React.Component<IGitPanelProps, IGitPanelState> {
851860
}
852861
return <div>{elem}</div>;
853862
}
863+
864+
/**
865+
*
866+
*/
867+
private _openSingleFileComparison(
868+
event: React.MouseEvent<HTMLLIElement, MouseEvent>
869+
): void {
870+
if (
871+
this.props.model.selectedHistoryFile &&
872+
this.state.referenceCommit &&
873+
this.state.challengerCommit
874+
) {
875+
openFileDiff(this.props.commands)(
876+
this.state.challengerCommit,
877+
this.state.referenceCommit
878+
)(
879+
this.props.model.selectedHistoryFile.to,
880+
!this.props.model.selectedHistoryFile.is_binary
881+
)(event);
882+
}
883+
}
854884
}

ui-tests/tests/commit-diff.spec.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ test.describe('Commits diff', () => {
1616

1717
// URL for merge conflict example repository
1818
await page.goto(`tree/${tmpPath}/repository`);
19-
20-
await page.sidebar.openTab('jp-git-sessions');
2119
});
2220

2321
test('should display commits diff from history', async ({ page }) => {
22+
await page.sidebar.openTab('jp-git-sessions');
2423
await page.click('button:has-text("History")');
2524
const commits = page.locator('li[title="View commit details"]');
2625

@@ -38,4 +37,32 @@ test.describe('Commits diff', () => {
3837

3938
expect(await page.waitForSelector('text=Changed')).toBeTruthy();
4039
});
40+
41+
test('should display diff from single file history', async ({ page }) => {
42+
await page.sidebar.openTab('filebrowser');
43+
await page.pause();
44+
await page.click('#filebrowser >> text=example.ipynb', {
45+
button: 'right'
46+
});
47+
await page.hover('ul[role="menu"] >> text=Git');
48+
await page.click('#jp-contextmenu-git >> text=History');
49+
50+
await page.waitForSelector('#jp-git-sessions >> ol >> text=example.ipynb');
51+
52+
const commits = page.locator('li[title="View file changes"]');
53+
54+
expect(await commits.count()).toBeGreaterThanOrEqual(2);
55+
56+
await commits.last().locator('button[title="Select for compare"]').click();
57+
await commits
58+
.first()
59+
.locator('button[title="Compare with selected"]')
60+
.click();
61+
62+
await expect(
63+
page.locator('.nbdime-Widget >> .jp-git-diff-banner')
64+
).toHaveText(
65+
/2c87684a46d49989211d666daf05ab92951ba7c4[\n\s]+b6ece342cf6eefaff3232d43345191abda10727/
66+
);
67+
});
4168
});

0 commit comments

Comments
 (0)