Skip to content

Topic/path and patch #702

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

Merged
merged 2 commits into from
Jun 12, 2014
Merged
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
76 changes: 39 additions & 37 deletions LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,12 @@ public void DetectsTheExactRenamingOfFilesByDefault()

Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature);

TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree);
var changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree);

Assert.Equal(1, changes.Count());
Assert.Equal(1, changes.Renamed.Count());
Assert.Equal("original.txt", changes.Renamed.Single().OldPath);
Assert.Equal("renamed.txt", changes.Renamed.Single().Path);
Assert.Equal(originalPath, changes.Renamed.Single().OldPath);
Assert.Equal(renamedPath, changes.Renamed.Single().Path);
}
}

Expand Down Expand Up @@ -337,7 +337,7 @@ public void RenameThresholdsAreObeyed()
};

compareOptions.Similarity.RenameThreshold = 30;
TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree, compareOptions: compareOptions);
var changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree, compareOptions: compareOptions);
Assert.True(changes.All(x => x.Status == ChangeKind.Renamed));

compareOptions.Similarity.RenameThreshold = 90;
Expand Down Expand Up @@ -366,16 +366,16 @@ public void ExactModeDetectsExactRenames()

Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature);

TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree,
var changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree,
compareOptions: new CompareOptions
{
Similarity = SimilarityOptions.Exact,
});

Assert.Equal(1, changes.Count());
Assert.Equal(1, changes.Renamed.Count());
Assert.Equal("original.txt", changes.Renamed.Single().OldPath);
Assert.Equal("renamed.txt", changes.Renamed.Single().Path);
Assert.Equal(originalPath, changes.Renamed.Single().OldPath);
Assert.Equal(renamedPath, changes.Renamed.Single().Path);
}
}

Expand All @@ -400,7 +400,7 @@ public void ExactModeDetectsExactCopies()

Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature);

TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree,
var changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree,
compareOptions: new CompareOptions
{
Similarity = SimilarityOptions.Exact,
Expand Down Expand Up @@ -433,7 +433,7 @@ public void ExactModeDoesntDetectRenamesWithEdits()

Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature);

TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree,
var changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree,
compareOptions: new CompareOptions
{
Similarity = SimilarityOptions.Exact,
Expand Down Expand Up @@ -469,7 +469,7 @@ public void CanIncludeUnmodifiedEntriesWhenDetectingTheExactRenamingOfFilesWhenE

Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature);

TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree,
var changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree,
compareOptions:
new CompareOptions
{
Expand All @@ -480,8 +480,8 @@ public void CanIncludeUnmodifiedEntriesWhenDetectingTheExactRenamingOfFilesWhenE
Assert.Equal(2, changes.Count());
Assert.Equal(1, changes.Unmodified.Count());
Assert.Equal(1, changes.Copied.Count());
Assert.Equal("original.txt", changes.Copied.Single().OldPath);
Assert.Equal("copied.txt", changes.Copied.Single().Path);
Assert.Equal(originalPath, changes.Copied.Single().OldPath);
Assert.Equal(copiedPath, changes.Copied.Single().Path);
}
}

Expand All @@ -505,7 +505,7 @@ public void CanNotDetectTheExactRenamingFilesWhenNotEnabled()

Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature);

TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree,
var changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree,
compareOptions:
new CompareOptions
{
Expand Down Expand Up @@ -540,7 +540,7 @@ public void CanDetectTheExactCopyingOfNonModifiedFilesWhenEnabled()

Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature);

TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree,
var changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree,
compareOptions:
new CompareOptions
{
Expand All @@ -549,8 +549,8 @@ public void CanDetectTheExactCopyingOfNonModifiedFilesWhenEnabled()

Assert.Equal(1, changes.Count());
Assert.Equal(1, changes.Copied.Count());
Assert.Equal("original.txt", changes.Copied.Single().OldPath);
Assert.Equal("copied.txt", changes.Copied.Single().Path);
Assert.Equal(originalPath, changes.Copied.Single().OldPath);
Assert.Equal(copiedPath, changes.Copied.Single().Path);
}
}

Expand All @@ -577,7 +577,7 @@ public void CanNotDetectTheExactCopyingOfNonModifiedFilesWhenNotEnabled()

Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature);

TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree);
var changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree);

Assert.Equal(1, changes.Count());
Assert.Equal(0, changes.Copied.Count());
Expand Down Expand Up @@ -610,7 +610,7 @@ public void CanDetectTheExactCopyingOfModifiedFilesWhenEnabled()

Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature);

TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree,
var changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree,
compareOptions:
new CompareOptions
{
Expand All @@ -619,8 +619,8 @@ public void CanDetectTheExactCopyingOfModifiedFilesWhenEnabled()

Assert.Equal(2, changes.Count());
Assert.Equal(1, changes.Copied.Count());
Assert.Equal("original.txt", changes.Copied.Single().OldPath);
Assert.Equal("copied.txt", changes.Copied.Single().Path);
Assert.Equal(originalPath, changes.Copied.Single().OldPath);
Assert.Equal(copiedPath, changes.Copied.Single().Path);
}
}

