Skip to content

Commit

Permalink
Try to checkout a new local branch when checking out a remote branch
Browse files Browse the repository at this point in the history
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
  • Loading branch information
segevfiner committed Jul 23, 2018
1 parent b8c522b commit 6bf1b8b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
8 changes: 3 additions & 5 deletions extensions/git/src/commands.ts
Expand Up @@ -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<void> {
const ref = this.treeish;
const ref = this.ref.name;

if (!ref) {
return;
Expand All @@ -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<void> {
if (!this.ref.name) {
return;
}

const match = /^[^/]+\/(.*)$/.exec(this.ref.name);
return match ? match[1] : this.ref.name;
await repository.checkoutTracking(this.ref.name);
}
}

Expand Down
6 changes: 5 additions & 1 deletion extensions/git/src/git.ts
Expand Up @@ -899,9 +899,13 @@ export class Repository {
await this.run(['update-index', '--cacheinfo', mode, hash, path]);
}

async checkout(treeish: string, paths: string[]): Promise<void> {
async checkout(treeish: string, paths: string[], opts: { track?: boolean } = Object.create(null)): Promise<void> {
const args = ['checkout', '-q'];

if (opts.track) {
args.push('--track');
}

if (treeish) {
args.push(treeish);
}
Expand Down
5 changes: 5 additions & 0 deletions extensions/git/src/repository.ts
Expand Up @@ -283,6 +283,7 @@ export enum Operation {
Clean = 'Clean',
Branch = 'Branch',
Checkout = 'Checkout',
CheckoutTracking = 'CheckoutTracking',
Reset = 'Reset',
Fetch = 'Fetch',
Pull = 'Pull',
Expand Down Expand Up @@ -769,6 +770,10 @@ export class Repository implements Disposable {
await this.run(Operation.Checkout, () => this.repository.checkout(treeish, []));
}

async checkoutTracking(treeish: string): Promise<void> {
await this.run(Operation.CheckoutTracking, () => this.repository.checkout(treeish, [], { track: true }));
}

async getCommit(ref: string): Promise<Commit> {
return await this.repository.getCommit(ref);
}
Expand Down

0 comments on commit 6bf1b8b

Please sign in to comment.