Skip to content

Ignore Case via Repository.PathComparer #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions LibGit2Sharp.Tests/BranchFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
Expand Down Expand Up @@ -183,7 +184,7 @@ public void CanListAllBranches()
{
using (var repo = new Repository(BareTestRepoPath))
{
Assert.Equal(expectedBranches, repo.Branches.Select(b => b.Name).ToArray());
Assert.Equal(expectedBranches, SortedBranches(repo.Branches, b => b.Name));

Assert.Equal(5, repo.Branches.Count());
}
Expand All @@ -204,9 +205,8 @@ public void CanListBranchesWithRemoteAndLocalBranchWithSameShortName()
"diff-test-cases", "i-do-numbers", "logo", "master", "origin/master", "track-local",
};

Assert.Equal(expectedWdBranches, repo.Branches
.Where(b => !b.IsRemote)
.Select(b => b.Name).ToArray());
Assert.Equal(expectedWdBranches,
SortedBranches(repo.Branches.Where(b => !b.IsRemote), b => b.Name));
}
}

Expand All @@ -222,7 +222,7 @@ public void CanListAllBranchesWhenGivenWorkingDir()
"origin/test"
};

Assert.Equal(expectedWdBranches, repo.Branches.Select(b => b.Name).ToArray());
Assert.Equal(expectedWdBranches, SortedBranches(repo.Branches, b => b.Name));
}
}

Expand All @@ -244,7 +244,8 @@ public void CanListAllBranchesIncludingRemoteRefs()
new { Name = "origin/packed-test", Sha = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", IsRemote = true },
new { Name = "origin/test", Sha = "e90810b8df3e80c413d903f631643c716887138d", IsRemote = true },
};
Assert.Equal(expectedBranchesIncludingRemoteRefs, repo.Branches.Select(b => new { b.Name, b.Tip.Sha, b.IsRemote }).ToArray());
Assert.Equal(expectedBranchesIncludingRemoteRefs,
SortedBranches(repo.Branches, b => new { b.Name, b.Tip.Sha, b.IsRemote }));
}
}

Expand Down Expand Up @@ -779,5 +780,10 @@ public void RemoteBranchesDoNotTrackAnything()
}
}
}

private static T[] SortedBranches<T>(IEnumerable<Branch> branches, Func<Branch, T> selector)
{
return branches.OrderBy(b => b.CanonicalName, StringComparer.Ordinal).Select(selector).ToArray();
}
}
}
5 changes: 4 additions & 1 deletion LibGit2Sharp.Tests/CommitFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,10 @@ private static void CanEnumerateCommitsFromATag(Func<Tag, object> transformer)
public void CanEnumerateAllCommits()
{
AssertEnumerationOfCommits(
repo => new Filter { Since = repo.Refs },
repo => new Filter
{
Since = repo.Refs.OrderBy(r => r.CanonicalName, StringComparer.Ordinal),
},
new[]
{
"44d5d18", "bb65291", "532740a", "503a16f", "3dfd6fd",
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void CanCompareACommitTreeAgainstATreeWithNoCommonAncestor()

Assert.Equal("readme.txt", changes.Deleted.Single().Path);
Assert.Equal(new[] { "1.txt", subBranchFilePath, "README", "branch_file.txt", "deleted_staged_file.txt", "deleted_unstaged_file.txt", "modified_staged_file.txt", "modified_unstaged_file.txt", "new.txt" },
changes.Added.Select(x => x.Path));
changes.Added.Select(x => x.Path).OrderBy(p => p, StringComparer.Ordinal).ToArray());

Assert.Equal(9, changes.LinesAdded);
Assert.Equal(2, changes.LinesDeleted);
Expand Down
3 changes: 2 additions & 1 deletion LibGit2Sharp.Tests/IndexFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public void CanEnumerateIndex()
{
using (var repo = new Repository(StandardTestRepoPath))
{
Assert.Equal(expectedEntries, repo.Index.Select(e => e.Path).ToArray());
Assert.Equal(expectedEntries,
repo.Index.Select(e => e.Path).OrderBy(p => p, StringComparer.Ordinal).ToArray());
}
}

Expand Down
1 change: 1 addition & 0 deletions LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml" />
<Reference Include="xunit">
<HintPath>..\Lib\xUnit\xunit.dll</HintPath>
</Reference>
Expand Down
31 changes: 21 additions & 10 deletions LibGit2Sharp.Tests/NoteFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;
Expand Down Expand Up @@ -61,19 +62,19 @@ public void CanRetrieveNotesFromAGitObject()
var notes = repo.Notes[new ObjectId("4a202b346bb0fb0db7eff3cffeb3c70babbd2045")];

Assert.NotNull(notes);
Assert.Equal(3, notes.Count());
Assert.Equal(expectedMessages, notes.Select(n => n.Message));
Assert.Equal(expectedMessages, SortedNotes(notes, n => n.Message));
}
}

