Skip to content

Commit

Permalink
handle BranchError in localGitProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiolms committed Oct 3, 2024
1 parent ccee185 commit eab5948
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 52 deletions.
8 changes: 4 additions & 4 deletions src/commands/git/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ export class BranchGitCommand extends QuickCommand {
} catch (ex) {
Logger.error(ex);
// TODO likely need some better error handling here
return showGenericErrorMessage(ex.WithBranch(state.name));
return showGenericErrorMessage(ex);
}
}
}
Expand Down Expand Up @@ -525,9 +525,9 @@ export class BranchGitCommand extends QuickCommand {
remote: state.flags.includes('--remotes'),
});
} catch (ex) {
Logger.error(ex);
// TODO likely need some better error handling here
return showGenericErrorMessage(ex.WithBranch(ref.name));
Logger.error(ex);
return showGenericErrorMessage(ex);
}
}
}
Expand Down Expand Up @@ -637,7 +637,7 @@ export class BranchGitCommand extends QuickCommand {
} catch (ex) {
Logger.error(ex);
// TODO likely need some better error handling here
return showGenericErrorMessage(ex.WithBranch(state.name));
return showGenericErrorMessage(ex);
}
}
}
Expand Down
111 changes: 64 additions & 47 deletions src/env/node/git/localGitProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
ApplyPatchCommitErrorReason,
BlameIgnoreRevsFileBadRevisionError,
BlameIgnoreRevsFileError,
BranchError,
CherryPickError,
CherryPickErrorReason,
FetchError,
Expand Down Expand Up @@ -182,17 +183,7 @@ import { countStringLength, filterMap } from '../../../system/array';
import { gate } from '../../../system/decorators/gate';
import { debug, log } from '../../../system/decorators/log';
import { debounce } from '../../../system/function';
import {
filterMap as filterMapIterable,
find,
first,
groupByMap,
join,
last,
map,
skip,
some,
} from '../../../system/iterable';
import { filterMap as filterMapIterable, find, first, join, last, map, skip, some } from '../../../system/iterable';
import { Logger } from '../../../system/logger';
import type { LogScope } from '../../../system/logger.scope';
import { getLogScope, setLogScopeExit } from '../../../system/logger.scope';
Expand Down Expand Up @@ -1241,13 +1232,29 @@ export class LocalGitProvider implements GitProvider, Disposable {
}

@log()
async createBranch(repoPath: string, name: string, ref: string): Promise<void> {
await this.git.branch(repoPath, name, ref);
createBranch(repoPath: string, name: string, ref: string): Promise<void> {
try {
return void this.git.branch(repoPath, name, ref);
} catch (ex) {
if (ex instanceof BranchError) {
throw ex.WithBranch(branch.name);
}

throw ex;
}
}

@log()
async renameBranch(repoPath: string, oldName: string, newName: string): Promise<void> {
await this.git.branch(repoPath, '-m', oldName, newName);
renameBranch(repoPath: string, oldName: string, newName: string): Promise<void> {
try {
return void this.git.branch(repoPath, '-m', oldName, newName);
} catch (ex) {
if (ex instanceof BranchError) {
throw ex.WithBranch(branch.name);
}

throw ex;
}
}

@log()
Expand All @@ -1256,46 +1263,56 @@ export class LocalGitProvider implements GitProvider, Disposable {
branch: GitBranchReference,
options: { force?: boolean; remote?: boolean },
): Promise<void> {
if (branch.remote) {
return this.git.push(repoPath, {
delete: {
remote: getRemoteNameFromBranchName(branch.name),
branch: branch.remote ? getBranchNameWithoutRemote(branch.name) : branch.name,
},
});
}
try {
if (branch.remote) {
await this.git.push(repoPath, {
delete: {
remote: getRemoteNameFromBranchName(branch.name),
branch: branch.remote ? getBranchNameWithoutRemote(branch.name) : branch.name,
},
});
return;
}

const args = ['--delete'];
if (options.force) {
args.push('--force');
}
const args = ['--delete'];
if (options.force) {
args.push('--force');
}

if (!options.remote || !branch.upstream) {
return this.git.branch(repoPath, ...args, branch.ref);
}
if (!options.remote || !branch.upstream) {
await this.git.branch(repoPath, ...args, branch.ref);
return;
}

const remote = getRemoteNameFromBranchName(branch.upstream.name);
remoteCommit = await this.git.rev_list(repoPath, `refs/remotes/${remote}/${branch.ref}`, {
maxResults: 1,
});
const remote = getRemoteNameFromBranchName(branch.upstream.name);
remoteCommit = await this.git.rev_list(repoPath, `refs/remotes/${remote}/${branch.ref}`, {
maxResults: 1,
});

await this.git.branch(repoPath, '--delete', '--remotes', `${remote}/${branch.ref}`);
await this.git.branch(repoPath, '--delete', '--remotes', `${remote}/${branch.ref}`);

try {
await this.git.branch(repoPath, ...args, branch.ref);
try {
await this.git.branch(repoPath, ...args, branch.ref);
} catch (ex) {
// If it fails, restore the remote branch
await this.git.update_ref(repoPath, `refs/remotes/${remote}/${branch.ref}`, commit);
await this.git.branch__set_upstream(repoPath, branch, remote, branch);
throw ex;
}

await this.git.push(repoPath, {
delete: {
remote: remote,
branch: getBranchNameWithoutRemote(branch.upstream.name),
},
});
} catch (ex) {
// If it fails, restore the remote branch
await this.git.update_ref(repoPath, `refs/remotes/${remote}/${branch.ref}`, commit);
await this.git.branch__set_upstream(repoPath, branch, remote, branch);
if (ex instanceof BranchError) {
throw ex.WithBranch(branch.name);
}

throw ex;
}

await this.git.push(repoPath, {
delete: {
remote: remote,
branch: getBranchNameWithoutRemote(branch.upstream.name),
},
});
}

@log()
Expand Down
2 changes: 1 addition & 1 deletion src/git/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export class BranchError extends Error {
}

private static buildBranchErrorMessage(reason?: BranchErrorReason, branch?: string): string {
const baseMessage = `Unable to perform action on branch${branch ? ` '${branch}'` : ''}`;
const baseMessage = `Unable to perform action ${branch ? `with branch '${branch}'` : 'on branch'}`;
switch (reason) {
case BranchErrorReason.BranchAlreadyExists:
return `${baseMessage} because it already exists`;
Expand Down

0 comments on commit eab5948

Please sign in to comment.