8000 Merge branch 'cmn/treedef-tests' · libgit2/libgit2sharp@185009f · GitHub
[go: up one dir, main page]

Skip to content

Commit 185009f

Browse files
committed
Merge branch 'cmn/treedef-tests'
2 parents 9a81748 + a8e9fc3 commit 185009f

20 files changed

+146
-87
lines changed

LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace LibGit2Sharp.Tests
1010
{
1111
public class DiffTreeToTreeFixture : BaseFixture
1212
{
13-
private static readonly string subBranchFilePath = Path.Combine("1", "branch_file.txt");
13+
private static readonly string subBranchFilePath = String.Join("/", "1", "branch_file.txt");
1414

1515
[Fact]
1616
public void ComparingATreeAgainstItselfReturnsNoDifference()

LibGit2Sharp.Tests/FileHistoryFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public void CanTellComplexCommitHistory()
151151
var commit2 = MakeAndCommitChange(repo, repoPath, path1, "Hello World again");
152152

153153
// Move the first file to a new directory.
154-
var newPath1 = Path.Combine(SubFolderPath1, path1);
154+
var newPath1 = Path.Combine(SubFolderPath1, path1).Replace(@"\", "/");
155155
Commands.Move(repo, path1, newPath1);
156156
var commit3 = repo.Commit("Moved " + path1 + " to " + newPath1,
157157
Constants.Signature, Constants.Signature);

LibGit2Sharp.Tests/IgnoreFixture.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public void CanCheckIfAPathIsIgnoredUsingThePreferedPlatformDirectorySeparatorCh
7979

8080
Assert.False(repo.Ignore.IsPathIgnored("File.txt"));
8181
Assert.True(repo.Ignore.IsPathIgnored("NewFolder"));
82-
Assert.True(repo.Ignore.IsPathIgnored(string.Format(@"NewFolder{0}NewFolder", Path.DirectorySeparatorChar)));
83-
Assert.True(repo.Ignore.IsPathIgnored(string.Format(@"NewFolder{0}NewFolder{0}File.txt", Path.DirectorySeparatorChar)));
82+
Assert.True(repo.Ignore.IsPathIgnored(string.Join("/", "NewFolder", "NewFolder")));
83+
Assert.True(repo.Ignore.IsPathIgnored(string.Join("/", "NewFolder", "NewFolder", "File.txt")));
8484
}
8585
}
8686

@@ -90,21 +90,19 @@ public void HonorDeeplyNestedGitIgnoreFile()
9090
string path = InitNewRepository();
9191
using (var repo = new Repository(path))
9292
{
93-
char pd = Path.DirectorySeparatorChar;
94-
95-
var gitIgnoreFile = string.Format("deeply{0}nested{0}.gitignore", pd);
93+
var gitIgnoreFile = string.Join("/", "deeply", "nested", ".gitignore");
9694
Touch(repo.Info.WorkingDirectory, gitIgnoreFile, "SmtCounters.h");
9795

9896
Commands.Stage(repo, gitIgnoreFile);
9997
repo.Commit("Add .gitignore", Constants.Signature, Constants.Signature);
10098

10199
Assert.False(repo.RetrieveStatus().IsDirty);
102100

103-
var ignoredFile = string.Format("deeply{0}nested{0}SmtCounters.h", pd);
101+
var ignoredFile = string.Join("/", "deeply", "nested", "SmtCounters.h");
104102
Touch(repo.Info.WorkingDirectory, ignoredFile, "Content");
105103
Assert.False(repo.RetrieveStatus().IsDirty);
106104

107-
var file = string.Format("deeply{0}nested{0}file.txt", pd);
105+
var file = string.Join("/", "deeply", "nested", "file.txt");
108106
Touch(repo.Info.WorkingDirectory, file, "Yeah!");
109107

110108
var repositoryStatus = repo.RetrieveStatus(new StatusOptions { IncludeIgnored = true });

LibGit2Sharp.Tests/IndexFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace LibGit2Sharp.Tests
1010
{
1111
public class IndexFixture : BaseFixture
1212
{
13-
private static readonly string subBranchFile = Path.Combine("1", "branch_file.txt");
13+
private static readonly string subBranchFile = string.Join("/", "1", "branch_file.txt");
1414
private readonly string[] expectedEntries = new[]
1515
{
1616
"1.txt",
@@ -208,7 +208,7 @@ private void InvalidMoveUseCases(string sourcePath, FileStatus sourceStatus, IEn
208208
public void PathsOfIndexEntriesAreExpressedInNativeFormat()
209209
{
210210
// Build relative path
211-
string relFilePath = Path.Combine("directory", "Testfile.txt");
211+
string relFilePath = Path.Combine("directory", "Testfile.txt").Replace('\\', '/');
212212

213213
string repoPath = InitNewRepository();
214214

LibGit2Sharp.Tests/ResetIndexFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void CanResetTheIndexToTheContentOfACommitWithCommitAsArgument()
7979

8080
RepositoryStatus newStatus = repo.RetrieveStatus();
8181

82-
var expected = new[] { "1.txt", Path.Combine("1", "branch_file.txt"), "deleted_staged_file.txt",
82+
var expected = new[] { "1.txt", string.Join("/", "1", "branch_file.txt"), "deleted_staged_file.txt",
8383
"deleted_unstaged_file.txt", "modified_staged_file.txt", "modified_unstaged_file.txt" };
8484

8585
Assert.Equal(expected.Length, newStatus.Where(IsStaged).Count());

LibGit2Sharp.Tests/StageFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public void CanStageANewFileWithARelativePathContainingNativeDirectorySeparatorC
240240

241241
const string posixifiedPath = "Project/a_file.txt";
242242
Assert.NotNull(repo.Index[posixifiedPath]);
243-
Assert.Equal(file, repo.Index[posixifiedPath].Path);
243+
Assert.Equal(posixifiedPath, repo.Index[posixifiedPath].Path);
244244
}
245245
}
246246

LibGit2Sharp.Tests/StatusFixture.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public void CanRetrieveTheStatusOfANewRepository(bool includeUnaltered)
268268
}
269269

270270
[Fact]
271-
public void RetrievingTheStatusOfARepositoryReturnNativeFilePaths()
271+
public void RetrievingTheStatusOfARepositoryReturnsGitPaths()
272272
{
273273
// Build relative path
274274
string relFilePath = Path.Combine("directory", "Testfile.txt");
@@ -289,7 +289,7 @@ public void RetrievingTheStatusOfARepositoryReturnNativeFilePaths()
289289
Assert.Equal(1, repoStatus.Count());
290290
StatusEntry statusEntry = repoStatus.Single();
291291

292-
Assert.Equal(relFilePath, statusEntry.FilePath);
292+
Assert.Equal(relFilePath.Replace('\\', '/'), statusEntry.FilePath);
293293

294294
Assert.Equal(statusEntry.FilePath, repoStatus.Added.Select(s => s.FilePath).Single());
295295
}
@@ -383,6 +383,7 @@ public void RetrievingTheStatusOfTheRepositoryHonorsTheGitIgnoreDirectives()
383383

384384
RepositoryStatus status = repo.RetrieveStatus();
385385

386+
relativePath = relativePath.Replace('\\', '/');
386387
Assert.Equal(new[] { relativePath, "new_untracked_file.txt" }, status.Untracked.Select(s => s.FilePath));
387388

388389
Touch(repo.Info.WorkingDirectory, ".gitignore", "*.txt" + Environment.NewLine);
@@ -488,8 +489,6 @@ private static void AssertStatus(bool shouldIgnoreCase, FileStatus expectedFileS
488489
[Fact]
489490
public void RetrievingTheStatusOfTheRepositoryHonorsTheGitIgnoreDirectivesThroughoutDirectories()
490491
{
491-
char dirSep = Path.DirectorySeparatorChar;
492-
493492
string path = SandboxStandardTestRepo();
494493
using (var repo = new Repository(path))
495494
{
@@ -503,7 +502,7 @@ public void RetrievingTheStatusOfTheRepositoryHonorsTheGitIgnoreDirectivesThroug
503502
Assert.Equal(FileStatus.Ignored, repo.RetrieveStatus("bin/what-about-me.txt"));
504503

505504
RepositoryStatus newStatus = repo.RetrieveStatus(new StatusOptions { IncludeIgnored = true });
506-
Assert.Equal(new[] { "bin" + dirSep }, newStatus.Ignored.Select(s => s.FilePath));
505+
Assert.Equal(new[] { "bin/" }, newStatus.Ignored.Select(s => s.FilePath));
507506

508507
var sb = new StringBuilder();
509508
sb.AppendLine("bin/*");
@@ -515,8 +514,8 @@ public void RetrievingTheStatusOfTheRepositoryHonorsTheGitIgnoreDirectivesThroug
515514

516515
newStatus = repo.RetrieveStatus(new StatusOptions { IncludeIgnored = true });
517516

518-
Assert.Equal(new[] { "bin" + dirSep + "look-ma.txt" }, newStatus.Ignored.Select(s => s.FilePath));
519-
Assert.True(newStatus.Untracked.Select(s => s.FilePath).Contains("bin" + dirSep + "what-about-me.txt"));
517+
Assert.Equal(new[] { "bin/look-ma.txt" }, newStatus.Ignored.Select(s => s.FilePath));
518+
Assert.True(newStatus.Untracked.Select(s => s.FilePath).Contains("bin/what-about-me.txt"));
520519
}
521520
}
522521

@@ -630,7 +629,7 @@ public void CanIncludeStatusOfUnalteredFiles()
630629
var path = SandboxStandardTestRepo();
631630
string[] unalteredPaths = {
632631
"1.txt",
633-
"1" + Path.DirectorySeparatorChar + "branch_file.txt",
632+
"1/branch_file.txt",
634633
"branch_file.txt",
635634
"new.txt",
636635
"README",

LibGit2Sharp.Tests/SubmoduleFixture.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,10 @@ public void CanEnumerateRepositorySubmodules()
133133
}
134134

135135
[Theory]
136-
[InlineData("sm_changed_head", false)]
137-
[InlineData("sm_changed_head", true)]
138-
public void CanStageChangeInSubmoduleViaIndexStage(string submodulePath, bool appendPathSeparator)
136+
[InlineData("sm_changed_head")]
137+
[InlineData("sm_changed_head/")]
138+
public void CanStageChangeInSubmoduleViaIndexStage(string submodulePath)
139139
{
140-
submodulePath += appendPathSeparator ? Path.DirectorySeparatorChar : default(char?);
141-
142140
var path = SandboxSubmoduleTestRepo();
143141
using (var repo = new Repository(path))
144142
{
@@ -156,12 +154,10 @@ public void CanStageChangeInSubmoduleViaIndexStage(string submodulePath, bool ap
156154
}
157155

158156
[Theory]
159-
[InlineData("sm_changed_head", false)]
160-
[InlineData("sm_changed_head", true)]
161-
public void CanStageChangeInSubmoduleViaIndexStageWithOtherPaths(string submodulePath, bool appendPathSeparator)
157+
[InlineData("sm_changed_head")]
158+
[InlineData("sm_changed_head/")]
159+
public void CanStageChangeInSubmoduleViaIndexStageWithOtherPaths(string submodulePath)
162160
{
163-
submodulePath += appendPathSeparator ? Path.DirectorySeparatorChar : default(char?);
164-
165161
var path = SandboxSubmoduleTestRepo();
166162
using (var repo = new Repository(path))
167163
{

LibGit2Sharp.Tests/TreeDefinitionFixture.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,17 @@ public void CanAddAnExistingGitLinkTreeEntryDefinition()
142142
}
143143
}
144144

145+
private const string StringOf40Chars = "0123456789012345678901234567890123456789";
146+
147+
/// <summary>
148+
/// Used to verify that windows path limitation to 260 chars is not limiting the size of
149+
/// the keys present in the object database.
150+
/// </summary>
151+
private const string StringOf600Chars =
152+
StringOf40Chars + StringOf40Chars + StringOf40Chars + StringOf40Chars + StringOf40Chars
153+
+ StringOf40Chars + StringOf40Chars + StringOf40Chars + StringOf40Chars + StringOf40Chars
154+
+ StringOf40Chars + StringOf40Chars + StringOf40Chars + StringOf40Chars + StringOf40Chars;
155+
145156
[Theory]
146157
[InlineData("README", "README_TOO")]
147158
[InlineData("README", "1/README")]
@@ -152,7 +163,10 @@ public void CanAddAnExistingGitLinkTreeEntryDefinition()
152163
[InlineData("1/branch_file.txt", "1/2/3/another_one.txt")]
153164
[InlineData("1", "2")]
154165
[InlineData("1", "2/3")]
155-
public void CanAddAnExistingTreeEntry(string sourcePath, string targetPath)
166+
[InlineData("1", "C:\\/10")]
167+
[InlineData("1", " : * ? \" < > |")]
168+
[InlineData("1", StringOf600Chars)]
169+
public void CanAddAndRemoveAnExistingTreeEntry(string sourcePath, string targetPath)
156170
{
157171
string path = SandboxBareTestRepo();
158172
using (var repo = new Repository(path))
@@ -168,6 +182,36 @@ public void CanAddAnExistingTreeEntry(string sourcePath, string targetPath)
168182
Assert.NotNull(fetched);
169183

170184
Assert.Equal(te.Target.Id, fetched.TargetId);
185+
186+
// Ensuring that the object database can handle uncommon paths.
187+
var newTree = repo.ObjectDatabase.CreateTree(td);
188+
Assert.Equal(newTree[targetPath].Target.Id, te.Target.Id);
189+
190+
td.Remove(targetPath);
191+
Assert.Null(td[targetPath]);
192+
}
193+
}
194+
195+
[Theory]
196+
[InlineData("C:\\")]
197+
[InlineData(" : * ? \" \n < > |")]
198+
[InlineData("a\\b")]
199+
[InlineData("\\\\b\a")]
200+
[InlineData("éàµ")]
201+
[InlineData(StringOf600Chars)]
202+
public void TreeNamesCanContainCharsForbiddenOnSomeOS(string targetName)
203+
{
204+
string path = SandboxBareTestRepo();
205+
using (var repo = new Repository(path))
206+
{
207+
var pointedItem = repo.Head.Tip.Tree;
208+
209+
var td = new TreeDefinition();
210+
td.Add(targetName, pointedItem);
211+
212+
var newTree = repo.ObjectDatabase.CreateTree(td);
213+
Assert.Equal(newTree[targetName].Target.Sha, pointedItem.Sha);
214+
Assert.Equal(newTree[targetName].Name, targetName);
171215
}
172216
}
173217

LibGit2Sharp.Tests/TreeFixture.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ public void TreeDataIsPresent()
168168
}
169169
}
170170

171+
[Fact]
172+
public void TreeUsesPosixStylePaths()
173+
{
174+
using (var repo = new Repository(BareTestRepoPath))
175+
{
176+
/* From a commit tree */
177+
var commitTree = repo.Lookup<Commit>("4c062a6").Tree;
178+
Assert.NotNull(commitTree["1/branch_file.txt"]);
179+
Assert.Null(commitTree["1\\branch_file.txt"]);
180+
}
181+
}
182+
171183
[Fact]
172184
public void CanRetrieveTreeEntryPath()
173185
{
@@ -180,7 +192,7 @@ public void CanRetrieveTreeEntryPath()
180192
TreeEntry treeTreeEntry = commitTree["1"];
181193
Assert.Equal("1", treeTreeEntry.Path);
182194

183-
string completePath = Path.Combine("1", "branch_file.txt");
195+
string completePath = "1/branch_file.txt";
184196

185197
TreeEntry blobTreeEntry = commitTree["1/branch_file.txt"];
186198
Assert.Equal(completePath, blobTreeEntry.Path);

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private static unsafe extern void giterr_set_str(
8686
private static extern unsafe int git_blame_file(
8787
out git_blame* blame,
8888
git_repository* repo,
89-
[CustomMarshaler(typeof(StrictFilePathMarshaler), typeof(FilePath))] byte* path,
89+
[CustomMarshaler(typeof(StrictUtf8Marshaler), typeof(string))] byte* path,
9090
git_blame_options options);
9191

9292
[DllImport(libgit2)]
@@ -108,7 +108,7 @@ private static extern unsafe int git_blob_create_fromworkdir(
108108
private static extern unsafe int git_blob_create_fromstream(
109109
out IntPtr stream,
110110
git_repository* repositoryPtr,
111-
[CustomMarshaler(typeof(StrictFilePathMarshaler), typeof(FilePath))] byte* hintpath);
111+
[CustomMarshaler(typeof(StrictUtf8Marshaler), typeof(string))] byte* hintpath);
112112

113113
[DllImport(libgit2)]
114114
internal static extern unsafe int git_blob_create_fromstream_commit(
@@ -119,7 +119,7 @@ internal static extern unsafe int git_blob_create_fromstream_commit(
119119
private static extern unsafe int git_blob_filtered_content(
120120
GitBuf buf,
121121
git_object* blob,
122-
[CustomMarshaler(typeof(StrictFilePathMarshaler), typeof(FilePath))] byte* as_path,
122+
[CustomMarshaler(typeof(StrictUtf8Marshaler), typeof(string))] byte* as_path,
123123
[MarshalAs(UnmanagedType.Bool)] bool check_for_binary_data);
124124

125125
[DllImport(libgit2)]
@@ -556,9 +556,9 @@ internal unsafe delegate int git_diff_binary_cb(
556556
[DllImport(libgit2)]
557557
private static extern unsafe int git_diff_blobs(
558558
git_object* oldBlob,
559-
[CustomMarshaler(typeof(StrictFilePathMarshaler), typeof(FilePath))] byte* old_as_path,
559+
[CustomMarshaler(typeof(StrictUtf8Marshaler), typeof(string))] byte* old_as_path,
560560
git_object* newBlob,
561-
[CustomMarshaler(typeof(StrictFilePathMarshaler), typeof(FilePath))] byte* new_as_path,
561+
[CustomMarshaler(typeof(StrictUtf8Marshaler), typeof(string))] byte* new_as_path,
562562
GitDiffOptions options,
563563
git_diff_file_cb fileCallback,
564564
git_diff_binary_cb binaryCallback,
@@ -644,7 +644,7 @@ private static extern unsafe int git_ignore_add_rule(
644644
private static extern unsafe int git_ignore_path_is_ignored(
645645
out int ignored,
646646
git_repository* repo,
647-
[CustomMarshaler(typeof(StrictFilePathMarshaler), typeof(FilePath))] byte* path);
647+
[CustomMarshaler(typeof(StrictUtf8Marshaler), typeof(string))] byte* path);
648648

649649
[DllImport(libgit2)]
650650
private static extern unsafe int git_index_add_bypath(
@@ -662,7 +662,7 @@ private static extern unsafe int git_index_conflict_get(
662662
out git_index_entry* ours,
663663
out git_index_entry* theirs,
664664
git_index* index,
665-
[CustomMarshaler(typeof(StrictFilePathMarshaler), typeof(FilePath))] byte* path);
665+
[CustomMarshaler(typeof(StrictUtf8Marshaler), typeof(string))] byte* path);
666666

667667
[DllImport(libgit2)]
668668
internal static extern unsafe int git_index_conflict_iterator_new(
@@ -695,7 +695,7 @@ internal static extern unsafe void git_index_conflict_iterator_free(
695695
[DllImport(libgit2)]
696696
private static extern unsafe git_index_entry* git_index_get_bypath(
697697
git_index* index,
698-
[CustomMarshaler(typeof(StrictFilePathMarshaler), typeof(FilePath))] byte* path,
698+
[CustomMarshaler(typeof(StrictUtf8Marshaler), typeof(string))] byte* path,
699699
int stage);
700700

701701
[DllImport(libgit2)]
@@ -720,7 +720,7 @@ internal static extern unsafe int git_index_read(
720720
[DllImport(libgit2)]
721721
private static extern unsafe int git_index_remove_bypath(
722722
git_index* index,
723-
[CustomMarshaler(typeof(StrictFilePathMarshaler), typeof(FilePath))] byte* path);
723+
[CustomMarshaler(typeof(StrictUtf8Marshaler), typeof(string))] byte* path);
724724

725725

726726
[DllImport(libgit2)]
@@ -732,7 +732,7 @@ private static extern unsafe int git_index_remove_bypath(
732732
[DllImport(libgit2)]
733733
private static extern unsafe git_index_reuc_entry* git_index_reuc_get_bypath(
734734
git_index* handle,
735-
[CustomMarshaler(typeof(StrictFilePathMarshaler), typeof(FilePath))] byte* path);
735+
[CustomMarshaler(typeof(StrictUtf8Marshaler), typeof(string))] byte* path);
736736

737737
[DllImport(libgit2)]
738738
internal static extern unsafe int git_index_write(git_index* index);
@@ -1605,7 +1605,7 @@ internal static extern void git_strarray_free(
16051605
private static extern unsafe int git_submodule_lookup(
16061606
out git_submodule* reference,
16071607
git_repository* repo,
1608-
[CustomMarshaler(typeof(StrictFilePathMarshaler), typeof(FilePath))] byte* name);
1608+
[CustomMarshaler(typeof(StrictUtf8Marshaler), typeof(string))] byte* name);
16091609

16101610
[DllImport(libgit2)]
16111611
private static extern unsafe int git_submodule_resolve_url(
@@ -1803,7 +1803,7 @@ private static unsafe extern int git_transport_unregister(
18031803
private static extern unsafe int git_tree_entry_bypath(
18041804
out git_tree_entry* tree,
18051805
git_object* root,
1806-
[CustomMarshaler(typeof(StrictFilePathMarshaler), typeof(FilePath))] byte* treeentry_path);
1806+
[CustomMarshaler(typeof(StrictUtf8Marshaler), typeof(string))] byte* treeentry_path);
18071807

18081808
[DllImport(libgit2)]
18091809
internal static extern unsafe void git_tree_entry_free(git_tree_entry* treeEntry);

0 commit comments

Comments
 (0)
0