Skip to content

Fix handling of blob mode change #201

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

Closed
wants to merge 1 commit into from
Closed
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
75 changes: 69 additions & 6 deletions LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void CanCompareACommitTreeAgainstItsParent()
Assert.Equal(1, changes.Count());
Assert.Equal(1, changes.Added.Count());

TreeEntryChanges treeEntryChanges = changes["1.txt"];
TreeEntryChanges treeEntryChanges = changes["1.txt"].Single();
Assert.False(treeEntryChanges.IsBinaryComparison);

Assert.Equal("1.txt", treeEntryChanges.Path);
Expand Down Expand Up @@ -128,7 +128,7 @@ public void CanCompareACommitTreeAgainstATreeWithNoCommonAncestor()

Assert.Equal(9, changes.LinesAdded);
Assert.Equal(2, changes.LinesDeleted);
Assert.Equal(2, changes["readme.txt"].LinesDeleted);
Assert.Equal(2, changes["readme.txt"].Single().LinesDeleted);
}
}

Expand Down Expand Up @@ -161,8 +161,8 @@ public void CanDetectTheRenamingOfAModifiedFile()
TreeChanges changes = repo.Diff.Compare(rootCommitTree, commitTreeWithRenamedFile);

Assert.Equal(1, changes.Count());
Assert.Equal("super-file.txt", changes["super-file.txt"].Path);
Assert.Equal("my-name-does-not-feel-right.txt", changes["super-file.txt"].OldPath);
Assert.Equal("super-file.txt", changes["super-file.txt"].Single().Path);
Assert.Equal("my-name-does-not-feel-right.txt", changes["super-file.txt"].Single().OldPath);
//Assert.Equal(1, changes.FilesRenamed.Count());
}
}
Expand Down Expand Up @@ -275,12 +275,12 @@ public void CanCompareTwoVersionsOfAFileWithADiffOfTwoHunks()
Assert.Equal(1, changes.Deleted.Count());
Assert.Equal(1, changes.Added.Count());

TreeEntryChanges treeEntryChanges = changes["numbers.txt"];
TreeEntryChanges treeEntryChanges = changes["numbers.txt"].Single();

Assert.Equal(3, treeEntryChanges.LinesAdded);
Assert.Equal(1, treeEntryChanges.LinesDeleted);

Assert.Equal(Mode.Nonexistent, changes["my-name-does-not-feel-right.txt"].Mode);
Assert.Equal(Mode.Nonexistent, changes["my-name-does-not-feel-right.txt"].Single().Mode);

var expected = new StringBuilder()
.Append("diff --git a/numbers.txt b/numbers.txt\n")
Expand Down Expand Up @@ -354,5 +354,68 @@ public void CanCompareTwoVersionsOfAFileWithADiffOfTwoHunks()
Assert.Equal(expected.ToString(), changes.Patch);
}
}

[Fact]
public void CanHandleTwoTreeEntryChangesWithTheSamePath()
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();

using (Repository repo = Repository.Init(scd.DirectoryPath))
{
Blob mainContent = CreateBlob(repo, "awesome content\n");
Blob linkContent = CreateBlob(repo, "../../objc/Nu.h");

const string path = "include/Nu/Nu.h";

var tdOld = new TreeDefinition()
.Add(path, linkContent, Mode.SymbolicLink)
.Add("objc/Nu.h", mainContent, Mode.NonExecutableFile);

Tree treeOld = repo.ObjectDatabase.CreateTree(tdOld);

var tdNew = new TreeDefinition()
.Add(path, mainContent, Mode.NonExecutableFile);

Tree treeNew = repo.ObjectDatabase.CreateTree(tdNew);

TreeChanges changes = repo.Diff.Compare(treeOld, treeNew);

if (IsRunningOnLinux())
{
TreeEntryChanges[] tecs = changes[path].ToArray();
Assert.Equal(2, tecs.Length);

TreeEntryChanges change1 = tecs[0];
Assert.Equal(Mode.SymbolicLink, change1.OldMode);
Assert.Equal(Mode.Nonexistent, change1.Mode);
Assert.Equal(ChangeKind.Deleted, change1.Status);
Assert.Equal("include/Nu/Nu.h", change1.Path);

TreeEntryChanges change2 = tecs[1];
Assert.Equal(Mode.Nonexistent, change2.OldMode);
Assert.Equal(Mode.NonExecutableFile, change2.Mode);
Assert.Equal(ChangeKind.Added, change2.Status);
Assert.Equal("include/Nu/Nu.h", change1.Path);

return;
}

Assert.Equal(1, changes[path].Count());
TreeEntryChanges change = changes[path].Single();

Assert.Equal(Mode.SymbolicLink, change.Mode);
Assert.Equal(change.OldMode, change.Mode);
Assert.Equal(ChangeKind.Modified, change.Status);
}
}

