Skip to content

Commit

Permalink
Fixes #709 - remote branch checkout creates local
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Jul 19, 2019
1 parent b3fc052 commit 95fbe81
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

### Fixed

- Fixes [#709](https://github.com/eamodio/vscode-gitlens/issues/709) - Checkout a remote branch as new local branch
- Fixes [#791](https://github.com/eamodio/vscode-gitlens/issues/791) - Notification of unstashed changes in working directory on failed checkout
- Fixes [#792](https://github.com/eamodio/vscode-gitlens/issues/792) - Show last commit message on repositories view instead of Git reference
- Fixes [#795](https://github.com/eamodio/vscode-gitlens/issues/795) - Commit quick access icons replaced with open file actions in File History View
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4326,7 +4326,7 @@
},
{
"command": "gitlens.views.terminalCheckoutBranch",
"when": "!gitlens:readonly && viewItem =~ /gitlens:branch\\b(?!.*?\\b\\+current\\b)//",
"when": "!gitlens:readonly && viewItem =~ /gitlens:branch\\b(?!.*?\\b\\+current\\b)/",
"group": "8_gitlens@1"
},
{
Expand Down
21 changes: 16 additions & 5 deletions src/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,23 @@ export class Git {
return git<string>({ cwd: repoPath, errors: GitErrorHandling.Ignore, local: true }, 'check-mailmap', author);
}

static checkout(repoPath: string, ref: string, fileName?: string) {
const params = ['checkout', ref, '--'];
if (fileName) {
[fileName, repoPath] = Git.splitPath(fileName, repoPath);
static checkout(
repoPath: string,
ref: string,
{ createBranch, fileName }: { createBranch?: string; fileName?: string } = {}
) {
const params = ['checkout'];
if (createBranch) {
params.push('-b', createBranch, '--track', ref, '--');
}
else {
params.push(ref, '--');

params.push(fileName);
if (fileName) {
[fileName, repoPath] = Git.splitPath(fileName, repoPath);

params.push(fileName);
}
}

return git<string>({ cwd: repoPath }, ...params);
Expand Down
8 changes: 5 additions & 3 deletions src/git/gitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,15 +498,17 @@ export class GitService implements Disposable {
}

@log()
async checkout(repoPath: string, ref: string, fileName?: string) {
async checkout(repoPath: string, ref: string, options: { createBranch?: string } | { fileName?: string } = {}) {
const cc = Logger.getCorrelationContext();

try {
return await Git.checkout(repoPath, ref, fileName);
return await Git.checkout(repoPath, ref, options);
}
catch (ex) {
if (/overwritten by checkout/i.test(ex.message)) {
void Messages.showGenericErrorMessage(`Unable to checkout '${ref}'. Please commit or stash your changes before switching branches`);
void Messages.showGenericErrorMessage(
`Unable to checkout '${ref}'. Please commit or stash your changes before switching branches`
);
return undefined;
}

Expand Down
8 changes: 7 additions & 1 deletion src/views/nodes/branchTrackingStatusNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ export class BranchTrackingStatusNode extends ViewNode<ViewWithFiles> implements
}
}

children = [...insertDateMarkers(Iterables.map(commits, c => new CommitNode(this.view, this, c, this.branch)), this, 1)];
children = [
...insertDateMarkers(
Iterables.map(commits, c => new CommitNode(this.view, this, c, this.branch)),
this,
1
)
];
}
else {
children = [
Expand Down
25 changes: 23 additions & 2 deletions src/views/viewCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,32 @@ export class ViewCommands {
}
}

private checkout(node: ViewRefNode | ViewRefFileNode) {
private async checkout(node: ViewRefNode | ViewRefFileNode) {
if (!(node instanceof ViewRefNode)) return undefined;

if (node instanceof ViewRefFileNode) {
return Container.git.checkout(node.repoPath, node.ref, node.fileName);
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 (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: node.branch.getName()
});
if (name === undefined || name.length === 0) return undefined;

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

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

0 comments on commit 95fbe81

Please sign in to comment.