Skip to content

Commit

Permalink
Paths in Git structures are strings not FilePaths
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
carlosmn committed Apr 27, 2016
1 parent 78500f6 commit 0ae6c64
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 28 deletions.
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs
Expand Up @@ -10,7 +10,7 @@ namespace LibGit2Sharp.Tests
{
public class DiffTreeToTreeFixture : BaseFixture
{
private static readonly string subBranchFilePath = Path.Combine("1", "branch_file.txt");
private static readonly string subBranchFilePath = String.Join("/", "1", "branch_file.txt");

[Fact]
public void ComparingATreeAgainstItselfReturnsNoDifference()
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/FileHistoryFixture.cs
Expand Up @@ -151,7 +151,7 @@ public void CanTellComplexCommitHistory()
var commit2 = MakeAndCommitChange(repo, repoPath, path1, "Hello World again");

// Move the first file to a new directory.
var newPath1 = Path.Combine(SubFolderPath1, path1);
var newPath1 = Path.Combine(SubFolderPath1, path1).Replace(@"\", "/");
Commands.Move(repo, path1, newPath1);
var commit3 = repo.Commit("Moved " + path1 + " to " + newPath1,
Constants.Signature, Constants.Signature);
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Core/NativeMethods.cs
Expand Up @@ -1821,7 +1821,7 @@ IntPtr data
internal static extern unsafe int git_tree_entry_bypath(
out git_tree_entry* tree,
git_object* root,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath treeentry_path);
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string treeentry_path);

[DllImport(libgit2)]
internal static extern unsafe void git_tree_entry_free(git_tree_entry* treeEntry);
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Core/Proxy.cs
Expand Up @@ -3220,7 +3220,7 @@ public static unsafe TreeEntryHandle git_tree_entry_byindex(ObjectHandle tree, l
return new TreeEntryHandle(handle, false);
}

public static unsafe TreeEntryHandle git_tree_entry_bypath(RepositoryHandle repo, ObjectId id, FilePath treeentry_path)
public static unsafe TreeEntryHandle git_tree_entry_bypath(RepositoryHandle repo, ObjectId id, string treeentry_path)
{
using (var obj = new ObjectSafeWrapper(id, repo))
{
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/GitObject.cs
Expand Up @@ -60,7 +60,7 @@ public virtual string Sha
get { return Id.Sha; }
}

internal static GitObject BuildFrom(Repository repo, ObjectId id, GitObjectType type, FilePath path)
internal static GitObject BuildFrom(Repository repo, ObjectId id, GitObjectType type, string path)
{
switch (type)
{
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Repository.cs
Expand Up @@ -527,7 +527,7 @@ public GitObject Lookup(string objectish, ObjectType type)
return Lookup(objectish, type.ToGitObjectType(), LookUpOptions.None);
}

internal GitObject LookupInternal(ObjectId id, GitObjectType type, FilePath knownPath)
internal GitObject LookupInternal(ObjectId id, GitObjectType type, string knownPath)
{
Ensure.ArgumentNotNull(id, "id");

Expand Down
36 changes: 26 additions & 10 deletions LibGit2Sharp/Tree.cs
Expand Up @@ -5,6 +5,8 @@
using System.Linq;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
using System.Text;
using System;

namespace LibGit2Sharp
{
Expand All @@ -14,7 +16,7 @@ namespace LibGit2Sharp
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class Tree : GitObject, IEnumerable<TreeEntry>
{
private readonly FilePath path;
private readonly string path;

private readonly ILazy<int> lazyCount;

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

internal Tree(Repository repo, ObjectId id, FilePath path)
internal Tree(Repository repo, ObjectId id, string path)
: base(repo, id)
{
this.path = path ?? "";
Expand All @@ -47,9 +49,9 @@ internal Tree(Repository repo, ObjectId id, FilePath path)
get { return RetrieveFromPath(relativePath); }
}

private unsafe TreeEntry RetrieveFromPath(FilePath relativePath)
private unsafe TreeEntry RetrieveFromPath(string relativePath)
{
if (relativePath.IsNullOrEmpty())
if (string.IsNullOrEmpty(relativePath))
{
return null;
}
Expand All @@ -61,28 +63,42 @@ private unsafe TreeEntry RetrieveFromPath(FilePath relativePath)
return null;
}

string posixPath = relativePath.Posix;
string filename = posixPath.Split('/').Last();
string parentPath = posixPath.Substring(0, posixPath.Length - filename.Length);
return new TreeEntry(treeEntry, Id, repo, path.Combine(parentPath));
string filename = relativePath.Split('/').Last();
string parentPath = relativePath.Substring(0, relativePath.Length - filename.Length);
return new TreeEntry(treeEntry, Id, repo, Tree.CombinePath(path, parentPath));
}
}

internal string Path
{
get { return path.Native; }
get { return path; }
}

#region IEnumerable<TreeEntry> Members

unsafe TreeEntry byIndex(ObjectSafeWrapper obj, uint i, ObjectId parentTreeId, Repository repo, FilePath parentPath)
unsafe TreeEntry byIndex(ObjectSafeWrapper obj, uint i, ObjectId parentTreeId, Repository repo, string parentPath)
{
using (var entryHandle = Proxy.git_tree_entry_byindex(obj.ObjectPtr, i))
{
return new TreeEntry(entryHandle, parentTreeId, repo, parentPath);
}
}

internal static string CombinePath(string a, string b)
{
var bld = new StringBuilder();
bld.Append(a);
if (!String.IsNullOrEmpty(a) &&
!a.EndsWith("/", StringComparison.InvariantCulture) &&
!b.StartsWith("/", StringComparison.InvariantCulture))
{
bld.Append('/');
}
bld.Append(b);

return bld.ToString();
}

/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
Expand Down
9 changes: 2 additions & 7 deletions LibGit2Sharp/TreeDefinition.cs
Expand Up @@ -126,11 +126,6 @@ public virtual TreeDefinition Add(string targetTreeEntryPath, TreeEntryDefinitio
Ensure.ArgumentNotNullOrEmptyString(targetTreeEntryPath, "targetTreeEntryPath");
Ensure.ArgumentNotNull(treeEntryDefinition, "treeEntryDefinition");

if (Path.IsPathRooted(targetTreeEntryPath))
{
throw new ArgumentException("The provided path is an absolute path.");
}

if (treeEntryDefinition is TransientTreeTreeEntryDefinition)
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
Expand Down Expand Up @@ -383,9 +378,9 @@ private void WrapTree(string entryName, TreeEntryDefinition treeEntryDefinition)
}
}

private static Tuple<string, string> ExtractPosixLeadingSegment(FilePath targetPath)
private static Tuple<string, string> ExtractPosixLeadingSegment(string targetPath)
{
string[] segments = targetPath.Posix.Split(new[] { '/' }, 2);
string[] segments = targetPath.Split(new[] { '/' }, 2);

if (segments[0] == string.Empty || (segments.Length == 2 && (segments[1] == string.Empty || segments[1].StartsWith("/", StringComparison.Ordinal))))
{
Expand Down
5 changes: 2 additions & 3 deletions LibGit2Sharp/TreeEntry.cs
@@ -1,7 +1,6 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;

Expand All @@ -28,7 +27,7 @@ public class TreeEntry : IEquatable<TreeEntry>
protected TreeEntry()
{ }

internal unsafe TreeEntry(TreeEntryHandle entry, ObjectId parentTreeId, Repository repo, FilePath parentPath)
internal unsafe TreeEntry(TreeEntryHandle entry, ObjectId parentTreeId, Repository repo, string parentPath)
{
this.parentTreeId = parentTreeId;
this.repo = repo;
Expand All @@ -41,7 +40,7 @@ internal unsafe TreeEntry(TreeEntryHandle entry, ObjectId parentTreeId, Reposito

Mode = Proxy.git_tree_entry_attributes(entry);
Name = Proxy.git_tree_entry_name(entry);
path = new Lazy<string>(() => System.IO.Path.Combine(parentPath.Native, Name));
path = new Lazy<string>(() => Tree.CombinePath(parentPath, Name));
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions LibGit2Sharp/TreeEntryChanges.cs
Expand Up @@ -18,8 +18,8 @@ protected TreeEntryChanges()

internal unsafe TreeEntryChanges(git_diff_delta* delta)
{
Path = LaxFilePathMarshaler.FromNative(delta->new_file.Path).Native;
OldPath = LaxFilePathMarshaler.FromNative(delta->old_file.Path).Native;
Path = LaxUtf8Marshaler.FromNative(delta->new_file.Path);
OldPath = LaxUtf8Marshaler.FromNative(delta->old_file.Path);

Mode = (Mode)delta->new_file.Mode;
OldMode = (Mode)delta->old_file.Mode;
Expand Down

0 comments on commit 0ae6c64

Please sign in to comment.