Skip to content

Lazy TreeChanges, move ownership of diff handle for diff results #1281

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 5 commits into from
Apr 12, 2016
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
336 changes: 185 additions & 151 deletions LibGit2Sharp.Tests/DiffTreeToTargetFixture.cs

Large diffs are not rendered by default.

448 changes: 247 additions & 201 deletions LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs

Large diffs are not rendered by default.

66 changes: 38 additions & 28 deletions LibGit2Sharp.Tests/DiffWorkdirToIndexFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ public void CanCompareTheWorkDirAgainstTheIndex()
var path = SandboxStandardTestRepoGitDir();
using (var repo = new Repository(path))
{
var changes = repo.Diff.Compare<TreeChanges>();

Assert.Equal(2, changes.Count());
Assert.Equal("deleted_unstaged_file.txt", changes.Deleted.Single().Path);
Assert.Equal("modified_unstaged_file.txt", changes.Modified.Single().Path);
using (var changes = repo.Diff.Compare<TreeChanges>())
{
Assert.Equal(2, changes.Count());
Assert.Equal("deleted_unstaged_file.txt", changes.Deleted.Single().Path);
Assert.Equal("modified_unstaged_file.txt", changes.Modified.Single().Path);
}
}
}

Expand All @@ -51,11 +52,15 @@ public void CanCompareTheWorkDirAgainstTheIndexWithLaxUnmatchedExplicitPathsVali
{
Assert.Equal(currentStatus, repo.RetrieveStatus(relativePath));

var changes = repo.Diff.Compare<TreeChanges>(new[] { relativePath }, false, new ExplicitPathsOptions { ShouldFailOnUnmatchedPath = false });
Assert.Equal(0, changes.Count());
using (var changes = repo.Diff.Compare<TreeChanges>(new[] { relativePath }, false, new ExplicitPathsOptions { ShouldFailOnUnmatchedPath = false }))
{
Assert.Equal(0, changes.Count());
}

changes = repo.Diff.Compare<TreeChanges>(new[] { relativePath });
Assert.Equal(0, changes.Count());
using (var changes = repo.Diff.Compare<TreeChanges>(new[] { relativePath }))
{
Assert.Equal(0, changes.Count());
}
}
}

Expand Down Expand Up @@ -85,12 +90,14 @@ public void CallbackForUnmatchedExplicitPathsIsCalledWhenSet(string relativePath
{
Assert.Equal(currentStatus, repo.RetrieveStatus(relativePath));

repo.Diff.Compare<TreeChanges>(new[] { relativePath }, false, new ExplicitPathsOptions
using (var changes = repo.Diff.Compare<TreeChanges>(new[] { relativePath }, false, new ExplicitPathsOptions
{
ShouldFailOnUnmatchedPath = false,
OnUnmatchedPath = callback.OnUnmatchedPath });

Assert.True(callback.WasCalled);
OnUnmatchedPath = callback.OnUnmatchedPath
}))
{
Assert.True(callback.WasCalled);
}
}
}

Expand Down Expand Up @@ -130,21 +137,23 @@ public void ComparingReliesOnProvidedConfigEntriesIfAny()
using (var repo = new Repository(path))
{
SetFilemode(repo, true);
var changes = repo.Diff.Compare<TreeChanges>(new[] { file });

Assert.Equal(1, changes.Count());
using(var changes = repo.Diff.Compare<TreeChanges>(new[] { file }))
{
Assert.Equal(1, changes.Count());

var change = changes.Modified.Single();
Assert.Equal(Mode.ExecutableFile, change.OldMode);
Assert.Equal(Mode.NonExecutableFile, change.Mode);
var change = changes.Modified.Single();
Assert.Equal(Mode.ExecutableFile, change.OldMode);
Assert.Equal(Mode.NonExecutableFile, change.Mode);
}
}

using (var repo = new Repository(path))
{
SetFilemode(repo, false);
var changes = repo.Diff.Compare<TreeChanges>(new[] { file });

Assert.Equal(0, changes.Count());
using(var changes = repo.Diff.Compare<TreeChanges>(new[] { file }))
{
Assert.Equal(0, changes.Count());
}
}
}

