Skip to content

Remove conflicts in Index.Remove #768

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 1 commit into from
Jun 19, 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
9 changes: 2 additions & 7 deletions LibGit2Sharp.Tests/ConflictFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ private static List<object[]> RenameConflictData
[InlineData(false, "ancestor-and-ours.txt", true, true, FileStatus.Removed |FileStatus.Untracked, 2)]
[InlineData(true, "ancestor-and-theirs.txt", true, false, FileStatus.Nonexistent, 2)]
[InlineData(false, "ancestor-and-theirs.txt", true, true, FileStatus.Untracked, 2)]
[InlineData(true, "ancestor-only.txt", false, false, FileStatus.Nonexistent, 1)]
[InlineData(false, "ancestor-only.txt", false, false, FileStatus.Nonexistent, 1)]
[InlineData(true, "conflicts-one.txt", true, false, FileStatus.Removed, 3)]
[InlineData(false, "conflicts-one.txt", true, true, FileStatus.Removed | FileStatus.Untracked, 3)]
[InlineData(true, "conflicts-two.txt", true, false, FileStatus.Removed, 3)]
Expand All @@ -61,13 +63,6 @@ private static List<object[]> RenameConflictData
[InlineData(false, "ours-only.txt", true, true, FileStatus.Removed | FileStatus.Untracked, 1)]
[InlineData(true, "theirs-only.txt", true, false, FileStatus.Nonexistent, 1)]
[InlineData(false, "theirs-only.txt", true, true, FileStatus.Untracked, 1)]
/* Conflicts clearing through Index.Remove() only works when a version of the entry exists in the workdir.
* This is because libgit2's git_iterator_for_index() seem to only care about stage level 0.
* Corrolary: other cases only work out of sheer luck (however, the behaviour is stable, so I guess we
* can rely on it for the moment.
* [InlineData(true, "ancestor-only.txt", false, false, FileStatus.Nonexistent, 0)]
* [InlineData(false, "ancestor-only.txt", false, false, FileStatus.Nonexistent, 0)]
*/
public void CanResolveConflictsByRemovingFromTheIndex(
bool removeFromWorkdir, string filename, bool existsBeforeRemove, bool existsAfterRemove, FileStatus lastStatus, int removedIndexEntries)
{
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/RemoveFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void RemovingFileWithBadParamsThrows()
Assert.Throws<ArgumentException>(() => repo.Index.Remove(string.Empty));
Assert.Throws<ArgumentNullException>(() => repo.Index.Remove((string)null));
Assert.Throws<ArgumentException>(() => repo.Index.Remove(new string[] { }));
Assert.Throws<ArgumentException>(() => repo.Index.Remove(new string[] { null }));
Assert.Throws<ArgumentNullException>(() => repo.Index.Remove(new string[] { null }));
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions LibGit2Sharp/Core/Ensure.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand All @@ -24,6 +26,21 @@ public static void ArgumentNotNull(object argumentValue, string argumentName)
}
}

/// <summary>
/// Checks an array argument to ensure it isn't null or empty.
/// </summary>
/// <param name="argumentValue">The argument value to check.</param>
/// <param name="argumentName">The name of the argument.</param>
public static void ArgumentNotNullOrEmptyEnumerable<T>(IEnumerable<T> argumentValue, string argumentName)
{
ArgumentNotNull(argumentValue, argumentName);

if (argumentValue.Count() == 0)
{
throw new ArgumentException("Enumerable cannot be empty", argumentName);
}
}

/// <summary>
/// Checks a string argument to ensure it isn't null or empty.
/// </summary>
Expand Down
54 changes: 41 additions & 13 deletions LibGit2Sharp/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,44 @@ public virtual void Remove(string path, bool removeFromWorkingDirectory = true,
/// </param>
public virtual void Remove(IEnumerable<string> paths, bool removeFromWorkingDirectory = true, ExplicitPathsOptions explicitPathsOptions = null)
{
var pathsList = paths.ToList();
var changes = repo.Diff.Compare<TreeChanges>(DiffModifiers.IncludeUnmodified | DiffModifiers.IncludeUntracked, pathsList, explicitPathsOptions);
Ensure.ArgumentNotNullOrEmptyEnumerable<string>(paths, "paths");

var pathsTodelete = pathsList.Where(p => Directory.Exists(Path.Combine(repo.Info.WorkingDirectory, p))).ToList();
var pathsToDelete = paths.Where(p => Directory.Exists(Path.Combine(repo.Info.WorkingDirectory, p))).ToList();
var notConflictedPaths = new List<string>();

foreach (var path in paths)
{
Ensure.ArgumentNotNullOrEmptyString(path, "path");

var conflict = repo.Index.Conflicts[path];

if (conflict != null)
{
pathsToDelete.Add(RemoveFromIndex(path));
}
else
{
notConflictedPaths.Add(path);
}
}

if (notConflictedPaths.Count > 0)
{
pathsToDelete.AddRange(RemoveStagedItems(notConflictedPaths, removeFromWorkingDirectory, explicitPathsOptions));
}

if (removeFromWorkingDirectory)
{
RemoveFilesAndFolders(pathsToDelete);
}

UpdatePhysicalIndex();
}

private IEnumerable<string> RemoveStagedItems(IEnumerable<string> paths, bool removeFromWorkingDirectory = true, ExplicitPathsOptions explicitPathsOptions = null)
{
var removed = new List<string>();
var changes = repo.Diff.Compare<TreeChanges>(DiffModifiers.IncludeUnmodified | DiffModifiers.IncludeUntracked, paths, explicitPathsOptions);

foreach (var treeEntryChanges in changes)
{
Expand All @@ -358,7 +392,7 @@ public virtual void Remove(IEnumerable<string> paths, bool removeFromWorkingDire
{
case ChangeKind.Added:
case ChangeKind.Deleted:
pathsTodelete.Add(RemoveFromIndex(treeEntryChanges.Path));
removed.Add(RemoveFromIndex(treeEntryChanges.Path));
break;

case ChangeKind.Unmodified:
Expand All @@ -369,7 +403,7 @@ public virtual void Remove(IEnumerable<string> paths, bool removeFromWorkingDire
throw new RemoveFromIndexException(string.Format(CultureInfo.InvariantCulture, "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));
}
pathsTodelete.Add(RemoveFromIndex(treeEntryChanges.Path));
removed.Add(RemoveFromIndex(treeEntryChanges.Path));
continue;

case ChangeKind.Modified:
Expand All @@ -383,22 +417,16 @@ public virtual void Remove(IEnumerable<string> paths, bool removeFromWorkingDire
throw new RemoveFromIndexException(string.Format(CultureInfo.InvariantCulture, "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));
}
pathsTodelete.Add(RemoveFromIndex(treeEntryChanges.Path));
removed.Add(RemoveFromIndex(treeEntryChanges.Path));
continue;


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

if (removeFromWorkingDirectory)
{
RemoveFilesAndFolders(pathsTodelete);
}

UpdatePhysicalIndex();
return removed;
}

private void RemoveFilesAndFolders(IEnumerable<string> pathsList)
Expand Down