Skip to content

Commit

Permalink
Adds fetch of a specific branch
Browse files Browse the repository at this point in the history
Changes fetch/pull/push on roots to affect the whole repo
  • Loading branch information
eamodio committed Dec 15, 2020
1 parent 8e0e37b commit db0eeef
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 41 deletions.
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6616,8 +6616,13 @@
},
{
"command": "gitlens.views.pull",
"when": "!gitlens:readonly && viewItem =~ /gitlens:branch\\b(?=.*?\\b\\+tracking\\b)/",
"group": "inline@9"
"when": "!gitlens:readonly && viewItem =~ /gitlens:branch\\b(?=.*?\\b\\+behind\\b)/",
"group": "inline@8"
},
{
"command": "gitlens.views.fetch",
"when": "!gitlens:readonly && viewItem =~ /gitlens:branch\\b(?!.*?\\b\\+ahead\\b)(?!.*?\\b\\+behind\\b)/",
"group": "inline@8"
},
{
"command": "gitlens.views.undoCommit",
Expand Down
80 changes: 51 additions & 29 deletions src/commands/git/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
import { GlyphChars } from '../../constants';
import { Container } from '../../container';
import { Repository } from '../../git/git';
import { GitBranchReference, GitReference, Repository } from '../../git/git';
import {
appendReposToTitle,
PartialStepState,
Expand All @@ -26,6 +26,7 @@ type Flags = '--all' | '--prune';

interface State {
repos: string | string[] | Repository | Repository[];
reference?: GitBranchReference;
flags: Flags[];
}

Expand Down Expand Up @@ -54,6 +55,10 @@ export class FetchGitCommand extends QuickCommand<State> {
}

execute(state: FetchStepState) {
if (GitReference.isBranch(state.reference)) {
return state.repos[0].fetch({ branch: state.reference });
}

return Container.git.fetchAll(state.repos, {
all: state.flags.includes('--all'),
prune: state.flags.includes('--prune'),
Expand Down Expand Up @@ -136,34 +141,51 @@ export class FetchGitCommand extends QuickCommand<State> {
}
}

const reposToFetch =
state.repos.length === 1 ? `$(repo) ${state.repos[0].formattedName}` : `${state.repos.length} repositories`;

const step: QuickPickStep<FlagsQuickPickItem<Flags>> = QuickCommand.createConfirmStep(
appendReposToTitle(`Confirm ${this.title}`, state, context, lastFetchedOn),
[
FlagsQuickPickItem.create<Flags>(state.flags, [], {
label: this.title,
detail: `Will fetch ${reposToFetch}`,
}),
FlagsQuickPickItem.create<Flags>(state.flags, ['--prune'], {
label: `${this.title} & Prune`,
description: '--prune',
detail: `Will fetch and prune ${reposToFetch}`,
}),
FlagsQuickPickItem.create<Flags>(state.flags, ['--all'], {
label: `${this.title} All`,
description: '--all',
detail: `Will fetch all remotes of ${reposToFetch}`,
}),
FlagsQuickPickItem.create<Flags>(state.flags, ['--all', '--prune'], {
label: `${this.title} All & Prune`,
description: '--all --prune',
detail: `Will fetch and prune all remotes of ${reposToFetch}`,
}),
],
context,
);
let step: QuickPickStep<FlagsQuickPickItem<Flags>>;

if (state.repos.length === 1 && GitReference.isBranch(state.reference)) {
step = this.createConfirmStep(
appendReposToTitle(`Confirm ${context.title}`, state, context, lastFetchedOn),
[
FlagsQuickPickItem.create<Flags>(state.flags, [], {
label: this.title,
detail: `Will fetch ${GitReference.toString(state.reference)}`,
}),
],
);
} else {
const reposToFetch =
state.repos.length === 1
? `$(repo) ${state.repos[0].formattedName}`
: `${state.repos.length} repositories`;

step = QuickCommand.createConfirmStep(
appendReposToTitle(`Confirm ${this.title}`, state, context, lastFetchedOn),
[
FlagsQuickPickItem.create<Flags>(state.flags, [], {
label: this.title,
detail: `Will fetch ${reposToFetch}`,
}),
FlagsQuickPickItem.create<Flags>(state.flags, ['--prune'], {
label: `${this.title} & Prune`,
description: '--prune',
detail: `Will fetch and prune ${reposToFetch}`,
}),
FlagsQuickPickItem.create<Flags>(state.flags, ['--all'], {
label: `${this.title} All`,
description: '--all',
detail: `Will fetch all remotes of ${reposToFetch}`,
}),
FlagsQuickPickItem.create<Flags>(state.flags, ['--all', '--prune'], {
label: `${this.title} All & Prune`,
description: '--all --prune',
detail: `Will fetch and prune all remotes of ${reposToFetch}`,
}),
],
context,
);
}

const selection: StepSelection<typeof step> = yield step;
return QuickCommand.canPickStepContinue(step, state, selection) ? selection[0].item : StepResult.Break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/gitCommands.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export namespace GitActions {
});
}

export function fetch(repos?: string | string[] | Repository | Repository[]) {
return executeGitCommand({ command: 'fetch', state: { repos: repos } });
export function fetch(repos?: string | string[] | Repository | Repository[], ref?: GitBranchReference) {
return executeGitCommand({ command: 'fetch', state: { repos: repos, reference: ref } });
}

export function merge(repo?: string | Repository, ref?: GitReference) {
Expand Down
4 changes: 2 additions & 2 deletions src/views/nodes/branchNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export class BranchNode
view: BranchesView | CommitsView | RemotesView | RepositoriesView,
parent: ViewNode,
public readonly branch: GitBranch,
// Specifies that the node is shown as a root under the repository node
private readonly root: boolean,
// Specifies that the node is shown as a root and not nested under the branches node
public readonly root: boolean,

options?: {
expanded?: boolean;
Expand Down
4 changes: 2 additions & 2 deletions src/views/nodes/branchTrackingStatusNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export class BranchTrackingStatusNode extends ViewNode<ViewsWithFiles> implement
public readonly branch: GitBranch,
public readonly status: BranchTrackingStatus,
public readonly upstreamType: 'ahead' | 'behind' | 'same' | 'none',
// Specifies that the node is shown as a root under the repository node
private readonly root: boolean = false,
// Specifies that the node is shown as a root and not nested under the branches node
public readonly root: boolean = false,
) {
super(GitUri.fromRepoPath(status.repoPath), view, parent);
}
Expand Down
10 changes: 6 additions & 4 deletions src/views/viewCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,12 @@ export class ViewCommands {
}

@debug()
private fetch(node: RemoteNode | RepositoryNode | BranchTrackingStatusNode) {
private fetch(node: RemoteNode | RepositoryNode | BranchNode | BranchTrackingStatusNode) {
if (node instanceof RepositoryNode) return GitActions.fetch(node.repo);
if (node instanceof RemoteNode) return GitActions.Remote.fetch(node.remote.repoPath, node.remote.name);
if (node instanceof BranchTrackingStatusNode) return GitActions.fetch(node.repoPath);
if (node instanceof BranchNode || node instanceof BranchTrackingStatusNode) {
return GitActions.fetch(node.repoPath, node.root ? undefined : node.branch);
}

return Promise.resolve();
}
Expand Down Expand Up @@ -424,7 +426,7 @@ export class ViewCommands {
private pull(node: RepositoryNode | BranchNode | BranchTrackingStatusNode) {
if (node instanceof RepositoryNode) return GitActions.pull(node.repo);
if (node instanceof BranchNode || node instanceof BranchTrackingStatusNode) {
return GitActions.pull(node.repoPath, node.branch);
return GitActions.pull(node.repoPath, node.root ? undefined : node.branch);
}

return Promise.resolve();
Expand All @@ -434,7 +436,7 @@ export class ViewCommands {
private push(node: RepositoryNode | BranchNode | BranchTrackingStatusNode, force?: boolean) {
if (node instanceof RepositoryNode) return GitActions.push(node.repo, force);
if (node instanceof BranchNode || node instanceof BranchTrackingStatusNode) {
return GitActions.push(node.repoPath, undefined, node.branch);
return GitActions.push(node.repoPath, undefined, node.root ? undefined : node.branch);
}

return Promise.resolve();
Expand Down

0 comments on commit db0eeef

Please sign in to comment.