8000 Move Repository.Reset(paths) into Index · devinvisible/libgit2sharp@605b623 · GitHub
[go: up one dir, main page]

Skip to content

Commit 605b623

Browse files
committed
Move Repository.Reset(paths) into Index
Per issue libgit2#805, the Repository.Reset() methods and extensions that take paths have been moved to Index. These methods have become additional Index.Replace() overloads. The old methods have been deprecated and have been changed to call the new Index.Replace() methods instead. The relevant tests have been updated to use the new methods. Tests that are no longer needed have been removed. Closes libgit2#805
1 parent 9e05f8d commit 605b623

File tree

6 files changed

+65
-74
lines changed

6 files changed

+65
-74
lines changed

LibGit2Sharp.Tests/ResetIndexFixture.cs

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,7 @@ public void ResetANewlyInitializedBareRepositoryThrows()
1414

1515
using (var repo = new Repository(repoPath))
1616
{
17-
Assert.Throws<BareRepositoryException>(() => repo.Reset());
18-
}
19-
}
20-
21-
[Fact]
22-
public void ResetANewlyInitializedNonBareRepositoryThrows()
23-
{
24-
string repoPath = InitNewRepository(false);
25-
26-
using (var repo = new Repository(repoPath))
27-
{
28-
Assert.Throws<UnbornBranchException>(() => repo.Reset());
17+
Assert.Throws<BareRepositoryException>(() => repo.Index.Replace(repo.Head.Tip));
2918
}
3019
}
3120

@@ -35,7 +24,7 @@ public void ResettingInABareRepositoryThrows()
3524
string path = SandboxBareTestRepo();
3625
using (var repo = new Repository(path))
3726
{
38-
Assert.Throws<BareRepositoryException>(() => repo.Reset());
27+
Assert.Throws<BareRepositoryException>(() => repo.Index.Replace(repo.Head.Tip));
3928
}
4029
}
4130

@@ -70,7 +59,7 @@ public void ResetTheIndexWithTheHeadUnstagesEverything()
7059

7160
var reflogEntriesCount = repo.Refs.Log(repo.Refs.Head).Count();
7261

73-
repo.Reset();
62+
repo.Index.Replace(repo.Head.Tip);
7463

7564
RepositoryStatus newStatus = repo.RetrieveStatus();
7665
Assert.Equal(0, newStatus.Where(IsStaged).Count());
@@ -80,31 +69,13 @@ public void ResetTheIndexWithTheHeadUnstagesEverything()
8069
}
8170
}
8271

83-
[Fact]
84-
public void CanResetTheIndexToTheContentOfACommitWithCommittishAsArgument()
85-
{
86-
string path = SandboxStandardTestRepo();
87-
using (var repo = new Repository(path))
88-
{
89-
repo.Reset("be3563a");
90-
91-
RepositoryStatus newStatus = repo.RetrieveStatus();
92-
93-
var expected = new[] { "1.txt", Path.Combine("1", "branch_file.txt"), "deleted_staged_file.txt",
94-
"deleted_unstaged_file.txt", "modified_staged_file.txt", "modified_unstaged_file.txt" };
95-
96-
Assert.Equal(expected.Length, newStatus.Where(IsStaged).Count());
97-
Assert.Equal(expected, newStatus.Removed.Select(s => s.FilePath));
98-
}
99-
}
100-
10172
[Fact]
10273
public void CanResetTheIndexToTheContentOfACommitWithCommitAsArgument()
10374
{
10475
string path = SandboxStandardTestRepo();
10576
using (var repo = new Repository(path))
10677
{
107-
repo.Reset(repo.Lookup<Commit>("be3563a"));
78+
repo.Index.Replace(repo.Lookup<Commit>("be3563a"));
10879

10980
RepositoryStatus newStatus = repo.RetrieveStatus();
11081

@@ -116,26 +87,13 @@ public void CanResetTheIndexToTheContentOfACommitWithCommitAsArgument()
11687
}
11788
}
11889

