Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit d1b0231

Browse files
committed
Ask user before cloning or opening a repository
1 parent c4cac4d commit d1b0231

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

src/GitHub.VisualStudio/Commands/OpenFromUrlCommand.cs

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
using GitHub.Services.Vssdk.Commands;
1212
using EnvDTE;
1313
using Microsoft.VisualStudio;
14+
using Microsoft.VisualStudio.Shell;
15+
using Microsoft.VisualStudio.Shell.Interop;
16+
using Task = System.Threading.Tasks.Task;
17+
using SVsServiceProvider = Microsoft.VisualStudio.Shell.SVsServiceProvider;
1418

1519
namespace GitHub.VisualStudio.Commands
1620
{
@@ -22,6 +26,7 @@ public class OpenFromUrlCommand : VsCommand<string>, IOpenFromUrlCommand
2226
readonly Lazy<IPullRequestEditorService> pullRequestEditorService;
2327
readonly Lazy<IGitHubToolWindowManager> gitHubToolWindowManager;
2428
readonly Lazy<DTE> dte;
29+
readonly IServiceProvider serviceProvider;
2530

2631
/// <summary>
2732
/// Gets the GUID of the group the command belongs to.
@@ -38,15 +43,16 @@ public OpenFromUrlCommand(
3843
Lazy<GitHubContextService> gitHubContextService,
3944
Lazy<IRepositoryCloneService> repositoryCloneService,
4045
Lazy<IPullRequestEditorService> pullRequestEditorService,
41-
[Import(typeof(Microsoft.VisualStudio.Shell.SVsServiceProvider))] IServiceProvider sp) :
46+
[Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider) :
4247
base(CommandSet, CommandId)
4348
{
4449
this.gitHubContextService = gitHubContextService;
4550
this.repositoryCloneService = repositoryCloneService;
4651
this.pullRequestEditorService = pullRequestEditorService;
47-
dte = new Lazy<DTE>(() => (DTE)sp.GetService(typeof(DTE)));
52+
this.serviceProvider = serviceProvider;
53+
dte = new Lazy<DTE>(() => (DTE)serviceProvider.GetService(typeof(DTE)));
4854
gitHubToolWindowManager = new Lazy<IGitHubToolWindowManager>(
49-
() => (IGitHubToolWindowManager)sp.GetService(typeof(IGitHubToolWindowManager)));
55+
() => (IGitHubToolWindowManager)serviceProvider.GetService(typeof(IGitHubToolWindowManager)));
5056

5157
// See https://code.msdn.microsoft.com/windowsdesktop/AllowParams-2005-9442298f
5258
ParametersDescription = "u"; // accept a single url
@@ -91,21 +97,58 @@ public override async Task Execute(string url)
9197

9298
if (!Directory.Exists(repositoryDir))
9399
{
94-
await repositoryCloneService.Value.CloneRepository(cloneUrl, repositoryDirName, targetDir);
100+
var result = ShowInfoMessage($"Clone '{cloneUrl}' to '{repositoryDir}'?");
101+
switch (result)
102+
{
103+
case VSConstants.MessageBoxResult.IDYES:
104+
await repositoryCloneService.Value.CloneRepository(cloneUrl, repositoryDirName, targetDir);
105+
// Open the cloned repository
106+
dte.Value.ExecuteCommand("File.OpenFolder", repositoryDir);
107+
dte.Value.ExecuteCommand("View.TfsTeamExplorer");
108+
break;
109+
case VSConstants.MessageBoxResult.IDNO:
110+
// Target the current solution
111+
repositoryDir = FindSolutionDirectory(dte.Value.Solution);
112+
if (repositoryDir == null)
113+
{
114+
// No current solution to use
115+
return;
116+
}
117+
118+
break;
119+
case VSConstants.MessageBoxResult.IDCANCEL:
120+
return;
121+
}
95122
}
96123

97124
var solutionDir = FindSolutionDirectory(dte.Value.Solution);
98125
if (solutionDir == null || !ContainsDirectory(repositoryDir, solutionDir))
99126
{
100-
// Open if current solution isn't in repository directory
101-
dte.Value.ExecuteCommand("File.OpenFolder", repositoryDir);
102-
dte.Value.ExecuteCommand("View.TfsTeamExplorer");
127+
var result = ShowInfoMessage($"Open repository fiolder at '{repositoryDir}'?");
128+
switch (result)
129+
{
130+
case VSConstants.MessageBoxResult.IDYES:
131+
// Open if current solution isn't in repository directory
132+
dte.Value.ExecuteCommand("File.OpenFolder", repositoryDir);
133+
dte.Value.ExecuteCommand("View.TfsTeamExplorer");
134+
break;
135+
case VSConstants.MessageBoxResult.IDNO:
136+
break;
137+
case VSConstants.MessageBoxResult.IDCANCEL:
138+
return;
139+
}
103140
}
104141

105142
await TryOpenPullRequest(gitHubUrl);
106143
TryOpenFile(gitHubUrl, context, repositoryDir);
107144
}
108145

146+
VSConstants.MessageBoxResult ShowInfoMessage(string message)
147+
{
148+
return (VSConstants.MessageBoxResult)VsShellUtilities.ShowMessageBox(serviceProvider, message, null,
149+
OLEMSGICON.OLEMSGICON_QUERY, OLEMSGBUTTON.OLEMSGBUTTON_YESNOCANCEL, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
150+
}
151+
109152
static bool ContainsDirectory(string repositoryDir, string solutionDir)
110153
{
111154
if (solutionDir.Equals(repositoryDir, StringComparison.OrdinalIgnoreCase))

src/GitHub.VisualStudio/GitHub.VisualStudio.vsct

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
<CommandFlag>IconIsMoniker</CommandFlag>
118118
<CommandFlag>AllowParams</CommandFlag>
119119
<Strings>
120-
<ButtonText>Open from GitHub</ButtonText>
120+
<ButtonText>Open from GitHub...</ButtonText>
121121
<CanonicalName>.GitHub.OpenFromUrl</CanonicalName>
122122
<LocCanonicalName>.GitHub.OpenFromUrl</LocCanonicalName>
123123
</Strings>

0 commit comments

Comments
 (0)