Skip to content

Commit

Permalink
add branchCreate and branchRename methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiolms committed Sep 25, 2024
1 parent 0d99fbc commit 8b2779d
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
### Changed

- Adds vscode-test to run unit-tests — closes [#3570](https://github.com/gitkraken/vscode-gitlens/issues/3570)

- Change `branch create` and `branch rename` terminal-run commands into normal commands
### Fixed

- Fixes [#3592](https://github.com/gitkraken/vscode-gitlens/issues/3592) - Connecting to an integration via Remotes view (but likely others) doesn't work
Expand Down
4 changes: 2 additions & 2 deletions src/commands/git/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ export class BranchGitCommand extends QuickCommand {
if (state.flags.includes('--switch')) {
await state.repo.switch(state.reference.ref, { createBranch: state.name });
} else {
state.repo.branch(...state.flags, state.name, state.reference.ref);
await state.repo.git.branchCreate(state.name, state.reference.ref);
}
}
}
Expand Down Expand Up @@ -614,7 +614,7 @@ export class BranchGitCommand extends QuickCommand {
state.flags = result;

endSteps(state);
state.repo.branch(...state.flags, state.reference.ref, state.name);
await state.repo.git.branchRename(state.reference.ref, state.name);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/env/node/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,10 @@ export class Git {
}
}

branch(repoPath: string, ...args: string[]) {
return this.git<string>({ cwd: repoPath }, 'branch', ...args);
}

branch__set_upstream(repoPath: string, branch: string, remote: string, remoteBranch: string) {
return this.git<string>({ cwd: repoPath }, 'branch', '--set-upstream-to', `${remote}/${remoteBranch}`, branch);
}
Expand Down
14 changes: 14 additions & 0 deletions src/env/node/git/localGitProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
WorktreeDeleteErrorReason,
} from '../../../git/errors';
import type {
GitBranchOptions,
GitCaches,
GitDir,
GitProvider,
Expand Down Expand Up @@ -1230,6 +1231,19 @@ export class LocalGitProvider implements GitProvider, Disposable {
}
}

@log()
async branch(repoPath: string, options: GitBranchOptions): Promise<void> {
if (options?.create != null) {
return this.git.branch(repoPath, options.create.name, options.create.startRef);
}

if (options?.rename != null) {
return this.git.branch(repoPath, '-m', options.rename.old, options.rename.new);
}

throw new Error('Invalid branch options');
}

@log()
async checkout(
repoPath: string,
Expand Down
13 changes: 13 additions & 0 deletions src/git/gitProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ export interface RepositoryVisibilityInfo {
remotesHash?: string;
}

export type GitBranchOptions = {
rename?: {
old: string;
new: string;
};
create?: {
name: string;
startRef: string;
};
};

export interface GitProviderRepository {
addRemote(repoPath: string, name: string, url: string, options?: { fetch?: boolean }): Promise<void>;
pruneRemote(repoPath: string, name: string): Promise<void>;
Expand All @@ -126,6 +137,7 @@ export interface GitProviderRepository {
stash?: boolean | 'prompt';
},
): Promise<void>;

checkout(
repoPath: string,
ref: string,
Expand Down Expand Up @@ -477,6 +489,7 @@ export interface GitProvider extends GitProviderRepository, Disposable {
getWorkingUri(repoPath: string, uri: Uri): Promise<Uri | undefined>;

applyChangesToWorkingFile(uri: GitUri, ref1?: string, ref2?: string): Promise<void>;
branch(_repoPath: string, _options: GitBranchOptions): Promise<void>;
clone?(url: string, parentPath: string): Promise<string | undefined>;
/**
* Returns the blame of a file
Expand Down
34 changes: 34 additions & 0 deletions src/git/gitProviderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { SearchQuery } from '../constants.search';
import type { Container } from '../container';
import { AccessDeniedError, CancellationError, ProviderNotFoundError } from '../errors';
import type { FeatureAccess, Features, PlusFeatures, RepoFeatureAccess } from '../features';
import { showGenericErrorMessage } from '../messages';
import { getApplicablePromo } from '../plus/gk/account/promos';
import type { Subscription } from '../plus/gk/account/subscription';
import { isSubscriptionPaidPlan, SubscriptionPlanId } from '../plus/gk/account/subscription';
Expand All @@ -43,6 +44,7 @@ import { configuration } from '../system/vscode/configuration';
import { setContext } from '../system/vscode/context';
import { getBestPath } from '../system/vscode/path';
import type {
GitBranchOptions,
GitCaches,
GitDir,
GitProvider,
Expand Down Expand Up @@ -1325,6 +1327,38 @@ export class GitProviderService implements Disposable {
return provider.applyUnreachableCommitForPatch?.(path, ref, options);
}

@log()
branchCreate(repoPath: string, name: string, startRef: string): Promise<void> {
const { provider, path } = this.getProvider(repoPath);
try {
return provider.branch(path, {
create: {
name: name,
startRef: startRef,
},
});
} catch (ex) {
Logger.error(ex);
return showGenericErrorMessage('Unable to create branch');
}
}

@log()
branchRename(repoPath: string, oldName: string, newName: string): Promise<void> {
const { provider, path } = this.getProvider(repoPath);
try {
return provider.branch(path, {
rename: {
old: oldName,
new: newName,
},
});
} catch (ex) {
Logger.error(ex);
return showGenericErrorMessage('Unable to rename branch');
}
}

@log()
checkout(
repoPath: string | Uri,
Expand Down
7 changes: 2 additions & 5 deletions src/git/models/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export type RepoGitProviderService = Pick<
[K in keyof GitProviderService]: RemoveFirstArg<GitProviderService[K]>;
},
| keyof GitProviderRepository
| 'branchCreate'
| 'branchRename'
| 'getBestRemoteWithIntegration'
| 'getBranch'
| 'getRemote'
Expand Down Expand Up @@ -560,11 +562,6 @@ export class Repository implements Disposable {
return remote;
}

@log()
branch(...args: string[]) {
void this.runTerminalCommand('branch', ...args);
}

@log()
branchDelete(branches: GitBranchReference | GitBranchReference[], options?: { force?: boolean; remote?: boolean }) {
if (!Array.isArray(branches)) {
Expand Down
4 changes: 4 additions & 0 deletions src/plus/integrations/providers/github/githubGitProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { Features } from '../../../../features';
import { GitSearchError } from '../../../../git/errors';
import type {
GitBranchOptions,
GitCaches,
GitProvider,
LeftRightCommitCountResult,
Expand Down Expand Up @@ -456,6 +457,9 @@ export class GitHubGitProvider implements GitProvider, Disposable {
@log()
async applyChangesToWorkingFile(_uri: GitUri, _ref1?: string, _ref2?: string): Promise<void> {}

@log()
async branch(_repoPath: string, _options: GitBranchOptions): Promise<void> {}

@log()
async branchContainsCommit(_repoPath: string, _name: string, _ref: string): Promise<boolean> {
return false;
Expand Down

0 comments on commit 8b2779d

Please sign in to comment.