Skip to content

Commit 0ae6c64

Browse files
committed
Paths in Git structures are strings not FilePaths
These paths use a slash for separation, and attempts to hide this behind FilePath end up making it more complex to figure out what we should accept. Move to accept strings and keep them as such instead of trying to convert them.
1 parent 78500f6 commit 0ae6c64

File tree

10 files changed

+38
-28
lines changed

10 files changed

+38
-28
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/Core/NativeMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,7 @@ internal static extern int git_transport_unregister(
18211821
internal static extern unsafe int git_tree_entry_bypath(
18221822
out git_tree_entry* tree,
18231823
git_object* root,
1824-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath treeentry_path);
1824+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string treeentry_path);
18251825

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

LibGit2Sharp/Core/Proxy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3220,7 +3220,7 @@ public static unsafe TreeEntryHandle git_tree_entry_byindex(ObjectHandle tree, l
32203220
return new TreeEntryHandle(handle, false);
32213221
}
32223222

3223-
public static unsafe TreeEntryHandle git_tree_entry_bypath(RepositoryHandle repo, ObjectId id, FilePath treeentry_path)
3223+
public static unsafe TreeEntryHandle git_tree_entry_bypath(RepositoryHandle repo, ObjectId id, string treeentry_path)
32243224
{
32253225
using (var obj = new ObjectSafeWrapper(id, repo))
32263226
{

LibGit2Sharp/GitObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public virtual string Sha
6060
get { return Id.Sha; }
6161
}
6262

63-
internal static GitObject BuildFrom(Repository repo, ObjectId id, GitObjectType type, FilePath path)
63+
internal static GitObject BuildFrom(Repository repo, ObjectId id, GitObjectType type, string path)
6464
{
6565
switch (type)
6666
{

LibGit2Sharp/Repository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ public GitObject Lookup(string objectish, ObjectType type)
527527
return Lookup(objectish, type.ToGitObjectType(), LookUpOptions.None);
528528
}
529529

530-
internal GitObject LookupInternal(ObjectId id, GitObjectType type, FilePath knownPath)
530+
internal GitObject LookupInternal(ObjectId id, GitObjectType type, string knownPath)
531531
{
532532
Ensure.ArgumentNotNull(id, "id");
533533

LibGit2Sharp/Tree.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.Linq;
66
using LibGit2Sharp.Core;
77
using LibGit2Sharp.Core.Handles;
8+
using System.Text;
9+
using System;
810

911
namespace LibGit2Sharp
1012
{
@@ -14,7 +16,7 @@ namespace LibGit2Sharp
1416
[DebuggerDisplay("{DebuggerDisplay,nq}")]
1517
public class Tree : GitObject, IEnumerable<TreeEntry>
1618
{
17-
private readonly FilePath path;
19+
private readonly string path;
1820

1921
private readonly ILazy<int> lazyCount;
2022

@@ -24,7 +26,7 @@ public class Tree : GitObject, IEnumerable<TreeEntry>
2426
protected Tree()
2527
{ }
2628

27-
internal Tree(Repository repo, ObjectId id, FilePath path)
29+
internal Tree(Repository repo, ObjectId id, string path)
2830
: base(repo, id)
2931
{
3032
this.path = path ?? "";
@@ -47,9 +49,9 @@ public virtual TreeEntry this[string relativePath]
4749
get { return RetrieveFromPath(relativePath); }
4850
}
4951

50-
private unsafe TreeEntry RetrieveFromPath(FilePath relativePath)
52+
private unsafe TreeEntry RetrieveFromPath(string relativePath)
5153
{
52-
if (relativePath.IsNullOrEmpty())
54+
if (string.IsNullOrEmpty(relativePath))
5355
{
5456
return null;
5557
}
@@ -61,28 +63,42 @@ private unsafe TreeEntry RetrieveFromPath(FilePath relativePath)
6163
return null;
6264
}
6365

64-
string posixPath = relativePath.Posix;
65-
string filename = posixPath.Split('/').Last();
66-
string parentPath = posixPath.Substring(0, posixPath.Length - filename.Length);
67-
return new TreeEntry(treeEntry, Id, repo, path.Combine(parentPath));
66+
string filename = relativePath.Split('/').Last();
67+
string parentPath = relativePath.Substring(0, relativePath.Length - filename.Length);
68+
return new TreeEntry(treeEntry, Id, repo, Tree.CombinePath(path, parentPath));
6869
}
6970
}
7071

7172
internal string Path
7273
{
73-
get { return path.Native; }
74+
get { return path; }
7475
}
7576

7677
#region IEnumerable<TreeEntry> Members
7778

78-
unsafe TreeEntry byIndex(ObjectSafeWrapper obj, uint i, ObjectId parentTreeId, Repository repo, FilePath parentPath)
79+
unsafe TreeEntry byIndex(ObjectSafeWrapper obj, uint i, ObjectId parentTreeId, Repository repo, string parentPath)
7980
{
8081
using (var entryHandle = Proxy.git_tree_entry_byindex(obj.ObjectPtr, i))
8182
{
8283
return new TreeEntry(entryHandle, parentTreeId, repo, parentPath);
8384
}
8485
}
8586

87+
internal static string CombinePath(string a, string b)
88+
{
89+
var bld = new StringBuilder();
90+
bld.Append(a);
91+
if (!String.IsNullOrEmpty(a) &&
92+
!a.EndsWith("/", StringComparison.InvariantCulture) &&
93+
!b.StartsWith("/", StringComparison.InvariantCulture))
94+
{
95+
bld.Append('/');
96+
}
97+
bld.Append(b);
98+
99+
return bld.ToString();
100+
}
101+
86102
/// <summary>
87103
/// Returns an enumerator that iterates through the collection.
88104
/// </summary>

LibGit2Sharp/TreeDefinition.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,6 @@ public virtual TreeDefinition Add(string targetTreeEntryPath, TreeEntryDefinitio
126126
Ensure.ArgumentNotNullOrEmptyString(targetTreeEntryPath, "targetTreeEntryPath");
127127
Ensure.ArgumentNotNull(treeEntryDefinition, "treeEntryDefinition");
128128

129-
if (Path.IsPathRooted(targetTreeEntryPath))
130-
{
131-
throw new ArgumentException("The provided path is an absolute path.");
132-
}
133-
134129
if (treeEntryDefinition is TransientTreeTreeEntryDefinition)
135130
{
136131
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
@@ -383,9 +378,9 @@ public virtual TreeEntryDefinition this[string treeEntryPath]
383378
}
384379
}
385380

386-
private static Tuple<string, string> ExtractPosixLeadingSegment(FilePath targetPath)
381+
private static Tuple<string, string> ExtractPosixLeadingSegment(string targetPath)
387382
{
388-
string[] segments = targetPath.Posix.Split(new[] { '/' }, 2);
383+
string[] segments = targetPath.Split(new[] { '/' }, 2);
389384

390385
if (segments[0] == string.Empty || (segments.Length == 2 && (segments[1] == string.Empty || segments[1].StartsWith("/", StringComparison.Ordinal))))
391386
{

LibGit2Sharp/TreeEntry.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Diagnostics;
33
using System.Globalization;
4-
using System.Runtime.InteropServices;
54
using LibGit2Sharp.Core;
65
using LibGit2Sharp.Core.Handles;
76

@@ -28,7 +27,7 @@ public class TreeEntry : IEquatable<TreeEntry>
2827
protected TreeEntry()
2928
{ }
3029

31-
internal unsafe TreeEntry(TreeEntryHandle entry, ObjectId parentTreeId, Repository repo, FilePath parentPath)
30+
internal unsafe TreeEntry(TreeEntryHandle entry, ObjectId parentTreeId, Repository repo, string parentPath)
3231
{
3332
this.parentTreeId = parentTreeId;
3433
this.repo = repo;
@@ -41,7 +40,7 @@ internal unsafe TreeEntry(TreeEntryHandle entry, ObjectId parentTreeId, Reposito
4140

4241
Mode = Proxy.git_tree_entry_attributes(entry);
4342
Name = Proxy.git_tree_entry_name(entry);
44-
path = new Lazy<string>(() => System.IO.Path.Combine(parentPath.Native, Name));
43+
path = new Lazy<string>(() => Tree.CombinePath(parentPath, Name));
4544
}
4645

4746
/// <summary>

LibGit2Sharp/TreeEntryChanges.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ protected TreeEntryChanges()
1818

1919
internal unsafe TreeEntryChanges(git_diff_delta* delta)
2020
{
21-
Path = LaxFilePathMarshaler.FromNative(delta->new_file.Path).Native;
22-
OldPath = LaxFilePathMarshaler.FromNative(delta->old_file.Path).Native;
21+
Path = LaxUtf8Marshaler.FromNative(delta->new_file.Path);
22+
OldPath = LaxUtf8Marshaler.FromNative(delta->old_file.Path);
2323

2424
Mode = (Mode)delta->new_file.Mode;
2525
OldMode = (Mode)delta->old_file.Mode;

0 commit comments

Comments
 (0)