Skip to content

Commit aae8254

Browse files
eamodiosergiolms
authored andcommitted
Refactors branch creation and renaming
- Moves error handling outside of GitProviderService (to allow callers control over handling) - Splits unified branch command into specific calls and makes them optional Refs #3528
1 parent d2c6ebd commit aae8254

File tree

7 files changed

+35
-57
lines changed

7 files changed

+35
-57
lines changed

Diff for: CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
1414

1515
### Changed
1616

17-
- Change `branch create` and `branch rename` terminal-run commands into normal commands
17+
- Changes how GitLens handles creating and renaming branches to avoid using the terminal — refs [#3528](https://github.com/gitkraken/vscode-gitlens/issues/3528)
1818

1919
### Fixed
2020

Diff for: src/commands/git/branch.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import { getNameWithoutRemote, getReferenceLabel, isRevisionReference } from '..
55
import { Repository } from '../../git/models/repository';
66
import type { GitWorktree } from '../../git/models/worktree';
77
import { getWorktreesByBranch } from '../../git/models/worktree';
8+
import { showGenericErrorMessage } from '../../messages';
89
import type { QuickPickItemOfT } from '../../quickpicks/items/common';
910
import { createQuickPickSeparator } from '../../quickpicks/items/common';
1011
import type { FlagsQuickPickItem } from '../../quickpicks/items/flags';
1112
import { createFlagsQuickPickItem } from '../../quickpicks/items/flags';
1213
import { ensureArray } from '../../system/array';
14+
import { Logger } from '../../system/logger';
1315
import { pluralize } from '../../system/string';
1416
import type { ViewsWithRepositoryFolders } from '../../views/viewBase';
1517
import type {
@@ -393,7 +395,13 @@ export class BranchGitCommand extends QuickCommand {
393395
if (state.flags.includes('--switch')) {
394396
await state.repo.switch(state.reference.ref, { createBranch: state.name });
395397
} else {
396-
await state.repo.git.branchCreate(state.name, state.reference.ref);
398+
try {
399+
await state.repo.git.createBranch(state.name, state.reference.ref);
400+
} catch (ex) {
401+
Logger.error(ex);
402+
// TODO likely need some better error handling here
403+
return showGenericErrorMessage('Unable to create branch');
404+
}
397405
}
398406
}
399407
}
@@ -614,7 +622,13 @@ export class BranchGitCommand extends QuickCommand {
614622
state.flags = result;
615623

616624
endSteps(state);
617-
await state.repo.git.branchRename(state.reference.ref, state.name);
625+
try {
626+
await state.repo.git.renameBranch(state.reference.ref, state.name);
627+
} catch (ex) {
628+
Logger.error(ex);
629+
// TODO likely need some better error handling here
630+
return showGenericErrorMessage('Unable to rename branch');
631+
}
618632
}
619633
}
620634

