diff --git a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs index c1b0bcce6..ea230a037 100644 --- a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs +++ b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs @@ -290,12 +290,12 @@ public void DetectsTheExactRenamingOfFilesByDefault() Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); - TreeChanges changes = repo.Diff.Compare(old.Tree, @new.Tree); + var changes = repo.Diff.Compare(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); } } @@ -337,7 +337,7 @@ public void RenameThresholdsAreObeyed() }; compareOptions.Similarity.RenameThreshold = 30; - TreeChanges changes = repo.Diff.Compare(old.Tree, @new.Tree, compareOptions: compareOptions); + var changes = repo.Diff.Compare(old.Tree, @new.Tree, compareOptions: compareOptions); Assert.True(changes.All(x => x.Status == ChangeKind.Renamed)); compareOptions.Similarity.RenameThreshold = 90; @@ -366,7 +366,7 @@ public void ExactModeDetectsExactRenames() Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); - TreeChanges changes = repo.Diff.Compare(old.Tree, @new.Tree, + var changes = repo.Diff.Compare(old.Tree, @new.Tree, compareOptions: new CompareOptions { Similarity = SimilarityOptions.Exact, @@ -374,8 +374,8 @@ public void ExactModeDetectsExactRenames() 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); } } @@ -400,7 +400,7 @@ public void ExactModeDetectsExactCopies() Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); - TreeChanges changes = repo.Diff.Compare(old.Tree, @new.Tree, + var changes = repo.Diff.Compare(old.Tree, @new.Tree, compareOptions: new CompareOptions { Similarity = SimilarityOptions.Exact, @@ -433,7 +433,7 @@ public void ExactModeDoesntDetectRenamesWithEdits() Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); - TreeChanges changes = repo.Diff.Compare(old.Tree, @new.Tree, + var changes = repo.Diff.Compare(old.Tree, @new.Tree, compareOptions: new CompareOptions { Similarity = SimilarityOptions.Exact, @@ -469,7 +469,7 @@ public void CanIncludeUnmodifiedEntriesWhenDetectingTheExactRenamingOfFilesWhenE Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); - TreeChanges changes = repo.Diff.Compare(old.Tree, @new.Tree, + var changes = repo.Diff.Compare(old.Tree, @new.Tree, compareOptions: new CompareOptions { @@ -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); } } @@ -505,7 +505,7 @@ public void CanNotDetectTheExactRenamingFilesWhenNotEnabled() Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); - TreeChanges changes = repo.Diff.Compare(old.Tree, @new.Tree, + var changes = repo.Diff.Compare(old.Tree, @new.Tree, compareOptions: new CompareOptions { @@ -540,7 +540,7 @@ public void CanDetectTheExactCopyingOfNonModifiedFilesWhenEnabled() Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); - TreeChanges changes = repo.Diff.Compare(old.Tree, @new.Tree, + var changes = repo.Diff.Compare(old.Tree, @new.Tree, compareOptions: new CompareOptions { @@ -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); } } @@ -577,7 +577,7 @@ public void CanNotDetectTheExactCopyingOfNonModifiedFilesWhenNotEnabled() Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); - TreeChanges changes = repo.Diff.Compare(old.Tree, @new.Tree); + var changes = repo.Diff.Compare(old.Tree, @new.Tree); Assert.Equal(1, changes.Count()); Assert.Equal(0, changes.Copied.Count()); @@ -610,7 +610,7 @@ public void CanDetectTheExactCopyingOfModifiedFilesWhenEnabled() Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); - TreeChanges changes = repo.Diff.Compare(old.Tree, @new.Tree, + var changes = repo.Diff.Compare(old.Tree, @new.Tree, compareOptions: new CompareOptions { @@ -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); } } @@ -650,7 +650,7 @@ public void CanNotDetectTheExactCopyingOfModifiedFilesWhenNotEnabled() Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); - TreeChanges changes = repo.Diff.Compare(old.Tree, @new.Tree); + var changes = repo.Diff.Compare(old.Tree, @new.Tree); Assert.Equal(2, changes.Count()); Assert.Equal(0, changes.Copied.Count()); @@ -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(old.Tree, @new.Tree, + var changes = repo.Diff.Compare(old.Tree, @new.Tree, compareOptions: new CompareOptions {IncludeUnmodified = true}); Assert.Equal(2, changes.Count()); @@ -722,7 +722,7 @@ public void CanDetectTheExactRenamingExactCopyingOfNonModifiedAndModifiedFilesWh Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); - TreeChanges changes = repo.Diff.Compare(old.Tree, @new.Tree, + var changes = repo.Diff.Compare(old.Tree, @new.Tree, compareOptions: new CompareOptions { @@ -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); } } /* @@ -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); } } @@ -881,14 +882,15 @@ public void CanCompareTwoVersionsOfAFileWithADiffOfTwoHunks(int contextLines, in var patch = repo.Diff.Compare(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); } } diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index 1da4fc85a..205aec220 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -95,6 +95,7 @@ + diff --git a/LibGit2Sharp/Patch.cs b/LibGit2Sharp/Patch.cs index c08807a6c..4bbb884bd 100644 --- a/LibGit2Sharp/Patch.cs +++ b/LibGit2Sharp/Patch.cs @@ -16,11 +16,11 @@ namespace LibGit2Sharp /// deleted, modified, ..., then consider using a simpler . /// [DebuggerDisplay("{DebuggerDisplay,nq}")] - public class Patch : IEnumerable + public class Patch : IEnumerable { private readonly StringBuilder fullPatchBuilder = new StringBuilder(); - private readonly IDictionary changes = new Dictionary(); + private readonly IDictionary changes = new Dictionary(); private int linesAdded; private int linesDeleted; @@ -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) @@ -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) @@ -91,13 +91,13 @@ private int PrintCallBack(GitDiffDelta delta, GitDiffHunk hunk, GitDiffLine line return 0; } - #region IEnumerable Members + #region IEnumerable Members /// /// Returns an enumerator that iterates through the collection. /// /// An object that can be used to iterate through the collection. - public virtual IEnumerator GetEnumerator() + public virtual IEnumerator GetEnumerator() { return changes.Values.GetEnumerator(); } @@ -116,19 +116,19 @@ IEnumerator IEnumerable.GetEnumerator() /// /// Gets the corresponding to the specified . /// - 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; diff --git a/LibGit2Sharp/PatchEntryChanges.cs b/LibGit2Sharp/PatchEntryChanges.cs new file mode 100644 index 000000000..0a7d925b7 --- /dev/null +++ b/LibGit2Sharp/PatchEntryChanges.cs @@ -0,0 +1,78 @@ +namespace LibGit2Sharp +{ + /// + /// Holds the changes between two versions of a file. + /// + public class PatchEntryChanges : ContentChanges + { + private readonly TreeEntryChanges treeEntryChanges; + + /// + /// Needed for mocking purposes. + /// + protected PatchEntryChanges() + { } + + internal PatchEntryChanges(bool isBinaryComparison, TreeEntryChanges treeEntryChanges) + : base(isBinaryComparison) + { + this.treeEntryChanges = treeEntryChanges; + } + + /// + /// The new path. + /// + public virtual string Path + { + get { return treeEntryChanges.Path; } + } + + /// + /// The new . + /// + public virtual Mode Mode + { + get { return treeEntryChanges.Mode; } + } + + /// + /// The new content hash. + /// + public virtual ObjectId Oid + { + get { return treeEntryChanges.Oid; } + } + + /// + /// The kind of change that has been done (added, deleted, modified ...). + /// + public virtual ChangeKind Status + { + get { return treeEntryChanges.Status; } + } + + /// + /// The old path. + /// + public virtual string OldPath + { + get { return treeEntryChanges.OldPath; } + } + + /// + /// The old . + /// + public virtual Mode OldMode + { + get { return treeEntryChanges.OldMode; } + } + + /// + /// The old content hash. + /// + public virtual ObjectId OldOid + { + get { return treeEntryChanges.OldOid; } + } + } +} diff --git a/LibGit2Sharp/TreeChanges.cs b/LibGit2Sharp/TreeChanges.cs index c05c3c077..1529a25cd 100644 --- a/LibGit2Sharp/TreeChanges.cs +++ b/LibGit2Sharp/TreeChanges.cs @@ -61,23 +61,10 @@ private int FileCallback(GitDiffDelta delta, float progress, IntPtr payload) private void AddFileChange(GitDiffDelta delta) { - var newFilePath = LaxFilePathMarshaler.FromNative(delta.NewFile.Path); + var treeEntryChanges = new TreeEntryChanges(delta); - var oldFilePath = LaxFilePathMarshaler.FromNative(delta.OldFile.Path); - var newMode = (Mode)delta.NewFile.Mode; - var oldMode = (Mode)delta.OldFile.Mode; - var newOid = delta.NewFile.Id; - var oldOid = delta.OldFile.Id; - - if (delta.Status == ChangeKind.Untracked || delta.Status == ChangeKind.Ignored) - { - delta.Status = ChangeKind.Added; - } - - var diffFile = new TreeEntryChanges(newFilePath, newMode, newOid, delta.Status, oldFilePath, oldMode, oldOid); - - fileDispatcher[delta.Status](this, diffFile); - changes.Add(newFilePath, diffFile); + fileDispatcher[treeEntryChanges.Status](this, treeEntryChanges); + changes.Add(treeEntryChanges.Path, treeEntryChanges); } #region IEnumerable Members diff --git a/LibGit2Sharp/TreeEntryChanges.cs b/LibGit2Sharp/TreeEntryChanges.cs index 62e76f04c..205ff42c3 100644 --- a/LibGit2Sharp/TreeEntryChanges.cs +++ b/LibGit2Sharp/TreeEntryChanges.cs @@ -16,15 +16,19 @@ public class TreeEntryChanges protected TreeEntryChanges() { } - internal TreeEntryChanges(FilePath path, Mode mode, ObjectId oid, ChangeKind status, FilePath oldPath, Mode oldMode, ObjectId oldOid) + internal TreeEntryChanges(GitDiffDelta delta) { - Path = path.Native; - Mode = mode; - Oid = oid; - Status = status; - OldPath = oldPath.Native; - OldMode = oldMode; - OldOid = oldOid; + Path = LaxFilePathMarshaler.FromNative(delta.NewFile.Path).Native; + OldPath = LaxFilePathMarshaler.FromNative(delta.OldFile.Path).Native; + + Mode = (Mode)delta.NewFile.Mode; + OldMode = (Mode)delta.OldFile.Mode; + Oid = delta.NewFile.Id; + OldOid = delta.OldFile.Id; + + Status = (delta.Status == ChangeKind.Untracked || delta.Status == ChangeKind.Ignored) + ? ChangeKind.Added + : delta.Status; } ///