diff options
author | Edward Thomson <ethomson@microsoft.com> | 2014-06-18 07:06:28 +0400 |
---|---|---|
committer | Edward Thomson <ethomson@microsoft.com> | 2014-06-19 02:24:01 +0400 |
commit | 4bc48574ec64f635ff861cf95d2503248b5a31cb (patch) | |
tree | 209991f601d319a47332717c8da527cc2a407a92 /LibGit2Sharp | |
parent | 6bfe4abeeb36fd96cdd1d539c75da4c7eabac43e (diff) |
Remove conflicts in Index.Remove
Diffstat (limited to 'LibGit2Sharp')
-rw-r--r-- | LibGit2Sharp/Core/Ensure.cs | 17 | ||||
-rw-r--r-- | LibGit2Sharp/Index.cs | 54 |
2 files changed, 58 insertions, 13 deletions
diff --git a/LibGit2Sharp/Core/Ensure.cs b/LibGit2Sharp/Core/Ensure.cs index 0c3c8ea6..df11d3f6 100644 --- a/LibGit2Sharp/Core/Ensure.cs +++ b/LibGit2Sharp/Core/Ensure.cs @@ -1,4 +1,6 @@ using System; +using System.Linq; +using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; @@ -25,6 +27,21 @@ namespace LibGit2Sharp.Core } /// <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> /// <param name="argumentValue">The argument value to check.</param> diff --git a/LibGit2Sharp/Index.cs b/LibGit2Sharp/Index.cs index 1f6501b4..e953910d 100644 --- a/LibGit2Sharp/Index.cs +++ b/LibGit2Sharp/Index.cs @@ -345,10 +345,44 @@ namespace LibGit2Sharp /// </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) { @@ -358,7 +392,7 @@ namespace LibGit2Sharp { case ChangeKind.Added: case ChangeKind.Deleted: - pathsTodelete.Add(RemoveFromIndex(treeEntryChanges.Path)); + removed.Add(RemoveFromIndex(treeEntryChanges.Path)); break; case ChangeKind.Unmodified: @@ -369,7 +403,7 @@ namespace LibGit2Sharp 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: @@ -383,22 +417,16 @@ namespace LibGit2Sharp 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) |