[Fact]
public void CanGetListOfNotesNamespaces()
{
var expectedNamespaces = new[] { "commits", "answer", "answer2" };
var expectedNamespaces = new[] { "answer", "answer2", "commits", };

using (var repo = new Repository(BareTestRepoPath))
{
Assert.Equal(expectedNamespaces, repo.Notes.Namespaces);
Assert.Equal(expectedNamespaces,
repo.Notes.Namespaces.OrderBy(n => n, StringComparer.Ordinal).ToArray());
Assert.Equal(repo.Notes.DefaultNamespace, repo.Notes.Namespaces.First());
}
}
Expand Down Expand Up @@ -105,12 +106,12 @@ public void CanAccessNotesFromACommit()
{
var commit = repo.Lookup<Commit>("4a202b346bb0fb0db7eff3cffeb3c70babbd2045");

Assert.Equal(expectedNamespaces, commit.Notes.Select(n => n.Message));
Assert.Equal(expectedNamespaces, SortedNotes(commit.Notes, n => n.Message));

// Make sure that Commit.Notes is not refreshed automatically
repo.Notes.Add(commit.Id, "I'm batman!\n", signatureNullToken, signatureYorah, "batmobile");

Assert.Equal(expectedNamespaces, commit.Notes.Select(n => n.Message));
Assert.Equal(expectedNamespaces, SortedNotes(commit.Notes, m => m.Message));
}
}

Expand Down Expand Up @@ -228,16 +229,26 @@ public void RemovingANonExistingNoteDoesntThrow()
[Fact]
public void CanRetrieveTheListOfNotesForAGivenNamespace()
{
var expectedNotes = new[] { new Tuple<string, string>("1a550e416326cdb4a8e127a04dd69d7a01b11cf4", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"),
new Tuple<string, string>("272a41cf2b22e57f2bc5bf6ef37b63568cd837e4", "8496071c1b46c854b31185ea97743be6a8774479") };
var expectedNotes = new[]
{
new { Blob = "272a41cf2b22e57f2bc5bf6ef37b63568cd837e4", Target = "8496071c1b46c854b31185ea97743be6a8774479" },
new { Blob = "1a550e416326cdb4a8e127a04dd69d7a01b11cf4", Target = "4a202b346bb0fb0db7eff3cffeb3c70babbd2045" },
};

using (var repo = new Repository(BareTestRepoPath))
{
Assert.Equal(expectedNotes, repo.Notes["commits"].Select(n => new Tuple<string, string>(n.BlobId.Sha, n.TargetObjectId.Sha)).ToArray());
Assert.Equal(expectedNotes,
SortedNotes(repo.Notes["commits"], n => new { Blob = n.BlobId.Sha, Target = n.TargetObjectId.Sha }));

Assert.Equal("commits", repo.Notes.DefaultNamespace);
Assert.Equal(expectedNotes, repo.Notes.Select(n => new Tuple<string, string>(n.BlobId.Sha, n.TargetObjectId.Sha)).ToArray());
Assert.Equal(expectedNotes,
SortedNotes(repo.Notes, n => new { Blob = n.BlobId.Sha, Target = n.TargetObjectId.Sha }));
}
}

private static T[] SortedNotes<T>(IEnumerable<Note> notes, Func<Note, T> selector)
{
return notes.OrderBy(n => n.Message, StringComparer.Ordinal).Select(selector).ToArray();
}
}
}
7 changes: 6 additions & 1 deletion LibGit2Sharp.Tests/ReferenceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public void CanListAllReferencesEvenCorruptedOnes()
{
CreateCorruptedDeadBeefHead(repo.Info.Path);

Assert.Equal(expectedRefs, repo.Refs.Select(r => r.CanonicalName).ToArray());
Assert.Equal(expectedRefs, SortedRefs(repo, r => r.CanonicalName));

Assert.Equal(13, repo.Refs.Count());
}
Expand Down Expand Up @@ -685,5 +685,10 @@ public void CanTellIfAReferenceIsValid(string refname, bool expectedResult)
Assert.Equal(expectedResult, repo.Refs.IsValidName(refname));
}
}

private static T[] SortedRefs<T>(IRepository repo, Func<Reference, T> selector)
{
return repo.Refs.OrderBy(r => r.CanonicalName, StringComparer.Ordinal).Select(selector).ToArray();
}
}
}
1 change: 0 additions & 1 deletion LibGit2Sharp.Tests/ResetHeadFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ public void HardResetUpdatesTheContentOfTheWorkingDirectory()
using (var repo = new Repository(clone.DirectoryPath))
{
var names = new DirectoryInfo(repo.Info.WorkingDirectory).GetFileSystemInfos().Select(fsi => fsi.Name).ToList();
names.Sort(StringComparer.Ordinal);

File.Delete(Path.Combine(repo.Info.WorkingDirectory, "README"));
File.WriteAllText(Path.Combine(repo.Info.WorkingDirectory, "WillNotBeRemoved.txt"), "content\n");
Expand Down
28 changes: 23 additions & 5 deletions LibGit2Sharp.Tests/StageFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,14 @@ public void CanStageANewFileInAPersistentManner()
}
}

