Skip to content

Commit e8b0741

Browse files
committed
Simplify Index.Unstage() implementation
1 parent 7737b3a commit e8b0741

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

LibGit2Sharp/Diff.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.IO;
35
using LibGit2Sharp.Core;
46
using LibGit2Sharp.Core.Handles;
57

@@ -31,11 +33,11 @@ private GitDiffOptions BuildOptions(DiffOptions diffOptions, IEnumerable<string>
3133
return options;
3234
}
3335

34-
options.PathSpec = GitStrArrayIn.BuildFrom(ToFilePaths(paths));
36+
options.PathSpec = GitStrArrayIn.BuildFrom(ToFilePaths(repo, paths));
3537
return options;
3638
}
3739

38-
private static FilePath[] ToFilePaths(IEnumerable<string> paths)
40+
private static FilePath[] ToFilePaths(Repository repo, IEnumerable<string> paths)
3941
{
4042
var filePaths = new List<FilePath>();
4143

@@ -46,7 +48,7 @@ private static FilePath[] ToFilePaths(IEnumerable<string> paths)
4648
throw new ArgumentException("At least one provided path is either null or empty.", "paths");
4749
}
4850

49-
filePaths.Add(path);
51+
filePaths.Add(BuildRelativePathFrom(repo, path));
5052
}
5153

5254
if (filePaths.Count == 0)
@@ -57,6 +59,26 @@ private static FilePath[] ToFilePaths(IEnumerable<string> paths)
5759
return filePaths.ToArray();
5860
}
5961

62+
private static string BuildRelativePathFrom(Repository repo, string path)
63+
{
64+
//TODO: To be removed when libgit2 natively implements this
65+
if (!Path.IsPathRooted(path))
66+
{
67+
return path;
68+
}
69+
70+
string normalizedPath = Path.GetFullPath(path);
71+
72+
if (!normalizedPath.StartsWith(repo.Info.WorkingDirectory, StringComparison.Ordinal))
73+
{
74+
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
75+
"Unable to process file '{0}'. This absolute filepath escapes out of the working directory of the repository ('{1}').",
76+
normalizedPath, repo.Info.WorkingDirectory));
77+
}
78+
79+
return normalizedPath.Substring(repo.Info.WorkingDirectory.Length);
80+
}
81+
6082
/// <summary>
6183
/// Needed for mocking purposes.
6284
/// </summary>

LibGit2Sharp/Index.cs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -213,33 +213,15 @@ public virtual void Unstage(IEnumerable<string> paths)
213213
{
214214
Ensure.ArgumentNotNull(paths, "paths");
215215

216-
IEnumerable<KeyValuePair<string, FileStatus>> batch = PrepareBatch(paths);
217-
218-
foreach (KeyValuePair<string, FileStatus> kvp in batch)
216+
if (repo.Info.IsHeadOrphaned)
219217
{
220-
if (Directory.Exists(kvp.Key))
221-
{
222-
throw new NotImplementedException();
223-
}
224-
}
218+
TreeChanges changes = repo.Diff.Compare(null, DiffTargets.Index, paths);
225219

226-
var commit = repo.Lookup("HEAD",
227-
GitObjectType.Any,
228-
LookUpOptions.DereferenceResultToCommit) as Commit;
229-
230-
if (null != commit)
231-
{
232-
repo.Reset("HEAD", paths);
220+
Reset(changes);
233221
}
234222
else
235223
{
236-
// HEAD doesn't exist, so all these staged paths must be Added.
237-
foreach (String path in paths)
238-
{
239-
RemoveFromIndex(path);
240-
}
241-
242-
UpdatePhysicalIndex();
224+
repo.Reset("HEAD", paths);
243225
}
244226
}
245227

0 commit comments

Comments
 (0)