This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathSyncSubmodulesCommand.cs
More file actions
100 lines (89 loc) · 3.56 KB
/
Copy pathSyncSubmodulesCommand.cs
File metadata and controls
100 lines (89 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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);
return new Tuple<bool, string>(false, writer.ToString());
}
statusBarNotificationService.ShowMessage(string.Empty);
return new Tuple<bool, string>(true, writer.ToString());
}
}
}