Skip to content

Commit

Permalink
Remove remote created by extension when deleting branch if nothing el…
Browse files Browse the repository at this point in the history
…se references it, #143
  • Loading branch information
Rachel Macfarlane committed Aug 8, 2018
1 parent f43a077 commit a32dacc
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
14 changes: 6 additions & 8 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { fromReviewUri } from './common/uri';
import { FileChangeNode } from './view/treeNodes/fileChangeNode';
import { PRNode } from './view/treeNodes/pullRequestNode';
import { IPullRequestManager, IPullRequestModel, IPullRequest } from './github/interface';
import { exec } from './common/git';

const _onDidClosePR = new vscode.EventEmitter<IPullRequest>();
export const onDidClosePR: vscode.Event<IPullRequest> = _onDidClosePR.event;
Expand Down Expand Up @@ -51,14 +50,13 @@ export function registerCommands(context: vscode.ExtensionContext, prManager: IP
}));

context.subscriptions.push(vscode.commands.registerCommand('pr.deleteLocalBranch', async (e: PRNode) => {
const result = await exec(['branch', '-D', e.pullRequestModel.localBranchName], { cwd: vscode.workspace.rootPath });

if (result.stderr) {
vscode.window.showErrorMessage(`Deleting local pull request branch failed: ${result.stderr}`);
return;
const pullRequestModel = ensurePR(prManager, e);
try {
await prManager.deleteLocalPullRequest(pullRequestModel);
vscode.commands.executeCommand('pr.refreshList');
} catch (e) {
vscode.window.showErrorMessage(`Deleting local pull request branch failed: ${e}`);
}

vscode.commands.executeCommand('pr.refreshList');
}));

context.subscriptions.push(vscode.commands.registerCommand('pr.pick', async (pr: PRNode | IPullRequestModel) => {
Expand Down
2 changes: 1 addition & 1 deletion src/common/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export class Repository {
return [];
}

return result.stdout.trim().split(/\r|\n|\r\n/).map(branchName => {
return result.stdout.trimRight().split(/\r|\n|\r\n/).map(branchName => {
return branchName.substr(2);
});
}
Expand Down
1 change: 1 addition & 0 deletions src/github/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export interface IPullRequestManager {
activePullRequest?: IPullRequestModel;
readonly onDidChangeActivePullRequest: vscode.Event<void>;
getLocalPullRequests(): Promise<IPullRequestModel[]>;
deleteLocalPullRequest(pullRequest: IPullRequestModel): Promise<void>;
getPullRequests(type: PRType, options?: IPullRequestsPagingOptions):Promise<[IPullRequestModel[], boolean]>;
mayHaveMorePages(): boolean;
getPullRequestComments(pullRequest: IPullRequestModel): Promise<Comment[]>;
Expand Down
7 changes: 3 additions & 4 deletions src/github/pullRequestGitHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,11 @@ export class PullRequestGitHelper {
Logger.appendLine(`GitHelper> create remote for ${cloneUrl}.`)

let remotes = repository.remotes;

remotes.forEach(remote => {
for (let remote of remotes) {
if (new Protocol(remote.url).equals(cloneUrl)) {
return remote.repositoryName;
return remote.remoteName;
}
});
}

let remoteName = PullRequestGitHelper.getUniqueRemoteName(repository, cloneUrl.owner);
await repository.addRemote(remoteName, cloneUrl.normalizeUri().toString());
Expand Down
31 changes: 31 additions & 0 deletions src/github/pullRequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,37 @@ export class PullRequestManager implements IPullRequestManager {
});
}

async deleteLocalPullRequest(pullRequest: PullRequestModel): Promise<void> {
const remoteName = await this._repository.getConfig(`branch.${pullRequest.localBranchName}.remote`);
if (!remoteName) {
throw new Error('Unable to find remote for branch');
}

const result = await this._repository.run(['branch', '-D', pullRequest.localBranchName]);
if (result.stderr) {
throw new Error(result.stderr);
}

// If the extension created a remote for the branch, remove it if there are no other branches associated with it
const isPRRemote = await PullRequestGitHelper.isRemoteCreatedForPullRequest(this._repository, remoteName);
if (isPRRemote) {
const configKeyValues = await this._repository.run(['config', '--local', '-l']);
if (configKeyValues.stderr) {
throw new Error(configKeyValues.stderr);
}

const result = configKeyValues.stdout.trim();
const hasOtherAssociatedBranches = new RegExp(`^branch.*\.remote=${remoteName}$`, 'm').test(result);

if (!hasOtherAssociatedBranches) {
const remoteResult = await this._repository.run(['remote', 'remove', remoteName]);
if (remoteResult.stderr) {
throw new Error(remoteResult.stderr);
}
}
}
}

async getPullRequests(type: PRType, options: IPullRequestsPagingOptions = { fetchNextPage: false }): Promise<[IPullRequestModel[], boolean]> {
let githubRepositories = this._githubRepositories;

Expand Down

0 comments on commit a32dacc

Please sign in to comment.