Skip to content

Move Repository.Reset(paths) into Index #959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 9 additions & 51 deletions LibGit2Sharp.Tests/ResetIndexFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,7 @@ public void ResetANewlyInitializedBareRepositoryThrows()

using (var repo = new Repository(repoPath))
{
Assert.Throws<BareRepositoryException>(() => repo.Reset());
}
}

[Fact]
public void ResetANewlyInitializedNonBareRepositoryThrows()
{
string repoPath = InitNewRepository(false);

using (var repo = new Repository(repoPath))
{
Assert.Throws<UnbornBranchException>(() => repo.Reset());
Assert.Throws<BareRepositoryException>(() => repo.Index.Replace(repo.Head.Tip));
}
}

Expand All @@ -35,7 +24,7 @@ public void ResettingInABareRepositoryThrows()
string path = SandboxBareTestRepo();
using (var repo = new Repository(path))
{
Assert.Throws<BareRepositoryException>(() => repo.Reset());
Assert.Throws<BareRepositoryException>(() => repo.Index.Replace(repo.Head.Tip));
}
}

Expand Down Expand Up @@ -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());
Expand All @@ -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<Commit>("be3563a"));
repo.Index.Replace(repo.Lookup<Commit>("be3563a"));

RepositoryStatus newStatus = repo.RetrieveStatus();

Expand All @@ -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<Commit>("5b5b025"), new[] { "new.txt", "non-existent-path-28.txt" },
repo.Index.Replace(repo.Lookup<Commit>("5b5b025"), new[] { "new.txt", "non-existent-path-28.txt" },
new ExplicitPathsOptions { ShouldFailOnUnmatchedPath = false });

Assert.Equal("a8233120f6ad708f843d861ce2b7228ec4e3dec6", repo.Index["README"].Id.Sha);
Expand All @@ -149,7 +107,7 @@ public void ResettingTheIndexToASubsetOfTheContentOfACommitWithCommitAsArgumentA
using (var repo = new Repository(SandboxStandardTestRepo()))
{
Assert.Throws<UnmatchedPathException>(() =>
repo.Reset(repo.Lookup<Commit>("5b5b025"), new[] { "new.txt", "non-existent-path-28.txt" }, new ExplicitPathsOptions()));
repo.Index.Replace(repo.Lookup<Commit>("5b5b025"), new[] { "new.txt", "non-existent-path-28.txt" }, new ExplicitPathsOptions()));
}
}

Expand All @@ -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<Commit>("32eab9c"));
repo.Index.Replace(repo.Lookup<Commit>("32eab9c"));

RepositoryStatus status = repo.RetrieveStatus();
Assert.Equal(0, status.Where(IsStaged).Count());
Expand All @@ -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<Commit>("32eab9c"), new string[] { "branch_file.txt" });
repo.Index.Replace(repo.Lookup<Commit>("32eab9c"), new string[] { "branch_file.txt" });

RepositoryStatus newStatus = repo.RetrieveStatus();
Assert.Equal(0, newStatus.RenamedInIndex.Count());
Expand All @@ -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<Commit>("32eab9c"), new string[] { "renamed_branch_file.txt" });
repo.Index.Replace(repo.Lookup<Commit>("32eab9c"), new string[] { "renamed_branch_file.txt" });

RepositoryStatus newStatus = repo.RetrieveStatus();
Assert.Equal(0, newStatus.RenamedInIndex.Count());
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/StageFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions LibGit2Sharp/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ public interface IRepository : IDisposable
/// If set, the passed <paramref name="paths"/> will be treated as explicit paths.
/// Use these options to determine how unmatched explicit paths should be handled.
/// </param>
[Obsolete("This method will be removed in the next release. Please use Index.Replace() instead.")]
void Reset(Commit commit, IEnumerable<string> paths, ExplicitPathsOptions explicitPathsOptions);

/// <summary>
Expand Down
54 changes: 45 additions & 9 deletions LibGit2Sharp/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ internal IndexSafeHandle Handle
}

/// <summary>
/// Gets the number of <see cref="IndexEntry"/> in the index.
/// Gets the number of <see cref="IndexEntry"/> in the <see cref="Index"/>.
/// </summary>
public virtual int Count
{
get { return Proxy.git_index_entrycount(handle); }
}

/// <summary>
/// Determines if the index is free from conflicts.
/// Determines if the <see cref="Index"/> is free from conflicts.
/// </summary>
public virtual bool IsFullyMerged
{
Expand Down Expand Up @@ -128,9 +128,9 @@ IEnumerator IEnumerable.GetEnumerator()
#endregion

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

/// <summary>
/// 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 <see cref="Index"/>. This is semantically equivalent to
/// creating an empty <see cref="Tree"/> object and resetting the <see cref="Index"/> to that <see cref="Tree"/>.
/// <para>
/// This overwrites all existing state in the staging area.
/// This overwrites all existing state in the <see cref="Index"/>.
/// </para>
/// </summary>
public virtual void Clear()
Expand All @@ -163,7 +163,7 @@ private void RemoveFromIndex(string relativePath)
}

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