119-
[Fact]
120-
public void CanResetTheIndexToASubsetOfTheContentOfACommitWithCommittishAsArgument()
121-
{
122-
string path = SandboxStandardTestRepo();
123-
using (var repo = new Repository(path))
124-
{
125-
repo.Reset("5b5b025", new[]{ "new.txt" });
126-
127-
Assert.Equal("a8233120f6ad708f843d861ce2b7228ec4e3dec6", repo.Index["README"].Id.Sha);
128-
Assert.Equal("fa49b077972391ad58037050f2a75f74e3671e92", repo.Index["new.txt"].Id.Sha);
129-
}
130-
}
131-
13290
[Fact]
13391
public void CanResetTheIndexToASubsetOfTheContentOfACommitWithCommitAsArgumentAndLaxUnmatchedExplicitPathsValidation()
13492
{
13593
string path = SandboxStandardTestRepo();
13694
using (var repo = new Repository(path))
13795
{
138-
repo.Reset(repo.Lookup<Commit>("5b5b025"), new[] { "new.txt", "non-existent-path-28.txt" },
96+
repo.Index.Replace(repo.Lookup<Commit>("5b5b025"), new[] { "new.txt", "non-existent-path-28.txt" },
13997
new ExplicitPathsOptions { ShouldFailOnUnmatchedPath = false });
14098

14199
Assert.Equal("a8233120f6ad708f843d861ce2b7228ec4e3dec6", repo.Index["README"].Id.Sha);
@@ -149,7 +107,7 @@ publ 10000 ic void ResettingTheIndexToASubsetOfTheContentOfACommitWithCommitAsArgumentA
149107
using (var repo = new Repository(SandboxStandardTestRepo()))
150108
{
151109
Assert.Throws<UnmatchedPathException>(() =>
152-
repo.Reset(repo.Lookup<Commit>("5b5b025"), new[] { "new.txt", "non-existent-path-28.txt" }, new ExplicitPathsOptions()));
110+
repo.Index.Replace(repo.Lookup<Commit>("5b5b025"), new[] { "new.txt", "non-existent-path-28.txt" }, new ExplicitPathsOptions()));
153111
}
154112
}
155113

@@ -159,7 +117,7 @@ public void CanResetTheIndexWhenARenameExists()
159117
using (var repo = new Repository(SandboxStandardTestRepo()))
160118
{
161119
repo.Move("branch_file.txt", "renamed_branch_file.txt");
162-
repo.Reset(repo.Lookup<Commit>("32eab9c"));
120+
repo.Index.Replace(repo.Lookup<Commit>("32eab9c"));
163121

164122
RepositoryStatus status = repo.RetrieveStatus();
165123
Assert.Equal(0, status.Where(IsStaged).Count());
@@ -178,7 +136,7 @@ public void CanResetSourceOfARenameInIndex()
178136
Assert.Equal(FileStatus.Nonexistent, oldStatus["branch_file.txt"].State);
179137
Assert.Equal(FileStatus.RenamedInIndex, oldStatus["renamed_branch_file.txt"].State);
180138

181-
repo.Reset(repo.Lookup<Commit>("32eab9c"), new string[] { "branch_file.txt" });
139+
repo.Index.Replace(repo.Lookup<Commit>("32eab9c"), new string[] { "branch_file.txt" });
182140

183141
RepositoryStatus newStatus = repo.RetrieveStatus();
184142
Assert.Equal(0, newStatus.RenamedInIndex.Count());
@@ -198,7 +156,7 @@ public void CanResetTargetOfARenameInIndex()
198156
Assert.Equal(1, oldStatus.RenamedInIndex.Count());
199157
Assert.Equal(FileStatus.RenamedInIndex, oldStatus["renamed_branch_file.txt"].State);
200158

201-
repo.Reset(repo.Lookup<Commit>("32eab9c"), new string[] { "renamed_branch_file.txt" });
159+
repo.Index.Replace(repo.Lookup<Commit>("32eab9c"), new string[] { "renamed_branch_file.txt" });
202160

203161
RepositoryStatus newStatus = repo.RetrieveStatus();
204162
Assert.Equal(0, newStatus.RenamedInIndex.Count());

LibGit2Sharp.Tests/StageFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff li F987 ne change
@@ -195,7 +195,7 @@ private static void AssertStage(bool? ignorecase, IRepository repo, string path)
195195
{
196196
repo.Stage(path);
197197
Assert.Equal(FileStatus.Added, repo.RetrieveStatus(path));
198-
repo.Reset();
198+
repo.Index.Replace(repo.Head.Tip);
199199
Assert.Equal(FileStatus.Untracked, repo.RetrieveStatus(path));
200200
}
201201
catch (ArgumentException)

LibGit2Sharp/IRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ public interface IRepository : IDisposable
179179
/// If set, the passed <paramref name="paths"/> will be treated as explicit paths.
180180
/// Use these options to determine how unmatched explicit paths should be handled.
181181
/// </param>
182+
[Obsolete("This method will be removed in the next release. Please use Index.Replace() instead.")]
182183
void Reset(Commit commit, IEnumerable<string> paths, ExplicitPathsOptions explicitPathsOptions);
183184

184185
/// <summary>

LibGit2Sharp/Index.cs

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ internal IndexSafeHandle Handle
5454
}
5555

5656
/// <summary>
57-
/// Gets the number of <see cref="IndexEntry"/> in the index.
57+
/// Gets the number of <see cref="IndexEntry"/> in the <see cref="Index"/>.
5858
/// </summary>
5959
public virtual int Count
6060
{
6161
get { return Proxy.git_index_entrycount(handle); }
6262
}
6363

6464
/// <summary>
65-
/// Determines if the index is free from conflicts.
65+
/// Determines if the <see cref="Index"/> is free from conflicts.
6666
/// </summary>
6767
public virtual bool IsFullyMerged
6868
{
@@ -128,9 +128,9 @@ IEnumerator IEnumerable.GetEnumerator()
128128
#endregion
129129

130130
/// <summary>
131-
/// Replaces entries in the staging area with entries from the specified tree.
131+
/// Replaces entries in the <see cref="Index"/> with entries from the specified <see cref="Tree"/>.
132132
/// <para>
133-
/// This overwrites all existing state in the staging area.
133+
/// This overwrites all existing state in the <see cref="Index"/>.
134134
/// </para>
135135
/// </summary>
136136
/// <param name="source">The <see cref="Tree"/> to read the entries from.</param>
@@ -145,10 +145,10 @@ public virtual void Replace(Tree source)
145145
}
146146

147147
/// <summary>
148-
/// Clears all entries the index. This is semantically equivalent to
149-
/// creating an empty tree object and resetting the index to that tree.
148+
/// Clears all entries the <see cref="Index"/>. This is semantically equivalent to
149+
/// creating an empty <see cref="Tree"/> object and resetting the <see cref="Index"/> to that <see cref="Tree"/>.
150150
/// <para>
151-
/// This overwrites all existing state in the staging area.
151+
/// This overwrites all existing state in the <see cref="Index"/>.
152152
/// </para>
153153
/// </summary>
154154
public virtual void Clear()
@@ -163,7 +163,7 @@ private void RemoveFromIndex(string relativePath)
163163
}
164164

