10000 Don't use FilePathMarshaller when passing Git paths · jianges/libgit2sharp@a8e9fc3 · GitHub
[go: up one dir, main page]

Skip to content

Commit a8e9fc3

Browse files
committed
Don't use FilePathMarshaller when passing Git paths
We treat a lot of the data as paths which would be available on the host system, but this doesn't need to be the case. There are paths which the filesystem or .NET won't accept which work just fine as paths inside Git. This also means the return values are also marshalled as strings, since we can't assume they contain paths which the path marshaller would like. It also keeps the separator as the slash rather than convert it into something which seems native, but is different from what we'd accept. The paths which we do refer to as paths inside the workdir are still marhshalled as paths.
1 parent 0ae6c64 commit a8e9fc3

File tree

10 files changed

+51
-58
lines changed

10 files changed

+51
-58
lines changed

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();

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",
@@ -202,7 +202,7 @@ private void InvalidMoveUseCases(string sourcePath, FileStatus sourceStatus, IEn
202202
public void PathsOfIndexEntriesAreExpressedInNativeFormat()
203203
{
204204
// Build relative path
205-
string relFilePath = Path.Combine("directory", "Testfile.txt");
205+
string relFilePath = Path.Combine("directory", "Testfile.txt").Replace('\\', '/');
206206

207207
string repoPath = InitNewRepository();
208208

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
@@ -222,7 +222,7 @@ public void CanStageANewFileWithARelativePathContainingNativeDirectorySeparatorC
222222

223223
const string posixifiedPath = "Project/a_file.txt";
224224
Assert.NotNull(repo.Index[posixifiedPath]);
225-
Assert.Equal(file, repo.Index[posixifiedPath].Path);
225+
Assert.Equal(posixifiedPath, repo.Index[posixifiedPath].Path);
226226
}
227227
}
228228

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
}
@@ -360,6 +360,7 @@ public void RetrievingTheStatusOfTheRepositoryHonorsTheGitIgnoreDirectives()
360360

361361
RepositoryStatus status = repo.RetrieveStatus();
362362

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

