From 6bf1b8bbec4fc47eb42d26ee4940bfb51754e69f Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Mon, 23 Jul 2018 22:11:32 +0300 Subject: [PATCH] Try to checkout a new local branch when checking out a remote branch This handles the case when no local branch exists. You will get an error if a local branch with the same name already exists. Better behavior for this case can be implemented later. Fixes #54601 --- extensions/git/src/commands.ts | 8 +++----- extensions/git/src/git.ts | 6 +++++- extensions/git/src/repository.ts | 5 +++++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index 19c3cbdbe59..59a4f58732e 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -23,14 +23,13 @@ const localize = nls.loadMessageBundle(); class CheckoutItem implements QuickPickItem { protected get shortCommit(): string { return (this.ref.commit || '').substr(0, 8); } - protected get treeish(): string | undefined { return this.ref.name; } get label(): string { return this.ref.name || this.shortCommit; } get description(): string { return this.shortCommit; } constructor(protected ref: Ref) { } async run(repository: Repository): Promise { - const ref = this.treeish; + const ref = this.ref.name; if (!ref) { return; @@ -53,13 +52,12 @@ class CheckoutRemoteHeadItem extends CheckoutItem { return localize('remote branch at', "Remote branch at {0}", this.shortCommit); } - protected get treeish(): string | undefined { + async run(repository: Repository): Promise { if (!this.ref.name) { return; } - const match = /^[^/]+\/(.*)$/.exec(this.ref.name); - return match ? match[1] : this.ref.name; + await repository.checkoutTracking(this.ref.name); } } diff --git a/extensions/git/src/git.ts b/extensions/git/src/git.ts index a360264a394..b9e841866a2 100644 --- a/extensions/git/src/git.ts +++ b/extensions/git/src/git.ts @@ -899,9 +899,13 @@ export class Repository { await this.run(['update-index', '--cacheinfo', mode, hash, path]); } - async checkout(treeish: string, paths: string[]): Promise { + async checkout(treeish: string, paths: string[], opts: { track?: boolean } = Object.create(null)): Promise { const args = ['checkout', '-q']; + if (opts.track) { + args.push('--track'); + } + if (treeish) { args.push(treeish); } diff --git a/extensions/git/src/repository.ts b/extensions/git/src/repository.ts index b3af0698312..042b3c8cb63 100644 --- a/extensions/git/src/repository.ts +++ b/extensions/git/src/repository.ts @@ -283,6 +283,7 @@ export enum Operation { Clean = 'Clean', Branch = 'Branch', Checkout = 'Checkout', + CheckoutTracking = 'CheckoutTracking', Reset = 'Reset', Fetch = 'Fetch', Pull = 'Pull', @@ -769,6 +770,10 @@ export class Repository implements Disposable { await this.run(Operation.Checkout, () => this.repository.checkout(treeish, [])); } + async checkoutTracking(treeish: string): Promise { + await this.run(Operation.CheckoutTracking, () => this.repository.checkout(treeish, [], { track: true })); + } + async getCommit(ref: string): Promise { return await this.repository.getCommit(ref); }