private static Blob CreateBlob(Repository repo, string content)
{
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content)))
using (var binReader = new BinaryReader(stream))
{
return repo.ObjectDatabase.CreateBlob(binReader);
}
}
}
}
55 changes: 55 additions & 0 deletions LibGit2Sharp.Tests/StatusFixture.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;

Expand Down Expand Up @@ -241,5 +242,59 @@ public void RetrievingTheStatusOfTheRepositoryHonorsTheGitIgnoreDirectives()
Assert.Equal(new[] { relativePath, "new_untracked_file.txt" }, newStatus.Ignored);
}
}

[Fact]
public void CanHandleTwoStatusEntryChangesWithTheSamePath()
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();

using (Repository repo = Repository.Init(scd.DirectoryPath))
{
Blob mainContent = CreateBlob(repo, "awesome content\n");
Blob linkContent = CreateBlob(repo, "../../objc/Nu.h");

const string path = "include/Nu/Nu.h";

var tdOld = new TreeDefinition()
.Add(path, linkContent, Mode.SymbolicLink)
.Add("objc/Nu.h", mainContent, Mode.NonExecutableFile);

Tree tree = repo.ObjectDatabase.CreateTree(tdOld);

Commit commit = repo.ObjectDatabase.CreateCommit("A symlink", DummySignature, DummySignature, tree, Enumerable.Empty<Commit>());
repo.Refs.UpdateTarget("HEAD", commit.Id.Sha);
repo.Reset(ResetOptions.Mixed);

string fullPath = Path.Combine(repo.Info.WorkingDirectory, "include/Nu");
Directory.CreateDirectory(fullPath);

File.WriteAllText(Path.Combine(fullPath, "Nu.h"), "awesome content\n");

RepositoryStatus status = repo.Index.RetrieveStatus();

if (IsRunningOnLinux())
{
Assert.Equal(3, status.Count());
Assert.Equal(new[] { Path.Combine(Path.Combine("include", "Nu"), "Nu.h"), Path.Combine("objc", "Nu.h") }, status.Missing.ToArray());
Assert.Equal(0, status.Added.Count());
Assert.Equal(0, status.Modified.Count());
Assert.Equal(Path.Combine(Path.Combine("include", "Nu"), "Nu.h"), status.Untracked.Single());
return;
}

Assert.Equal(2, status.Count());
Assert.Equal(Path.Combine(Path.Combine("include", "Nu"), "Nu.h"), status.Modified.Single());
Assert.Equal(Path.Combine("objc", "Nu.h"), status.Missing.Single());
}
}

private static Blob CreateBlob(Repository repo, string content)
{
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content)))
using (var binReader = new BinaryReader(stream))
{
return repo.ObjectDatabase.CreateBlob(binReader);
}
}
}
}
7 changes: 7 additions & 0 deletions LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,12 @@ protected static void AssertValueInConfigFile(string configFilePath, string rege
var r = new Regex(regex, RegexOptions.Multiline).Match(text);
Assert.True(r.Success, text);
}

