Skip to content

Commit

Permalink
Update CheckoutPaths method to handle null or empty paths parameter
Browse files Browse the repository at this point in the history
Fixes an issue where passing an empty enumeration to CheckoutPaths results in the entire commit being checked out - which is not how I expect the method to operate. Instead, this method will now ensure that the path parameter is not null and if the path parameter is an empty list the method will return without checking out any paths.
  • Loading branch information
jamill committed Aug 13, 2013
1 parent 88b4c1b commit d370aa6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
21 changes: 21 additions & 0 deletions LibGit2Sharp.Tests/CheckoutFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,27 @@ public void CanCheckoutPaths()
}
}

[Fact]
public void CannotCheckoutPathsWithEmptyOrNullPathArgument()
{
string repoPath = CloneStandardTestRepo();

using (var repo = new Repository(repoPath))
{
// Set the working directory to the current head
ResetAndCleanWorkingDirectory(repo);
Assert.False(repo.Index.RetrieveStatus().IsDirty);

// Passing null 'paths' parameter should throw
Assert.Throws(typeof(ArgumentNullException),
() => repo.CheckoutPaths("i-do-numbers", null));

// Passing empty list should do nothing
repo.CheckoutPaths("i-do-numbers", Enumerable.Empty<string>());
Assert.False(repo.Index.RetrieveStatus().IsDirty);
}
}

[Theory]
[InlineData("new.txt")]
[InlineData("1.txt")]
Expand Down
9 changes: 8 additions & 1 deletion LibGit2Sharp/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -838,11 +838,18 @@ public void CheckoutPaths(string committishOrBranchSpec, IList<string> paths, Ch
/// </para>
/// </summary>
/// <param name = "committishOrBranchSpec">A revparse spec for the commit or branch to checkout paths from.</param>
/// <param name="paths">The paths to checkout.</param>
/// <param name="paths">The paths to checkout. Will throw if null is passed in. Passing an empty enumeration results in nothing being checked out.</param>
/// <param name="checkoutOptions">Collection of parameters controlling checkout behavior.</param>
public void CheckoutPaths(string committishOrBranchSpec, IEnumerable<string> paths, CheckoutOptions checkoutOptions = null)
{
Ensure.ArgumentNotNullOrEmptyString(committishOrBranchSpec, "committishOrBranchSpec");
Ensure.ArgumentNotNull(paths, "paths");

// If there are no paths, then there is nothing to do.
if (!paths.Any())
{
return;
}

Commit commit = LookupCommit(committishOrBranchSpec);
CheckoutTree(commit.Tree, paths.ToList(), checkoutOptions ?? new CheckoutOptions());
Expand Down

0 comments on commit d370aa6

Please sign in to comment.