Skip to content

Commit d2c6ebd

Browse files
committed
Adds branchCreate and branchRename methods
1 parent 218327b commit d2c6ebd

File tree

8 files changed

+74
-7
lines changed

8 files changed

+74
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ 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
18+
1719
### Fixed
1820

1921
- Fixes [#3592](https://github.com/gitkraken/vscode-gitlens/issues/3592) - Connecting to an integration via Remotes view (but likely others) doesn't work

src/commands/git/branch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ export class BranchGitCommand extends QuickCommand {
393393
if (state.flags.includes('--switch')) {
394394
await state.repo.switch(state.reference.ref, { createBranch: state.name });
395395
} else {
396-
state.repo.branch(...state.flags, state.name, state.reference.ref);
396+
await state.repo.git.branchCreate(state.name, state.reference.ref);
397397
}
398398
}
399399
}
@@ -614,7 +614,7 @@ export class BranchGitCommand extends QuickCommand {
614614
state.flags = result;
615615

616616
endSteps(state);
617-
state.repo.branch(...state.flags, state.reference.ref, state.name);
617+
await state.repo.git.branchRename(state.reference.ref, state.name);
618618
}
619619
}
620620

src/env/node/git/git.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,10 @@ export class Git {
506506
}
507507
}
508508

509+
branch(repoPath: string, ...args: string[]) {
510+
return this.git<string>({ cwd: repoPath }, 'branch', ...args);
511+
}
512+
509513
branch__set_upstream(repoPath: string, branch: string, remote: string, remoteBranch: string) {
510514
return this.git<string>({ cwd: repoPath }, 'branch', '--set-upstream-to', `${remote}/${remoteBranch}`, branch);
511515
}

src/env/node/git/localGitProvider.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
WorktreeDeleteErrorReason,
4040
} from '../../../git/errors';
4141
import type {
42+
GitBranchOptions,
4243
GitCaches,
4344
GitDir,
4445
GitProvider,
@@ -1230,6 +1231,19 @@ export class LocalGitProvider implements GitProvider, Disposable {
12301231
}
12311232
}
12321233

1234+
@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+
}
1243+
1244+
throw new Error('Invalid branch options');
1245+
}
1246+
12331247
@log()
12341248
async checkout(
12351249
repoPath: string,

src/git/gitProvider.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ 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+
115126
export interface GitProviderRepository {
116127
addRemote?(repoPath: string, name: string, url: string, options?: { fetch?: boolean }): Promise<void>;
117128
pruneRemote?(repoPath: string, name: string): Promise<void>;
@@ -477,6 +488,7 @@ export interface GitProvider extends GitProviderRepository, Disposable {
477488
getWorkingUri(repoPath: string, uri: Uri): Promise<Uri | undefined>;
478489

479490
applyChangesToWorkingFile?(uri: GitUri, ref1?: string, ref2?: string): Promise<void>;
491+
branch(_repoPath: string, _options: GitBranchOptions): Promise<void>;
480492
clone?(url: string, parentPath: string): Promise<string | undefined>;
481493
/**
482494
* Returns the blame of a file

src/git/gitProviderService.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ 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';
2223
import { getApplicablePromo } from '../plus/gk/account/promos';
2324
import type { Subscription } from '../plus/gk/account/subscription';
2425
import { isSubscriptionPaidPlan } from '../plus/gk/account/subscription';
@@ -44,6 +45,7 @@ import { configuration } from '../system/vscode/configuration';
4445
import { setContext } from '../system/vscode/context';
4546
import { getBestPath } from '../system/vscode/path';
4647
import type {
48+
GitBranchOptions,
4749
GitCaches,
4850
GitDir,
4951
GitProvider,
@@ -1338,6 +1340,38 @@ export class GitProviderService implements Disposable {
13381340
return provider.applyUnreachableCommitForPatch(path, ref, options);
13391341
}
13401342

1343+
@log()
1344+
branchCreate(repoPath: string, name: string, startRef: string): Promise<void> {
1345+
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+
}
1357+
}
1358+
1359+
@log()
1360+
branchRename(repoPath: string, oldName: string, newName: string): Promise<void> {
1361+
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+
}
1373+
}
1374+
13411375
@log()
13421376
checkout(
13431377
repoPath: string | Uri,

src/git/models/repository.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export type RepoGitProviderService = Pick<
3939
[K in keyof GitProviderService]: RemoveFirstArg<GitProviderService[K]>;
4040
},
4141
| keyof GitProviderRepository
42+
| 'branchCreate'
43+
| 'branchRename'
4244
| 'getBestRemoteWithIntegration'
4345
| 'getBranch'
4446
| 'getRemote'
@@ -560,11 +562,6 @@ export class Repository implements Disposable {
560562
return remote;
561563
}
562564

563-
@log()
564-
branch(...args: string[]) {
565-
void this.runTerminalCommand('branch', ...args);
566-
}
567-
568565
@log()
569566
branchDelete(branches: GitBranchReference | GitBranchReference[], options?: { force?: boolean; remote?: boolean }) {
570567
if (!Array.isArray(branches)) {

src/plus/integrations/providers/github/githubGitProvider.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
import { Features } from '../../../../features';
2727
import { GitSearchError } from '../../../../git/errors';
2828
import type {
29+
GitBranchOptions,
2930
GitCaches,
3031
GitProvider,
3132
LeftRightCommitCountResult,
@@ -443,6 +444,9 @@ export class GitHubGitProvider implements GitProvider, Disposable {
443444
return this.createVirtualUri(repoPath, undefined, uri.path);
444445
}
445446

447+
@log()
448+
async branch(_repoPath: string, _options: GitBranchOptions): Promise<void> {}
449+
446450
@log()
447451
async branchContainsCommit(_repoPath: string, _name: string, _ref: string): Promise<boolean> {
448452
return false;

0 commit comments

Comments
 (0)