8000 Eagerly load the Configuration if any config path is provided · ben/libgit2sharp@2511fab · GitHub
[go: up one dir, main page]

Skip to content

Commit 2511fab

Browse files
committed
Eagerly load the Configuration if any config path is provided
Fix libgit2#277
1 parent 9cb9c59 commit 2511fab

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23
using System.Linq;
34
using System.Text;
45
using LibGit2Sharp.Tests.TestHelpers;
@@ -388,5 +389,71 @@ public void ComparingTwoNullTreesReturnsAnEmptyTreeChanges()
388389
Assert.Equal(0, changes.Count());
389390
}
390391
}
392+
393+
[Fact]
394+
public void ComparingReliesOnProvidedConfigEntriesIfAny()
395+
{
396+
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
397+
398+
const string file = "1/branch_file.txt";
399+
400+
using (var repo = new Repository(path.DirectoryPath))
401+
{
402+
TreeEntry entry = repo.Head[file];
403+
Assert.Equal(Mode.ExecutableFile, entry.Mode);
404+
405+
// Recreate the file in the workdir without the executable bit
406+
string fullpath = Path.Combine(repo.Info.WorkingDirectory, file);
407+
File.Delete(fullpath);
408+
File.WriteAllBytes(fullpath, ((Blob)(entry.Target)).Content);
409+
410+
// Unset the local core.filemode, if any.
411+
repo.Config.Unset("core.filemode", ConfigurationLevel.Local);
412+
}
413+
414+
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
415+
416+
var options = BuildFakeSystemConfigFilemodeOption(scd, true);
417+
418+
using (var repo = new Repository(path.DirectoryPath, options))
419+
{
420+
TreeChanges changes = repo.Diff.Compare(new []{ file });
421+
422+
Assert.Equal(1, changes.Count());
423+
424+
var change = changes.Modified.Single();
425+
Assert.Equal(Mode.ExecutableFile, change.OldMode);
426+
Assert.Equal(Mode.NonExecutableFile, change.Mode);
427+
}
428+
429+
options = BuildFakeSystemConfigFilemodeOption(scd, false);
430+
431+
using (var repo = new Repository(path.DirectoryPath, options))
432+
{
433+
TreeChanges changes = repo.Diff.Compare(new[] { file });
434+
435+
Assert.Equal(0, changes.Count());
436+
}
437+
}
438+
439+
private RepositoryOptions BuildFakeSystemConfigFilemodeOption(
440+
SelfCleaningDirectory scd,
441+
bool value)
442+
{
443+
Directory.CreateDirectory(scd.DirectoryPath);
444+
445+
var options = new RepositoryOptions
446+
{
447+
SystemConfigurationLocation = Path.Combine(
448+
scd.RootedDirectoryPath, "fake-system.config")
449+
};
450+
451+
StringBuilder sb = new StringBuilder()
452+
.AppendFormat("[core]{0}", Environment.NewLine)
453+
.AppendFormat("filemode = {1}{0}", Environment.NewLine, value);
454+
File.WriteAllText(options.SystemConfigurationLocation, sb.ToString());
455+
456+
return options;
457+
}
391458
}
392459
}

LibGit2Sharp/Repository.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,31 @@ public Repository(string path, RepositoryOptions options = null)
102102
odb = new Lazy<ObjectDatabase>(() => new ObjectDatabase(this));
103103
diff = new Diff(this);
104104
notes = new NoteCollection(this);
105+
106+
EagerlyLoadTheConfigIfAnyPathHaveBeenPassed(options);
107+
}
108+
109+
private void EagerlyLoadTheConfigIfAnyPathHaveBeenPassed(RepositoryOptions options)
110+
{
111+
if (options == null)
112+
{
113+
return;
114+
}
115+
116+
if (options.GlobalConfigurationLocation == null &&
117+
options.XdgConfigurationLocation == null &&
118+
options.SystemConfigurationLocation == null)
119+
{
120+
return;
121+
}
122+
123+
// Dirty hack to force the eager load of the configuration
124+
// without Resharper pestering about useless code
125+
126+
if (!Config.HasConfig(ConfigurationLevel.Local))
127+
{
128+
throw new InvalidOperationException("Unexpected state.");
129+
}
105130
}
106131< 341A code class="diff-text syntax-highlighted-line">

107132
/// <summary>

0 commit comments

Comments
 (0)
0