Skip to content

Commit

Permalink
Make InvalidRepoException exit normally
Browse files Browse the repository at this point in the history
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
  • Loading branch information
derrickstolee committed Feb 20, 2020
1 parent 41c3240 commit 45817be
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 23 deletions.
6 changes: 3 additions & 3 deletions Scalar.Common/Enlistment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ protected Enlistment(
GitProcess.ConfigResult originResult = gitProcess.GetOriginUrl();
if (!originResult.TryParseAsString(out string originUrl, out string error))
{
throw new InvalidRepoException("Could not get origin url. git error: " + error);
throw new InvalidRepoException(this.WorkingDirectoryRoot, "Could not get origin url. git error: " + error);
}

if (originUrl == null)
{
throw new InvalidRepoException("Could not get origin url. remote 'origin' is not configured for this repo.'");
throw new InvalidRepoException(this.WorkingDirectoryRoot, "Could not get origin url. remote 'origin' is not configured for this repo.'");
}

this.RepoUrl = originUrl.Trim();
}

this.Authentication = authentication ?? new GitAuthentication(gitProcess, this.RepoUrl);
this.Authentication = authentication ?? new GitAuthentication(gitProcess, this.RepoUrl, this.WorkingDirectoryRoot);
}

public string EnlistmentRoot { get; }
Expand Down
4 changes: 2 additions & 2 deletions Scalar.Common/Git/GitAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public class GitAuthentication

private bool isInitialized;

public GitAuthentication(GitProcess git, string repoUrl)
public GitAuthentication(GitProcess git, string repoUrl, string repoPath)
{
this.credentialStore = git;
this.repoUrl = repoUrl;

if (git.TryGetConfigUrlMatch("http", this.repoUrl, out Dictionary<string, GitConfigSetting> configSettings))
{
this.GitSsl = new GitSsl(configSettings);
this.GitSsl = new GitSsl(repoPath, configSettings);
}
}

Expand Down
9 changes: 5 additions & 4 deletions Scalar.Common/Git/GitSsl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class GitSsl
private readonly PhysicalFileSystem fileSystem;

public GitSsl(
string repoPath,
IDictionary<string, GitConfigSetting> configSettings,
Func<SystemCertificateStore> createCertificateStore = null,
CertificateVerifier certificateVerifier = null,
Expand All @@ -32,9 +33,9 @@ public GitSsl(
this.certificatePathOrSubjectCommonName = sslCerts.Values.Last();
}

this.isCertificatePasswordProtected = SetBoolSettingOrThrow(configSettings, GitConfigSetting.HttpSslCertPasswordProtected, this.isCertificatePasswordProtected);
this.isCertificatePasswordProtected = SetBoolSettingOrThrow(repoPath, configSettings, GitConfigSetting.HttpSslCertPasswordProtected, this.isCertificatePasswordProtected);

this.ShouldVerify = SetBoolSettingOrThrow(configSettings, GitConfigSetting.HttpSslVerify, this.ShouldVerify);
this.ShouldVerify = SetBoolSettingOrThrow(repoPath, configSettings, GitConfigSetting.HttpSslVerify, this.ShouldVerify);
}
}

Expand Down Expand Up @@ -87,7 +88,7 @@ public X509Certificate2 GetCertificate(ITracer tracer, GitProcess gitProcess)
return result;
}

private static bool SetBoolSettingOrThrow(IDictionary<string, GitConfigSetting> configSettings, string settingName, bool currentValue)
private static bool SetBoolSettingOrThrow(string repoPath, IDictionary<string, GitConfigSetting> configSettings, string settingName, bool currentValue)
{
if (configSettings.TryGetValue(settingName, out GitConfigSetting settingValues))
{
Expand All @@ -97,7 +98,7 @@ private static bool SetBoolSettingOrThrow(IDictionary<string, GitConfigSetting>
}
catch (FormatException)
{
throw new InvalidRepoException($"{settingName} git setting did not have a bool-parsable value. Found: {string.Join(" ", settingValues.Values)}");
throw new InvalidRepoException(repoPath, $"{settingName} git setting did not have a bool-parsable value. Found: {string.Join(" ", settingValues.Values)}");
}
}

Expand Down
6 changes: 3 additions & 3 deletions Scalar.Common/Http/CacheServerResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static string GetUrlFromConfig(Enlistment enlistment)

// TODO 1057500: Remove support for encoded-repo-url cache config setting
return
GetValueFromConfig(git, ScalarConstants.GitConfig.CacheServer, localOnly: true)
GetValueFromConfig(enlistment.WorkingDirectoryRoot, git, ScalarConstants.GitConfig.CacheServer, localOnly: true)
?? enlistment.RepoUrl;
}

Expand Down Expand Up @@ -128,7 +128,7 @@ public bool TrySaveUrlToLocalConfig(CacheServerInfo cache, out string error)
return result.ExitCodeIsSuccess;
}

