Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions src/GitHub.App/ViewModels/GitHubPane/PullRequestDetailViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;
using System.Threading.Tasks;
using System.Globalization;
using GitHub.App;
using GitHub.Commands;
using GitHub.Extensions;
using GitHub.Factories;
using GitHub.Helpers;
Expand Down Expand Up @@ -36,7 +36,7 @@ public sealed class PullRequestDetailViewModel : PanePageViewModelBase, IPullReq
readonly IPullRequestSessionManager sessionManager;
readonly IUsageTracker usageTracker;
readonly ITeamExplorerContext teamExplorerContext;
readonly IStatusBarNotificationService statusBarNotificationService;
readonly ISyncSubmodulesCommand syncSubmodulesCommand;
IModelService modelService;
IPullRequestModel model;
string sourceBranchDisplayName;
Expand Down Expand Up @@ -71,22 +71,22 @@ public PullRequestDetailViewModel(
IModelServiceFactory modelServiceFactory,
IUsageTracker usageTracker,
ITeamExplorerContext teamExplorerContext,
IStatusBarNotificationService statusBarNotificationService,
IPullRequestFilesViewModel files)
IPullRequestFilesViewModel files,
ISyncSubmodulesCommand syncSubmodulesCommand)
{
Guard.ArgumentNotNull(pullRequestsService, nameof(pullRequestsService));
Guard.ArgumentNotNull(sessionManager, nameof(sessionManager));
Guard.ArgumentNotNull(modelServiceFactory, nameof(modelServiceFactory));
Guard.ArgumentNotNull(usageTracker, nameof(usageTracker));
Guard.ArgumentNotNull(teamExplorerContext, nameof(teamExplorerContext));
Guard.ArgumentNotNull(statusBarNotificationService, nameof(statusBarNotificationService));
Guard.ArgumentNotNull(syncSubmodulesCommand, nameof(syncSubmodulesCommand));

this.pullRequestsService = pullRequestsService;
this.sessionManager = sessionManager;
this.modelServiceFactory = modelServiceFactory;
this.usageTracker = usageTracker;
this.teamExplorerContext = teamExplorerContext;
this.statusBarNotificationService = statusBarNotificationService;
this.syncSubmodulesCommand = syncSubmodulesCommand;
Files = files;

Checkout = ReactiveCommand.CreateAsyncObservable(
Expand Down Expand Up @@ -606,21 +606,17 @@ async Task DoSyncSubmodules(object unused)
IsBusy = true;
usageTracker.IncrementCounter(x => x.NumberOfSyncSubmodules).Forget();

var writer = new StringWriter(CultureInfo.CurrentCulture);
var complete = await pullRequestsService.SyncSubmodules(LocalRepository, line =>
{
writer.WriteLine(line);
statusBarNotificationService.ShowMessage(line);
});
var result = await syncSubmodulesCommand.SyncSubmodules();
var complete = result.Item1;
var summary = result.Item2;
if (!complete)
{
throw new ApplicationException(writer.ToString());
throw new ApplicationException(summary);
}
}
finally
{
IsBusy = false;
statusBarNotificationService.ShowMessage(string.Empty);
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/GitHub.Exports/Commands/ISyncSubmodulesCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Threading.Tasks;

namespace GitHub.Commands
{
/// <summary>
/// Sync submodules in local repository.
/// </summary>
public interface ISyncSubmodulesCommand : IVsCommand
{
/// <summary>
/// Sync submodules in local repository.
/// </summary>
/// <returns>Tuple with bool that is true if command completed successfully and string with
/// output from sync submodules Git command.</returns>
Task<Tuple<bool, string>> SyncSubmodules();
}
}
1 change: 1 addition & 0 deletions src/GitHub.Exports/GitHub.Exports.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
<Compile Include="Commands\IOpenLinkCommand.cs" />
<Compile Include="Commands\ICreateGistCommand.cs" />
<Compile Include="Commands\IShowCurrentPullRequestCommand.cs" />
<Compile Include="Commands\ISyncSubmodulesCommand.cs" />
<Compile Include="Commands\IShowGitHubPaneCommand.cs" />
<Compile Include="Commands\IOpenPullRequestsCommand.cs" />
<Compile Include="Commands\IAddConnectionCommand.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/GitHub.Exports/Settings/PkgCmdID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static class PkgCmdIDList
public const int showGitHubPaneCommand = 0x200;
public const int openPullRequestsCommand = 0x201;
public const int showCurrentPullRequestCommand = 0x202;
public const int syncSubmodulesCommand = 0x203;
public const int backCommand = 0x300;
public const int forwardCommand = 0x301;
public const int refreshCommand = 0x302;
Expand Down
100 changes: 100 additions & 0 deletions src/GitHub.VisualStudio/Commands/SyncSubmodulesCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Threading.Tasks;
using System.ComponentModel.Composition;
using GitHub.Logging;
using GitHub.Commands;
using GitHub.Services;
using GitHub.Services.Vssdk.Commands;
using Serilog;
using System.IO;
using System.Globalization;
using System.Linq;

namespace GitHub.VisualStudio.Commands
{
/// <summary>
/// Command to sync submodules in local repository.
/// </summary>
[Export(typeof(ISyncSubmodulesCommand))]
public class SyncSubmodulesCommand : VsCommand, ISyncSubmodulesCommand
{
static readonly ILogger log = LogManager.ForContext<SyncSubmodulesCommand>();

readonly Lazy<IPullRequestService> lazyPullRequestService;
readonly Lazy<IStatusBarNotificationService> lazyStatusBarNotificationService;
readonly Lazy<IVSGitExt> lazyVSGitExt;

[ImportingConstructor]
protected SyncSubmodulesCommand(
Lazy<IPullRequestService> pullRequestService,
Lazy<IStatusBarNotificationService> statusBarNotificationService,
Lazy<IVSGitExt> gitExt)
: base(CommandSet, CommandId)
{
lazyPullRequestService = pullRequestService;
lazyStatusBarNotificationService = statusBarNotificationService;
lazyVSGitExt = gitExt;
}

/// <summary>
/// Gets the GUID of the group the command belongs to.
/// </summary>
public static readonly Guid CommandSet = Guids.guidGitHubCmdSet;

/// <summary>
/// Gets the numeric identifier of the command.
/// </summary>
public const int CommandId = PkgCmdIDList.syncSubmodulesCommand;

/// <summary>
/// Syncs submodules.
/// </summary>
public override async Task Execute()
{
try
{
var complete = await SyncSubmodules();
}
catch (Exception ex)
{
log.Error(ex, "Error syncing submodules");
lazyStatusBarNotificationService.Value.ShowMessage("Error syncing submodules");
}
}

/// <summary>
/// Sync submodules in local repository.
/// </summary>
/// <returns>Tuple with bool that is true if command completed successfully and string with
/// output from sync submodules Git command.</returns>
public async Task<Tuple<bool, string>> SyncSubmodules()
{
var pullRequestService = lazyPullRequestService.Value;
var statusBarNotificationService = lazyStatusBarNotificationService.Value;
var gitExt = lazyVSGitExt.Value;

var repository = gitExt.ActiveRepositories.FirstOrDefault();
if (repository == null)
{
statusBarNotificationService.ShowMessage("No local Git repository");
return new Tuple<bool, string>(true, "No local Git repository");
}

var writer = new StringWriter(CultureInfo.CurrentCulture);
var complete = await pullRequestService.SyncSubmodules(repository, line =>
{
writer.WriteLine(line);
statusBarNotificationService.ShowMessage(line);
});

if (!complete)
{
statusBarNotificationService.ShowMessage("Failed to sync submodules." + Environment.NewLine + writer);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it normal for VS commands to write directly to the status bar? Is there no concept of "output" for commands, which e.g. shows in the command pane?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the status bar is what commands tend to use as their ambient UI. There isn't really any concept of a native UI or output for a command.

One possibility would be to pass in a progress object that the command could report back to. This could also sidestep the issue of returning results.

I wanted to start off with a simple refactoring and and avoid any extra implementation detail. We can revisit next time this command is touched (if we decide to surface it somewhere else).

return new Tuple<bool, string>(false, writer.ToString());
}

statusBarNotificationService.ShowMessage(string.Empty);
return new Tuple<bool, string>(true, writer.ToString());
}
}
}
1 change: 1 addition & 0 deletions src/GitHub.VisualStudio/GitHub.VisualStudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@
<Compile Include="Commands\LinkCommandBase.cs" />
<Compile Include="Commands\CopyLinkCommand.cs" />
<Compile Include="Commands\OpenLinkCommand.cs" />
<Compile Include="Commands\SyncSubmodulesCommand.cs" />
<Compile Include="Commands\ShowCurrentPullRequestCommand.cs" />
<Compile Include="Commands\ShowGitHubPaneCommand.cs" />
<Compile Include="Commands\OpenPullRequestsCommand.cs" />
Expand Down
11 changes: 11 additions & 0 deletions src/GitHub.VisualStudio/GitHub.VisualStudio.vsct
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@
</Strings>
</Button>

<Button guid="guidGitHubCmdSet" id="syncSubmodulesCommand" type="Button">
<Icon guid="guidImages" id="logo" />
<CommandFlag>IconIsMoniker</CommandFlag>
<Strings>
<ButtonText>Sync Submodules</ButtonText>
<CanonicalName>.GitHub.SyncSubmodules</CanonicalName>
<LocCanonicalName>.GitHub.SyncSubmodules</LocCanonicalName>
</Strings>
</Button>

<!--- Toolbar buttons -->
<Button guid="guidGitHubToolbarCmdSet" id="backCommand" type="Button">
<Icon guid="guidImages" id="arrow_left" />
Expand Down Expand Up @@ -321,6 +331,7 @@
<IDSymbol name="addConnectionCommand" value="0x110"/>
<IDSymbol name="showGitHubPaneCommand" value="0x200"/>
<IDSymbol name="showCurrentPullRequestCommand" value="0x202"/>
<IDSymbol name="syncSubmodulesCommand" value="0x0203" />
</GuidSymbol>

<!-- This is the Manage Connections menu -->
Expand Down
5 changes: 3 additions & 2 deletions src/GitHub.VisualStudio/GitHubPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
{
LogVersionInformation();
await base.InitializeAsync(cancellationToken, progress);

await InitializeLoggingAsync();
await GetServiceAsync(typeof(IUsageTracker));

Expand Down Expand Up @@ -85,7 +85,8 @@ async Task InitializeMenus()
exports.GetExportedValue<IOpenLinkCommand>(),
exports.GetExportedValue<IOpenPullRequestsCommand>(),
exports.GetExportedValue<IShowCurrentPullRequestCommand>(),
exports.GetExportedValue<IShowGitHubPaneCommand>()
exports.GetExportedValue<IShowGitHubPaneCommand>(),
exports.GetExportedValue<ISyncSubmodulesCommand>()
};

await JoinableTaskFactory.SwitchToMainThreadAsync();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reactive;
Expand All @@ -9,6 +8,7 @@
using GitHub.Factories;
using GitHub.Models;
using GitHub.Primitives;
using GitHub.Commands;
using GitHub.Services;
using GitHub.ViewModels.GitHubPane;
using LibGit2Sharp;
Expand Down Expand Up @@ -577,8 +577,8 @@ static Tuple<PullRequestDetailViewModel, IPullRequestService> CreateTargetAndSer
Substitute.For<IModelServiceFactory>(),
Substitute.For<IUsageTracker>(),
Substitute.For<ITeamExplorerContext>(),
Substitute.For<IStatusBarNotificationService>(),
Substitute.For<IPullRequestFilesViewModel>());
Substitute.For<IPullRequestFilesViewModel>(),
Substitute.For<ISyncSubmodulesCommand>());
vm.InitializeAsync(repository, Substitute.For<IConnection>(), "owner", "repo", 1).Wait();

return Tuple.Create(vm, pullRequestService);
Expand Down