From c6a0593cb011c0cf84402a580cd1e6883f86608b Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Wed, 6 Jan 2021 03:12:52 -0500 Subject: [PATCH] Fixes #1309 - fix fetch & pull of remote branches --- CHANGELOG.md | 4 ++++ package.json | 2 +- src/commands/git/pull.ts | 2 +- src/git/git.ts | 52 ++++++++++++++++++++++++++------------------ src/git/gitService.ts | 25 ++++++++++++++++----- src/git/models/repository.ts | 5 +++-- 6 files changed, 59 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 738c25b..0f501c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p - Renames _Browse from Here_ to _Repository from Here_ on the _Browse_ submenu of commits in the views - Renames _Browse from Here in New Window_ to _Repository from Here in New Window_ on the _Browse_ submenu of commits in the views +### Fixed + +- Fixes [#1309](https://github.com/eamodio/vscode-gitlens/issues/1309) - "Fetch" not working on remote branches + ## [11.1.3] - 2021-01-05 ### Fixed diff --git a/package.json b/package.json index 4c1df24..145d27e 100644 --- a/package.json +++ b/package.json @@ -7046,7 +7046,7 @@ }, { "command": "gitlens.views.fetch", - "when": "gitlens:hasRemotes && !gitlens:readonly && viewItem =~ /gitlens:branch\\b(?=.*?\\b\\+tracking\\b)(?!.*?\\b\\+ahead\\b)(?!.*?\\b\\+behind\\b)/", + "when": "gitlens:hasRemotes && !gitlens:readonly && viewItem =~ /gitlens:branch\\b(?=.*?\\b\\+(remote|tracking)\\b)(?!.*?\\b\\+ahead\\b)(?!.*?\\b\\+behind\\b)/", "group": "inline@8" }, { diff --git a/src/commands/git/pull.ts b/src/commands/git/pull.ts index 064ad7f..41b8660 100644 --- a/src/commands/git/pull.ts +++ b/src/commands/git/pull.ts @@ -63,7 +63,7 @@ export class PullGitCommand extends QuickCommand { if (!GitBranch.is(state.reference) || !state.reference.current) { const currentBranch = await state.repos[0].getBranch(); if (currentBranch?.name !== state.reference.name) { - return state.repos[0].fetch({ branch: state.reference }); + return state.repos[0].fetch({ branch: state.reference, pull: true }); } } } diff --git a/src/git/git.ts b/src/git/git.ts index 892560a..b55f44e 100644 --- a/src/git/git.ts +++ b/src/git/git.ts @@ -671,34 +671,44 @@ export namespace Git { repoPath: string, options: | { all?: boolean; branch?: undefined; prune?: boolean; remote?: string } - | { all?: undefined; branch: string; prune?: undefined; remote: string; upstream: string } = {}, + | { + all?: undefined; + branch: string; + prune?: undefined; + pull?: boolean; + remote: string; + upstream: string; + } = {}, ): Promise { const params = ['fetch']; - if (options.branch) { - params.push('-u', options.remote, `${options.upstream}:${options.branch}`); - - try { - void (await git({ cwd: repoPath }, ...params)); - return; - } catch (ex) { - const msg: string = ex?.toString() ?? ''; - if (GitErrors.noFastForward.test(msg)) { - void window.showErrorMessage( - `Unable to pull the '${options.branch}' branch, as it can't be fast-forwarded.`, - ); - - return; - } - - throw ex; - } - } if (options.prune) { params.push('--prune'); } - if (options.remote) { + if (options.branch && options.remote) { + if (options.upstream && options.pull) { + params.push('-u', options.remote, `${options.upstream}:${options.branch}`); + + try { + void (await git({ cwd: repoPath }, ...params)); + return; + } catch (ex) { + const msg: string = ex?.toString() ?? ''; + if (GitErrors.noFastForward.test(msg)) { + void window.showErrorMessage( + `Unable to pull the '${options.branch}' branch, as it can't be fast-forwarded.`, + ); + + return; + } + + throw ex; + } + } else { + params.push(options.remote, options.branch); + } + } else if (options.remote) { params.push(options.remote); } else if (options.all) { params.push('--all'); diff --git a/src/git/gitService.ts b/src/git/gitService.ts index e81511e..3a3bc13 100644 --- a/src/git/gitService.ts +++ b/src/git/gitService.ts @@ -627,18 +627,19 @@ export class GitService implements Disposable { @log() async fetch( repoPath: string, - options: { all?: boolean; branch?: GitBranchReference; prune?: boolean; remote?: string } = {}, + options: { all?: boolean; branch?: GitBranchReference; prune?: boolean; pull?: boolean; remote?: string } = {}, ): Promise { const { branch: branchRef, ...opts } = options; if (GitReference.isBranch(branchRef)) { const repo = await this.getRepository(repoPath); const branch = await repo?.getBranch(branchRef?.name); - if (branch?.tracking == null) return undefined; + if (!branch?.remote && branch?.tracking == null) return undefined; return Git.fetch(repoPath, { - branch: branch.name, + branch: branch.getNameWithoutRemote(), remote: branch.getRemoteName()!, upstream: branch.getTrackingWithoutRemote()!, + pull: options.pull, }); } @@ -1189,7 +1190,11 @@ export class GitService implements Disposable { return undefined; } - @log() + @log({ + args: { + 1: () => false, + }, + }) async getBranches( repoPath: string | undefined, options: { @@ -1247,7 +1252,11 @@ export class GitService implements Disposable { return branches; } - @log() + @log({ + args: { + 1: () => false, + }, + }) async getBranchesAndOrTags( repoPath: string | undefined, { @@ -3302,7 +3311,11 @@ export class GitService implements Disposable { return status; } - @log() + @log({ + args: { + 1: () => false, + }, + }) async getTags( repoPath: string | undefined, options: { filter?: (t: GitTag) => boolean; sort?: boolean | { orderBy?: TagSorting } } = {}, diff --git a/src/git/models/repository.ts b/src/git/models/repository.ts index 8daa1fd..9e42963 100644 --- a/src/git/models/repository.ts +++ b/src/git/models/repository.ts @@ -410,6 +410,7 @@ export class Repository implements Disposable { branch?: GitBranchReference; progress?: boolean; prune?: boolean; + pull?: boolean; remote?: string; } = {}, ) { @@ -421,7 +422,7 @@ export class Repository implements Disposable { location: ProgressLocation.Notification, title: opts.branch != null - ? `Pulling ${opts.branch.name}...` + ? `${opts.pull ? 'Pulling' : 'Fetching'} ${opts.branch.name}...` : `Fetching ${opts.remote ? `${opts.remote} of ` : ''}${this.formattedName}...`, }, () => this.fetchCore(opts), @@ -429,7 +430,7 @@ export class Repository implements Disposable { } private async fetchCore( - options: { all?: boolean; branch?: GitBranchReference; prune?: boolean; remote?: string } = {}, + options: { all?: boolean; branch?: GitBranchReference; prune?: boolean; pull?: boolean; remote?: string } = {}, ) { try { void (await Container.git.fetch(this.path, options));