365366
Touch(repo.Info.WorkingDirectory, ".gitignore", "*.txt" + Environment.NewLine);
@@ -461,8 +462,6 @@ private static void AssertStatus(bool shouldIgnoreCase, FileStatus expectedFileS
461462
[Fact]
462463
public void RetrievingTheStatusOfTheRepositoryHonorsTheGitIgnoreDirectivesThroughoutDirectories()
463464
{
464-
char dirSep = Path.DirectorySeparatorChar;
465-
466465
string path = SandboxStandardTestRepo();
467466
using (var repo = new Repository(path))
468467
{
@@ -476,7 +475,7 @@ public void RetrievingTheStatusOfTheRepositoryHonorsTheGitIgnoreDirectivesThroug
476475
Assert.Equal(FileStatus.Ignored, repo.RetrieveStatus("bin/what-about-me.txt"));
477476

478477
RepositoryStatus newStatus = repo.RetrieveStatus();
479-
Assert.Equal(new[] { "bin" + dirSep }, newStatus.Ignored.Select(s => s.FilePath));
478+
Assert.Equal(new[] { "bin/" }, newStatus.Ignored.Select(s => s.FilePath));
480479

481480
var sb = new StringBuilder();
482481
sb.AppendLine("bin/*");
@@ -488,8 +487,8 @@ public void RetrievingTheStatusOfTheRepositoryHonorsTheGitIgnoreDirectivesThroug
488487

489488
newStatus = repo.RetrieveStatus();
490489

491-
Assert.Equal(new[] { "bin" + dirSep + "look-ma.txt" }, newStatus.Ignored.Select(s => s.FilePath));
492-
Assert.True(newStatus.Untracked.Select(s => s.FilePath).Contains("bin" + dirSep + "what-about-me.txt"));
490< F438 code class="diff-text syntax-highlighted-line addition">+
Assert.Equal(new[] { "bin/look-ma.txt" }, newStatus.Ignored.Select(s => s.FilePath));
491+
Assert.True(newStatus.Untracked.Select(s => s.FilePath).Contains("bin/what-about-me.txt"));
493492
}
494493
}
495494

@@ -603,7 +602,7 @@ public void CanIncludeStatusOfUnalteredFiles()
603602
var path = SandboxStandardTestRepo();
604603
string[] unalteredPaths = {
605604
"1.txt",
606-
"1" + Path.DirectorySeparatorChar + "branch_file.txt",
605+
"1/branch_file.txt",
607606
"branch_file.txt",
608607
"new.txt",
609608
"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/Core/NativeMethods.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ internal static extern void giterr_set_str(
103103
internal static extern unsafe int git_blame_file(
104104
out git_blame* blame,
105105
git_repository* repo,
106-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath path,
106+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string path,
107107
git_blame_options options);
108108

109109
[DllImport(libgit2)]
@@ -130,7 +130,7 @@ internal delegate int source_callback(
130130
internal static extern unsafe int git_blob_create_fromstream(
131131
out IntPtr stream,
132132
git_repository* repositoryPtr,
133-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath hintpath);
133+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string hintpath);
134134

135135
[DllImport(libgit2)]
136136
internal static extern unsafe int git_blob_create_fromstream_commit(
@@ -141,15 +141,15 @@ internal static extern unsafe int git_blob_create_fromstream_commit(
141141
internal static extern unsafe int git_blob_create_fromchunks(
142142
ref GitOid oid,
143143
git_repository* repositoryPtr,
144-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath hintpath,
144+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string hintpath,
145145
source_callback fileCallback,
146146
IntPtr data);
147147

148148
[DllImport(libgit2)]
149149
internal static extern unsafe int git_blob_filtered_content(
150150
GitBuf buf,
151151
git_object* blob,
152-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath as_path,
152+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string as_path,
153153
[MarshalAs(UnmanagedType.Bool)] bool check_for_binary_data);
154154

155155
[DllImport(libgit2)]
@@ -587,9 +587,9 @@ internal unsafe delegate int git_diff_binary_cb(
587587
[DllImport(libgit2)]
588588
internal static extern unsafe int git_diff_blobs(
589589
git_object* oldBlob,
590-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath old_as_path,
590+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string old_as_path,
591591
git_object* newBlob,
592-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath new_as_path,
592+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string new_as_path,
593593
GitDiffOptions options,
594594
git_diff_file_cb fileCallback,
595595
git_diff_binary_cb binaryCallback,
@@ -672,7 +672,7 @@ internal static extern unsafe int git_ignore_add_rule(
672672
internal static extern unsafe int git_ignore_path_is_ignored(
673673
out int ignored,
674674
git_repository* repo,
675-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath path);
675+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string path);
676676

677677
[DllImport(libgit2)]
678678
internal static extern unsafe int git_index_add_bypath(
@@ -690,7 +690,7 @@ internal static extern unsafe int git_index_conflict_get(
690690
out git_index_entry* ours,
691691
out git_index_entry* theirs,
692692
git_index* index,
693-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath path);
693+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string path);
694694

695695
[DllImport(libgit2)]
< 10000 code>696696
internal static extern unsafe int git_index_conflict_iterator_new(
@@ -723,7 +723,7 @@ internal static extern unsafe void git_index_conflict_iterator_free(
723723
[DllImport(libgit2)]
724724
internal static extern unsafe git_index_entry* git_index_get_bypath(
725725
git_index* index,
726-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath path,
726+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string path,
727727
int stage);
728728

729729
[DllImport(libgit2)]
@@ -748,7 +748,7 @@ internal static extern unsafe int git_index_read(
748748
[DllImport(libgit2)]
749749
internal static extern unsafe int git_index_remove_bypath(
750750
git_index* index,
751-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath path);
751+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string path);
752752

753753

754754
[DllImport(libgit2)]
@@ -760,7 +760,7 @@ internal static extern unsafe int git_index_remove_bypath(
760760
[DllImport(libgit2)]
761761
internal static extern unsafe git_index_reuc_entry* git_index_reuc_get_bypath(
762762
git_index* handle,
763-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath path);
763+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string path);
764764

765765
[DllImport(libgit2)]
766766
internal static extern unsafe int git_index_write(git_index* index);
@@ -1623,7 +1623,7 @@ internal static extern void git_strarray_free(
16231623
internal static extern unsafe int git_submodule_lookup(
16241624
out git_submodule* reference,
16251625
git_repository* repo,
1626-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath name);
1626+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string name);
16271627

16281628
[DllImport(libgit2)]
16291629
internal static extern unsafe int git_submodule_resolve_url(

LibGit2Sharp/Core/Proxy.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private static void BuildErrorMessageFromException(StringBuilder sb, int level,
9797

9898
public static unsafe BlameHandle git_blame_file(
9999
RepositoryHandle repo,
100-
FilePath path,
100+
string path,
101101
git_blame_options options)
102102
{
103103
git_blame* ptr;
@@ -115,7 +115,7 @@ public static unsafe BlameHandle git_blame_file(
115115

116116
#region git_blob_
117117

118-
public static unsafe IntPtr git_blob_create_fromstream(RepositoryHandle repo, FilePath hintpath)
118+
public static unsafe IntPtr git_blob_create_fromstream(RepositoryHandle repo, string hintpath)
119119
{
120120
IntPtr writestream_ptr;
121121

@@ -148,7 +148,7 @@ public static unsafe ObjectId git_blob_create_fromfile(RepositoryHandle repo, Fi
148148
return oid;
149149
}
150150

151-
public static unsafe UnmanagedMemoryStream git_blob_filtered_content_stream(RepositoryHandle repo, ObjectId id, FilePath path, bool check_for_binary_data)
151+
public static unsafe UnmanagedMemoryStream git_blob_filtered_content_stream(RepositoryHandle repo, ObjectId id, string path, bool check_for_binary_data)
152152
{
153153
var buf = new GitBuf();
154154
var handle = new ObjectSafeWrapper(id, repo).ObjectPtr;
@@ -1008,7 +1008,7 @@ public static unsafe void git_index_add_bypath(IndexHandle index, FilePath path)
10081008

10091009
public static unsafe Conflict git_index_conflict_get(
10101010
IndexHandle index,
1011-
FilePath path)
1011+
string path)
10121012
{
10131013
git_index_entry* ancestor, ours, theirs;
10141014

@@ -1073,7 +1073,7 @@ public static unsafe StageLevel git_index_entry_stage(git_index_entry* entry)
10731073
return NativeMethods.git_index_get_byindex(index, n);
10741074
}
10751075

1076-
public static unsafe git_index_entry* git_index_get_bypath(IndexHandle index, FilePath path, int stage)
1076+
public static unsafe git_index_entry* git_index_get_bypath(IndexHandle index, string path, int stage)
10771077
{
10781078
return NativeMethods.git_index_get_bypath(index, path, stage);
10791079
}
@@ -1112,7 +1112,7 @@ public static unsafe void git_index_read(IndexHandle index)
11121112
Ensure.ZeroResult(res);
11131113
}
11141114

1115-
public static unsafe void git_index_remove_bypath(IndexHandle index, FilePath path)
1115+
public static unsafe void git_index_remove_bypath(IndexHandle index, string path)
11161116
{
11171117
int res = NativeMethods.git_index_remove_bypath(index, path);
11181118
Ensure.ZeroResult(res);
@@ -2922,7 +2922,7 @@ public static unsafe int git_status_list_entrycount(StatusListHandle list)
29222922
/// Returns a handle to the corresponding submodule,
29232923
/// or an invalid handle if a submodule is not found.
29242924
/// </summary>
2925-
public static unsafe SubmoduleHandle git_submodule_lookup(RepositoryHandle repo, FilePath name)
2925+
public static unsafe SubmoduleHandle git_submodule_lookup(RepositoryHandle repo, string name)
29262926
{
29272927
git_submodule* submodule;
29282928
var res = NativeMethods.git_submodule_lookup(out submodule, repo, name);

LibGit2Sharp/IndexEntry.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ internal static unsafe IndexEntry BuildFromPtr(git_index_entry* entry)
4747
return null;
4848
}
4949

50-
FilePath path = LaxFilePathMarshaler.FromNative(entry->path);
50+
string path = LaxUtf8Marshaler.FromNative(entry->path);
5151

5252
return new IndexEntry
5353
{
54-
Path = path.Native,
54+
Path = path,
5555
Id = new ObjectId(entry->id.Id),
5656
StageLevel = Proxy.git_index_entry_stage(entry),
5757
Mode = (Mode)entry->mode,

0 commit comments

Comments
 (0)
0