Skip to content

Commit

Permalink
Combines contains sha call into single git call
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Mar 20, 2024
1 parent b7087bf commit a5c3e12
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 35 deletions.
13 changes: 8 additions & 5 deletions src/env/node/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ export class Git {

branchOrTag__containsOrPointsAt(
repoPath: string,
ref: string,
refs: string[],
options?: {
type?: 'branch' | 'tag';
all?: boolean;
Expand All @@ -526,10 +526,13 @@ export class Git {
} else if (options?.remotes) {
params.push('-r');
}
params.push(
options?.mode === 'pointsAt' ? `--points-at=${ref}` : `--contains=${ref}`,
'--format=%(refname:short)',
);

params.push('--format=%(refname:short)');

for (const ref of refs) {
params.push(options?.mode === 'pointsAt' ? `--points-at=${ref}` : `--contains=${ref}`);
}

if (options?.name != null) {
params.push(options.name);
}
Expand Down
12 changes: 6 additions & 6 deletions src/env/node/git/localGitProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2235,22 +2235,22 @@ export class LocalGitProvider implements GitProvider, Disposable {
@log()
async getCommitBranches(
repoPath: string,
ref: string,
refs: string[],
branch?: string | undefined,
options?:
| { all?: boolean; commitDate?: Date; mode?: 'contains' | 'pointsAt' }
| { commitDate?: Date; mode?: 'contains' | 'pointsAt'; remotes?: boolean },
): Promise<string[]> {
if (branch != null) {
const data = await this.git.branchOrTag__containsOrPointsAt(repoPath, ref, {
const data = await this.git.branchOrTag__containsOrPointsAt(repoPath, refs, {
type: 'branch',
mode: 'contains',
name: branch,
});
return data ? [data?.trim()] : [];
}

const data = await this.git.branchOrTag__containsOrPointsAt(repoPath, ref, { type: 'branch', ...options });
const data = await this.git.branchOrTag__containsOrPointsAt(repoPath, refs, { type: 'branch', ...options });
if (!data) return [];

return filterMap(data.split('\n'), b => b.trim() || undefined);
Expand Down Expand Up @@ -2829,7 +2829,7 @@ export class LocalGitProvider implements GitProvider, Disposable {
ref: string,
options?: { commitDate?: Date; mode?: 'contains' | 'pointsAt' },
): Promise<string[]> {
const data = await this.git.branchOrTag__containsOrPointsAt(repoPath, ref, { type: 'tag', ...options });
const data = await this.git.branchOrTag__containsOrPointsAt(repoPath, [ref], { type: 'tag', ...options });
if (!data) return [];

return filterMap(data.split('\n'), b => b.trim() || undefined);
Expand Down Expand Up @@ -4056,7 +4056,7 @@ export class LocalGitProvider implements GitProvider, Disposable {
const [branchResult, mergeBaseResult, possibleSourceBranchesResult] = await Promise.allSettled([
this.getBranch(repoPath),
this.getMergeBase(repoPath, 'MERGE_HEAD', 'HEAD'),
this.getCommitBranches(repoPath, 'MERGE_HEAD', undefined, { all: true, mode: 'pointsAt' }),
this.getCommitBranches(repoPath, ['MERGE_HEAD'], undefined, { all: true, mode: 'pointsAt' }),
]);

const branch = getSettledValue(branchResult);
Expand Down Expand Up @@ -4142,7 +4142,7 @@ export class LocalGitProvider implements GitProvider, Disposable {
}

const [branchTipsResult, tagTipsResult] = await Promise.allSettled([
this.getCommitBranches(repoPath, onto, undefined, { all: true, mode: 'pointsAt' }),
this.getCommitBranches(repoPath, [onto], undefined, { all: true, mode: 'pointsAt' }),
this.getCommitTags(repoPath, onto, { mode: 'pointsAt' }),
]);

Expand Down
2 changes: 1 addition & 1 deletion src/git/gitProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export interface GitProvider extends Disposable {
getCommit(repoPath: string, ref: string): Promise<GitCommit | undefined>;
getCommitBranches(
repoPath: string,
ref: string,
refs: string[],
branch?: string | undefined,
options?:
| { all?: boolean; commitDate?: Date; mode?: 'contains' | 'pointsAt' }
Expand Down
4 changes: 2 additions & 2 deletions src/git/gitProviderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1681,14 +1681,14 @@ export class GitProviderService implements Disposable {
@log()
getCommitBranches(
repoPath: string | Uri,
ref: string,
refs: string | string[],
branch?: string | undefined,
options?:
| { all?: boolean; commitDate?: Date; mode?: 'contains' | 'pointsAt' }
| { commitDate?: Date; mode?: 'contains' | 'pointsAt'; remotes?: boolean },
): Promise<string[]> {
const { provider, path } = this.getProvider(repoPath);
return provider.getCommitBranches(path, ref, branch, options);
return provider.getCommitBranches(path, typeof refs === 'string' ? [refs] : refs, branch, options);
}

@log()
Expand Down
11 changes: 4 additions & 7 deletions src/plus/drafts/draftsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,10 @@ export class DraftService implements Disposable {
const [branchNamesResult, diffResult, firstShaResult, remoteResult, userResult] = await Promise.allSettled([
isWIP
? this.container.git.getBranch(change.repository.uri).then(b => (b != null ? [b.name] : undefined))
: this.container.git
.getCommitBranches(change.repository.uri, change.revision.to)
.then(branches =>
branches.length
? branches
: this.container.git.getCommitBranches(change.repository.uri, change.revision.from),
),
: this.container.git.getCommitBranches(change.repository.uri, [
change.revision.to,
change.revision.from,
]),
change.contents == null
? this.container.git.getDiff(change.repository.path, change.revision.to, change.revision.from)
: undefined,
Expand Down
35 changes: 24 additions & 11 deletions src/plus/integrations/providers/github/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,14 @@ export class GitHubApi implements Disposable {
}

@debug<GitHubApi['getCommitBranches']>({ args: { 0: '<token>' } })
async getCommitBranches(token: string, owner: string, repo: string, ref: string, date: Date): Promise<string[]> {
async getCommitBranches(
token: string,
owner: string,
repo: string,
refs: string[],
mode: 'contains' | 'pointsAt',
date?: Date,
): Promise<string[]> {
const scope = getLogScope();

interface QueryResult {
Expand All @@ -1158,6 +1165,8 @@ export class GitHubApi implements Disposable {
};
}

const limit = mode === 'contains' ? 10 : 1;

try {
const query = `query getCommitBranches(
$owner: String!
Expand All @@ -1171,7 +1180,7 @@ export class GitHubApi implements Disposable {
name
target {
... on Commit {
history(first: 3, since: $since until: $until) {
history(first: ${limit}, since: $since until: $until) {
nodes { oid }
}
}
Expand All @@ -1187,8 +1196,8 @@ export class GitHubApi implements Disposable {
{
owner: owner,
repo: repo,
since: date.toISOString(),
until: date.toISOString(),
since: date?.toISOString(),
until: date?.toISOString(),
},
scope,
);
Expand All @@ -1200,7 +1209,7 @@ export class GitHubApi implements Disposable {

for (const branch of nodes) {
for (const commit of branch.target.history.nodes) {
if (commit.oid === ref) {
if (refs.includes(commit.oid)) {
branches.push(branch.name);
break;
}
Expand Down Expand Up @@ -1275,8 +1284,9 @@ export class GitHubApi implements Disposable {
owner: string,
repo: string,
branch: string,
ref: string,
date: Date,
refs: string[],
mode: 'contains' | 'pointsAt',
date?: Date,
): Promise<string[]> {
const scope = getLogScope();

Expand All @@ -1291,6 +1301,9 @@ export class GitHubApi implements Disposable {
};
};
}

const limit = mode === 'contains' ? 100 : 1;

try {
const query = `query getCommitOnBranch(
$owner: String!
Expand All @@ -1303,7 +1316,7 @@ export class GitHubApi implements Disposable {
ref(qualifiedName: $ref) {
target {
... on Commit {
history(first: 3, since: $since until: $until) {
history(first: ${limit}, since: $since until: $until) {
nodes { oid }
}
}
Expand All @@ -1319,8 +1332,8 @@ export class GitHubApi implements Disposable {
owner: owner,
repo: repo,
ref: `refs/heads/${branch}`,
since: date.toISOString(),
until: date.toISOString(),
since: date?.toISOString(),
until: date?.toISOString(),
},
scope,
);
Expand All @@ -1331,7 +1344,7 @@ export class GitHubApi implements Disposable {
const branches = [];

for (const commit of nodes) {
if (commit.oid === ref) {
if (refs.includes(commit.oid)) {
branches.push(branch);
break;
}
Expand Down
8 changes: 5 additions & 3 deletions src/plus/integrations/providers/github/githubGitProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ export class GitHubGitProvider implements GitProvider, Disposable {
@log()
async getCommitBranches(
repoPath: string,
ref: string,
refs: string[],
branch?: string | undefined,
options?:
| { all?: boolean; commitDate?: Date; mode?: 'contains' | 'pointsAt' }
Expand All @@ -1080,15 +1080,17 @@ export class GitHubGitProvider implements GitProvider, Disposable {
metadata.repo.owner,
metadata.repo.name,
branch,
ref,
refs,
options?.mode ?? 'contains',
options?.commitDate,
);
} else {
branches = await github.getCommitBranches(
session.accessToken,
metadata.repo.owner,
metadata.repo.name,
ref,
refs,
options?.mode ?? 'contains',
options?.commitDate,
);
}
Expand Down

0 comments on commit a5c3e12

Please sign in to comment.