Skip to content

Commit 1e69000

Browse files
authored
Merge pull request #69 from mjcheetham/optimise-gitcfg
New Install 1/6: Optimise the use of Git configuration from the Settings component
2 parents 182e0eb + f835d13 commit 1e69000

File tree

23 files changed

+633
-283
lines changed

23 files changed

+633
-283
lines changed

src/shared/Git-Credential-Manager/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static class Program
1010
{
1111
public static void Main(string[] args)
1212
{
13-
var context = new CommandContext();
13+
using (var context = new CommandContext())
1414
using (var app = new Application(context))
1515
{
1616
// Register all supported host providers

src/shared/GitHub/GitHubHostProvider.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public override string GetCredentialKey(InputArguments input)
6464

6565
public override async Task<ICredential> GenerateCredentialAsync(InputArguments input)
6666
{
67+
ThrowIfDisposed();
68+
6769
// We should not allow unencrypted communication and should inform the user
6870
if (StringComparer.OrdinalIgnoreCase.Equals(input.Protocol, "http"))
6971
{
@@ -105,14 +107,10 @@ public override async Task<ICredential> GenerateCredentialAsync(InputArguments i
105107
throw new Exception($"Interactive logon for '{targetUri}' failed.");
106108
}
107109

108-
protected override void Dispose(bool disposing)
110+
protected override void ReleaseManagedResources()
109111
{
110-
if (disposing)
111-
{
112-
_gitHubApi.Dispose();
113-
}
114-
115-
base.Dispose(disposing);
112+
_gitHubApi.Dispose();
113+
base.ReleaseManagedResources();
116114
}
117115

118116
#region Private Methods

src/shared/Microsoft.AzureRepos/AzureReposHostProvider.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public override string GetCredentialKey(InputArguments input)
5454

5555
public override async Task<ICredential> GenerateCredentialAsync(InputArguments input)
5656
{
57+
ThrowIfDisposed();
58+
5759
// We should not allow unencrypted communication and should inform the user
5860
if (StringComparer.OrdinalIgnoreCase.Equals(input.Protocol, "http"))
5961
{
@@ -94,14 +96,10 @@ public override async Task<ICredential> GenerateCredentialAsync(InputArguments i
9496
return new GitCredential(Constants.PersonalAccessTokenUserName, pat);
9597
}
9698

97-
protected override void Dispose(bool disposing)
99+
protected override void ReleaseManagedResources()
98100
{
99-
if (disposing)
100-
{
101-
_azDevOps.Dispose();
102-
}
103-
104-
base.Dispose(disposing);
101+
_azDevOps.Dispose();
102+
base.ReleaseManagedResources();
105103
}
106104

107105
#endregion

src/shared/Microsoft.Git.CredentialManager.Tests/Interop/LibGit2Tests.cs

Lines changed: 110 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Text;
88
using Microsoft.Git.CredentialManager.Interop;
99
using Microsoft.Git.CredentialManager.Interop.Posix.Native;
10+
using Microsoft.Git.CredentialManager.Tests.Objects;
1011
using Xunit;
1112

1213
namespace Microsoft.Git.CredentialManager.Tests.Interop
@@ -16,7 +17,8 @@ public class LibGit2Tests
1617
[Fact]
1718
public void LibGit2_GetRepositoryPath_NotInsideRepository_ReturnsNull()
1819
{
19-
var git = new LibGit2();
20+
var trace = new NullTrace();
21+
var git = new LibGit2(trace);
2022
string randomPath = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid():N}");
2123
Directory.CreateDirectory(randomPath);
2224

@@ -28,20 +30,97 @@ public void LibGit2_GetRepositoryPath_NotInsideRepository_ReturnsNull()
2830
[Fact]
2931
public void LibGit2_GetConfiguration_ReturnsConfiguration()
3032
{
31-
var git = new LibGit2();
33+
var trace = new NullTrace();
34+
var git = new LibGit2(trace);
3235
using (var config = git.GetConfiguration())
3336
{
3437
Assert.NotNull(config);
3538
}
3639
}
3740

41+
[Fact]
42+
public void LibGit2Configuration_Enumerate_CallbackReturnsTrue_InvokesCallbackForEachEntry()
43+
{
44+
string repoPath = CreateRepository();
45+
Git(repoPath, "config --local foo.name lancelot").AssertSuccess();
46+
Git(repoPath, "config --local foo.quest seek-holy-grail").AssertSuccess();
47+
Git(repoPath, "config --local foo.favcolor blue").AssertSuccess();
48+
49+
var expectedVisitedEntries = new List<(string name, string value)>
50+
{
51+
("foo.name", "lancelot"),
52+
("foo.quest", "seek-holy-grail"),
53+
("foo.favcolor", "blue")
54+
};
55+
56+
var trace = new NullTrace();
57+
var git = new LibGit2(trace);
58+
using (var config = git.GetConfiguration(repoPath))
59+
{
60+
var actualVisitedEntries = new List<(string name, string value)>();
61+
62+
bool cb(string name, string value)
63+
{
64+
if (name.StartsWith("foo."))
65+
{
66+
actualVisitedEntries.Add((name, value));
67+
}
68+
69+
// Continue enumeration
70+
return true;
71+
}
72+
73+
config.Enumerate(cb);
74+
75+
Assert.Equal(expectedVisitedEntries, actualVisitedEntries);
76+
}
77+
}
78+
79+
[Fact]
80+
public void LibGit2Configuration_Enumerate_CallbackReturnsFalse_InvokesCallbackForEachEntryUntilReturnsFalse()
81+
{
82+
string repoPath = CreateRepository();
83+
Git(repoPath, "config --local foo.name lancelot").AssertSuccess();
84+
Git(repoPath, "config --local foo.quest seek-holy-grail").AssertSuccess();
85+
Git(repoPath, "config --local foo.favcolor blue").AssertSuccess();
86+
87+
var expectedVisitedEntries = new List<(string name, string value)>
88+
{
89+
("foo.name", "lancelot"),
90+
("foo.quest", "seek-holy-grail")
91+
};
92+
93+
var trace = new NullTrace();
94+
var git = new LibGit2(trace);
95+
using (var config = git.GetConfiguration(repoPath))
96+
{
97+
var actualVisitedEntries = new List<(string name, string value)>();
98+
99+
bool cb(string name, string value)
100+
{
101+
if (name.StartsWith("foo."))
102+
{
103+
actualVisitedEntries.Add((name, value));
104+
}
105+
106+
// Stop enumeration after 2 'foo' entries
107+
return actualVisitedEntries.Count < 2;
108+
}
109+
110+
config.Enumerate(cb);
111+
112+
Assert.Equal(expectedVisitedEntries, actualVisitedEntries);
113+
}
114+
}
115+
38116
[Fact]
39117
public void LibGit2Configuration_TryGetValue_Name_Exists_ReturnsTrueOutString()
40118
{
41119
string repoPath = CreateRepository();
42120
Git(repoPath, "config --local user.name john.doe").AssertSuccess();
43121

44-
var git = new LibGit2();
122+
var trace = new NullTrace();
123+
var git = new LibGit2(trace);
45124
using (var config = git.GetConfiguration(repoPath))
46125
{
47126
bool result = config.TryGetValue("user.name", out string value);
@@ -56,7 +135,8 @@ public void LibGit2Configuration_TryGetValue_Name_DoesNotExists_ReturnsFalse()
56135
{
57136
string repoPath = CreateRepository();
58137

59-
var git = new LibGit2();
138+
var trace = new NullTrace();
139+
var git = new LibGit2(trace);
60140
using (var config = git.GetConfiguration(repoPath))
61141
{
62142
string randomName = $"{Guid.NewGuid():N}.{Guid.NewGuid():N}";
@@ -72,7 +152,8 @@ public void LibGit2Configuration_TryGetValue_SectionProperty_Exists_ReturnsTrueO
72152
string repoPath = CreateRepository();
73153
Git(repoPath, "config --local user.name john.doe").AssertSuccess();
74154

75-
var git = new LibGit2();
155+
var trace = new NullTrace();
156+
var git = new LibGit2(trace);
76157
using (var config = git.GetConfiguration(repoPath))
77158
{
78159
bool result = config.TryGetValue("user", "name", out string value);
@@ -87,7 +168,8 @@ public void LibGit2Configuration_TryGetValue_SectionProperty_DoesNotExists_Retur
87168
{
88169
string repoPath = CreateRepository();
89170

90-
var git = new LibGit2();
171+
var trace = new NullTrace();
172+
var git = new LibGit2(trace);
91173
using (var config = git.GetConfiguration(repoPath))
92174
{
93175
string randomSection = Guid.NewGuid().ToString("N");
@@ -104,7 +186,8 @@ public void LibGit2Configuration_TryGetValue_SectionScopeProperty_Exists_Returns
104186
string repoPath = CreateRepository();
105187
Git(repoPath, "config --local user.example.com.name john.doe").AssertSuccess();
106188

107-
var git = new LibGit2();
189+
var trace = new NullTrace();
190+
var git = new LibGit2(trace);
108191
using (var config = git.GetConfiguration(repoPath))
109192
{
110193
bool result = config.TryGetValue("user", "example.com", "name", out string value);
@@ -120,7 +203,8 @@ public void LibGit2Configuration_TryGetValue_SectionScopeProperty_NullScope_Retu
120203
string repoPath = CreateRepository();
121204
Git(repoPath, "config --local user.name john.doe").AssertSuccess();
122205

123-
var git = new LibGit2();
206+
var trace = new NullTrace();
207+
var git = new LibGit2(trace);
124208
using (var config = git.GetConfiguration(repoPath))
125209
{
126210
bool result = config.TryGetValue("user", null, "name", out string value);
@@ -135,7 +219,8 @@ public void LibGit2Configuration_TryGetValue_SectionScopeProperty_DoesNotExists_
135219
{
136220
string repoPath = CreateRepository();
137221

138-
var git = new LibGit2();
222+
var trace = new NullTrace();
223+
var git = new LibGit2(trace);
139224
using (var config = git.GetConfiguration(repoPath))
140225
{
141226
string randomSection = Guid.NewGuid().ToString("N");
@@ -153,7 +238,8 @@ public void LibGit2Configuration_GetString_Name_Exists_ReturnsString()
153238
string repoPath = CreateRepository();
154239
Git(repoPath, "config --local user.name john.doe").AssertSuccess();
155240

156-
var git = new LibGit2();
241+
var trace = new NullTrace();
242+
var git = new LibGit2(trace);
157243
using (var config = git.GetConfiguration(repoPath))
158244
{
159245
string value = config.GetValue("user.name");
@@ -167,7 +253,8 @@ public void LibGit2Configuration_GetString_Name_DoesNotExists_ThrowsException()
167253
{
168254
string repoPath = CreateRepository();
169255

170-
var git = new LibGit2();
256+
var trace = new NullTrace();
257+
var git = new LibGit2(trace);
171258
using (var config = git.GetConfiguration(repoPath))
172259
{
173260
string randomName = $"{Guid.NewGuid():N}.{Guid.NewGuid():N}";
@@ -181,7 +268,8 @@ public void LibGit2Configuration_GetString_SectionProperty_Exists_ReturnsString(
181268
string repoPath = CreateRepository();
182269
Git(repoPath, "config --local user.name john.doe").AssertSuccess();
183270

184-
var git = new LibGit2();
271+
var trace = new NullTrace();
272+
var git = new LibGit2(trace);
185273
using (var config = git.GetConfiguration(repoPath))
186274
{
187275
string value = config.GetValue("user", "name");
@@ -195,7 +283,8 @@ public void LibGit2Configuration_GetString_SectionProperty_DoesNotExists_ThrowsE
195283
{
196284
string repoPath = CreateRepository();
197285

198-
var git = new LibGit2();
286+
var trace = new NullTrace();
287+
var git = new LibGit2(trace);
199288
using (var config = git.GetConfiguration(repoPath))
200289
{
201290
string randomSection = Guid.NewGuid().ToString("N");
@@ -210,7 +299,8 @@ public void LibGit2Configuration_GetString_SectionScopeProperty_Exists_ReturnsSt
210299
string repoPath = CreateRepository();
211300
Git(repoPath, "config --local user.example.com.name john.doe").AssertSuccess();
212301

213-
var git = new LibGit2();
302+
var trace = new NullTrace();
303+
var git = new LibGit2(trace);
214304
using (var config = git.GetConfiguration(repoPath))
215305
{
216306
string value = config.GetValue("user", "example.com", "name");
@@ -225,7 +315,8 @@ public void LibGit2Configuration_GetString_SectionScopeProperty_NullScope_Return
225315
string repoPath = CreateRepository();
226316
Git(repoPath, "config --local user.name john.doe").AssertSuccess();
227317

228-
var git = new LibGit2();
318+
var trace = new NullTrace();
319+
var git = new LibGit2(trace);
229320
using (var config = git.GetConfiguration(repoPath))
230321
{
231322
string value = config.GetValue("user", null, "name");
@@ -239,7 +330,8 @@ public void LibGit2Configuration_GetString_SectionScopeProperty_DoesNotExists_Th
239330
{
240331
string repoPath = CreateRepository();
241332

242-
var git = new LibGit2();
333+
var trace = new NullTrace();
334+
var git = new LibGit2(trace);
243335
using (var config = git.GetConfiguration(repoPath))
244336
{
245337
string randomSection = Guid.NewGuid().ToString("N");
@@ -259,7 +351,8 @@ public void LibGit2Configuration_GetRepositoryPath_ReturnsRepositoryPath()
259351
string fileL1Path = Path.Combine(directoryL0Path, "inner-file.txt");
260352
string directoryL1Path = Path.Combine(directoryL0Path, "sub-directory");
261353

262-
var git = new LibGit2();
354+
var trace = new NullTrace();
355+
var git = new LibGit2(trace);
263356

264357
// Create files and directories
265358
Directory.CreateDirectory(directoryL0Path);

0 commit comments

Comments
 (0)