From 2bba14260fb6f32c78dd7ae86cf1b10794237da6 Mon Sep 17 00:00:00 2001
From: Eric Amodio <eamodio@gmail.com>
Date: Sun, 3 Sep 2017 16:33:52 -0400
Subject: [PATCH] Adds Open Branches in Remote command to the Remote custom
 view items Adds Open Repository in Remote command to the Remote custom view
 items

---
 CHANGELOG.md                         | 12 +++++++++---
 README.md                            | 12 +++++++++---
 package.json                         | 10 ++++++++++
 src/commands/common.ts               |  6 +++++-
 src/commands/openBranchesInRemote.ts | 24 +++++++++++++++++++++---
 src/commands/openRepoInRemote.ts     | 24 +++++++++++++++++++++---
 6 files changed, 75 insertions(+), 13 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index fcc3fd0..8210e8a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,11 +28,17 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
         - Expand each revision (commit) to quickly see the set of files changed, complete with status indicators for adds, changes, renames, and deletes
            - Provides a context menu on each changed file with `Open Changes`, `Open Changes with Working Tree`, `Open File`, `Open Revision`, `Open File in Remote`, `Open Revision in Remote`, `Apply Changes`, and `Show Commit File Details` commands
         - Provides a context menu on each revision (commit) with `Open Commit in Remote`, `Copy Commit ID to Clipboard`, `Copy Commit Message to Clipboard`, `Open Files`, `Open Revisions`, `Show Commit Details`, and `Refresh` commands
+        - Provides a context menu on each branch with `Open Branch in Remote`, and `Refresh` commands
       - Provides a context menu with `Open Branches in Remote`, and `Refresh` commands
-      - Provides a context menu on each branch with `Open Branch in Remote`, and `Refresh` commands
 
-    - `Remotes` node — provides a list of the remote branches
-      - See `Branches` node above for details
+    - `Remotes` node — provides a list of remotes
+      - Expand each remote to see its list of branches
+        - Expand each branch to easily see its revision (commit) history
+          - Expand each revision (commit) to quickly see the set of files changed, complete with status indicators for adds, changes, renames, and deletes
+            - Provides a context menu on each changed file with `Open Changes`, `Open Changes with Working Tree`, `Open File`, `Open Revision`, `Open File in Remote`, `Open Revision in Remote`, `Apply Changes`, and `Show Commit File Details` commands
+          - Provides a context menu on each revision (commit) with `Open Commit in Remote`, `Copy Commit ID to Clipboard`, `Copy Commit Message to Clipboard`, `Open Files`, `Open Revisions`, `Show Commit Details`, and `Refresh` commands
+        - Provides a context menu on each remote with `Open Branches in Remote`, `Open Repository in Remote`, and `Refresh` commands
+      - Provides a context menu with a `Refresh` command
 
     - `Stashes` node — provides a list of stashed changes
       - Expand each stash to quickly see the set of files stashed, complete with status indicators for adds, changes, renames, and deletes
diff --git a/README.md b/README.md
index 3758e86..356a947 100644
--- a/README.md
+++ b/README.md
@@ -133,11 +133,17 @@ GitLens provides an unobtrusive blame annotation at the end of the current line,
         - Expand each revision (commit) to quickly see the set of files changed, complete with status indicators for adds, changes, renames, and deletes
            - Provides a context menu on each changed file with `Open Changes`, `Open Changes with Working Tree`, `Open File`, `Open Revision`, `Open File in Remote`, `Open Revision in Remote`, `Apply Changes`, and `Show Commit File Details` commands
         - Provides a context menu on each revision (commit) with `Open Commit in Remote`, `Copy Commit ID to Clipboard`, `Copy Commit Message to Clipboard`, `Open Files`, `Open Revisions`, `Show Commit Details`, and `Refresh` commands
+        - Provides a context menu on each branch with `Open Branch in Remote`, and `Refresh` commands
       - Provides a context menu with `Open Branches in Remote`, and `Refresh` commands
-      - Provides a context menu on each branch with `Open Branch in Remote`, and `Refresh` commands
 