165165
/// <summary>
166-
/// Removes a specified entry from the index.
166+
/// Removes a specified entry from the <see cref="Index"/>.
167167
/// </summary>
168168
/// <param name="indexEntryPath">The path of the <see cref="Index"/> entry to be removed.</param>
169169
public virtual void Remove(string indexEntryPath)
@@ -179,7 +179,7 @@ public virtual void Remove(string indexEntryPath)
179179
}
180180

181181
/// <summary>
182-
/// Adds a file from the workdir in the <see cref="Index"/>.
182+
/// Adds a file from the working directory in the <see cref="Index"/>.
183183
/// <para>
184184
/// If an entry with the same path already exists in the <see cref="Index"/>,
185185
/// the newly added one will overwrite it.
@@ -296,5 +296,41 @@ private string DebuggerDisplay
296296
"Count = {0}", Count);
297297
}
298298
}
299+
300+
/// <summary>
301+
/// Replaces entries in the <see cref="Index"/> with entries from the specified <see cref="Commit"/>.
302+
/// </summary>
303+
/// <param name="commit">The target <see cref="Commit"/> object.</param>
304+
public virtual void Replace(Commit commit)
305+
{
306+
Replace(commit, null, null);
307+
}
308+
309+
/// <summary>
310+
/// Replaces entries in the <see cref="Index"/> with entries from the specified <see cref="Commit"/>.
311+
/// </summary>
312+
/// <param name="commit">The target <see cref="Commit"/> object.</param>
313+
/// <param name="paths">The list of paths (either files or directories) that should be considered.</param>
314+
public virtual void Replace(Commit commit, IEnumerable<string> paths)
315+
{
316+
Replace(commit, paths, null);
317+
}
318+
319+
/// <summary>
320+
/// Replaces entries in the <see cref="Index"/> with entries from the specified <see cref="Commit"/>.
321+
/// </summary>
322+
/// <param name="commit">The target <see cref="Commit"/> object.</param>
323+
/// <param name="paths">The list of paths (either files or directories) that should be considered.</param>
324+
/// <param name="explicitPathsOptions">
325+
/// If set, the passed <paramref name="paths"/> will be treated as explicit paths.
326+
/// Use these options to determine how unmatched explicit paths should be handled.
327+
/// </param>
328+
public virtual void Replace(Commit commit, IEnumerable<string> paths, ExplicitPathsOptions explicitPathsOptions)
329+
{
330+
Ensure.ArgumentNotNull(commit, "commit");
331+
332+
var changes = repo.Diff.Compare<TreeChanges>(commit.Tree, DiffTargets.Index, paths, explicitPathsOptions, new CompareOptions { Similarity = SimilarityOptions.None });
333+
Replace(changes);
334+
}
299335
}
300336
}