Expand All @@ -159,12 +168,13 @@ public void CanCompareTheWorkDirAgainstTheIndexWithUntrackedFiles()
var path = SandboxStandardTestRepoGitDir();
using (var repo = new Repository(path))
{
var changes = repo.Diff.Compare<TreeChanges>(null, true);

Assert.Equal(3, changes.Count());
Assert.Equal("deleted_unstaged_file.txt", changes.Deleted.Single().Path);
Assert.Equal("modified_unstaged_file.txt", changes.Modified.Single().Path);
Assert.Equal("new_untracked_file.txt", changes.Added.Single().Path);
using (var changes = repo.Diff.Compare<TreeChanges>(null, true))
{
Assert.Equal(3, changes.Count());
Assert.Equal("deleted_unstaged_file.txt", changes.Deleted.Single().Path);
Assert.Equal("modified_unstaged_file.txt", changes.Modified.Single().Path);
Assert.Equal("new_untracked_file.txt", changes.Added.Single().Path);
}
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions LibGit2Sharp.Tests/PatchStatsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ public void CanExtractStatisticsFromDiff()
{
var oldTree = repo.Lookup<Commit>("origin/packed-test").Tree;
var newTree = repo.Lookup<Commit>("HEAD").Tree;
var stats = repo.Diff.Compare<PatchStats>(oldTree, newTree);
using (var stats = repo.Diff.Compare<PatchStats>(oldTree, newTree))
{
Assert.Equal(8, stats.TotalLinesAdded);
Assert.Equal(1, stats.TotalLinesDeleted);

Assert.Equal(8, stats.TotalLinesAdded);
Assert.Equal(1, stats.TotalLinesDeleted);

var contentStats = stats["new.txt"];
Assert.Equal(1, contentStats.LinesAdded);
Assert.Equal(1, contentStats.LinesDeleted);
var contentStats = stats["new.txt"];
Assert.Equal(1, contentStats.LinesAdded);
Assert.Equal(1, contentStats.LinesDeleted);
}
}
}
}
Expand Down
94 changes: 48 additions & 46 deletions LibGit2Sharp/Commands/Remove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,58 +182,60 @@ private static void RemoveFilesAndFolders(IRepository repository, IEnumerable<st
private static IEnumerable<string> RemoveStagedItems(IRepository repository, IEnumerable<string> paths, bool removeFromWorkingDirectory = true, ExplicitPathsOptions explicitPathsOptions = null)
{
var removed = new List<string>();
var changes = repository.Diff.Compare<TreeChanges>(DiffModifiers.IncludeUnmodified | DiffModifiers.IncludeUntracked, paths, explicitPathsOptions);
var index = repository.Index;

foreach (var treeEntryChanges in changes)
using (var changes = repository.Diff.Compare<TreeChanges>(DiffModifiers.IncludeUnmodified | DiffModifiers.IncludeUntracked, paths, explicitPathsOptions))
{
var status = repository.RetrieveStatus(treeEntryChanges.Path);
var index = repository.Index;

switch (treeEntryChanges.Status)
foreach (var treeEntryChanges in changes)
{
case ChangeKind.Added:
case ChangeKind.Deleted:
removed.Add(treeEntryChanges.Path);
index.Remove(treeEntryChanges.Path);
break;

case ChangeKind.Unmodified:
if (removeFromWorkingDirectory && (
status.HasFlag(FileStatus.ModifiedInIndex) ||
status.HasFlag(FileStatus.NewInIndex)))
{
throw new RemoveFromIndexException("Unable to remove file '{0}', as it has changes staged in the index. You can call the Remove() method with removeFromWorkingDirectory=false if you want to remove it from the index only.",
treeEntryChanges.Path);
}
removed.Add(treeEntryChanges.Path);
index.Remove(treeEntryChanges.Path);
continue;

case ChangeKind.Modified:
if (status.HasFlag(FileStatus.ModifiedInWorkdir) && status.HasFlag(FileStatus.ModifiedInIndex))
{
throw new RemoveFromIndexException("Unable to remove file '{0}', as it has staged content different from both the working directory and the HEAD.",
treeEntryChanges.Path);
}
if (removeFromWorkingDirectory)
{
throw new RemoveFromIndexException("Unable to remove file '{0}', as it has local modifications. You can call the Remove() method with removeFromWorkingDirectory=false if you want to remove it from the index only.",
treeEntryChanges.Path);
}
removed.Add(treeEntryChanges.Path);
index.Remove(treeEntryChanges.Path);
continue;

default:
throw new RemoveFromIndexException("Unable to remove file '{0}'. Its current status is '{1}'.",
treeEntryChanges.Path,
treeEntryChanges.Status);
var status = repository.RetrieveStatus(treeEntryChanges.Path);

switch (treeEntryChanges.Status)
{
case ChangeKind.Added:
case ChangeKind.Deleted:
removed.Add(treeEntryChanges.Path);
index.Remove(treeEntryChanges.Path);
break;

case ChangeKind.Unmodified:
if (removeFromWorkingDirectory && (
status.HasFlag(FileStatus.ModifiedInIndex) ||
status.HasFlag(FileStatus.NewInIndex)))
{
throw new RemoveFromIndexException("Unable to remove file '{0}', as it has changes staged in the index. You can call the Remove() method with removeFromWorkingDirectory=false if you want to remove it from the index only.",
treeEntryChanges.Path);
}
removed.Add(treeEntryChanges.Path);
index.Remove(treeEntryChanges.Path);
continue;

case ChangeKind.Modified:
if (status.HasFlag(FileStatus.ModifiedInWorkdir) && status.HasFlag(FileStatus.ModifiedInIndex))
{
throw new RemoveFromIndexException("Unable to remove file '{0}', as it has staged content different from both the working directory and the HEAD.",
treeEntryChanges.Path);
}
if (removeFromWorkingDirectory)
{
throw new RemoveFromIndexException("Unable to remove file '{0}', as it has local modifications. You can call the Remove() method with removeFromWorkingDirectory=false if you want to remove it from the index only.",
treeEntryChanges.Path);
}
removed.Add(treeEntryChanges.Path);
index.Remove(treeEntryChanges.Path);
continue;

default:
throw new RemoveFromIndexException("Unable to remove file '{0}'. Its current status is '{1}'.",
treeEntryChanges.Path,
treeEntryChanges.Status);
}
}
}

index.Write();
index.Write();

return removed;
return removed;
}
}
}
}
Expand Down
108 changes: 54 additions & 54 deletions LibGit2Sharp/Commands/Stage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,68 +74,69 @@ public static void Stage(IRepository repository, IEnumerable<string> paths, Stag
diffModifiers |= DiffModifiers.IncludeIgnored;
}

