11using System ;
22using System . Collections . Generic ;
3- using System . Diagnostics ;
4- using System . Linq ;
53using System . Text ;
64
75namespace GitCredentialManager
@@ -326,11 +324,14 @@ public class GitProcessConfiguration : IGitConfiguration
326324 private static readonly GitVersion ConfigListTypeMinVersion = new GitVersion ( 2 , 54 , 0 ) ;
327325 private static readonly GitVersion ConfigListTypeMinVfsBase = new GitVersion ( 2 , 53 , 0 ) ;
328326 private static readonly GitVersion ConfigListTypeMinVfsSuffix = new GitVersion ( 0 , 1 ) ;
327+ private static readonly GitVersion ConfigListRawMinVersion = new GitVersion ( 2 , 26 , 0 ) ;
328+ private static readonly GitVersion ConfigListSubcommandMinVersion = new GitVersion ( 2 , 46 , 0 ) ;
329329
330330 private readonly ITrace _trace ;
331331 private readonly GitProcess _git ;
332332 private readonly Dictionary < GitConfigurationType , ConfigCache > _cache ;
333- private readonly bool _useCache ;
333+ private readonly bool _useConfigTypeCache ;
334+ private readonly bool _useConfigRawCache ;
334335
335336 internal GitProcessConfiguration ( ITrace trace , GitProcess git ) : this ( trace , git , useCache : true )
336337 {
@@ -348,12 +349,24 @@ internal GitProcessConfiguration(ITrace trace, GitProcess git, bool useCache)
348349 // or microsoft/git fork 2.53.0.vfs.0.1+
349350 if ( useCache && ! SupportsConfigListType ( git ) )
350351 {
351- trace . WriteLine ( $ "Git version { git . Version . OriginalString } does not support 'git config list --type'; config cache disabled") ;
352- useCache = false ;
352+ trace . WriteLine ( $ "Git version { git . Version . OriginalString } does not support 'git config --list --type'; config cache disabled") ;
353+ _useConfigTypeCache = false ;
354+ }
355+ else
356+ {
357+ _useConfigTypeCache = useCache ;
353358 }
354359
355- _useCache = useCache ;
356- _cache = useCache ? new Dictionary < GitConfigurationType , ConfigCache > ( ) : null ;
360+ if ( useCache && ! SupportsConfigListRaw ( git ) )
361+ {
362+ trace . WriteLine ( $ "Git version { git . Version . OriginalString } does not support 'git config list --type'; config cache disabled") ;
363+ _useConfigRawCache = false ;
364+ }
365+ else
366+ {
367+ _useConfigRawCache = useCache ;
368+ }
369+ _cache = _useConfigTypeCache || _useConfigRawCache ? new Dictionary < GitConfigurationType , ConfigCache > ( ) : null ;
357370 }
358371
359372 private static bool SupportsConfigListType ( GitProcess git )
@@ -381,10 +394,20 @@ private static bool SupportsConfigListType(GitProcess git)
381394 return false ;
382395 }
383396
397+ private static bool SupportsConfigListRaw ( GitProcess git )
398+ {
399+ return git . Version >= ConfigListRawMinVersion ;
400+ }
401+
402+ private bool UseConfigCache ( GitConfigurationType type )
403+ {
404+ return type != GitConfigurationType . Raw ? _useConfigTypeCache : _useConfigRawCache ;
405+ }
406+
384407 private void EnsureCacheLoaded ( GitConfigurationType type )
385408 {
386409 ConfigCache cache ;
387- if ( ! _useCache || ( _cache . TryGetValue ( type , out cache ) && cache . IsLoaded ) )
410+ if ( ! UseConfigCache ( type ) || ( _cache . TryGetValue ( type , out cache ) && cache . IsLoaded ) )
388411 {
389412 return ;
390413 }
@@ -400,7 +423,7 @@ private void EnsureCacheLoaded(GitConfigurationType type)
400423 switch ( type )
401424 {
402425 case GitConfigurationType . Raw :
403- typeArg = "--no-type " ;
426+ typeArg = "" ;
404427 break ;
405428
406429 case GitConfigurationType . Path :
@@ -415,7 +438,8 @@ private void EnsureCacheLoaded(GitConfigurationType type)
415438 return ;
416439 }
417440
418- using ( ChildProcess git = _git . CreateProcess ( $ "config list --show-scope -z { typeArg } ") )
441+ string argList = ( _git . Version >= ConfigListSubcommandMinVersion ) ? "list" : "--list" ;
442+ using ( ChildProcess git = _git . CreateProcess ( $ "config { argList } --show-scope -z { typeArg } ") )
419443 {
420444 git . Start ( Trace2ProcessClass . Git ) ;
421445 // To avoid deadlocks, always read the output stream first and then wait
@@ -437,7 +461,7 @@ private void EnsureCacheLoaded(GitConfigurationType type)
437461
438462 private void InvalidateCache ( )
439463 {
440- if ( _useCache )
464+ if ( _useConfigTypeCache || _useConfigRawCache )
441465 {
442466 foreach ( ConfigCache cache in _cache . Values )
443467 {
@@ -448,7 +472,7 @@ private void InvalidateCache()
448472
449473 public void Enumerate ( GitConfigurationLevel level , GitConfigurationEnumerationCallback cb )
450474 {
451- if ( _useCache )
475+ if ( _useConfigRawCache )
452476 {
453477 EnsureCacheLoaded ( GitConfigurationType . Raw ) ;
454478
@@ -463,7 +487,7 @@ public void Enumerate(GitConfigurationLevel level, GitConfigurationEnumerationCa
463487
464488 // Fall back to original implementation
465489 string levelArg = GetLevelFilterArg ( level ) ;
466- using ( ChildProcess git = _git . CreateProcess ( $ "config --null { levelArg } --list") )
490+ using ( ChildProcess git = _git . CreateProcess ( $ "config --null { levelArg } --includes -- list") )
467491 {
468492 git . Start ( Trace2ProcessClass . Git ) ;
469493 // To avoid deadlocks, always read the output stream first and then wait
@@ -530,7 +554,7 @@ public void Enumerate(GitConfigurationLevel level, GitConfigurationEnumerationCa
530554
531555 public bool TryGet ( GitConfigurationLevel level , GitConfigurationType type , string name , out string value )
532556 {
533- if ( _useCache )
557+ if ( UseConfigCache ( type ) )
534558 {
535559 EnsureCacheLoaded ( type ) ;
536560
@@ -545,7 +569,7 @@ public bool TryGet(GitConfigurationLevel level, GitConfigurationType type, strin
545569 // Fall back to individual git config command if cache not available
546570 string levelArg = GetLevelFilterArg ( level ) ;
547571 string typeArg = GetCanonicalizeTypeArg ( type ) ;
548- using ( ChildProcess git = _git . CreateProcess ( $ "config --null { levelArg } { typeArg } { QuoteCmdArg ( name ) } ") )
572+ using ( ChildProcess git = _git . CreateProcess ( $ "config --null { levelArg } --includes { typeArg } { QuoteCmdArg ( name ) } ") )
549573 {
550574 git . Start ( Trace2ProcessClass . Git ) ;
551575 // To avoid deadlocks, always read the output stream first and then wait
@@ -647,7 +671,7 @@ public void Unset(GitConfigurationLevel level, string name)
647671
648672 public IEnumerable < string > GetAll ( GitConfigurationLevel level , GitConfigurationType type , string name )
649673 {
650- if ( _useCache )
674+ if ( UseConfigCache ( type ) )
651675 {
652676 EnsureCacheLoaded ( type ) ;
653677
@@ -667,7 +691,7 @@ public IEnumerable<string> GetAll(GitConfigurationLevel level, GitConfigurationT
667691 string levelArg = GetLevelFilterArg ( level ) ;
668692 string typeArg = GetCanonicalizeTypeArg ( type ) ;
669693
670- var gitArgs = $ "config --null { levelArg } { typeArg } --get-all { QuoteCmdArg ( name ) } ";
694+ var gitArgs = $ "config --null { levelArg } --includes { typeArg } --get-all { QuoteCmdArg ( name ) } ";
671695
672696 using ( ChildProcess git = _git . CreateProcess ( gitArgs ) )
673697 {
@@ -705,7 +729,7 @@ public IEnumerable<string> GetRegex(GitConfigurationLevel level, GitConfiguratio
705729 string levelArg = GetLevelFilterArg ( level ) ;
706730 string typeArg = GetCanonicalizeTypeArg ( type ) ;
707731
708- var gitArgs = $ "config --null { levelArg } { typeArg } --get-regex { QuoteCmdArg ( nameRegex ) } ";
732+ var gitArgs = $ "config --null { levelArg } --includes { typeArg } --get-regex { QuoteCmdArg ( nameRegex ) } ";
709733 if ( valueRegex != null )
710734 {
711735 gitArgs += $ " { QuoteCmdArg ( valueRegex ) } ";
0 commit comments