Skip to content

Commit 1f75fd5

Browse files
author
Tomas Urbonaitis
committed
Throwing InvalidRepoExcpetion, when ssl-related bool git config keys have non-bool values. Handling InvalidRepoException in CloneVerb.
1 parent fdb9231 commit 1f75fd5

2 files changed

Lines changed: 55 additions & 34 deletions

File tree

GVFS/GVFS.Common/Git/GitSsl.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,9 @@ public GitSsl(IDictionary<string, GitConfigSetting> configSettings) : this()
3434
this.certificatePathOrSubjectCommonName = sslCerts.Values.Last();
3535
}
3636

37-
if (configSettings.TryGetValue(GitConfigSetting.HttpSslCertPasswordProtected, out GitConfigSetting isSslCertPasswordProtected))
38-
{
39-
this.isCertificatePasswordProtected = isSslCertPasswordProtected.Values.Select(bool.Parse).Last();
40-
}
37+
this.isCertificatePasswordProtected = SetBoolSettingOrThrow(configSettings, GitConfigSetting.HttpSslCertPasswordProtected, this.isCertificatePasswordProtected);
4138

42-
if (configSettings.TryGetValue(GitConfigSetting.HttpSslVerify, out GitConfigSetting sslVerify))
43-
{
44-
this.ShouldVerify = sslVerify.Values.Select(bool.Parse).Last();
45-
}
39+
this.ShouldVerify = SetBoolSettingOrThrow(configSettings, GitConfigSetting.HttpSslVerify, this.ShouldVerify);
4640
}
4741
}
4842

@@ -76,6 +70,23 @@ public X509Certificate2 GetCertificate(ITracer tracer, GitProcess gitProcess)
7670
}
7771

7872
return result;
73+
}
74+
75+
private static bool SetBoolSettingOrThrow(IDictionary<string, GitConfigSetting> configSettings, string settingName, bool currentValue)
76+
{
77+
if (configSettings.TryGetValue(settingName, out GitConfigSetting settingValues))
78+
{
79+
try
80+
{
81+
return settingValues.Values.Select(bool.Parse).Last();
82+
}
83+
catch (FormatException)
84+
{
85+
throw new InvalidRepoException($"{settingName} git setting did not have a bool-parsable value. Found: {string.Join(" ", settingValues.Values)}");
86+
}
87+
}
88+
89+
return currentValue;
7990
}
8091

8192
private static void LogWithAppropriateLevel(ITracer tracer, EventMetadata metadata, IEnumerable<X509Certificate2> certificates, string logMessage)

GVFS/GVFS/CommandLine/CloneVerb.cs

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public override void Execute()
8585
this.ValidatePathParameter(this.EnlistmentRootPathParameter);
8686
this.ValidatePathParameter(this.LocalCacheRoot);
8787

88-
string fullEnlistmentRootPathParameter;
88+
string fullEnlistmentRootPathParameter;
8989
string normalizedEnlistmentRootPath = this.GetCloneRoot(out fullEnlistmentRootPathParameter);
9090

9191
if (!string.IsNullOrWhiteSpace(this.LocalCacheRoot))
@@ -144,8 +144,8 @@ public override void Execute()
144144
{ "NamedPipeName", enlistment.NamedPipeName },
145145
{ nameof(this.EnlistmentRootPathParameter), this.EnlistmentRootPathParameter },
146146
{ nameof(fullEnlistmentRootPathParameter), fullEnlistmentRootPathParameter },
147-
});
148-
147+
});
148+
149149
CacheServerResolver cacheServerResolver = new CacheServerResolver(tracer, enlistment);
150150
cacheServer = cacheServerResolver.ParseUrlOrFriendlyName(this.CacheServerUrl);
151151

@@ -177,8 +177,8 @@ public override void Execute()
177177

178178
cacheServer = this.ResolveCacheServer(tracer, cacheServer, cacheServerResolver, serverGVFSConfig);
179179

