Skip to content

Commit 7468fa8

Browse files
authored
fix(config): enable file include in scoped queries (#2345)
Avoid duplication of entries already present in included files: - [x] enable config cache for `Raw` values in all Git versions supporting `--show-scope` - [x] consistent _include_ of files in Git config queries with [scope limit](https://git-scm.de/docs/git-config#Documentation/git-config.txt---includes) - [x] add test for file includes Improves consistency regarding _include_ behavior and Fixes #2342 Fixes #2328 Fixes #1696 Fixes #1337
2 parents e8ce762 + 228be3d commit 7468fa8

2 files changed

Lines changed: 84 additions & 18 deletions

File tree

‎src/Core.Tests/GitConfigurationTests.cs‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,5 +656,47 @@ public void GitConfiguration_TypedQuery_CanonicalizesValues()
656656
// Value should be canonicalized path, not raw "~/example"
657657
Assert.NotEqual("~/example", value);
658658
}
659+
660+
[Fact]
661+
public void GitConfiguration_ScopedConfig_ProcessesIncludes()
662+
{
663+
const string configKey = "test.value";
664+
const string configTrigger = "remote.*.url:http://localhost/**";
665+
const string configValue = "foo123";
666+
string repoPath = CreateRepository(out string workDirPath);
667+
668+
string gitGlobal = System.Environment.GetEnvironmentVariable("GIT_CONFIG_GLOBAL");
669+
670+
try {
671+
System.Environment.SetEnvironmentVariable("GIT_CONFIG_GLOBAL", repoPath + "/global.config");
672+
673+
ExecGit(repoPath, workDirPath, $"config --file {workDirPath}/git.config.glob {configKey} {configValue}").AssertSuccess();
674+
ExecGit(repoPath, workDirPath, $"config --global includeif.hasconfig:{configTrigger}.path {workDirPath}/git.config.glob").AssertSuccess();
675+
676+
ExecGit(repoPath, workDirPath, $"config --file {workDirPath}/git.config.loc {configKey} {configValue}").AssertSuccess();
677+
ExecGit(repoPath, workDirPath, $"config --local include.path {workDirPath}/git.config.loc").AssertSuccess();
678+
679+
ExecGit(repoPath, workDirPath, $"config --local remote.test.url http://localhost/my/repo").AssertSuccess();
680+
681+
string gitPath = GetGitPath();
682+
var trace = new NullTrace();
683+
var trace2 = new NullTrace2();
684+
var processManager = new TestProcessManager();
685+
686+
var git = new GitProcess(trace, trace2, processManager, gitPath, repoPath);
687+
IGitConfiguration config = git.GetConfiguration();
688+
689+
// value of included file set in scopes
690+
foreach (var scope in new GitConfigurationLevel[] { GitConfigurationLevel.Global, GitConfigurationLevel.Local })
691+
{
692+
Assert.True(config.TryGet(scope, GitConfigurationType.Raw, configKey, out string value));
693+
Assert.Equal(configValue, value);
694+
}
695+
}
696+
finally
697+
{
698+
System.Environment.SetEnvironmentVariable("GIT_CONFIG_GLOBAL", gitGlobal);
699+
}
700+
}
659701
}
660702
}

‎src/Core/GitConfiguration.cs‎

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics;
4-
using System.Linq;
53
using System.Text;
64

75
namespace 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

Comments
 (0)