Expand Down Expand Up @@ -650,7 +650,7 @@ public void CanNotDetectTheExactCopyingOfModifiedFilesWhenNotEnabled()

Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature);

TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree);
var changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree);

Assert.Equal(2, changes.Count());
Assert.Equal(0, changes.Copied.Count());
Expand All @@ -674,7 +674,7 @@ public void CanIncludeUnmodifiedEntriesWhenEnabled()
repo.Index.Stage("b.txt");
Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature);

TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree,
var changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree,
compareOptions: new CompareOptions {IncludeUnmodified = true});

Assert.Equal(2, changes.Count());
Expand Down Expand Up @@ -722,7 +722,7 @@ public void CanDetectTheExactRenamingExactCopyingOfNonModifiedAndModifiedFilesWh

Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature);

TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree,
var changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree,
compareOptions:
new CompareOptions
{
Expand All @@ -732,13 +732,13 @@ public void CanDetectTheExactRenamingExactCopyingOfNonModifiedAndModifiedFilesWh
Assert.Equal(4, changes.Count());
Assert.Equal(1, changes.Modified.Count());
Assert.Equal(1, changes.Renamed.Count());
Assert.Equal("original.txt", changes.Renamed.Single().OldPath);
Assert.Equal("renamed.txt", changes.Renamed.Single().Path);
Assert.Equal(originalPath, changes.Renamed.Single().OldPath);
Assert.Equal(renamedPath, changes.Renamed.Single().Path);
Assert.Equal(2, changes.Copied.Count());
Assert.Equal("original2.txt", changes.Copied.ElementAt(0).OldPath);
Assert.Equal("copied.txt", changes.Copied.ElementAt(0).Path);
Assert.Equal("original3.txt", changes.Copied.ElementAt(1).OldPath);
Assert.Equal("copied2.txt", changes.Copied.ElementAt(1).Path);
Assert.Equal(originalPath2, changes.Copied.ElementAt(0).OldPath);
Assert.Equal(copiedPath1, changes.Copied.ElementAt(0).Path);
Assert.Equal(originalPath3, changes.Copied.ElementAt(1).OldPath);
Assert.Equal(copiedPath2, changes.Copied.ElementAt(1).Path);
}
}
/*
Expand Down Expand Up @@ -786,11 +786,12 @@ public void CanCompareTwoVersionsOfAFileWithATrailingNewlineDeletion(int context

Assert.Equal(expectedPatchLength, patch.Content.Length);

ContentChanges contentChanges = patch["numbers.txt"];
PatchEntryChanges entryChanges = patch["numbers.txt"];

Assert.Equal(2, contentChanges.LinesAdded);
Assert.Equal(1, contentChanges.LinesDeleted);
Assert.Equal(expectedPatchLength, contentChanges.Patch.Length);
Assert.Equal(2, entryChanges.LinesAdded);
Assert.Equal(1, entryChanges.LinesDeleted);
Assert.Equal(expectedPatchLength, entryChanges.Patch.Length);
Assert.Equal("numbers.txt", entryChanges.Path);
}
}

Expand Down Expand Up @@ -881,14 +882,15 @@ public void CanCompareTwoVersionsOfAFileWithADiffOfTwoHunks(int contextLines, in

var patch = repo.Diff.Compare<Patch>(rootCommitTree, mergedCommitTree, compareOptions: compareOptions);

ContentChanges contentChanges = patch["numbers.txt"];
PatchEntryChanges entryChanges = patch["numbers.txt"];

Assert.Equal(3, contentChanges.LinesAdded);
Assert.Equal(1, contentChanges.LinesDeleted);
Assert.Equal(3, entryChanges.LinesAdded);
Assert.Equal(1, entryChanges.LinesDeleted);
Assert.Equal(Expected("f8d44d7...7252fe2/numbers.txt-{0}-{1}.diff", contextLines, interhunkLines),
contentChanges.Patch);
entryChanges.Patch);
Assert.Equal(Expected("f8d44d7...7252fe2/full-{0}-{1}.diff", contextLines, interhunkLines),
patch);
Assert.Equal("numbers.txt", entryChanges.Path);
}
}

Expand Down
1 change: 1 addition & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<Compile Include="GlobalSettings.cs" />
<Compile Include="MergeOptions.cs" />
<Compile Include="MergeResult.cs" />
<Compile Include="PatchEntryChanges.cs" />
<Compile Include="PatchStats.cs" />
<Compile Include="PullOptions.cs" />
<Compile Include="RefSpec.cs" />
Expand Down
26 changes: 13 additions & 13 deletions LibGit2Sharp/Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ namespace LibGit2Sharp
/// deleted, modified, ..., then consider using a simpler <see cref="TreeChanges"/>.</para>
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class Patch : IEnumerable<ContentChanges>
public class Patch : IEnumerable<PatchEntryChanges>
{
private readonly StringBuilder fullPatchBuilder = new StringBuilder();

private readonly IDictionary<FilePath, ContentChanges> changes = new Dictionary<FilePath, ContentChanges>();
private readonly IDictionary<FilePath, PatchEntryChanges> changes = new Dictionary<FilePath, PatchEntryChanges>();
private int linesAdded;
private int linesDeleted;

Expand All @@ -47,9 +47,9 @@ internal Patch(DiffSafeHandle diff)

private void AddFileChange(GitDiffDelta delta)
{
var pathPtr = delta.NewFile.Path != IntPtr.Zero ? delta.NewFile.Path : delta.OldFile.Path;
var newFilePath = LaxFilePathMarshaler.FromNative(pathPtr);
changes.Add(newFilePath, new ContentChanges(delta.IsBinary()));
var treeEntryChanges = new TreeEntryChanges(delta);

changes.Add(treeEntryChanges.Path, new PatchEntryChanges(delta.IsBinary(), treeEntryChanges));
}

private int PrintCallBack(GitDiffDelta delta, GitDiffHunk hunk, GitDiffLine line, IntPtr payload)
Expand All @@ -61,7 +61,7 @@ private int PrintCallBack(GitDiffDelta delta, GitDiffHunk hunk, GitDiffLine line
var pathPtr = delta.NewFile.Path != IntPtr.Zero ? delta.NewFile.Path : delta.OldFile.Path;
var filePath = LaxFilePathMarshaler.FromNative(pathPtr);

ContentChanges currentChange = this[filePath];
PatchEntryChanges currentChange = this[filePath];
string prefix = string.Empty;

switch (line.lineOrigin)
Expand Down Expand Up @@ -91,13 +91,13 @@ private int PrintCallBack(GitDiffDelta delta, GitDiffHunk hunk, GitDiffLine line
return 0;
}

#region IEnumerable<ContentChanges> Members
#region IEnumerable<PatchEntryChanges> Members

/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns>
public virtual IEnumerator<ContentChanges> GetEnumerator()
public virtual IEnumerator<PatchEntryChanges> GetEnumerator()
{
return changes.Values.GetEnumerator();
}
Expand All @@ -116,19 +116,19 @@ IEnumerator IEnumerable.GetEnumerator()
/// <summary>
/// Gets the <see cref="ContentChanges"/> corresponding to the specified <paramref name="path"/>.
/// </summary>
public virtual ContentChanges this[string path]
public virtual PatchEntryChanges this[string path]
{
get { return this[(FilePath)path]; }
}

private ContentChanges this[FilePath path]
private PatchEntryChanges this[FilePath path]
{
get
{
ContentChanges contentChanges;
if (changes.TryGetValue(path, out contentChanges))
PatchEntryChanges entryChanges;
if (changes.TryGetValue(path, out entryChanges))
{
return contentChanges;
return entryChanges;
}

return null;
Expand Down
78 changes: 78 additions & 0 deletions LibGit2Sharp/PatchEntryChanges.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
namespace LibGit2Sharp
{
/// <summary>
/// Holds the changes between two versions of a file.
/// </summary>
public class PatchEntryChanges : ContentChanges
{
private readonly TreeEntryChanges treeEntryChanges;

/// <summary>
/// Needed for mocking purposes.
/// </summary>
protected PatchEntryChanges()
{ }

internal PatchEntryChanges(bool isBinaryComparison, TreeEntryChanges treeEntryChanges)
: base(isBinaryComparison)
{
this.treeEntryChanges = treeEntryChanges;
}

/// <summary>
/// The new path.
/// </summary>
public virtual string Path
{
get { return treeEntryChanges.Path; }
}

/// <summary>
/// The new <see cref="Mode"/>.
/// </summary>
public virtual Mode Mode
{
get { return treeEntryChanges.Mode; }
}

/// <summary>
/// The new content hash.
/// </summary>
public virtual ObjectId Oid
{
get { return treeEntryChanges.Oid; }
}

/// <summary>
/// The kind of change that has been done (added, deleted, modified ...).
/// </summary>
public virtual ChangeKind Status
{
get { return treeEntryChanges.Status; }
}

/// <summary>
/// The old path.
/// </summary>
public virtual string OldPath
{
get { return treeEntryChanges.OldPath; }
}

/// <summary>
/// The old <see cref="Mode"/>.
/// </summary>
public virtual Mode OldMode
{
get { return treeEntryChanges.OldMode; }
}

/// <summary>
/// The old content hash.
/// </summary>
public virtual ObjectId OldOid
{
get { return treeEntryChanges.OldOid; }
}
}
}
Loading