Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryorah <yoram.harmelin@gmail.com>2013-04-18 19:34:32 +0400
committernulltoken <emeric.fermas@gmail.com>2013-04-24 21:50:24 +0400
commit068d4958813afb78f2782f9892444783ffd8a779 (patch)
tree97381401e22acd8d7f687250d0287718454ca40f
parent6a02dd8b7761828b25a8b1420199a0040034c1a2 (diff)
Make Index.Remove() diff-based
Note: it means that the paths passed to Remove() are pathspecs, and thus behavior has been aligned with Stage()/Unstage() => calling Remove() by passing a pathspec which doesn't match any paths doesn't throw anymore (breaking change).
-rw-r--r--LibGit2Sharp.Tests/IndexFixture.cs9
-rw-r--r--LibGit2Sharp/Index.cs59
-rw-r--r--LibGit2Sharp/TreeChanges.cs9
3 files changed, 23 insertions, 54 deletions
diff --git a/LibGit2Sharp.Tests/IndexFixture.cs b/LibGit2Sharp.Tests/IndexFixture.cs
index 3e004ff5..49c0f451 100644
--- a/LibGit2Sharp.Tests/IndexFixture.cs
+++ b/LibGit2Sharp.Tests/IndexFixture.cs
@@ -217,15 +217,12 @@ namespace LibGit2Sharp.Tests
}
}
- [Theory]
- [InlineData("deleted_staged_file.txt")]
- [InlineData("modified_unstaged_file.txt")]
- [InlineData("shadowcopy_of_an_unseen_ghost.txt")]
- public void RemovingAInvalidFileThrows(string filepath)
+ [Fact]
+ public void RemovingAModifiedFileThrows()
{
using (var repo = new Repository(StandardTestRepoPath))
{
- Assert.Throws<LibGit2SharpException>(() => repo.Index.Remove(filepath));
+ Assert.Throws<LibGit2SharpException>(() => repo.Index.Remove("modified_unstaged_file.txt"));
}
}
diff --git a/LibGit2Sharp/Index.cs b/LibGit2Sharp/Index.cs
index 9b8c3c46..5eb14b61 100644
--- a/LibGit2Sharp/Index.cs
+++ b/LibGit2Sharp/Index.cs
@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
+using System.Linq;
using System.Runtime.InteropServices;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;
@@ -319,64 +320,38 @@ namespace LibGit2Sharp
//TODO: Remove() should support following use cases:
// - Removing a directory and its content
- IEnumerable<KeyValuePair<string, FileStatus>> batch = PrepareBatch(paths);
+ var pathsList = paths.ToList();
+ TreeChanges changes = repo.Diff.Compare(DiffOptions.IncludeUnmodified | DiffOptions.IncludeUntracked, pathsList);
- foreach (KeyValuePair<string, FileStatus> keyValuePair in batch)
+ foreach (var treeEntryChanges in changes)
{
- if (Directory.Exists(keyValuePair.Key))
+ switch (treeEntryChanges.Status)
{
- throw new NotImplementedException();
- }
+ case ChangeKind.Added:
+ case ChangeKind.Deleted:
+ case ChangeKind.Unmodified:
+ RemoveFromIndex(treeEntryChanges.Path);
+ continue;
- if (!keyValuePair.Value.HasAny(new[] { FileStatus.Nonexistent, FileStatus.Removed, FileStatus.Modified, FileStatus.Untracked }))
- {
- continue;
+ default:
+ throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture, "Unable to remove file '{0}'. Its current status is '{1}'.",
+ treeEntryChanges.Path, treeEntryChanges.Status));
}
-
- throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture, "Unable to remove file '{0}'. Its current status is '{1}'.", keyValuePair.Key, keyValuePair.Value));
}
string wd = repo.Info.WorkingDirectory;
- foreach (KeyValuePair<string, FileStatus> keyValuePair in batch)
+ foreach (string path in pathsList.Where(p => !Directory.Exists(p)))
{
- RemoveFromIndex(keyValuePair.Key);
-
- if (File.Exists(Path.Combine(wd, keyValuePair.Key)))
+ string fullPath = Path.Combine(wd, path);
+ if (File.Exists(fullPath))
{
- File.Delete(Path.Combine(wd, keyValuePair.Key));
+ File.Delete(fullPath);
}
}
UpdatePhysicalIndex();
}
- private IEnumerable<KeyValuePair<string, FileStatus>> PrepareBatch(IEnumerable<string> paths)
- {
- Ensure.ArgumentNotNull(paths, "paths");
-
- IDictionary<string, FileStatus> dic = new Dictionary<string, FileStatus>();
-
- foreach (string path in paths)
- {
- if (string.IsNullOrEmpty(path))
- {
- throw new ArgumentException("At least one provided path is either null or empty.", "paths");
- }
-
- string relativePath = repo.BuildRelativePathFrom(path);
- FileStatus fileStatus = RetrieveStatus(relativePath);
-
- dic.Add(relativePath, fileStatus);
- }
-
- if (dic.Count == 0)
- {
- throw new ArgumentException("No path has been provided.", "paths");
- }
-
- return dic;
- }
-
private IDictionary<Tuple<string, FileStatus>, Tuple<string, FileStatus>> PrepareBatch(IEnumerable<string> leftPaths, IEnumerable<string> rightPaths)
{
IDictionary<Tuple<string, FileStatus>, Tuple<string, FileStatus>> dic = new Dictionary<Tuple<string, FileStatus>, Tuple<string, FileStatus>>();
diff --git a/LibGit2Sharp/TreeChanges.cs b/LibGit2Sharp/TreeChanges.cs
index 0571d20e..c1079803 100644
--- a/LibGit2Sharp/TreeChanges.cs
+++ b/LibGit2Sharp/TreeChanges.cs
@@ -22,6 +22,7 @@ namespace LibGit2Sharp
private readonly List<TreeEntryChanges> deleted = new List<TreeEntryChanges>();
private readonly List<TreeEntryChanges> modified = new List<TreeEntryChanges>();
private readonly List<TreeEntryChanges> typeChanged = new List<TreeEntryChanges>();
+ private readonly List<TreeEntryChanges> unmodified = new List<TreeEntryChanges>();
private int linesAdded;
private int linesDeleted;
@@ -37,6 +38,7 @@ namespace LibGit2Sharp
{ ChangeKind.Deleted, (de, d) => de.deleted.Add(d) },
{ ChangeKind.Added, (de, d) => de.added.Add(d) },
{ ChangeKind.TypeChanged, (de, d) => de.typeChanged.Add(d) },
+ { ChangeKind.Unmodified, (de, d) => de.unmodified.Add(d) },
};
}
@@ -56,7 +58,7 @@ namespace LibGit2Sharp
string formattedoutput = Utf8Marshaler.FromNative(content, (int)contentlen);
TreeEntryChanges currentChange = AddFileChange(delta, lineorigin);
- if (currentChange == null)
+ if (delta.Status == ChangeKind.Unmodified)
{
return 0;
}
@@ -87,11 +89,6 @@ namespace LibGit2Sharp
private TreeEntryChanges AddFileChange(GitDiffDelta delta, GitDiffLineOrigin lineorigin)
{
- if (delta.Status == ChangeKind.Unmodified)
- {
- return null;
- }
-
var newFilePath = FilePathMarshaler.FromNative(delta.NewFile.Path);
if (lineorigin != GitDiffLineOrigin.GIT_DIFF_LINE_FILE_HDR)