/// <summary>
/// Adds a file from the workdir in the <see cref="Index"/>.
/// Adds a file from the working directory in the <see cref="Index"/>.
/// <para>
/// If an entry with the same path already exists in the <see cref="Index"/>,
/// the newly added one will overwrite it.
Expand Down Expand Up @@ -296,5 +296,41 @@ private string DebuggerDisplay
"Count = {0}", Count);
}
}

/// <summary>
/// Replaces entries in the <see cref="Index"/> with entries from the specified <see cref="Commit"/>.
/// </summary>
/// <param name="commit">The target <see cref="Commit"/> object.</param>
public virtual void Replace(Commit commit)
{
Replace(commit, null, null);
}

/// <summary>
/// Replaces entries in the <see cref="Index"/> with entries from the specified <see cref="Commit"/>.
/// </summary>
/// <param name="commit">The target <see cref="Commit"/> object.</param>
/// <param name="paths">The list of paths (either files or directories) that should be considered.</param>
public virtual void Replace(Commit commit, IEnumerable<string> paths)
{
Replace(commit, paths, null);
}

/// <summary>
/// Replaces entries in the <see cref="Index"/> with entries from the specified <see cref="Commit"/>.
/// </summary>
/// <param name="commit">The target <see cref="Commit"/> object.</param>
/// <param name="paths">The list of paths (either files or directories) that should be considered.</param>
/// <param name="explicitPathsOptions">
/// If set, the passed <paramref name="paths"/> will be treated as explicit paths.
/// Use these options to determine how unmatched explicit paths should be handled.
/// </param>
public virtual void Replace(Commit commit, IEnumerable<string> paths, ExplicitPathsOptions explicitPathsOptions)
{
Ensure.ArgumentNotNull(commit, "commit");

var changes = repo.Diff.Compare<TreeChanges>(commit.Tree, DiffTargets.Index, paths, explicitPathsOptions, new CompareOptions { Similarity = SimilarityOptions.None });
Replace(changes);
}
}
}
13 changes: 3 additions & 10 deletions LibGit2Sharp/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -819,17 +819,10 @@ public void CheckoutPaths(string committishOrBranchSpec, IEnumerable<string> pat
/// If set, the passed <paramref name="paths"/> will be treated as explicit paths.
/// Use these options to determine how unmatched explicit paths should be handled.
/// </param>
[Obsolete("This method will be removed in the next release. Please use Index.Replace() instead.")]
public void Reset(Commit commit, IEnumerable<string> 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<TreeChanges>(commit.Tree, DiffTargets.Index, paths, explicitPathsOptions, new CompareOptions { Similarity = SimilarityOptions.None });
Index.Replace(changes);
Index.Replace(commit, paths, explicitPathsOptions);
}

/// <summary>
Expand Down Expand Up @@ -1573,7 +1566,7 @@ public void Unstage(IEnumerable<string> paths, ExplicitPathsOptions explicitPath
}
else
{
this.Reset("HEAD", paths, explicitPathsOptions);
Index.Replace(Head.Tip, paths, explicitPathsOptions);
}
}

Expand Down
9 changes: 6 additions & 3 deletions LibGit2Sharp/RepositoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public static void Reset(this IRepository repository, ResetMode resetMode, strin
/// If set, the passed <paramref name="paths"/> will be treated as explicit paths.
/// Use these options to determine how unmatched explicit paths should be handled.
/// </param>
[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<string> paths = null, ExplicitPathsOptions explicitPathsOptions = null)
{
if (repository.Info.IsBare)
Expand All @@ -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)
Expand Down Expand Up @@ -541,19 +542,21 @@ public static void Reset(this IRepository repository, ResetMode resetMode, Commi
/// <param name="repository">The <see cref="IRepository"/> being worked with.</param>
/// <param name="commit">The target commit object.</param>
/// <param name="paths">The list of paths (either files or directories) that should be considered.</param>
[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<string> paths)
{
repository.Reset(commit, paths, null);
repository.Index.Replace(commit, paths, null);
}

/// <summary>
/// Replaces entries in the <see cref="Repository.Index"/> with entries from the specified commit.
/// </summary>
/// <param name="repository">The <see cref="IRepository"/> being worked with.</param>
/// <param name="commit">The target commit object.</param>
[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);
}

/// <summary>
Expand Down