private static string GetValueFromConfig(GitProcess git, string configName, bool localOnly)
private static string GetValueFromConfig(string repoPath, GitProcess git, string configName, bool localOnly)
{
GitProcess.ConfigResult result =
localOnly
Expand All @@ -137,7 +137,7 @@ private static string GetValueFromConfig(GitProcess git, string configName, bool

if (!result.TryParseAsString(out string value, out string error))
{
throw new InvalidRepoException(error);
throw new InvalidRepoException(repoPath, error);
}

return value;
Expand Down
5 changes: 4 additions & 1 deletion Scalar.Common/InvalidRepoException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ namespace Scalar.Common
{
public class InvalidRepoException : Exception
{
public InvalidRepoException(string message)
public string RepoPath { get; }

public InvalidRepoException(string repoPath, string message)
: base(message)
{
this.RepoPath = repoPath;
}
}
}
4 changes: 2 additions & 2 deletions Scalar.Common/ScalarEnlistment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static ScalarEnlistment CreateFromDirectory(

if (!TryGetScalarEnlistmentRoot(directory, out enlistmentRoot, out workingDirectory))
{
throw new InvalidRepoException($"Could not get enlistment root.");
throw new InvalidRepoException(directory, $"Could not get enlistment root.");
}

if (createWithoutRepoURL)
Expand All @@ -80,7 +80,7 @@ public static ScalarEnlistment CreateFromDirectory(
return new ScalarEnlistment(enlistmentRoot, workingDirectory, null, gitBinRoot, authentication);
}

throw new InvalidRepoException($"Directory '{directory}' does not exist");
throw new InvalidRepoException(directory, $"Directory '{directory}' does not exist");
}

public static string GetNewScalarLogFileName(
Expand Down
16 changes: 8 additions & 8 deletions Scalar.UnitTests/Git/GitAuthenticationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void AuthShouldBackoffAfterFirstRetryFailure()
MockTracer tracer = new MockTracer();
MockGitProcess gitProcess = this.GetGitProcess();

GitAuthentication dut = new GitAuthentication(gitProcess, "mock://repoUrl");
GitAuthentication dut = new GitAuthentication(gitProcess, "mock://repoUrl", "fake path");
dut.TryInitializeAndRequireAuth(tracer, out _);

string authString;
Expand All @@ -54,7 +54,7 @@ public void BackoffIsNotInEffectAfterSuccess()
MockTracer tracer = new MockTracer();
MockGitProcess gitProcess = this.GetGitProcess();

GitAuthentication dut = new GitAuthentication(gitProcess, "mock://repoUrl");
GitAuthentication dut = new GitAuthentication(gitProcess, "mock://repoUrl", "fake path");
dut.TryInitializeAndRequireAuth(tracer, out _);

string authString;
Expand All @@ -78,7 +78,7 @@ public void ContinuesToBackoffIfTryGetCredentialsFails()
MockTracer tracer = new MockTracer();
MockGitProcess gitProcess = this.GetGitProcess();

GitAuthentication dut = new GitAuthentication(gitProcess, "mock://repoUrl");
GitAuthentication dut = new GitAuthentication(gitProcess, "mock://repoUrl", "fake path");
dut.TryInitializeAndRequireAuth(tracer, out _);

string authString;
Expand All @@ -105,7 +105,7 @@ public void TwoThreadsFailAtOnceStillRetriesOnce()
MockTracer tracer = new MockTracer();
MockGitProcess gitProcess = this.GetGitProcess();

GitAuthentication dut = new GitAuthentication(gitProcess, "mock://repoUrl");
GitAuthentication dut = new GitAuthentication(gitProcess, "mock://repoUrl", "fake path");
dut.TryInitializeAndRequireAuth(tracer, out _);

string authString;
Expand All @@ -132,7 +132,7 @@ public void TwoThreadsInterleavingFailuresStillRetriesOnce()
MockTracer tracer = new MockTracer();
MockGitProcess gitProcess = this.GetGitProcess();

GitAuthentication dut = new GitAuthentication(gitProcess, "mock://repoUrl");
GitAuthentication dut = new GitAuthentication(gitProcess, "mock://repoUrl", "fake path");
dut.TryInitializeAndRequireAuth(tracer, out _);

string thread1Auth;
Expand Down Expand Up @@ -168,7 +168,7 @@ public void TwoThreadsInterleavingFailuresShouldntStompASuccess()
MockTracer tracer = new MockTracer();
MockGitProcess gitProcess = this.GetGitProcess();

GitAuthentication dut = new GitAuthentication(gitProcess, "mock://repoUrl");
GitAuthentication dut = new GitAuthentication(gitProcess, "mock://repoUrl", "fake path");
dut.TryInitializeAndRequireAuth(tracer, out _);

string thread1Auth;
Expand Down Expand Up @@ -205,7 +205,7 @@ public void DontDoubleStoreExistingCredential()
MockTracer tracer = new MockTracer();
MockGitProcess gitProcess = this.GetGitProcess();

GitAuthentication dut = new GitAuthentication(gitProcess, "mock://repoUrl");
GitAuthentication dut = new GitAuthentication(gitProcess, "mock://repoUrl", "fake path");
dut.TryInitializeAndRequireAuth(tracer, out _);

string authString;
Expand All @@ -228,7 +228,7 @@ public void DontStoreDifferentCredentialFromCachedValue()
MockTracer tracer = new MockTracer();
MockGitProcess gitProcess = this.GetGitProcess();

GitAuthentication dut = new GitAuthentication(gitProcess, "mock://repoUrl");
GitAuthentication dut = new GitAuthentication(gitProcess, "mock://repoUrl", "fake path");
dut.TryInitializeAndRequireAuth(tracer, out _);

// Get and store an initial value that will be cached
Expand Down
4 changes: 4 additions & 0 deletions Scalar/CommandLine/ScalarVerb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ protected ReturnCode Execute<TVerb>(
{
verb.Execute();
}
catch (InvalidRepoException ire)
{
this.ReportErrorAndExit($"Invalid repository: {ire.Message}");
}
catch (VerbAbortedException)
{
}
Expand Down

0 comments on commit 45817be

Please sign in to comment.