180-
this.ValidateClientVersions(tracer, enlistment, serverGVFSConfig, showWarnings: true);
181-
180+
this.ValidateClientVersions(tracer, enlistment, serverGVFSConfig, showWarnings: true);
181+
182182
this.ShowStatusWhileRunning(
183183
() =>
184184
{
@@ -276,7 +276,7 @@ private static bool IsForceCheckoutErrorCloneFailure(string checkoutError)
276276

277277
private Result TryCreateEnlistment(
278278
string fullEnlistmentRootPathParameter,
279-
string normalizedEnlistementRootPath,
279+
string normalizedEnlistementRootPath,
280280
out GVFSEnlistment enlistment)
281281
{
282282
enlistment = null;
@@ -297,25 +297,35 @@ private Result TryCreateEnlistment(
297297
if (string.IsNullOrWhiteSpace(gitBinPath))
298298
{
299299
return new Result(GVFSConstants.GitIsNotInstalledError);
300-
}
301-
300+
}
301+
302302
string hooksPath = this.GetGVFSHooksPathAndCheckVersion(tracer: null, hooksVersion: out _);
303303

304-
enlistment = new GVFSEnlistment(
305-
normalizedEnlistementRootPath,
306-
this.RepositoryURL,
307-
gitBinPath,
308-
hooksPath,
309-
authentication: null);
310-
304+
try
305+
{
306+
enlistment = new GVFSEnlistment(
307+
normalizedEnlistementRootPath,
308+
this.RepositoryURL,
309+
gitBinPath,
310+
hooksPath,
311+
authentication: null);
312+
}
313+
catch (InvalidRepoException e)
314+
{
315+
this.ReportErrorAndExit(
316+
"Error when creating a new GVFS enlistment at '{0}'. {1}",
317+
normalizedEnlistementRootPath,
318+
e.Message);
319+
}
320+
311321
return new Result(true);
312-
}
313-
322+
}
323+
314324
private Result TryClone(
315-
JsonTracer tracer,
316-
GVFSEnlistment enlistment,
317-
CacheServerInfo cacheServer,
318-
RetryConfig retryConfig,
325+
JsonTracer tracer,
326+
GVFSEnlistment enlistment,
327+
CacheServerInfo cacheServer,
328+
RetryConfig retryConfig,
319329
ServerGVFSConfig serverGVFSConfig,
320330
string resolvedLocalCacheRoot)
321331
{
@@ -325,8 +335,8 @@ private Result TryClone(
325335
if (!pipeResult.Success)
326336
{
327337
return pipeResult;
328-
}
329-
338+
}
339+
330340
using (GitObjectsHttpRequestor objectRequestor = new GitObjectsHttpRequestor(tracer, enlistment, cacheServer, retryConfig))
331341
{
332342
GitRefs refs = objectRequestor.QueryInfoRefs(this.SingleBranch ? this.Branch : null);
@@ -445,7 +455,7 @@ private void CheckNotInsideExistingRepo(string normalizedEnlistmentRootPath)
445455
string errorMessage;
446456
string existingEnlistmentRoot;
447457
if (GVFSPlatform.Instance.TryGetGVFSEnlistmentRoot(normalizedEnlistmentRootPath, out existingEnlistmentRoot, out errorMessage))
448-
{
458+
{
449459
this.ReportErrorAndExit("Error: You can't clone inside an existing GVFS repo ({0})", existingEnlistmentRoot);
450460
}
451461
}
@@ -504,8 +514,8 @@ private Result CreateClone(
504514
if (!this.TryCreateAlternatesFile(fileSystem, enlistment, out errorMessage))
505515
{
506516
return new Result("Error configuring alternate: " + errorMessage);
507-
}
508-
517+
}
518+
509519
GitRepo gitRepo = new GitRepo(tracer, enlistment, fileSystem);
510520
GVFSContext context = new GVFSContext(tracer, fileSystem, gitRepo, enlistment);
511521
GVFSGitObjects gitObjects = new GVFSGitObjects(context, objectRequestor);

0 commit comments

Comments
 (0)