Skip to content

Commit bd8e7f1

Browse files
committed
Adds new diff parser
Renames diff models for clarity
1 parent ad5f369 commit bd8e7f1

File tree

12 files changed

+147
-79
lines changed

12 files changed

+147
-79
lines changed

src/annotations/gutterChangesAnnotationProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { CancellationToken, DecorationOptions, Disposable, TextDocument, Te
22
import { Hover, languages, Position, Range, Selection, TextEditorRevealType } from 'vscode';
33
import type { Container } from '../container';
44
import type { GitCommit } from '../git/models/commit';
5-
import type { GitDiffFile } from '../git/models/diff';
5+
import type { ParsedGitDiffHunks } from '../git/models/diff';
66
import { localChangesMessage } from '../hovers/hovers';
77
import { configuration } from '../system/-webview/configuration';
88
import { log } from '../system/decorators/log';
@@ -25,7 +25,7 @@ export interface ChangesAnnotationContext extends AnnotationContext {
2525
export class GutterChangesAnnotationProvider extends AnnotationProviderBase<ChangesAnnotationContext> {
2626
private hoverProviderDisposable: Disposable | undefined;
2727
private sortedHunkStarts: number[] | undefined;
28-
private state: { commit: GitCommit | undefined; diffs: GitDiffFile[] } | undefined;
28+
private state: { commit: GitCommit | undefined; diffs: ParsedGitDiffHunks[] } | undefined;
2929

3030
constructor(
3131
container: Container,

src/env/node/git/localGitProvider.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { isGitUri } from '../../../git/gitUri';
3535
import { encodeGitLensRevisionUriAuthority } from '../../../git/gitUri.authority';
3636
import type { GitBlame, GitBlameAuthor, GitBlameLine } from '../../../git/models/blame';
3737
import type { GitCommit } from '../../../git/models/commit';
38-
import type { GitDiffFile, GitDiffLine } from '../../../git/models/diff';
38+
import type { GitLineDiff, ParsedGitDiffHunks } from '../../../git/models/diff';
3939
import type { GitLog } from '../../../git/models/log';
4040
import type { GitBranchReference, GitReference } from '../../../git/models/reference';
4141
import type { GitRemote } from '../../../git/models/remote';
@@ -95,7 +95,7 @@ import { StatusGitSubProvider } from './sub-providers/status';
9595
import { TagsGitSubProvider } from './sub-providers/tags';
9696
import { WorktreesGitSubProvider } from './sub-providers/worktrees';
9797

98-
const emptyPromise: Promise<GitBlame | GitDiffFile | GitLog | undefined> = Promise.resolve(undefined);
98+
const emptyPromise: Promise<GitBlame | ParsedGitDiffHunks | GitLog | undefined> = Promise.resolve(undefined);
9999
const slash = 47;
100100

101101
const RepoSearchWarnings = {
@@ -1696,7 +1696,11 @@ export class LocalGitProvider implements GitProvider, Disposable {
16961696
}
16971697

16981698
@log()
1699-
async getDiffForFile(uri: GitUri, ref1: string | undefined, ref2?: string): Promise<GitDiffFile | undefined> {
1699+
async getDiffForFile(
1700+
uri: GitUri,
1701+
ref1: string | undefined,
1702+
ref2?: string,
1703+
): Promise<ParsedGitDiffHunks | undefined> {
17001704
const scope = getLogScope();
17011705

17021706
let key = 'diff';
@@ -1738,7 +1742,7 @@ export class LocalGitProvider implements GitProvider, Disposable {
17381742
Logger.debug(scope, `Cache add: '${key}'`);
17391743

17401744
const value: CachedDiff = {
1741-
item: promise as Promise<GitDiffFile>,
1745+
item: promise as Promise<ParsedGitDiffHunks>,
17421746
};
17431747
doc.state.setDiff(key, value);
17441748
}
@@ -1755,7 +1759,7 @@ export class LocalGitProvider implements GitProvider, Disposable {
17551759
document: TrackedGitDocument,
17561760
key: string,
17571761
scope: LogScope | undefined,
1758-
): Promise<GitDiffFile | undefined> {
1762+
): Promise<ParsedGitDiffHunks | undefined> {
17591763
const [relativePath, root] = splitPath(path, repoPath);
17601764

17611765
try {
@@ -1776,20 +1780,20 @@ export class LocalGitProvider implements GitProvider, Disposable {
17761780
Logger.debug(scope, `Cache replace (with empty promise): '${key}'`);
17771781

17781782
const value: CachedDiff = {
1779-
item: emptyPromise as Promise<GitDiffFile>,
1783+
item: emptyPromise as Promise<ParsedGitDiffHunks>,
17801784
errorMessage: msg,
17811785
};
17821786
document.state.setDiff(key, value);
17831787

1784-
return emptyPromise as Promise<GitDiffFile>;
1788+
return emptyPromise as Promise<ParsedGitDiffHunks>;
17851789
}
17861790

17871791
return undefined;
17881792
}
17891793
}
17901794

17911795
@log<LocalGitProvider['getDiffForFileContents']>({ args: { 1: '<contents>' } })
1792-
async getDiffForFileContents(uri: GitUri, ref: string, contents: string): Promise<GitDiffFile | undefined> {
1796+
async getDiffForFileContents(uri: GitUri, ref: string, contents: string): Promise<ParsedGitDiffHunks | undefined> {
17931797
const scope = getLogScope();
17941798

17951799
const key = `diff:${md5(contents)}`;
@@ -1825,7 +1829,7 @@ export class LocalGitProvider implements GitProvider, Disposable {
18251829
Logger.debug(scope, `Cache add: '${key}'`);
18261830

18271831
const value: CachedDiff = {
1828-
item: promise as Promise<GitDiffFile>,
1832+
item: promise as Promise<ParsedGitDiffHunks>,
18291833
};
18301834
doc.state.setDiff(key, value);
18311835
}
@@ -1842,7 +1846,7 @@ export class LocalGitProvider implements GitProvider, Disposable {
18421846
document: TrackedGitDocument,
18431847
key: string,
18441848
scope: LogScope | undefined,
1845-
): Promise<GitDiffFile | undefined> {
1849+
): Promise<ParsedGitDiffHunks | undefined> {
18461850
const [relativePath, root] = splitPath(path, repoPath);
18471851

18481852
try {
@@ -1861,12 +1865,12 @@ export class LocalGitProvider implements GitProvider, Disposable {
18611865
Logger.debug(scope, `Cache replace (with empty promise): '${key}'`);
18621866

18631867
const value: CachedDiff = {
1864-
item: emptyPromise as Promise<GitDiffFile>,
1868+
item: emptyPromise as Promise<ParsedGitDiffHunks>,
18651869
errorMessage: msg,
18661870
};
18671871
document.state.setDiff(key, value);
18681872

1869-
return emptyPromise as Promise<GitDiffFile>;
1873+
return emptyPromise as Promise<ParsedGitDiffHunks>;
18701874
}
18711875

18721876
return undefined;
@@ -1879,7 +1883,7 @@ export class LocalGitProvider implements GitProvider, Disposable {
18791883
editorLine: number, // 0-based, Git is 1-based
18801884
ref1: string | undefined,
18811885
ref2?: string,
1882-
): Promise<GitDiffLine | undefined> {
1886+
): Promise<GitLineDiff | undefined> {
18831887
try {
18841888
const diff = await this.getDiffForFile(uri, ref1, ref2);
18851889
if (diff == null) return undefined;

src/env/node/git/sub-providers/commits.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { GitCommitsSubProvider, LeftRightCommitCountResult } from '../../..
88
import { GitUri } from '../../../../git/gitUri';
99
import type { GitBlame } from '../../../../git/models/blame';
1010
import type { GitCommit, GitStashCommit } from '../../../../git/models/commit';
11-
import type { GitDiffFile } from '../../../../git/models/diff';
11+
import type { ParsedGitDiffHunks } from '../../../../git/models/diff';
1212
import type { GitFile } from '../../../../git/models/file';
1313
import { GitFileChange } from '../../../../git/models/fileChange';
1414
import type { GitFileStatus } from '../../../../git/models/fileStatus';
@@ -44,7 +44,7 @@ import type { Git } from '../git';
4444
import { gitLogDefaultConfigs, gitLogDefaultConfigsWithFiles } from '../git';
4545
import type { LocalGitProvider } from '../localGitProvider';
4646

47-
const emptyPromise: Promise<GitBlame | GitDiffFile | GitLog | undefined> = Promise.resolve(undefined);
47+
const emptyPromise: Promise<GitBlame | ParsedGitDiffHunks | GitLog | undefined> = Promise.resolve(undefined);
4848
const reflogCommands = ['merge', 'pull'];
4949

5050
export class CommitsGitSubProvider implements GitCommitsSubProvider {

src/git/gitProvider.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ import type { GitBlame, GitBlameLine } from './models/blame';
1111
import type { GitBranch } from './models/branch';
1212
import type { GitCommit, GitCommitStats } from './models/commit';
1313
import type { GitContributor, GitContributorsStats } from './models/contributor';
14-
import type { GitDiff, GitDiffFile, GitDiffFiles, GitDiffFilter, GitDiffLine, GitDiffShortStat } from './models/diff';
14+
import type {
15+
GitDiff,
16+
GitDiffFiles,
17+
GitDiffFilter,
18+
GitDiffShortStat,
19+
GitLineDiff,
20+
ParsedGitDiffHunks,
21+
} from './models/diff';
1522
import type { GitFile } from './models/file';
1623
import type { GitFileChange } from './models/fileChange';
1724
import type { GitGraph } from './models/graph';
@@ -750,14 +757,14 @@ export interface GitProvider extends GitRepositoryProvider, Disposable {
750757
* @param ref1 Commit to diff from
751758
* @param ref2 Commit to diff to
752759
*/
753-
getDiffForFile(uri: GitUri, ref1: string | undefined, ref2?: string): Promise<GitDiffFile | undefined>;
760+
getDiffForFile(uri: GitUri, ref1: string | undefined, ref2?: string): Promise<ParsedGitDiffHunks | undefined>;
754761
/**
755762
* Returns a file diff between a commit and the specified contents
756763
* @param uri Uri of the file to diff
757764
* @param ref Commit to diff from
758765
* @param contents Contents to use for the diff
759766
*/
760-
getDiffForFileContents(uri: GitUri, ref: string, contents: string): Promise<GitDiffFile | undefined>;
767+
getDiffForFileContents(uri: GitUri, ref: string, contents: string): Promise<ParsedGitDiffHunks | undefined>;
761768
/**
762769
* Returns a line diff between two commits
763770
* @param uri Uri of the file to diff
@@ -770,7 +777,7 @@ export interface GitProvider extends GitRepositoryProvider, Disposable {
770777
editorLine: number,
771778
ref1: string | undefined,
772779
ref2?: string,
773-
): Promise<GitDiffLine | undefined>;
780+
): Promise<GitLineDiff | undefined>;
774781
hasUnsafeRepositories?(): boolean;
775782
isTrackable(uri: Uri): boolean;
776783
isTracked(uri: Uri): Promise<boolean>;

src/git/gitProviderService.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ import { createSubProviderProxyForRepo } from './gitProvider';
7070
import type { GitUri } from './gitUri';
7171
import type { GitBlame, GitBlameLine } from './models/blame';
7272
import type { GitBranch } from './models/branch';
73-
import type { GitDiffFile, GitDiffLine } from './models/diff';
73+
import type { GitLineDiff, ParsedGitDiffHunks } from './models/diff';
7474
import type { GitFile } from './models/file';
7575
import type { GitBranchReference, GitReference } from './models/reference';
7676
import type { GitRemote } from './models/remote';
@@ -1691,7 +1691,7 @@ export class GitProviderService implements Disposable {
16911691
* @param ref1 Commit to diff from
16921692
* @param ref2 Commit to diff to
16931693
*/
1694-
getDiffForFile(uri: GitUri, ref1: string | undefined, ref2?: string): Promise<GitDiffFile | undefined> {
1694+
getDiffForFile(uri: GitUri, ref1: string | undefined, ref2?: string): Promise<ParsedGitDiffHunks | undefined> {
16951695
const { provider } = this.getProvider(uri);
16961696
return provider.getDiffForFile(uri, ref1, ref2);
16971697
}
@@ -1703,7 +1703,7 @@ export class GitProviderService implements Disposable {
17031703
* @param ref Commit to diff from
17041704
* @param contents Contents to use for the diff
17051705
*/
1706-
getDiffForFileContents(uri: GitUri, ref: string, contents: string): Promise<GitDiffFile | undefined> {
1706+
getDiffForFileContents(uri: GitUri, ref: string, contents: string): Promise<ParsedGitDiffHunks | undefined> {
17071707
const { provider } = this.getProvider(uri);
17081708
return provider.getDiffForFileContents(uri, ref, contents);
17091709
}
@@ -1721,7 +1721,7 @@ export class GitProviderService implements Disposable {
17211721
editorLine: number,
17221722
ref1: string | undefined,
17231723
ref2?: string,
1724-
): Promise<GitDiffLine | undefined> {
1724+
): Promise<GitLineDiff | undefined> {
17251725
const { provider } = this.getProvider(uri);
17261726
return provider.getDiffForLine(uri, editorLine, ref1, ref2);
17271727
}

src/git/models/diff.ts

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { GitFileChange } from './fileChange';
1+
import type { GitFileChange, GitFileChangeShape } from './fileChange';
22
import type { GitRevisionRangeNotation } from './revision';
33

44
export interface GitDiff {
@@ -8,49 +8,56 @@ export interface GitDiff {
88
readonly notation: GitRevisionRangeNotation | undefined;
99
}
1010

11-
export interface GitDiffHunkLine {
12-
current: string | undefined;
13-
previous: string | undefined;
14-
state: 'added' | 'changed' | 'removed' | 'unchanged';
15-
}
16-
17-
export interface GitDiffHunk {
18-
readonly contents: string;
19-
readonly current: {
20-
readonly count: number;
21-
readonly position: { readonly start: number; readonly end: number };
22-
};
23-
readonly previous: {
24-
readonly count: number;
25-
readonly position: { readonly start: number; readonly end: number };
26-
};
27-
readonly lines: Map<number, GitDiffHunkLine>;
11+
export interface GitDiffFiles {
12+
readonly files: GitFileChange[];
2813
}
2914

30-
export interface GitDiffFile {
31-
readonly hunks: GitDiffHunk[];
32-
readonly contents?: string;
15+
export interface GitDiffFileStats {
16+
readonly added: number;
17+
readonly deleted: number;
18+
readonly changed: number;
3319
}
3420

35-
export interface GitDiffLine {
36-
readonly hunk: GitDiffHunk;
37-
readonly line: GitDiffHunkLine;
38-
}
21+
export type GitDiffFilter = 'A' | 'C' | 'D' | 'M' | 'R' | 'T' | 'U' | 'X' | 'B' | '*';
3922

4023
export interface GitDiffShortStat {
4124
readonly files: number;
4225
readonly additions: number;
4326
readonly deletions: number;
4427
}
4528

46-
export interface GitDiffFiles {
47-
readonly files: GitFileChange[];
29+
export interface GitLineDiff {
30+
readonly hunk: ParsedGitDiffHunk;
31+
readonly line: ParsedGitDiffHunkLine;
4832
}
4933

50-
export interface GitDiffFileStats {
51-
readonly added: number;
52-
readonly deleted: number;
53-
readonly changed: number;
34+
export interface ParsedGitDiff {
35+
readonly files: ParsedGitDiffFile[];
36+
readonly contents?: string;
5437
}
5538

56-
export type GitDiffFilter = 'A' | 'C' | 'D' | 'M' | 'R' | 'T' | 'U' | 'X' | 'B' | '*';
39+
export interface ParsedGitDiffFile extends Omit<GitFileChangeShape, 'repoPath'>, ParsedGitDiffHunks {}
40+
41+
export interface ParsedGitDiffHunks {
42+
readonly hunks: ParsedGitDiffHunk[];
43+
readonly contents?: string;
44+
}
45+
46+
export interface ParsedGitDiffHunk {
47+
readonly contents: string;
48+
readonly current: {
49+
readonly count: number;
50+
readonly position: { readonly start: number; readonly end: number };
51+
};
52+
readonly previous: {
53+
readonly count: number;
54+
readonly position: { readonly start: number; readonly end: number };
55+
};
56+
readonly lines: Map<number, ParsedGitDiffHunkLine>;
57+
}
58+
59+
export interface ParsedGitDiffHunkLine {
60+
current: string | undefined;
61+
previous: string | undefined;
62+
state: 'added' | 'changed' | 'removed' | 'unchanged';
63+
}

0 commit comments

Comments
 (0)