Diff for: src/env/node/git/localGitProvider.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import {
3939
WorktreeDeleteErrorReason,
4040
} from '../../../git/errors';
4141
import type {
42-
GitBranchOptions,
4342
GitCaches,
4443
GitDir,
4544
GitProvider,
@@ -1232,16 +1231,13 @@ export class LocalGitProvider implements GitProvider, Disposable {
12321231
}
12331232

12341233
@log()
1235-
async branch(repoPath: string, options: GitBranchOptions): Promise<void> {
1236-
if (options?.create != null) {
1237-
return this.git.branch(repoPath, options.create.name, options.create.startRef);
1238-
}
1239-
1240-
if (options?.rename != null) {
1241-
return this.git.branch(repoPath, '-m', options.rename.old, options.rename.new);
1242-
}
1234+
async createBranch(repoPath: string, name: string, ref: string): Promise<void> {
1235+
await this.git.branch(repoPath, name, ref);
1236+
}
12431237

1244-
throw new Error('Invalid branch options');
1238+
@log()
1239+
async renameBranch(repoPath: string, oldName: string, newName: string): Promise<void> {
1240+
await this.git.branch(repoPath, '-m', oldName, newName);
12451241
}
12461242

12471243
@log()

Diff for: src/git/gitProvider.ts

+4-12
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,14 @@ export interface RepositoryVisibilityInfo {
112112
remotesHash?: string;
113113
}
114114

115-
export type GitBranchOptions = {
116-
rename?: {
117-
old: string;
118-
new: string;
119-
};
120-
create?: {
121-
name: string;
122-
startRef: string;
123-
};
124-
};
125-
126115
export interface GitProviderRepository {
116+
createBranch?(repoPath: string, name: string, ref: string): Promise<void>;
117+
renameBranch?(repoPath: string, oldName: string, newName: string): Promise<void>;
118+
127119
addRemote?(repoPath: string, name: string, url: string, options?: { fetch?: boolean }): Promise<void>;
128120
pruneRemote?(repoPath: string, name: string): Promise<void>;
129121
removeRemote?(repoPath: string, name: string): Promise<void>;
122+
130123
applyUnreachableCommitForPatch?(
131124
repoPath: string,
132125
ref: string,
@@ -488,7 +481,6 @@ export interface GitProvider extends GitProviderRepository, Disposable {
488481
getWorkingUri(repoPath: string, uri: Uri): Promise<Uri | undefined>;
489482

490483
applyChangesToWorkingFile?(uri: GitUri, ref1?: string, ref2?: string): Promise<void>;
491-
branch(_repoPath: string, _options: GitBranchOptions): Promise<void>;
492484
clone?(url: string, parentPath: string): Promise<string | undefined>;
493485
/**
494486
* Returns the blame of a file

Diff for: src/git/gitProviderService.ts

+8-26
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { SubscriptionPlanId } from '../constants.subscription';
1919
import type { Container } from '../container';
2020
import { AccessDeniedError, CancellationError, ProviderNotFoundError, ProviderNotSupportedError } from '../errors';
2121
import type { FeatureAccess, Features, PlusFeatures, RepoFeatureAccess } from '../features';
22-
import { showGenericErrorMessage } from '../messages';
2322
import { getApplicablePromo } from '../plus/gk/account/promos';
2423
import type { Subscription } from '../plus/gk/account/subscription';
2524
import { isSubscriptionPaidPlan } from '../plus/gk/account/subscription';
@@ -45,7 +44,6 @@ import { configuration } from '../system/vscode/configuration';
4544
import { setContext } from '../system/vscode/context';
4645
import { getBestPath } from '../system/vscode/path';
4746
import type {
48-
GitBranchOptions,
4947
GitCaches,
5048
GitDir,
5149
GitProvider,
@@ -1341,35 +1339,19 @@ export class GitProviderService implements Disposable {
13411339
}
13421340

13431341
@log()
1344-
branchCreate(repoPath: string, name: string, startRef: string): Promise<void> {
1342+
createBranch(repoPath: string | Uri, name: string, ref: string): Promise<void> {
13451343
const { provider, path } = this.getProvider(repoPath);
1346-
try {
1347-
return provider.branch(path, {
1348-
create: {
1349-
name: name,
1350-
startRef: startRef,
1351-
},
1352-
});
1353-
} catch (ex) {
1354-
Logger.error(ex);
1355-
return showGenericErrorMessage('Unable to create branch');
1356-
}
1344+
if (provider.createBranch == null) throw new ProviderNotSupportedError(provider.descriptor.name);
1345+
1346+
return provider.createBranch(path, name, ref);
13571347
}
13581348

13591349
@log()
1360-
branchRename(repoPath: string, oldName: string, newName: string): Promise<void> {
1350+
renameBranch(repoPath: string | Uri, oldName: string, newName: string): Promise<void> {
13611351
const { provider, path } = this.getProvider(repoPath);
1362-
try {
1363-
return provider.branch(path, {
1364-
rename: {
1365-
old: oldName,
1366-
new: newName,
1367-
},
1368-
});
1369-
} catch (ex) {
1370-
Logger.error(ex);
1371-
return showGenericErrorMessage('Unable to rename branch');
1372-
}
1352+
if (provider.renameBranch == null) throw new ProviderNotSupportedError(provider.descriptor.name);
1353+
1354+
return provider.renameBranch(path, oldName, newName);
13731355
}
13741356

13751357
@log()

Diff for: src/git/models/repository.ts

-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ export type RepoGitProviderService = Pick<
3939
[K in keyof GitProviderService]: RemoveFirstArg<GitProviderService[K]>;
4040
},
4141
| keyof GitProviderRepository
42-
| 'branchCreate'
43-
| 'branchRename'
4442
| 'getBestRemoteWithIntegration'
4543
| 'getBranch'
4644
| 'getRemote'

Diff for: src/plus/integrations/providers/github/githubGitProvider.ts

-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
import { Features } from '../../../../features';
2727
import { GitSearchError } from '../../../../git/errors';
2828
import type {
29-
GitBranchOptions,
3029
GitCaches,
3130
GitProvider,
3231
LeftRightCommitCountResult,
@@ -444,9 +443,6 @@ export class GitHubGitProvider implements GitProvider, Disposable {
444443
return this.createVirtualUri(repoPath, undefined, uri.path);
445444
}
446445

447-
@log()
448-
async branch(_repoPath: string, _options: GitBranchOptions): Promise<void> {}
449-
450446
@log()
451447
async branchContainsCommit(_repoPath: string, _name: string, _ref: string): Promise<boolean> {
452448
return false;

0 commit comments

Comments
 (0)