Skip to content

Commit

Permalink
Fixes #1309 - fix fetch & pull of remote branches
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Jan 6, 2021
1 parent 0d0ab90 commit c6a0593
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/commands/git/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class PullGitCommand extends QuickCommand<State> {
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 });
}
}
}
Expand Down
52 changes: 31 additions & 21 deletions src/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const params = ['fetch'];
if (options.branch) {
params.push('-u', options.remote, `${options.upstream}:${options.branch}`);

try {
void (await git<string>({ 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<string>({ 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');
Expand Down
25 changes: 19 additions & 6 deletions src/git/gitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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,
});
}

Expand Down Expand Up @@ -1189,7 +1190,11 @@ export class GitService implements Disposable {
return undefined;
}

@log()
@log({
args: {
1: () => false,
},
})
async getBranches(
repoPath: string | undefined,
options: {
Expand Down Expand Up @@ -1247,7 +1252,11 @@ export class GitService implements Disposable {
return branches;
}

@log()
@log({
args: {
1: () => false,
},
})
async getBranchesAndOrTags(
repoPath: string | undefined,
{
Expand Down Expand Up @@ -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 } } = {},
Expand Down
5 changes: 3 additions & 2 deletions src/git/models/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ export class Repository implements Disposable {
branch?: GitBranchReference;
progress?: boolean;
prune?: boolean;
pull?: boolean;
remote?: string;
} = {},
) {
Expand All @@ -421,15 +422,15 @@ 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),
));
}

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));
Expand Down

0 comments on commit c6a0593

Please sign in to comment.