Skip to content

Commit

Permalink
clone: register repo when cloning
Browse files Browse the repository at this point in the history
Register the repo when cloning rather than when mounting
  • Loading branch information
wilbaker committed Oct 29, 2019
1 parent a388f0c commit 09d6e8d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 72 deletions.
79 changes: 79 additions & 0 deletions Scalar/CommandLine/CloneVerb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Scalar.Common.FileSystem;
using Scalar.Common.Git;
using Scalar.Common.Http;
using Scalar.Common.NamedPipes;
using Scalar.Common.Tracing;
using System;
using System.Diagnostics;
Expand Down Expand Up @@ -289,6 +290,8 @@ private Result DoClone(string fullEnlistmentRootPathParameter, string normalized
});

cloneResult = this.CheckoutRepo();

this.RegisterWithService();
}

return cloneResult;
Expand Down Expand Up @@ -587,6 +590,82 @@ private Result CreateClone()
return new Result(true);
}

private void RegisterWithService()
{
if (!this.Unattended)
{
this.tracer.RelatedInfo($"{nameof(this.Execute)}: Registering with service");

string errorMessage = string.Empty;
if (this.ShowStatusWhileRunning(
() => { return this.RegisterRepoWithService(out errorMessage); },
"Registering with service"))
{
this.tracer.RelatedInfo($"{nameof(this.Execute)}: Registered with service");
}
else
{
this.Output.WriteLine(" WARNING: " + errorMessage);
this.tracer.RelatedInfo($"{nameof(this.Execute)}: Failed to register with service");
}
}
}

private bool RegisterRepoWithService(out string errorMessage)
{
errorMessage = string.Empty;

NamedPipeMessages.RegisterRepoRequest request = new NamedPipeMessages.RegisterRepoRequest();
request.EnlistmentRoot = this.enlistment.EnlistmentRoot;

request.OwnerSID = ScalarPlatform.Instance.GetCurrentUser();

using (NamedPipeClient client = new NamedPipeClient(this.ServicePipeName))
{
if (!client.Connect())
{
errorMessage = "Unable to register repo because Scalar.Service is not responding.";
return false;
}

try
{
client.SendRequest(request.ToMessage());
NamedPipeMessages.Message response = client.ReadResponse();
if (response.Header == NamedPipeMessages.RegisterRepoRequest.Response.Header)
{
NamedPipeMessages.RegisterRepoRequest.Response message = NamedPipeMessages.RegisterRepoRequest.Response.FromMessage(response);

if (!string.IsNullOrEmpty(message.ErrorMessage))
{
errorMessage = message.ErrorMessage;
return false;
}

if (message.State != NamedPipeMessages.CompletionState.Success)
{
errorMessage = "Unable to register repo. " + errorMessage;
return false;
}
else
{
return true;
}
}
else
{
errorMessage = string.Format("Scalar.Service responded with unexpected message: {0}", response);
return false;
}
}
catch (BrokenPipeException e)
{
errorMessage = "Unable to communicate with Scalar.Service: " + e.ToString();
return false;
}
}
}

private Result TryInitRepo()
{
string repoPath = this.enlistment.WorkingDirectoryBackingRoot;
Expand Down
72 changes: 0 additions & 72 deletions Scalar/CommandLine/MountVerb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,6 @@ protected override void Execute(ScalarEnlistment enlistment)
{
this.ReportErrorAndExit(tracer, errorMessage);
}

if (!this.Unattended)
{
tracer.RelatedInfo($"{nameof(this.Execute)}: Registering for automount");

if (this.ShowStatusWhileRunning(
() => { return this.RegisterMount(enlistment, out errorMessage); },
"Registering for automount"))
{
tracer.RelatedInfo($"{nameof(this.Execute)}: Registered for automount");
}
else
{
this.Output.WriteLine(" WARNING: " + errorMessage);
tracer.RelatedInfo($"{nameof(this.Execute)}: Failed to register for automount");
}
}
}
}

Expand Down Expand Up @@ -252,61 +235,6 @@ private bool TryMount(ITracer tracer, ScalarEnlistment enlistment, string mountE
return ScalarEnlistment.WaitUntilMounted(tracer, enlistment.EnlistmentRoot, this.Unattended, out errorMessage);
}

private bool RegisterMount(ScalarEnlistment enlistment, out string errorMessage)
{
errorMessage = string.Empty;

NamedPipeMessages.RegisterRepoRequest request = new NamedPipeMessages.RegisterRepoRequest();
request.EnlistmentRoot = enlistment.EnlistmentRoot;

request.OwnerSID = ScalarPlatform.Instance.GetCurrentUser();

using (NamedPipeClient client = new NamedPipeClient(this.ServicePipeName))
{
if (!client.Connect())
{
errorMessage = "Unable to register repo because Scalar.Service is not responding.";
return false;
}

try
{
client.SendRequest(request.ToMessage());
NamedPipeMessages.Message response = client.ReadResponse();
if (response.Header == NamedPipeMessages.RegisterRepoRequest.Response.Header)
{
NamedPipeMessages.RegisterRepoRequest.Response message = NamedPipeMessages.RegisterRepoRequest.Response.FromMessage(response);

if (!string.IsNullOrEmpty(message.ErrorMessage))
{
errorMessage = message.ErrorMessage;
return false;
}

if (message.State != NamedPipeMessages.CompletionState.Success)
{
errorMessage = "Unable to register repo. " + errorMessage;
return false;
}
else
{
return true;
}
}
else
{
errorMessage = string.Format("Scalar.Service responded with unexpected message: {0}", response);
return false;
}
}
catch (BrokenPipeException e)
{
errorMessage = "Unable to communicate with Scalar.Service: " + e.ToString();
return false;
}
}
}

private void ParseEnumArgs(out EventLevel verbosity, out Keywords keywords)
{
if (!Enum.TryParse(this.KeywordsCsv, out keywords))
Expand Down

0 comments on commit 09d6e8d

Please sign in to comment.