-    - `Remotes` node — provides a list of the remote branches
-      - See `Branches` node above for details
+    - `Remotes` node — provides a list of remotes
+      - Expand each remote to see its list of branches
+        - Expand each branch to easily see its revision (commit) history
+          - Expand each revision (commit) to quickly see the set of files changed, complete with status indicators for adds, changes, renames, and deletes
+            - Provides a context menu on each changed file with `Open Changes`, `Open Changes with Working Tree`, `Open File`, `Open Revision`, `Open File in Remote`, `Open Revision in Remote`, `Apply Changes`, and `Show Commit File Details` commands
+          - Provides a context menu on each revision (commit) with `Open Commit in Remote`, `Copy Commit ID to Clipboard`, `Copy Commit Message to Clipboard`, `Open Files`, `Open Revisions`, `Show Commit Details`, and `Refresh` commands
+        - Provides a context menu on each remote with `Open Branches in Remote`, `Open Repository in Remote`, and `Refresh` commands
+      - Provides a context menu with a `Refresh` command
 
     - `Stashes` node — provides a list of stashed changes
       - Expand each stash to quickly see the set of files stashed, complete with status indicators for adds, changes, renames, and deletes
diff --git a/package.json b/package.json
index 063d11c..07bf4bb 100644
--- a/package.json
+++ b/package.json
@@ -1577,6 +1577,16 @@
                     "group": "1_gitlens@2"
                 },
                 {
+                    "command": "gitlens.openBranchesInRemote",
+                    "when": "gitlens:enabled && view == gitlens.gitExplorer && viewItem == gitlens:remote",
+                    "group": "1_gitlens@1"
+                },
+                {
+                    "command": "gitlens.openRepoInRemote",
+                    "when": "gitlens:enabled && view == gitlens.gitExplorer && viewItem == gitlens:remote",
+                    "group": "1_gitlens@2"
+                },
+                {
                     "command": "gitlens.stashSave",
                     "when": "gitlens:enabled && view == gitlens.gitExplorer && viewItem == gitlens:stashes",
                     "group": "1_gitlens@1"
diff --git a/src/commands/common.ts b/src/commands/common.ts
index b7129f3..d7a1e69 100644
--- a/src/commands/common.ts
+++ b/src/commands/common.ts
@@ -1,7 +1,7 @@
 'use strict';
 import { commands, Disposable, SourceControlResourceGroup, SourceControlResourceState, TextDocumentShowOptions, TextEditor, TextEditorEdit, Uri, window, workspace } from 'vscode';
 import { ExplorerNode } from '../views/explorerNodes';
-import { GitBranch, GitCommit } from '../gitService';
+import { GitBranch, GitCommit, GitRemote } from '../gitService';
 import { Logger } from '../logger';
 import { Telemetry } from '../telemetry';
 
@@ -140,6 +140,10 @@ export function isCommandViewContextWithCommit<T extends GitCommit>(context: Com
     return context.type === 'view' && (context.node as any).commit && (context.node as any).commit instanceof GitCommit;
 }
 
+export function isCommandViewContextWithRemote(context: CommandContext): context is CommandViewContext & { node: (ExplorerNode & { remote: GitRemote }) } {
+    return context.type === 'view' && (context.node as any).remote && (context.node as any).remote instanceof GitRemote;
+}
+
 export type CommandContext = CommandScmGroupsContext | CommandScmStatesContext | CommandUnknownContext | CommandUriContext | CommandViewContext;
 
 function isScmResourceGroup(group: any): group is SourceControlResourceGroup {
diff --git a/src/commands/openBranchesInRemote.ts b/src/commands/openBranchesInRemote.ts
index 1ebdc1c..ddee149 100644
--- a/src/commands/openBranchesInRemote.ts
+++ b/src/commands/openBranchesInRemote.ts
@@ -1,18 +1,32 @@
 'use strict';
 import { Arrays } from '../system';
 import { commands, TextEditor, Uri, window } from 'vscode';
-import { ActiveEditorCommand, Commands, getCommandUri } from './common';
+import { ActiveEditorCommand, CommandContext, Commands, getCommandUri, isCommandViewContextWithRemote } from './common';
 import { GitService, GitUri } from '../gitService';
 import { Logger } from '../logger';
 import { OpenInRemoteCommandArgs } from './openInRemote';
 
+export interface OpenBranchesInRemoteCommandArgs {
+    remote?: string;
+}
+
 export class OpenBranchesInRemoteCommand extends ActiveEditorCommand {
 
     constructor(private git: GitService) {
         super(Commands.OpenBranchesInRemote);
     }
 
-    async execute(editor?: TextEditor, uri?: Uri) {
+    protected async preExecute(context: CommandContext, args: OpenBranchesInRemoteCommandArgs = {}): Promise<any> {
+        if (isCommandViewContextWithRemote(context)) {
+            args = { ...args };
+            args.remote = context.node.remote.name;
+            return this.execute(context.editor, context.uri, args);
+        }
+
+        return this.execute(context.editor, context.uri, args);
+    }
+
+    async execute(editor?: TextEditor, uri?: Uri, args: OpenBranchesInRemoteCommandArgs = {}) {
         uri = getCommandUri(uri, editor);
 
         const gitUri = uri && await GitUri.fromUri(uri, this.git);
@@ -21,7 +35,11 @@ export class OpenBranchesInRemoteCommand extends ActiveEditorCommand {
         if (!repoPath) return undefined;
 
         try {
-            const remotes = Arrays.uniqueBy(await this.git.getRemotes(repoPath), _ => _.url, _ => !!_.provider);
+            let remotes = Arrays.uniqueBy(await this.git.getRemotes(repoPath), r => r.url, r => !!r.provider);
+            if (args.remote !== undefined) {
+                remotes = remotes.filter(r => r.name === args.remote);
+            }
+
             return commands.executeCommand(Commands.OpenInRemote, uri, {
                 resource: {
                     type: 'branches'
diff --git a/src/commands/openRepoInRemote.ts b/src/commands/openRepoInRemote.ts
index c301c25..82bbe8b 100644
--- a/src/commands/openRepoInRemote.ts
+++ b/src/commands/openRepoInRemote.ts
@@ -1,18 +1,32 @@
 'use strict';
 import { Arrays } from '../system';
 import { commands, TextEditor, Uri, window } from 'vscode';
-import { ActiveEditorCommand, Commands, getCommandUri } from './common';
+import { ActiveEditorCommand, CommandContext, Commands, getCommandUri, isCommandViewContextWithRemote } from './common';
 import { GitService, GitUri } from '../gitService';
 import { Logger } from '../logger';
 import { OpenInRemoteCommandArgs } from './openInRemote';
 
+export interface OpenRepoInRemoteCommandArgs {
+    remote?: string;
+}
+
 export class OpenRepoInRemoteCommand extends ActiveEditorCommand {
 
     constructor(private git: GitService) {
         super(Commands.OpenRepoInRemote);
     }
 
-    async execute(editor?: TextEditor, uri?: Uri) {
+    protected async preExecute(context: CommandContext, args: OpenRepoInRemoteCommandArgs = {}): Promise<any> {
+        if (isCommandViewContextWithRemote(context)) {
+            args = { ...args };
+            args.remote = context.node.remote.name;
+            return this.execute(context.editor, context.uri, args);
+        }
+
+        return this.execute(context.editor, context.uri, args);
+    }
+
+    async execute(editor?: TextEditor, uri?: Uri, args: OpenRepoInRemoteCommandArgs = {}) {
         uri = getCommandUri(uri, editor);
 
         const gitUri = uri && await GitUri.fromUri(uri, this.git);
@@ -21,7 +35,11 @@ export class OpenRepoInRemoteCommand extends ActiveEditorCommand {
         if (!repoPath) return undefined;
 
         try {
-            const remotes = Arrays.uniqueBy(await this.git.getRemotes(repoPath), _ => _.url, _ => !!_.provider);
+            let remotes = Arrays.uniqueBy(await this.git.getRemotes(repoPath), r => r.url, r => !!r.provider);
+            if (args.remote !== undefined) {
+                remotes = remotes.filter(r => r.name === args.remote);
+            }
+
             return commands.executeCommand(Commands.OpenInRemote, uri, {
                 resource: {
                     type: 'repo'