Skip to content

Commit 06a026c

Browse files
committed
Removes redundant methods to sub-providers
1 parent a7bfe8b commit 06a026c

File tree

9 files changed

+71
-57
lines changed

9 files changed

+71
-57
lines changed

src/commands/git/remote.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,9 @@ export class RemoteGitCommand extends QuickCommand<State> {
328328

329329
endSteps(state);
330330

331-
const remote = await state.repo.addRemote(
332-
state.name,
333-
state.url,
334-
state.flags.includes('-f') ? { fetch: true } : undefined,
335-
);
331+
const remote = await state.repo.git
332+
.remotes()
333+
.addRemoteWithResult?.(state.name, state.url, state.flags.includes('-f') ? { fetch: true } : undefined);
336334
if (state.reveal !== false) {
337335
void reveal(remote, {
338336
focus: true,

src/commands/git/worktree.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,10 +522,12 @@ export class WorktreeGitCommand extends QuickCommand<State> {
522522
let worktree: GitWorktree | undefined;
523523
try {
524524
if (state.addRemote != null) {
525-
await state.repo.addRemote(state.addRemote.name, state.addRemote.url, { fetch: true });
525+
await state.repo.git
526+
.remotes()
527+
.addRemoteWithResult?.(state.addRemote.name, state.addRemote.url, { fetch: true });
526528
}
527529

528-
worktree = await state.repo.createWorktree(uri, {
530+
worktree = await state.repo.git.worktrees()?.createWorktreeWithResult(uri.fsPath, {
529531
commitish: state.reference?.name,
530532
createBranch: state.flags.includes('-b') ? state.createBranch : undefined,
531533
detach: state.flags.includes('--detach'),

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type { GitCommit } from '../../../../git/models/commit';
1717
import { log } from '../../../../system/decorators/log';
1818
import { Logger } from '../../../../system/logger';
1919
import { getLogScope } from '../../../../system/logger.scope';
20-
import { joinPaths, normalizePath } from '../../../../system/path';
20+
import { joinPaths } from '../../../../system/path';
2121
import type { Git } from '../git';
2222
import type { LocalGitProvider } from '../localGitProvider';
2323

@@ -94,10 +94,23 @@ export class PatchGitSubProvider implements GitPatchSubProvider {
9494
}
9595

9696
try {
97-
await this.provider.worktrees.createWorktree(repoPath, options.createWorktreePath, {
98-
commitish: options?.branchName != null && branchExists ? options.branchName : currentBranch?.name,
99-
createBranch: shouldCreate ? options.branchName : undefined,
100-
});
97+
const worktree = await this.provider.worktrees.createWorktreeWithResult(
98+
repoPath,
99+
options.createWorktreePath,
100+
{
101+
commitish:
102+
options?.branchName != null && branchExists ? options.branchName : currentBranch?.name,
103+
createBranch: shouldCreate ? options.branchName : undefined,
104+
},
105+
);
106+
if (worktree == null) {
107+
throw new ApplyPatchCommitError(
108+
ApplyPatchCommitErrorReason.CreateWorktreeFailed,
109+
'Unable to apply patch; failed creating worktree',
110+
);
111+
}
112+
113+
targetPath = worktree.uri.fsPath;
101114
} catch (ex) {
102115
Logger.error(ex, scope);
103116
throw new ApplyPatchCommitError(
@@ -108,19 +121,6 @@ export class PatchGitSubProvider implements GitPatchSubProvider {
108121
ex,
109122
);
110123
}
111-
112-
const worktree = await this.provider.worktrees?.getWorktree(
113-
repoPath,
114-
w => normalizePath(w.uri.fsPath) === normalizePath(options.createWorktreePath!),
115-
);
116-
if (worktree == null) {
117-
throw new ApplyPatchCommitError(
118-
ApplyPatchCommitErrorReason.CreateWorktreeFailed,
119-
'Unable to apply patch; failed creating worktree',
120-
);
121-
}
122-
123-
targetPath = worktree.uri.fsPath;
124124
}
125125

126126
if (options?.branchName != null && currentBranch?.name !== options.branchName) {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ export class RemotesGitSubProvider extends RemotesGitProviderBase implements Git
8282
this.container.events.fire('git:cache:reset', { repoPath: repoPath, caches: ['remotes'] });
8383
}
8484

85+
@gate()
86+
@log()
87+
async addRemoteWithResult(
88+
repoPath: string,
89+
name: string,
90+
url: string,
91+
options?: { fetch?: boolean },
92+
): Promise<GitRemote | undefined> {
93+
await this.addRemote(repoPath, name, url, options);
94+
const [remote] = await this.getRemotes(repoPath, { filter: r => r.url === url });
95+
return remote;
96+
}
97+
8598
@gate()
8699
@log()
87100
async pruneRemote(repoPath: string, name: string): Promise<void> {

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ export class WorktreesGitSubProvider implements GitWorktreesSubProvider {
3737
repoPath: string,
3838
path: string,
3939
options?: { commitish?: string; createBranch?: string; detach?: boolean; force?: boolean },
40-
) {
40+
): Promise<void> {
4141
const scope = getLogScope();
4242

4343
try {
4444
await this.git.worktree__add(repoPath, path, options);
45-
this.container.events.fire('git:cache:reset', { repoPath: repoPath, caches: ['worktrees'] });
46-
if (options?.createBranch) {
47-
this.container.events.fire('git:cache:reset', { repoPath: repoPath, caches: ['branches'] });
48-
}
45+
this.container.events.fire('git:cache:reset', {
46+
repoPath: repoPath,
47+
caches: options?.createBranch ? ['branches', 'worktrees'] : ['worktrees'],
48+
});
4949
} catch (ex) {
5050
Logger.error(ex, scope);
5151

@@ -63,6 +63,16 @@ export class WorktreesGitSubProvider implements GitWorktreesSubProvider {
6363
}
6464
}
6565

66+
async createWorktreeWithResult(
67+
repoPath: string,
68+
path: string,
69+
options?: { commitish?: string; createBranch?: string; detach?: boolean; force?: boolean },
70+
): Promise<GitWorktree | undefined> {
71+
await this.createWorktree(repoPath, path, options);
72+
const normalized = normalizePath(path);
73+
return this.getWorktree(repoPath, w => normalizePath(w.uri.fsPath) === normalized);
74+
}
75+
6676
@log()
6777
async getWorktree(repoPath: string, predicate: (w: GitWorktree) => boolean): Promise<GitWorktree | undefined> {
6878
return (await this.getWorktrees(repoPath)).find(predicate);

src/git/gitProvider.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,12 @@ export interface GitRemotesSubProvider {
482482
cancellation?: CancellationToken,
483483
): Promise<GitRemote<RemoteProvider> | undefined>;
484484
addRemote?(repoPath: string, name: string, url: string, options?: { fetch?: boolean }): Promise<void>;
485+
addRemoteWithResult?(
486+
repoPath: string,
487+
name: string,
488+
url: string,
489+
options?: { fetch?: boolean },
490+
): Promise<GitRemote | undefined>;
485491
pruneRemote?(repoPath: string, name: string): Promise<void>;
486492
removeRemote?(repoPath: string, name: string): Promise<void>;
487493
setRemoteAsDefault(repoPath: string, name: string, value?: boolean): Promise<void>;
@@ -546,6 +552,11 @@ export interface GitWorktreesSubProvider {
546552
path: string,
547553
options?: { commitish?: string; createBranch?: string; detach?: boolean; force?: boolean },
548554
): Promise<void>;
555+
createWorktreeWithResult(
556+
repoPath: string,
557+
path: string,
558+
options?: { commitish?: string; createBranch?: string; detach?: boolean; force?: boolean },
559+
): Promise<GitWorktree | undefined>;
549560
getWorktree(repoPath: string, predicate: (w: GitWorktree) => boolean): Promise<GitWorktree | undefined>;
550561
getWorktrees(repoPath: string): Promise<GitWorktree[]>;
551562
getWorktreesDefaultUri(repoPath: string): Promise<Uri | undefined>;

src/git/models/pullRequest.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,9 @@ export async function ensurePullRequestRemote(
400400
);
401401

402402
if (result === confirm) {
403-
await repo.addRemote(identity.provider.repoDomain, identity.remote.url, { fetch: true });
403+
await repo.git
404+
.remotes()
405+
.addRemoteWithResult?.(identity.provider.repoDomain, identity.remote.url, { fetch: true });
404406
return true;
405407
}
406408

src/git/models/repository.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ import type { GitBranch } from './branch';
2828
import { getBranchNameWithoutRemote, getNameWithoutRemote, getRemoteNameFromBranchName } from './branch.utils';
2929
import type { GitBranchReference, GitReference } from './reference';
3030
import { isBranchReference } from './reference.utils';
31-
import type { GitRemote } from './remote';
32-
import type { GitWorktree } from './worktree';
3331

3432
type GitProviderRepoKeys = keyof GitRepositoryProvider | 'supports';
3533

@@ -543,14 +541,6 @@ export class Repository implements Disposable {
543541
return this.container.git.access(feature, this.uri);
544542
}
545543

546-
// TODO: Can we remove this -- since no callers use the return value (though maybe they need that await?)
547-
@log()
548-
async addRemote(name: string, url: string, options?: { fetch?: boolean }): Promise<GitRemote | undefined> {
549-
await this.git.remotes().addRemote?.(name, url, options);
550-
const [remote] = await this.git.remotes().getRemotes({ filter: r => r.url === url });
551-
return remote;
552-
}
553-
554544
@log()
555545
branchDelete(branches: GitBranchReference | GitBranchReference[], options?: { force?: boolean; remote?: boolean }) {
556546
if (!Array.isArray(branches)) {
@@ -687,20 +677,6 @@ export class Repository implements Disposable {
687677
return this._lastFetched ?? 0;
688678
}
689679

690-
// TODO: Move to GitProviderService?
691-
@log()
692-
async createWorktree(
693-
uri: Uri,
694-
options?: { commitish?: string; createBranch?: string; detach?: boolean; force?: boolean },
695-
): Promise<GitWorktree | undefined> {
696-
const worktrees = this.git.worktrees();
697-
if (worktrees == null) return undefined;
698-
699-
await worktrees.createWorktree(uri.fsPath, options);
700-
const url = uri.toString();
701-
return worktrees.getWorktree(w => w.uri.toString() === url);
702-
}
703-
704680
@log()
705681
merge(...args: string[]) {
706682
void this.runTerminalCommand('merge', ...args);

src/uris/deepLinks/deepLinkService.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ export class DeepLinkService implements Disposable {
924924

925925
if (remoteName) {
926926
try {
927-
await repo.addRemote(remoteName, remoteUrl, { fetch: true });
927+
await repo.git.remotes().addRemoteWithResult?.(remoteName, remoteUrl, { fetch: true });
928928
} catch {
929929
action = DeepLinkServiceAction.DeepLinkErrored;
930930
message = 'Failed to add remote.';
@@ -953,7 +953,9 @@ export class DeepLinkService implements Disposable {
953953

954954
if (secondaryRemoteName) {
955955
try {
956-
await repo.addRemote(secondaryRemoteName, secondaryRemoteUrl, { fetch: true });
956+
await repo.git
957+
.remotes()
958+
.addRemoteWithResult?.(secondaryRemoteName, secondaryRemoteUrl, { fetch: true });
957959
} catch {
958960
action = DeepLinkServiceAction.DeepLinkErrored;
959961
message = 'Failed to add remote.';

0 commit comments

Comments
 (0)