Skip to content

Commit

Permalink
Adds checkout to current branch w/ picker
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Jul 25, 2019
1 parent c4f37eb commit 345be49
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4254,7 +4254,7 @@
},
{
"command": "gitlens.views.checkout",
"when": "!gitlens:readonly && viewItem =~ /gitlens:branch\\b(?!.*?\\b\\+current\\b)/",
"when": "!gitlens:readonly && viewItem =~ /gitlens:branch/",
"group": "inline@10"
},
{
Expand Down
12 changes: 12 additions & 0 deletions src/quickpicks/referencesQuickPick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ export interface ReferencesQuickPickOptions {
export class ReferencesQuickPick {
constructor(public readonly repoPath: string | undefined) {}

async show(
placeHolder: string,
options?: Exclude<ReferencesQuickPickOptions, CommandQuickPickItem> & { include: 'branches' }
): Promise<BranchQuickPickItem | undefined>;
async show(
placeHolder: string,
options?: Exclude<ReferencesQuickPickOptions, CommandQuickPickItem> & { include: 'tags' }
): Promise<TagQuickPickItem | undefined>;
async show(
placeHolder: string,
options?: Exclude<ReferencesQuickPickOptions, CommandQuickPickItem>
): Promise<ReferencesQuickPickItem | undefined>;
async show(
placeHolder: string,
options: ReferencesQuickPickOptions = { checkmarks: true }
Expand Down
43 changes: 28 additions & 15 deletions src/views/viewCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
} from './nodes';
import { Strings } from '../system/string';
import { runGitCommandInTerminal } from '../terminal';
import { ReferencesQuickPick } from '../quickpicks';

interface CompareSelectedInfo {
ref: string;
Expand Down Expand Up @@ -243,25 +244,37 @@ export class ViewCommands {
return Container.git.checkout(node.repoPath, node.ref, { fileName: node.fileName });
}

if (node instanceof BranchNode && node.branch.remote) {
const branches = await Container.git.getBranches(node.repoPath, {
filter: b => {
return b.tracking === node.branch.name;
}
});
if (node instanceof BranchNode) {
let branch = node.branch;
if (branch.current) {
const pick = await new ReferencesQuickPick(node.repoPath).show('Choose a branch to check out to', { checkmarks: false, filterBranches: b => !b.current, include: 'branches' });
if (pick === undefined) return undefined;

if (branches.length !== 0) {
return Container.git.checkout(node.repoPath, branches[0].ref);
branch = pick.item;
}

const name = await window.showInputBox({
prompt: "Please provide a name for the local branch (Press 'Enter' to confirm or 'Escape' to cancel)",
placeHolder: 'Local branch name',
value: node.branch.getName()
});
if (name === undefined || name.length === 0) return undefined;
if (branch.remote) {
const branches = await Container.git.getBranches(node.repoPath, {
filter: b => {
return b.tracking === branch.name;
}
});

if (branches.length !== 0) {
return Container.git.checkout(node.repoPath, branches[0].ref);
}

const name = await window.showInputBox({
prompt: "Please provide a name for the local branch (Press 'Enter' to confirm or 'Escape' to cancel)",
placeHolder: 'Local branch name',
value: branch.getName()
});
if (name === undefined || name.length === 0) return undefined;

return Container.git.checkout(node.repoPath, branch.ref, { createBranch: name });
}

return Container.git.checkout(node.repoPath, node.ref, { createBranch: name });
return Container.git.checkout(branch.repoPath, branch.ref);
}

return Container.git.checkout(node.repoPath, node.ref);
Expand Down

0 comments on commit 345be49

Please sign in to comment.