Skip to content

Commit 8b2779d

Browse files
committed
add branchCreate and branchRename methods
1 parent 0d99fbc commit 8b2779d

File tree

8 files changed

+74
-8
lines changed

8 files changed

+74
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
1515
### Changed
1616

1717
- Adds vscode-test to run unit-tests — closes [#3570](https://github.com/gitkraken/vscode-gitlens/issues/3570)
18-
18+
- Change `branch create` and `branch rename` terminal-run commands into normal commands
1919
### Fixed
2020

2121
- 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: 13 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>;
@@ -126,6 +137,7 @@ export interface GitProviderRepository {
126137
stash?: boolean | 'prompt';
127138
},
128139
): Promise<void>;
140+
129141
checkout(
130142
repoPath: string,
131143
ref: string,
@@ -477,6 +489,7 @@ export interface GitProvider extends GitProviderRepository, Disposable {
477489
getWorkingUri(repoPath: string, uri: Uri): Promise<Uri | undefined>;
478490

479491
applyChangesToWorkingFile(uri: GitUri, ref1?: string, ref2?: string): Promise<void>;
492+
branch(_repoPath: string, _options: GitBranchOptions): Promise<void>;
480493
clone?(url: string, parentPath: string): Promise<string | undefined>;
481494
/**
482495
* Returns the blame of a file

src/git/gitProviderService.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type { SearchQuery } from '../constants.search';
1818
import type { Container } from '../container';
1919
import { AccessDeniedError, CancellationError, ProviderNotFoundError } from '../errors';
2020
import type { FeatureAccess, Features, PlusFeatures, RepoFeatureAccess } from '../features';
21+
import { showGenericErrorMessage } from '../messages';
2122
import { getApplicablePromo } from '../plus/gk/account/promos';
2223
import type { Subscription } from '../plus/gk/account/subscription';
2324
import { isSubscriptionPaidPlan, SubscriptionPlanId } from '../plus/gk/account/subscription';
@@ -43,6 +44,7 @@ import { configuration } from '../system/vscode/configuration';
4344
import { setContext } from '../system/vscode/context';
4445
import { getBestPath } from '../system/vscode/path';
4546
import type {
47+
GitBranchOptions,
4648
GitCaches,
4749
GitDir,
4850
GitProvider,
@@ -1325,6 +1327,38 @@ export class GitProviderService implements Disposable {
13251327
return provider.applyUnreachableCommitForPatch?.(path, ref, options);
13261328
}
13271329

1330+
@log()
1331+
branchCreate(repoPath: string, name: string, startRef: string): Promise<void> {
1332+
const { provider, path } = this.getProvider(repoPath);
1333+
try {
1334+
return provider.branch(path, {
1335+
create: {
1336+
name: name,
1337+
startRef: startRef,
1338+
},
1339+
});
1340+
} catch (ex) {
1341+
Logger.error(ex);
1342+
return showGenericErrorMessage('Unable to create branch');
1343+
}
1344+
}
1345+
1346+
@log()
1347+
branchRename(repoPath: string, oldName: string, newName: string): Promise<void> {
1348+
const { provider, path } = this.getProvider(repoPath);
1349+
try {
1350+
return provider.branch(path, {
1351+
rename: {
1352+
old: oldName,
1353+
new: newName,
1354+
},
1355+
});
1356+
} catch (ex) {
1357+
Logger.error(ex);
1358+
return showGenericErrorMessage('Unable to rename branch');
1359+
}
1360+
}
1361+
13281362
@log()
13291363
checkout(
13301364
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
@@ -25,6 +25,7 @@ import {
2525
import { Features } from '../../../../features';
2626
import { GitSearchError } from '../../../../git/errors';
2727
import type {
28+
GitBranchOptions,
2829
GitCaches,
2930
GitProvider,
3031
LeftRightCommitCountResult,
@@ -456,6 +457,9 @@ export class GitHubGitProvider implements GitProvider, Disposable {
456457
@log()
457458
async applyChangesToWorkingFile(_uri: GitUri, _ref1?: string, _ref2?: string): Promise<void> {}
458459

460+
@log()
461+
async branch(_repoPath: string, _options: GitBranchOptions): Promise<void> {}
462+
459463
@log()
460464
async branchContainsCommit(_repoPath: string, _name: string, _ref: string): Promise<boolean> {
461465
return false;

0 commit comments

Comments
 (0)