Skip to content

Commit

Permalink
Merge pull request #54897 from segevfiner/checkout-new-remote-branch
Browse files Browse the repository at this point in the history
Try to checkout a new local branch when checking out a remote branch
  • Loading branch information
joaomoreno committed Sep 12, 2018
2 parents 827a101 + 6bf1b8b commit 15344fc
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,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 @@ -54,13 +53,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
Original file line number Diff line number Diff line change
Expand Up @@ -913,9 +913,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
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ export const enum Operation {
SetBranchUpstream = 'SetBranchUpstream',
HashObject = 'HashObject',
Checkout = 'Checkout',
CheckoutTracking = 'CheckoutTracking',
Reset = 'Reset',
Remote = 'Remote',
Fetch = 'Fetch',
Expand Down Expand Up @@ -868,6 +869,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 15344fc

Please sign in to comment.