LibGit2Sharp/Repository.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -819,17 +819,10 @@ public void CheckoutPaths(string committishOrBranchSpec, IEnumerable<string> pat
819819
/// If set, the passed <paramref name="paths"/> will be treated as explicit paths.
820820
/// Use these options to determine how unmatched explicit paths should be handled.
821821
/// </param>
822+
[Obsolete("This method will be removed in the next release. Please use Index.Replace() instead.")]
822823
public void Reset(Commit commit, IEnumerable<string> paths, ExplicitPathsOptions explicitPathsOptions)
823824
{
824-
if (Info.IsBare)
825-
{
826-
throw new BareRepositoryException("Reset is not allowed in a bare repository");
827-
}
828-
829-
Ensure.ArgumentNotNull(commit, "commit");
830-
831-
var changes = Diff.Compare<TreeChanges>(commit.Tree, DiffTargets.Index, paths, explicitPathsOptions, new CompareOptions { Similarity = SimilarityOptions.None });
832-
Index.Replace(changes);
825+
Index.Replace(commit, paths, explicitPathsOptions);
833826
}
834827

835828
/// <summary>
@@ -1573,7 +1566,7 @@ public void Unstage(IEnumerable<string> paths, ExplicitPathsOptions explicitPath
15731566
}
15741567
else
15751568
{
1576-
this.Reset("HEAD", paths, explicitPathsOptions);
1569+
Index.Replace(Head.Tip, paths, explicitPathsOptions);
15771570
}
15781571
}
15791572

LibGit2Sharp/RepositoryExtensions.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ public static void Reset(this IRepository repository, ResetMode resetMode, strin
183183
/// If set, the passed <paramref name="paths"/> will be treated as explicit paths.
184184
/// Use these options to determine how unmatched explicit paths should be handled.
185185
/// </param>
186+
[Obsolete("This method will be removed in the next release. Please use Index.Replace() instead.")]
186187
public static void Reset(this IRepository repository, string committish = "HEAD", IEnumerable<string> paths = null, ExplicitPathsOptions explicitPathsOptions = null)
187188
{
188189
if (repository.Info.IsBare)
@@ -194,7 +195,7 @@ public static void Reset(this IRepository repository, string committish = "HEAD"
194195

195196
Commit commit = LookUpCommit(repository, committish);
196197

197-
repository.Reset(commit, paths, explicitPathsOptions);
198+
repository.Index.Replace(commit, paths, explicitPathsOptions);
198199
}
199200

200201
private static Commit LookUpCommit(IRepository repository, string committish)
@@ -541,19 +542,21 @@ public static void Reset(this IRepository repository, ResetMode resetMode, Commi
541542
/// <param name="repository">The <see cref="IRepository"/> being worked with.</param>
542543
/// <param name="commit">The target commit object.</param>
543544
/// <param name="paths">The list of paths (either files or directories) that should be considered.</param>
545+
[Obsolete("This method will be removed in the next release. Please use Index.Replace() instead.")]
544546
public static void Reset(this IRepository repository, Commit commit, IEnumerable<string> paths)
545547
{
546-
repository.Reset(commit, paths, null);
548+
repository.Index.Replace(commit, paths, null);
547549
}
548550

549551
/// <summary>
550552
/// Replaces entries in the <see cref="Repository.Index"/> with entries from the specified commit.
551553
/// </summary>
552554
/// <param name="repository">The <see cref="IRepository"/> being worked with.</param>
553555
/// <param name="commit">The target commit object.</param>
556+
[Obsolete("This method will be removed in the next release. Please use Index.Replace() instead.")]
554557
public static void Reset(this IRepository repository, Commit commit)
555558
{
556-
repository.Reset(commit, null, null);
559+
repository.Index.Replace(commit, null, null);
557560
}
558561

559562
/// <summary>

0 commit comments

Comments
 (0)
0