[Fact]
public void CanStageANewFileWithAFullPath()
[SkippableTheory]
[InlineData(false)]
[InlineData(true)]
public void CanStageANewFileWithAFullPath(bool ignorecase)
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
SetIgnoreCaseOrSkip(path.RepositoryPath, ignorecase);

using (var repo = new Repository(path.RepositoryPath))
{
int count = repo.Index.Count;
Expand All @@ -133,10 +137,24 @@ public void CanStageANewFileWithAFullPath()
string fullPath = Path.Combine(repo.Info.WorkingDirectory, filename);
Assert.True(File.Exists(fullPath));

repo.Index.Stage(fullPath);
AssertStage(null, repo, fullPath);
AssertStage(ignorecase, repo, fullPath.ToUpperInvariant());
AssertStage(ignorecase, repo, fullPath.ToLowerInvariant());
}
}

Assert.Equal(count + 1, repo.Index.Count);
Assert.NotNull(repo.Index[filename]);
private static void AssertStage(bool? ignorecase, IRepository repo, string path)
{
try
{
repo.Index.Stage(path);
Assert.Equal(FileStatus.Added, repo.Index.RetrieveStatus(path));
repo.Reset();
Assert.Equal(FileStatus.Untracked, repo.Index.RetrieveStatus(path));
}
catch (ArgumentException)
{
Assert.False(ignorecase ?? true);
}
}

Expand Down
15 changes: 15 additions & 0 deletions LibGit2Sharp.Tests/StatusFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,21 @@ FileStatus expectedCamelCasedFileStatus

Assert.Equal(expectedlowerCasedFileStatus, repo.Index.RetrieveStatus("plop"));
Assert.Equal(expectedCamelCasedFileStatus, repo.Index.RetrieveStatus("Plop"));

AssertStatus(shouldIgnoreCase, expectedlowerCasedFileStatus, repo, camelCasedPath.ToLowerInvariant());
AssertStatus(shouldIgnoreCase, expectedCamelCasedFileStatus, repo, camelCasedPath.ToUpperInvariant());
}
}

private static void AssertStatus(bool shouldIgnoreCase, FileStatus expectedFileStatus, IRepository repo, string path)
{
try
{
Assert.Equal(expectedFileStatus, repo.Index.RetrieveStatus(path));
}
catch (ArgumentException)
{
Assert.False(shouldIgnoreCase);
}
}
}
Expand Down
22 changes: 6 additions & 16 deletions LibGit2Sharp.Tests/TagFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ public void CanListTags()
{
using (var repo = new Repository(BareTestRepoPath))
{
Assert.Equal(expectedTags, repo.Tags.Select(t => t.Name).ToArray());
Assert.Equal(expectedTags, SortedTags(repo.Tags, t => t.Name));

Assert.Equal(4, repo.Tags.Count());
}
Expand All @@ -608,21 +608,6 @@ public void CanListAllTagsInAEmptyRepository()
}
}

[Fact]
// Ported from cgit (https://github.com/git/git/blob/1c08bf50cfcf924094eca56c2486a90e2bf1e6e2/t/t7004-tag.sh#L165)
public void ListAllTagsShouldOutputThemInAnOrderedWay()
{
using (var repo = new Repository(BareTestRepoPath))
{
List<string> tagNames = repo.Tags.Select(t => t.Name).ToList();

List<string> sortedTags = expectedTags.ToList();
sortedTags.Sort();

Assert.Equal(sortedTags, tagNames);
}
}

[Fact]
public void CanLookupALightweightTag()
{
Expand Down Expand Up @@ -693,5 +678,10 @@ public void LookupNullTagNameThrows()
Assert.Throws<ArgumentNullException>(() => { Tag t = repo.Tags[null]; });
}
}

private static T[] SortedTags<T>(IEnumerable<Tag> tags, Func<Tag, T> selector)
{
return tags.OrderBy(t => t.CanonicalName, StringComparer.Ordinal).Select(selector).ToArray();
}
}
}
12 changes: 12 additions & 0 deletions LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ protected static void AssertValueInConfigFile(string configFilePath, string rege
Assert.True(r.Success, text);
}

protected static void SetIgnoreCaseOrSkip(string path, bool ignorecase)
{
var canIgnoreCase = Directory.Exists(path.ToUpperInvariant()) && Directory.Exists(path.ToLowerInvariant());
if (!canIgnoreCase && ignorecase)
throw new SkipException("Skipping 'ignorecase = true' test on case-sensitive file system.");

using (var repo = new Repository(path))
{
repo.Config.Set("core.ignorecase", ignorecase);
}
}

public RepositoryOptions BuildFakeConfigs(SelfCleaningDirectory scd)
{
var options = BuildFakeRepositoryOptions(scd);
Expand Down
Loading