Skip to content

Commit 5540e01

Browse files
committed
Merge commit 'refs/pull/61149/head' of github.com:Microsoft/vscode into pr/61149
2 parents 9c15c9e + a84421b commit 5540e01

4 files changed

Lines changed: 39 additions & 5 deletions

File tree

extensions/git/src/api/git.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ export interface Ref {
2626
readonly remote?: string;
2727
}
2828

29+
export interface TrackingShip {
30+
readonly local: string;
31+
readonly upstream: string;
32+
}
33+
2934
export interface UpstreamRef {
3035
readonly remote: string;
3136
readonly name: string;
@@ -235,4 +240,4 @@ export const enum GitErrorCodes {
235240
CantRebaseMultipleBranches = 'CantRebaseMultipleBranches',
236241
PatchDoesNotApply = 'PatchDoesNotApply',
237242
NoPathFound = 'NoPathFound'
238-
}
243+
}

extensions/git/src/commands.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,20 @@ class CheckoutRemoteHeadItem extends CheckoutItem {
5252
}
5353

5454
async run(repository: Repository): Promise<void> {
55-
if (!this.ref.name) {
55+
const ref = this.ref.name;
56+
if (!ref) {
5657
return;
5758
}
5859

59-
await repository.checkoutTracking(this.ref.name);
60+
// Check whether there's a local branch which already has the target branch as an upstream
61+
const trackings = await repository.getTracking(ref);
62+
if (trackings.length > 0) {
63+
//Just checkout the local branch
64+
await repository.checkout(trackings[0].local);
65+
} else {
66+
// Default: checkout a new local branch tracking the upstream
67+
await repository.checkoutTracking(ref);
68+
}
6069
}
6170
}
6271

extensions/git/src/git.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import * as filetype from 'file-type';
1414
import { assign, groupBy, denodeify, IDisposable, toDisposable, dispose, mkdirp, readBytes, detectUnicodeEncoding, Encoding, onceEvent } from './util';
1515
import { CancellationToken, Uri, workspace } from 'vscode';
1616
import { detectEncoding } from './encoding';
17-
import { Ref, RefType, Branch, Remote, GitErrorCodes, LogOptions, Change, Status } from './api/git';
17+
import { Ref, RefType, Branch, Remote, GitErrorCodes, LogOptions, Change, Status, TrackingShip } from './api/git';
1818

1919
const readfile = denodeify<string, string | null, string>(fs.readFile);
2020

@@ -1578,6 +1578,21 @@ export class Repository {
15781578
}
15791579
}
15801580

1581+
async GetTracking(upstreamBranch: string): Promise<TrackingShip[]> {
1582+
const result = await this.run(['for-each-ref', '--format', '%(if)%(upstream:short)%(then)%(refname:short)->%(upstream:short) %(else)* %(end)', 'refs/heads']);
1583+
return result.stdout.trim().split('\n')
1584+
.map(line => line.trim())
1585+
.filter(line => line !== '*')
1586+
.map(line => {
1587+
const splited = line.split('->');
1588+
return {
1589+
local: splited[0],
1590+
upstream: splited[1]
1591+
} as TrackingShip;
1592+
})
1593+
.filter(trackingShip => trackingShip.upstream === upstreamBranch);
1594+
}
1595+
15811596
async getRefs(): Promise<Ref[]> {
15821597
const result = await this.run(['for-each-ref', '--format', '%(refname) %(objectname)', '--sort', '-committerdate']);
15831598

extensions/git/src/repository.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import * as path from 'path';
1313
import * as nls from 'vscode-nls';
1414
import * as fs from 'fs';
1515
import { StatusBarCommands } from './statusbar';
16-
import { Branch, Ref, Remote, RefType, GitErrorCodes, Status, LogOptions, Change } from './api/git';
16+
import { Branch, Ref, Remote, RefType, GitErrorCodes, Status, LogOptions, Change, TrackingShip } from './api/git';
1717

1818
const timeout = (millis: number) => new Promise(c => setTimeout(c, millis));
1919

@@ -299,6 +299,7 @@ export const enum Operation {
299299
GetObjectDetails = 'GetObjectDetails',
300300
SubmoduleUpdate = 'SubmoduleUpdate',
301301
RebaseContinue = 'RebaseContinue',
302+
GetTracking = 'GetTracking',
302303
Apply = 'Apply',
303304
Blame = 'Blame',
304305
Log = 'Log',
@@ -909,6 +910,10 @@ export class Repository implements Disposable {
909910
await this.run(Operation.CheckoutTracking, () => this.repository.checkout(treeish, [], { track: true }));
910911
}
911912

913+
async getTracking(treeish: string): Promise<TrackingShip[]> {
914+
return await this.run(Operation.GetTracking, () => this.repository.GetTracking(treeish));
915+
}
916+
912917
async getCommit(ref: string): Promise<Commit> {
913918
return await this.repository.getCommit(ref);
914919
}

0 commit comments

Comments
 (0)