diff --git a/LibGit2Sharp.Tests/ResetIndexFixture.cs b/LibGit2Sharp.Tests/ResetIndexFixture.cs index a87c4ef23..5e709f61c 100644 --- a/LibGit2Sharp.Tests/ResetIndexFixture.cs +++ b/LibGit2Sharp.Tests/ResetIndexFixture.cs @@ -14,18 +14,7 @@ public void ResetANewlyInitializedBareRepositoryThrows() using (var repo = new Repository(repoPath)) { - Assert.Throws(() => repo.Reset()); - } - } - - [Fact] - public void ResetANewlyInitializedNonBareRepositoryThrows() - { - string repoPath = InitNewRepository(false); - - using (var repo = new Repository(repoPath)) - { - Assert.Throws(() => repo.Reset()); + Assert.Throws(() => repo.Index.Replace(repo.Head.Tip)); } } @@ -35,7 +24,7 @@ public void ResettingInABareRepositoryThrows() string path = SandboxBareTestRepo(); using (var repo = new Repository(path)) { - Assert.Throws(() => repo.Reset()); + Assert.Throws(() => repo.Index.Replace(repo.Head.Tip)); } } @@ -70,7 +59,7 @@ public void ResetTheIndexWithTheHeadUnstagesEverything() var reflogEntriesCount = repo.Refs.Log(repo.Refs.Head).Count(); - repo.Reset(); + repo.Index.Replace(repo.Head.Tip); RepositoryStatus newStatus = repo.RetrieveStatus(); Assert.Equal(0, newStatus.Where(IsStaged).Count()); @@ -80,31 +69,13 @@ public void ResetTheIndexWithTheHeadUnstagesEverything() } } - [Fact] - public void CanResetTheIndexToTheContentOfACommitWithCommittishAsArgument() - { - string path = SandboxStandardTestRepo(); - using (var repo = new Repository(path)) - { - repo.Reset("be3563a"); - - RepositoryStatus newStatus = repo.RetrieveStatus(); - - var expected = new[] { "1.txt", Path.Combine("1", "branch_file.txt"), "deleted_staged_file.txt", - "deleted_unstaged_file.txt", "modified_staged_file.txt", "modified_unstaged_file.txt" }; - - Assert.Equal(expected.Length, newStatus.Where(IsStaged).Count()); - Assert.Equal(expected, newStatus.Removed.Select(s => s.FilePath)); - } - } - [Fact] public void CanResetTheIndexToTheContentOfACommitWithCommitAsArgument() { string path = SandboxStandardTestRepo(); using (var repo = new Repository(path)) { - repo.Reset(repo.Lookup("be3563a")); + repo.Index.Replace(repo.Lookup("be3563a")); RepositoryStatus newStatus = repo.RetrieveStatus(); @@ -116,26 +87,13 @@ public void CanResetTheIndexToTheContentOfACommitWithCommitAsArgument() } } - [Fact] - public void CanResetTheIndexToASubsetOfTheContentOfACommitWithCommittishAsArgument() - { - string path = SandboxStandardTestRepo(); - using (var repo = new Repository(path)) - { - repo.Reset("5b5b025", new[]{ "new.txt" }); - - Assert.Equal("a8233120f6ad708f843d861ce2b7228ec4e3dec6", repo.Index["README"].Id.Sha); - Assert.Equal("fa49b077972391ad58037050f2a75f74e3671e92", repo.Index["new.txt"].Id.Sha); - } - } - [Fact] public void CanResetTheIndexToASubsetOfTheContentOfACommitWithCommitAsArgumentAndLaxUnmatchedExplicitPathsValidation() { string path = SandboxStandardTestRepo(); using (var repo = new Repository(path)) { - repo.Reset(repo.Lookup("5b5b025"), new[] { "new.txt", "non-existent-path-28.txt" }, + repo.Index.Replace(repo.Lookup("5b5b025"), new[] { "new.txt", "non-existent-path-28.txt" }, new ExplicitPathsOptions { ShouldFailOnUnmatchedPath = false }); Assert.Equal("a8233120f6ad708f843d861ce2b7228ec4e3dec6", repo.Index["README"].Id.Sha); @@ -149,7 +107,7 @@ public void ResettingTheIndexToASubsetOfTheContentOfACommitWithCommitAsArgumentA using (var repo = new Repository(SandboxStandardTestRepo())) { Assert.Throws(() => - repo.Reset(repo.Lookup("5b5b025"), new[] { "new.txt", "non-existent-path-28.txt" }, new ExplicitPathsOptions())); + repo.Index.Replace(repo.Lookup("5b5b025"), new[] { "new.txt", "non-existent-path-28.txt" }, new ExplicitPathsOptions())); } } @@ -159,7 +117,7 @@ public void CanResetTheIndexWhenARenameExists() using (var repo = new Repository(SandboxStandardTestRepo())) { repo.Move("branch_file.txt", "renamed_branch_file.txt"); - repo.Reset(repo.Lookup("32eab9c")); + repo.Index.Replace(repo.Lookup("32eab9c")); RepositoryStatus status = repo.RetrieveStatus(); Assert.Equal(0, status.Where(IsStaged).Count()); @@ -178,7 +136,7 @@ public void CanResetSourceOfARenameInIndex() Assert.Equal(FileStatus.Nonexistent, oldStatus["branch_file.txt"].State); Assert.Equal(FileStatus.RenamedInIndex, oldStatus["renamed_branch_file.txt"].State); - repo.Reset(repo.Lookup("32eab9c"), new string[] { "branch_file.txt" }); + repo.Index.Replace(repo.Lookup("32eab9c"), new string[] { "branch_file.txt" }); RepositoryStatus newStatus = repo.RetrieveStatus(); Assert.Equal(0, newStatus.RenamedInIndex.Count()); @@ -198,7 +156,7 @@ public void CanResetTargetOfARenameInIndex() Assert.Equal(1, oldStatus.RenamedInIndex.Count()); Assert.Equal(FileStatus.RenamedInIndex, oldStatus["renamed_branch_file.txt"].State); - repo.Reset(repo.Lookup("32eab9c"), new string[] { "renamed_branch_file.txt" }); + repo.Index.Replace(repo.Lookup("32eab9c"), new string[] { "renamed_branch_file.txt" }); RepositoryStatus newStatus = repo.RetrieveStatus(); Assert.Equal(0, newStatus.RenamedInIndex.Count()); diff --git a/LibGit2Sharp.Tests/StageFixture.cs b/LibGit2Sharp.Tests/StageFixture.cs index 679f20486..e3ea496a2 100644 --- a/LibGit2Sharp.Tests/StageFixture.cs +++ b/LibGit2Sharp.Tests/StageFixture.cs @@ -195,7 +195,7 @@ private static void AssertStage(bool? ignorecase, IRepository repo, string path) { repo.Stage(path); Assert.Equal(FileStatus.Added, repo.RetrieveStatus(path)); - repo.Reset(); + repo.Index.Replace(repo.Head.Tip); Assert.Equal(FileStatus.Untracked, repo.RetrieveStatus(path)); } catch (ArgumentException) diff --git a/LibGit2Sharp/IRepository.cs b/LibGit2Sharp/IRepository.cs index f6614d7e8..1557d07ee 100644 --- a/LibGit2Sharp/IRepository.cs +++ b/LibGit2Sharp/IRepository.cs @@ -179,6 +179,7 @@ public interface IRepository : IDisposable /// If set, the passed will be treated as explicit paths. /// Use these options to determine how unmatched explicit paths should be handled. /// + [Obsolete("This method will be removed in the next release. Please use Index.Replace() instead.")] void Reset(Commit commit, IEnumerable paths, ExplicitPathsOptions explicitPathsOptions); /// diff --git a/LibGit2Sharp/Index.cs b/LibGit2Sharp/Index.cs index 3c29db985..826e0baf2 100644 --- a/LibGit2Sharp/Index.cs +++ b/LibGit2Sharp/Index.cs @@ -54,7 +54,7 @@ internal IndexSafeHandle Handle } /// - /// Gets the number of in the index. + /// Gets the number of in the . /// public virtual int Count { @@ -62,7 +62,7 @@ public virtual int Count } /// - /// Determines if the index is free from conflicts. + /// Determines if the is free from conflicts. /// public virtual bool IsFullyMerged { @@ -128,9 +128,9 @@ IEnumerator IEnumerable.GetEnumerator() #endregion /// - /// Replaces entries in the staging area with entries from the specified tree. + /// Replaces entries in the with entries from the specified . /// - /// This overwrites all existing state in the staging area. + /// This overwrites all existing state in the . /// /// /// The to read the entries from. @@ -145,10 +145,10 @@ public virtual void Replace(Tree source) } /// - /// Clears all entries the index. This is semantically equivalent to - /// creating an empty tree object and resetting the index to that tree. + /// Clears all entries the . This is semantically equivalent to + /// creating an empty object and resetting the to that . /// - /// This overwrites all existing state in the staging area. + /// This overwrites all existing state in the . /// /// public virtual void Clear() @@ -163,7 +163,7 @@ private void RemoveFromIndex(string relativePath) } /// - /// Removes a specified entry from the index. + /// Removes a specified entry from the . /// /// The path of the entry to be removed. public virtual void Remove(string indexEntryPath) @@ -179,7 +179,7 @@ public virtual void Remove(string indexEntryPath) } /// - /// Adds a file from the workdir in the . + /// Adds a file from the working directory in the . /// /// If an entry with the same path already exists in the , /// the newly added one will overwrite it. @@ -296,5 +296,41 @@ private string DebuggerDisplay "Count = {0}", Count); } } + + /// + /// Replaces entries in the with entries from the specified . + /// + /// The target object. + public virtual void Replace(Commit commit) + { + Replace(commit, null, null); + } + + /// + /// Replaces entries in the with entries from the specified . + /// + /// The target object. + /// The list of paths (either files or directories) that should be considered. + public virtual void Replace(Commit commit, IEnumerable paths) + { + Replace(commit, paths, null); + } + + /// + /// Replaces entries in the with entries from the specified . + /// + /// The target object. + /// The list of paths (either files or directories) that should be considered. + /// + /// If set, the passed will be treated as explicit paths. + /// Use these options to determine how unmatched explicit paths should be handled. + /// + public virtual void Replace(Commit commit, IEnumerable paths, ExplicitPathsOptions explicitPathsOptions) + { + Ensure.ArgumentNotNull(commit, "commit"); + + var changes = repo.Diff.Compare(commit.Tree, DiffTargets.Index, paths, explicitPathsOptions, new CompareOptions { Similarity = SimilarityOptions.None }); + Replace(changes); + } } } diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index 8e6834872..d2188b17a 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -819,17 +819,10 @@ public void CheckoutPaths(string committishOrBranchSpec, IEnumerable pat /// If set, the passed will be treated as explicit paths. /// Use these options to determine how unmatched explicit paths should be handled. /// + [Obsolete("This method will be removed in the next release. Please use Index.Replace() instead.")] public void Reset(Commit commit, IEnumerable paths, ExplicitPathsOptions explicitPathsOptions) { - if (Info.IsBare) - { - throw new BareRepositoryException("Reset is not allowed in a bare repository"); - } - - Ensure.ArgumentNotNull(commit, "commit"); - - var changes = Diff.Compare(commit.Tree, DiffTargets.Index, paths, explicitPathsOptions, new CompareOptions { Similarity = SimilarityOptions.None }); - Index.Replace(changes); + Index.Replace(commit, paths, explicitPathsOptions); } /// @@ -1573,7 +1566,7 @@ public void Unstage(IEnumerable paths, ExplicitPathsOptions explicitPath } else { - this.Reset("HEAD", paths, explicitPathsOptions); + Index.Replace(Head.Tip, paths, explicitPathsOptions); } } diff --git a/LibGit2Sharp/RepositoryExtensions.cs b/LibGit2Sharp/RepositoryExtensions.cs index 3423f418f..e52514298 100644 --- a/LibGit2Sharp/RepositoryExtensions.cs +++ b/LibGit2Sharp/RepositoryExtensions.cs @@ -183,6 +183,7 @@ public static void Reset(this IRepository repository, ResetMode resetMode, strin /// If set, the passed will be treated as explicit paths. /// Use these options to determine how unmatched explicit paths should be handled. /// + [Obsolete("This method will be removed in the next release. Please use Index.Replace() instead.")] public static void Reset(this IRepository repository, string committish = "HEAD", IEnumerable paths = null, ExplicitPathsOptions explicitPathsOptions = null) { if (repository.Info.IsBare) @@ -194,7 +195,7 @@ public static void Reset(this IRepository repository, string committish = "HEAD" Commit commit = LookUpCommit(repository, committish); - repository.Reset(commit, paths, explicitPathsOptions); + repository.Index.Replace(commit, paths, explicitPathsOptions); } private static Commit LookUpCommit(IRepository repository, string committish) @@ -541,9 +542,10 @@ public static void Reset(this IRepository repository, ResetMode resetMode, Commi /// The being worked with. /// The target commit object. /// The list of paths (either files or directories) that should be considered. + [Obsolete("This method will be removed in the next release. Please use Index.Replace() instead.")] public static void Reset(this IRepository repository, Commit commit, IEnumerable paths) { - repository.Reset(commit, paths, null); + repository.Index.Replace(commit, paths, null); } /// @@ -551,9 +553,10 @@ public static void Reset(this IRepository repository, Commit commit, IEnumerable /// /// The being worked with. /// The target commit object. + [Obsolete("This method will be removed in the next release. Please use Index.Replace() instead.")] public static void Reset(this IRepository repository, Commit commit) { - repository.Reset(commit, null, null); + repository.Index.Replace(commit, null, null); } ///