protected static bool IsRunningOnLinux()
{
// see http://mono-project.com/FAQ%3a_Technical#Mono_Platforms
var p = (int)Environment.OSVersion.Platform;
return (p == 4) || (p == 6) || (p == 128);
}
}
}
17 changes: 10 additions & 7 deletions LibGit2Sharp/TreeChanges.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
Expand All @@ -13,7 +14,7 @@ namespace LibGit2Sharp
/// </summary>
public class TreeChanges : IEnumerable<TreeEntryChanges>
{
private readonly IDictionary<FilePath, TreeEntryChanges> changes = new Dictionary<FilePath, TreeEntryChanges>();
private readonly IDictionary<FilePath, List<TreeEntryChanges>> changes = new Dictionary<FilePath, List<TreeEntryChanges>>();
private readonly List<TreeEntryChanges> added = new List<TreeEntryChanges>();
private readonly List<TreeEntryChanges> deleted = new List<TreeEntryChanges>();
private readonly List<TreeEntryChanges> modified = new List<TreeEntryChanges>();
Expand Down Expand Up @@ -79,7 +80,7 @@ private TreeEntryChanges AddFileChange(GitDiffDelta delta, GitDiffLineOrigin lin
var newFilePath = FilePathMarshaler.FromNative(delta.NewFile.Path);

if (lineorigin != GitDiffLineOrigin.GIT_DIFF_LINE_FILE_HDR)
return this[newFilePath];
return this[newFilePath].Last();

var oldFilePath = FilePathMarshaler.FromNative(delta.OldFile.Path);
var newMode = (Mode)delta.NewFile.Mode;
Expand All @@ -90,7 +91,9 @@ private TreeEntryChanges AddFileChange(GitDiffDelta delta, GitDiffLineOrigin lin
var diffFile = new TreeEntryChanges(newFilePath, newMode, newOid, delta.Status, oldFilePath, oldMode, oldOid, delta.IsBinary());

fileDispatcher[delta.Status](this, diffFile);
changes.Add(newFilePath, diffFile);

var newFilePathChanges = this[newFilePath] ?? (changes[newFilePath] = new List<TreeEntryChanges>());
newFilePathChanges.Add(diffFile);
return diffFile;
}

Expand All @@ -102,7 +105,7 @@ private TreeEntryChanges AddFileChange(GitDiffDelta delta, GitDiffLineOrigin lin
/// <returns>An <see cref = "IEnumerator{T}" /> object that can be used to iterate through the collection.</returns>
public virtual IEnumerator<TreeEntryChanges> GetEnumerator()
{
return changes.Values.GetEnumerator();
return changes.Values.SelectMany(tec => tec.AsEnumerable()).GetEnumerator();
}

/// <summary>
Expand All @@ -119,16 +122,16 @@ IEnumerator IEnumerable.GetEnumerator()
/// <summary>
/// Gets the <see cref = "TreeEntryChanges"/> corresponding to the specified <paramref name = "path"/>.
/// </summary>
public virtual TreeEntryChanges this[string path]
public virtual IEnumerable<TreeEntryChanges> this[string path]
{
get { return this[(FilePath)path]; }
}

private TreeEntryChanges this[FilePath path]
private List<TreeEntryChanges> this[FilePath path]
{
get
{
TreeEntryChanges treeEntryChanges;
List<TreeEntryChanges> treeEntryChanges;
if (changes.TryGetValue(path, out treeEntryChanges))
{
return treeEntryChanges;
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/TreeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public TreeDefinition Add(string targetTreeEntryPath, Blob blob, Mode mode)
{
Ensure.ArgumentNotNull(blob, "blob");
Ensure.ArgumentConformsTo(mode,
m => m.HasAny(new[] { Mode.ExecutableFile, Mode.NonExecutableFile, Mode.NonExecutableGroupWritableFile }), "mode");
m => m.HasAny(new[] { Mode.ExecutableFile, Mode.NonExecutableFile, Mode.NonExecutableGroupWritableFile, Mode.SymbolicLink }), "mode");

TreeEntryDefinition ted = TreeEntryDefinition.From(blob, mode);

Expand Down