11using System ;
22using System . Collections . Generic ;
3- using System . IO ;
4- using System . Runtime . InteropServices ;
5- using DotNetConfig ;
6- using KnownEnvars = GitCredentialManager . Constants . EnvironmentVariables ;
7- using KnownGitCfg = GitCredentialManager . Constants . GitConfiguration ;
3+ using GitCredentialManager . Interop . Windows ;
84
95namespace GitCredentialManager ;
106
@@ -34,7 +30,10 @@ public static ICredentialStore Create(string? @namespace = default)
3430
3531 class CommandContextWrapper ( CommandContext context , string ? @namespace ) : ICommandContext
3632 {
37- readonly ISettings settings = new SettingsWrapper ( context . Settings , @namespace ) ;
33+ readonly ISettings settings = new SettingsWrapper (
34+ context . Settings is WindowsSettings ?
35+ new NoGitWindowsSettings ( context . Environment , context . Git , context . Trace ) :
36+ new NoGitSettings ( context . Environment , context . Git ) , @namespace ) ;
3837
3938 public ISettings Settings => settings ;
4039
@@ -60,7 +59,7 @@ class CommandContextWrapper(CommandContext context, string? @namespace) : IComma
6059
6160 public IHttpClientFactory HttpClientFactory => ( ( ICommandContext ) context ) . HttpClientFactory ;
6261
63- public IGit Git => ( ( ICommandContext ) context ) . Git ;
62+ public IGit Git => new NoGit ( context . Git ) ;
6463
6564 public IEnvironment Environment => ( ( ICommandContext ) context ) . Environment ;
6665
@@ -71,65 +70,27 @@ class CommandContextWrapper(CommandContext context, string? @namespace) : IComma
7170 #endregion
7271 }
7372
73+ class NoGitSettings ( IEnvironment environment , IGit git ) : Settings ( environment , new NoGit ( git ) ) { }
74+
75+ class NoGitWindowsSettings ( IEnvironment environment , IGit git , ITrace trace ) : WindowsSettings ( environment , new NoGit ( git ) , trace ) { }
76+
7477 class SettingsWrapper ( ISettings settings , string ? @namespace ) : ISettings
7578 {
76- static readonly Config gitconfig ;
77-
78- static SettingsWrapper ( )
79- {
80- string homeDir ;
81-
82- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
83- {
84- homeDir = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ;
85- }
86- else
87- {
88- // On Linux/Mac it's $HOME
89- homeDir = Environment . GetEnvironmentVariable ( "HOME" )
90- ?? Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ;
91- }
92-
93- gitconfig = Config . Build ( Path . Combine ( homeDir , ".gitconfig" ) ) ;
94- }
95-
96- static bool TryGetWrappedSetting ( string envarName , string section , string property , out string value )
97- {
98- if ( envarName != null && Environment . GetEnvironmentVariable ( envarName ) is { Length : > 0 } envvar )
99- {
100- value = envvar ;
101- return true ;
102- }
103-
104- return gitconfig . TryGetString ( section , property , out value ) ;
105- }
106-
107- // Overriden namespace to scope credential operations.
108- public string CredentialNamespace => @namespace ?? (
109- TryGetWrappedSetting ( KnownEnvars . GcmCredNamespace ,
110- KnownGitCfg . Credential . SectionName , KnownGitCfg . Credential . CredNamespace ,
111- out var ns ) ? ns : Constants . DefaultCredentialNamespace ) ;
112-
113- public string CredentialBackingStore =>
114- TryGetWrappedSetting (
115- KnownEnvars . GcmCredentialStore ,
116- KnownGitCfg . Credential . SectionName ,
117- KnownGitCfg . Credential . CredentialStore ,
118- out string credStore )
119- ? credStore
120- : null ! ;
121-
122- public bool UseMsAuthDefaultAccount =>
123- TryGetWrappedSetting (
124- KnownEnvars . MsAuthUseDefaultAccount ,
125- KnownGitCfg . Credential . SectionName ,
126- KnownGitCfg . Credential . MsAuthUseDefaultAccount ,
127- out string str )
128- ? str . IsTruthy ( )
129- : PlatformUtils . IsDevBox ( ) ; // default to true in DevBox environment
79+ public string CredentialNamespace => @namespace ?? settings . CredentialNamespace ;
13080
13181 #region pass-through impl.
13282
83+ public string CredentialBackingStore => settings . CredentialBackingStore ;
84+
85+ public bool UseMsAuthDefaultAccount => settings . UseMsAuthDefaultAccount ;
86+
87+ public IEnumerable < string > GetSettingValues ( string envarName , string section , string property , bool isPath )
88+ => settings . GetSettingValues ( envarName , section , property , isPath ) ;
89+
90+ public bool GetTracingEnabled ( out string value ) => settings . GetTracingEnabled ( out value ) ;
91+ public bool TryGetPathSetting ( string envarName , string section , string property , out string value ) => settings . TryGetPathSetting ( envarName , section , property , out value ) ;
92+ public bool TryGetSetting ( string envarName , string section , string property , out string value ) => settings . TryGetSetting ( envarName , section , property , out value ) ;
93+
13394 public Uri RemoteUri { get => settings . RemoteUri ; set => settings . RemoteUri = value ; }
13495
13596 public bool IsDebuggingEnabled => settings . IsDebuggingEnabled ;
@@ -172,12 +133,7 @@ static bool TryGetWrappedSetting(string envarName, string section, string proper
172133
173134 public void Dispose ( ) => settings . Dispose ( ) ;
174135 public ProxyConfiguration GetProxyConfiguration ( ) => settings . GetProxyConfiguration ( ) ;
175- public IEnumerable < string > GetSettingValues ( string envarName , string section , string property , bool isPath ) => settings . GetSettingValues ( envarName , section , property , isPath ) ;
176136 public Trace2Settings GetTrace2Settings ( ) => settings . GetTrace2Settings ( ) ;
177- public bool GetTracingEnabled ( out string value ) => settings . GetTracingEnabled ( out value ) ;
178- public bool TryGetPathSetting ( string envarName , string section , string property , out string value ) => settings . TryGetPathSetting ( envarName , section , property , out value ) ;
179- public bool TryGetSetting ( string envarName , string section , string property , out string value ) => settings . TryGetSetting ( envarName , section , property , out value ) ;
180-
181137 #endregion
182138 }
183139}
0 commit comments