Browse Source

Fixes #709 - remote branch checkout creates local

main
Eric Amodio 5 years ago
parent
commit
95fbe81d28
6 changed files with 53 additions and 12 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +1
    -1
      package.json
  3. +16
    -5
      src/git/git.ts
  4. +5
    -3
      src/git/gitService.ts
  5. +7
    -1
      src/views/nodes/branchTrackingStatusNode.ts
  6. +23
    -2
      src/views/viewCommands.ts

+ 1
- 0
CHANGELOG.md View File

@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
### Fixed ### 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 [#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 [#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 - Fixes [#795](https://github.com/eamodio/vscode-gitlens/issues/795) - Commit quick access icons replaced with open file actions in File History View

+ 1
- 1
package.json View File

@ -4326,7 +4326,7 @@
}, },
{ {
"command": "gitlens.views.terminalCheckoutBranch", "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" "group": "8_gitlens@1"
}, },
{ {

+ 16
- 5
src/git/git.ts View File

@ -449,12 +449,23 @@ export class Git {
return git<string>({ cwd: repoPath, errors: GitErrorHandling.Ignore, local: true }, 'check-mailmap', author); 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); return git<string>({ cwd: repoPath }, ...params);

+ 5
- 3
src/git/gitService.ts View File

@ -498,15 +498,17 @@ export class GitService implements Disposable {
} }
@log() @log()
async checkout(repoPath: string, ref: string, fileName?: string) {
async checkout(repoPath: string, ref: string, options: { createBranch?: string } | { fileName?: string } = {}) {
const cc = Logger.getCorrelationContext(); const cc = Logger.getCorrelationContext();
try { try {
return await Git.checkout(repoPath, ref, fileName);
return await Git.checkout(repoPath, ref, options);
} }
catch (ex) { catch (ex) {
if (/overwritten by checkout/i.test(ex.message)) { 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; return undefined;
} }

+ 7
- 1
src/views/nodes/branchTrackingStatusNode.ts View File

@ -63,7 +63,13 @@ export class BranchTrackingStatusNode extends ViewNode 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 { else {
children = [ children = [

+ 23
- 2
src/views/viewCommands.ts View File

@ -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 ViewRefNode)) return undefined;
if (node instanceof ViewRefFileNode) { 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); return Container.git.checkout(node.repoPath, node.ref);

Loading…
Cancel
Save