var changes = repository.Diff.Compare<TreeChanges>(diffModifiers, paths, explicitPathsOptions,
new CompareOptions { Similarity = SimilarityOptions.None });

var unexpectedTypesOfChanges = changes
.Where(
tec => tec.Status != ChangeKind.Added &&
tec.Status != ChangeKind.Modified &&
tec.Status != ChangeKind.Conflicted &&
tec.Status != ChangeKind.Unmodified &&
tec.Status != ChangeKind.Deleted).ToList();

if (unexpectedTypesOfChanges.Count > 0)
using (var changes = repository.Diff.Compare<TreeChanges>(diffModifiers, paths, explicitPathsOptions,
new CompareOptions { Similarity = SimilarityOptions.None }))
{
throw new InvalidOperationException(
string.Format(CultureInfo.InvariantCulture,
"Entry '{0}' bears an unexpected ChangeKind '{1}'",
unexpectedTypesOfChanges[0].Path, unexpectedTypesOfChanges[0].Status));
}
var unexpectedTypesOfChanges = changes
.Where(
tec => tec.Status != ChangeKind.Added &&
tec.Status != ChangeKind.Modified &&
tec.Status != ChangeKind.Conflicted &&
tec.Status != ChangeKind.Unmodified &&
tec.Status != ChangeKind.Deleted).ToList();

