Skip to content

Commit 51efd0c

Browse files
committed
Expands getChangedFilesCount range support
1 parent f0dfaa2 commit 51efd0c

File tree

2 files changed

+51
-31
lines changed

2 files changed

+51
-31
lines changed

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

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,28 @@ export class DiffGitSubProvider implements GitDiffSubProvider {
4343
) {}
4444

4545
@log()
46-
async getChangedFilesCount(repoPath: string, ref?: string): Promise<GitDiffShortStat | undefined> {
46+
async getChangedFilesCount(
47+
repoPath: string,
48+
to?: string,
49+
from?: string,
50+
options?: { uris?: Uri[] },
51+
): Promise<GitDiffShortStat | undefined> {
4752
const scope = getLogScope();
4853

54+
const args: string[] = [];
55+
if (to != null) {
56+
prepareToFromDiffArgs(to, from, args);
57+
}
58+
4959
try {
5060
const data = await this.git.exec(
5161
{ cwd: repoPath, configs: gitDiffDefaultConfigs },
5262
'diff',
5363
'--shortstat',
5464
'--no-ext-diff',
55-
ref ? ref : undefined,
65+
...args,
5666
'--',
67+
options?.uris?.map(u => this.provider.getRelativePath(u, repoPath)) ?? undefined,
5768
);
5869
if (!data) return undefined;
5970
return parseGitDiffShortStat(data);
@@ -78,34 +89,7 @@ export class DiffGitSubProvider implements GitDiffSubProvider {
7889
const scope = getLogScope();
7990
const args = [`-U${options?.context ?? 3}`];
8091

81-
if (to === uncommitted) {
82-
if (from != null) {
83-
args.push(from);
84-
} else {
85-
// Get only unstaged changes
86-
from = 'HEAD';
87-
}
88-
} else if (to === uncommittedStaged) {
89-
args.push('--staged');
90-
if (from != null) {
91-
args.push(from);
92-
} else {
93-
// Get only staged changes
94-
from = 'HEAD';
95-
}
96-
} else if (from == null) {
97-
if (to === '' || to.toUpperCase() === 'HEAD') {
98-
from = 'HEAD';
99-
args.push(from);
100-
} else {
101-
from = `${to}^`;
102-
args.push(from, to);
103-
}
104-
} else if (to === '') {
105-
args.push(from);
106-
} else {
107-
args.push(from, to);
108-
}
92+
from = prepareToFromDiffArgs(to, from, args);
10993

11094
let paths: Set<string> | undefined;
11195
let untrackedPaths: string[] | undefined;
@@ -641,3 +625,34 @@ export class DiffGitSubProvider implements GitDiffSubProvider {
641625
}
642626
}
643627
}
628+
function prepareToFromDiffArgs(to: string, from: string | undefined, args: string[]) {
629+
if (to === uncommitted) {
630+
if (from != null) {
631+
args.push(from);
632+
} else {
633+
// Get only unstaged changes
634+
from = 'HEAD';
635+
}
636+
} else if (to === uncommittedStaged) {
637+
args.push('--staged');
638+
if (from != null) {
639+
args.push(from);
640+
} else {
641+
// Get only staged changes
642+
from = 'HEAD';
643+
}
644+
} else if (from == null) {
645+
if (to === '' || to.toUpperCase() === 'HEAD') {
646+
from = 'HEAD';
647+
args.push(from);
648+
} else {
649+
from = `${to}^`;
650+
args.push(from, to);
651+
}
652+
} else if (to === '') {
653+
args.push(from);
654+
} else {
655+
args.push(from, to);
656+
}
657+
return from;
658+
}

src/git/gitProvider.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,12 @@ export interface GitContributorsSubProvider {
358358
}
359359

360360
export interface GitDiffSubProvider {
361-
getChangedFilesCount(repoPath: string, ref?: string): Promise<GitDiffShortStat | undefined>;
361+
getChangedFilesCount(
362+
repoPath: string,
363+
to?: string,
364+
from?: string,
365+
options?: { uris?: Uri[] },
366+
): Promise<GitDiffShortStat | undefined>;
362367
getDiff?(
363368
repoPath: string | Uri,
364369
to: string,

0 commit comments

Comments
 (0)