if (unexpectedTypesOfChanges.Count > 0)
{
throw new InvalidOperationException(
string.Format(CultureInfo.InvariantCulture,
"Entry '{0}' bears an unexpected ChangeKind '{1}'",
unexpectedTypesOfChanges[0].Path, unexpectedTypesOfChanges[0].Status));
}

/* Remove files from the index that don't exist on disk */
foreach (TreeEntryChanges treeEntryChanges in changes)
{
switch (treeEntryChanges.Status)
/* Remove files from the index that don't exist on disk */
foreach (TreeEntryChanges treeEntryChanges in changes)
{
case ChangeKind.Conflicted:
if (!treeEntryChanges.Exists)
{
switch (treeEntryChanges.Status)
{
case ChangeKind.Conflicted:
if (!treeEntryChanges.Exists)
{
repository.Index.Remove(treeEntryChanges.Path);
}
break;

case ChangeKind.Deleted:
repository.Index.Remove(treeEntryChanges.Path);
}
break;

case ChangeKind.Deleted:
repository.Index.Remove(treeEntryChanges.Path);
break;
break;

default:
continue;
default:
continue;
}
}
}

foreach (TreeEntryChanges treeEntryChanges in changes)
{
switch (treeEntryChanges.Status)
foreach (TreeEntryChanges treeEntryChanges in changes)
{
case ChangeKind.Added:
case ChangeKind.Modified:
repository.Index.Add(treeEntryChanges.Path);
break;

case ChangeKind.Conflicted:
if (treeEntryChanges.Exists)
{
switch (treeEntryChanges.Status)
{
case ChangeKind.Added:
case ChangeKind.Modified:
repository.Index.Add(treeEntryChanges.Path);
}
break;

default:
continue;
break;

case ChangeKind.Conflicted:
if (treeEntryChanges.Exists)
{
repository.Index.Add(treeEntryChanges.Path);
}
break;

default:
continue;
}
}
}

repository.Index.Write();
repository.Index.Write();
}
}

/// <summary>
Expand Down Expand Up @@ -191,9 +192,8 @@ public static void Unstage(IRepository repository, IEnumerable<string> paths, Ex

if (repository.Info.IsHeadUnborn)
{
var changes = repository.Diff.Compare<TreeChanges>(null, DiffTargets.Index, paths, explicitPathsOptions, new CompareOptions { Similarity = SimilarityOptions.None });

repository.Index.Replace(changes);
using (var changes = repository.Diff.Compare<TreeChanges>(null, DiffTargets.Index, paths, explicitPathsOptions, new CompareOptions { Similarity = SimilarityOptions.None }))
repository.Index.Replace(changes);
}
else
{
Expand Down
12 changes: 7 additions & 5 deletions LibGit2Sharp/Core/FileHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,13 @@ private static void DetermineParentPaths(IRepository repo, Commit currentCommit,

private static string ParentPath(IRepository repo, Commit currentCommit, string currentPath, Commit parentCommit)
{
var treeChanges = repo.Diff.Compare<TreeChanges>(parentCommit.Tree, currentCommit.Tree);
var treeEntryChanges = treeChanges.FirstOrDefault(c => c.Path == currentPath);
return treeEntryChanges != null && treeEntryChanges.Status == ChangeKind.Renamed
? treeEntryChanges.OldPath
: currentPath;
using (var treeChanges = repo.Diff.Compare<TreeChanges>(parentCommit.Tree, currentCommit.Tree))
{
var treeEntryChanges = treeChanges.FirstOrDefault(c => c.Path == currentPath);
return treeEntryChanges != null && treeEntryChanges.Status == ChangeKind.Renamed
? treeEntryChanges.OldPath
: currentPath;
}
}
}
}
Loading